refactor(proxy): 移除代理检查国家功能并优化 VPN 启动逻辑

- 删除了 ClashUtil 类中的 checkCountryIsUS 方法,移除了检查国家是否为美国的功能
- 修改了 LoadDeviceWorker 类中的 startProxyVpn 方法,去除了返回值,简化了逻辑- 优化了 executeSingleLogic 方法的流程,移除了与代理检查国家相关的代码
This commit is contained in:
yjj38 2025-07-03 17:25:34 +08:00
parent 0ac201e93b
commit c999ee6f69
3 changed files with 2 additions and 59 deletions

View File

@ -139,55 +139,4 @@ public class ClashUtil {
System.out.println("Failed to switch proxy: " + e.getMessage()); System.out.println("Failed to switch proxy: " + e.getMessage());
} }
} }
public static boolean checkCountryIsUS() {
Request request = new Request.Builder()
.url("http://ipinfo.io/json")
.build();
OkHttpClient client = new OkHttpClient();
try (Response response = client.newCall(request).execute()) { // Synchronous call
if (!response.isSuccessful()) {
// Server returned an error
Log.e("ClashUtil", "OkHttp request unsuccessful: " + response.code());
// Consider how to handle this error synchronously.
// Maybe throw an exception or return a specific error indicator.
return false; // Or throw new IOException("Request failed with code " + response.code());
}
try (ResponseBody responseBody = response.body()) {
if (responseBody == null) {
Log.e("ClashUtil", "Response body is null");
return false; // Or throw new IOException("Response body is null");
}
String jsonData = responseBody.string();
JSONObject jsonObject = new JSONObject(jsonData);
String country = jsonObject.optString("country");
boolean isUS = "US".equalsIgnoreCase(country);
if (isUS) {
Log.i("ClashUtil", "Country is US. Full data: " + jsonData);
} else {
Log.i("ClashUtil", "Country is NOT US. It is: " + (country.isEmpty() ? "未知" : country) + ". Full data: " + jsonData);
}
return isUS;
} catch (JSONException e) {
Log.e("ClashUtil", "JSON parsing error: ", e);
// Consider re-throwing or returning an error indicator
return false;
} catch (IOException e) {
Log.e("ClashUtil", "IOException reading response body: ", e);
// Consider re-throwing or returning an error indicator
return false;
}
} catch (IOException e) {
// Network request failed
Log.e("ClashUtil", "OkHttp request failed: ", e);
// Consider re-throwing or returning an error indicator
return false;
}
}
} }

View File

@ -64,9 +64,7 @@ public class LoadDeviceWorker extends CoroutineWorker {
public void executeSingleLogic(Context context) { public void executeSingleLogic(Context context) {
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Proxy not active, starting VPN",null); LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Proxy not active, starting VPN",null);
if (!startProxyVpn(context)){ startProxyVpn(context);
return;
}
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Changing device info",null); LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Changing device info",null);
ChangeDeviceInfoUtil.changeDeviceInfo(context.getPackageName(), context); ChangeDeviceInfoUtil.changeDeviceInfo(context.getPackageName(), context);
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script",null); LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script",null);
@ -74,11 +72,10 @@ public class LoadDeviceWorker extends CoroutineWorker {
AutoJsUtil.runAutojsScript(context); AutoJsUtil.runAutojsScript(context);
} }
private boolean startProxyVpn(Context context) { private void startProxyVpn(Context context) {
if (!isNetworkAvailable(context)) { if (!isNetworkAvailable(context)) {
Toast.makeText(context, "Network is not available", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Network is not available", Toast.LENGTH_SHORT).show();
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Network is not available.",null); LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Network is not available.",null);
return false;
} }
// if (!(context instanceof Activity)) { // if (!(context instanceof Activity)) {
@ -86,15 +83,12 @@ public class LoadDeviceWorker extends CoroutineWorker {
// LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Context is not an Activity.",null); // LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Context is not an Activity.",null);
// return; // return;
// } // }
try { try {
ClashUtil.startProxy(context); // 在主线程中调用 ClashUtil.startProxy(context); // 在主线程中调用
ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170"); ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170");
return ClashUtil.checkCountryIsUS();
} catch (Exception e) { } catch (Exception e) {
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Failed to start VPN",e); LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Failed to start VPN",e);
Toast.makeText(context, "Failed to start VPN: " + (e.getMessage() != null ? e.getMessage() : "Unknown error"), Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Failed to start VPN: " + (e.getMessage() != null ? e.getMessage() : "Unknown error"), Toast.LENGTH_SHORT).show();
return false;
} }
} }
} }