refactor(proxy): 重构代理工具类并添加地理位置信息获取功能- 移除了 ClashUtil 中的 checkCountryIsUS 方法

- 在 IpUtil 中添加了 fetchGeoInfo 方法,用于获取地理位置信息
- 更新了 MainActivity 中的代理启动逻辑
- 修改了 TaskUtil 中的设备信息上传逻辑,增加了 IP信息参数
This commit is contained in:
yjj38 2025-07-03 17:06:17 +08:00
parent 11ae6347cd
commit 645c4b076a
5 changed files with 355 additions and 384 deletions

View File

@ -35,6 +35,7 @@ import com.example.studyapp.device.ChangeDeviceInfoUtil;
import com.example.studyapp.proxy.ClashUtil; import com.example.studyapp.proxy.ClashUtil;
import com.example.studyapp.service.MyAccessibilityService; import com.example.studyapp.service.MyAccessibilityService;
import com.example.studyapp.task.TaskUtil; import com.example.studyapp.task.TaskUtil;
import com.example.studyapp.utils.IpUtil;
import com.example.studyapp.utils.LogFileUtil; import com.example.studyapp.utils.LogFileUtil;
import com.example.studyapp.worker.CheckAccessibilityWorker; import com.example.studyapp.worker.CheckAccessibilityWorker;
@ -47,53 +48,53 @@ import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private static WeakReference<MainActivity> instance; private static WeakReference<MainActivity> instance;
private static final int REQUEST_CODE_STORAGE_PERMISSION = 1; private static final int REQUEST_CODE_STORAGE_PERMISSION = 1;
private static final int ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE = 1001; private static final int ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE = 1001;
public static ExecutorService executorService; public static ExecutorService executorService;
// 假设我们从配置文件中提取出了以下 name 项数据仅为部分示例数据 // 假设我们从配置文件中提取出了以下 name 项数据仅为部分示例数据
private final String[] proxyNames = { private final String[] proxyNames = {
"mr", "sr", "bq", "ml", "ht", "ga", "mk", "by", "pr", "hr", "hu", "mr", "sr", "bq", "ml", "ht", "ga", "mk", "by", "pr", "hr", "hu",
"in", "gt", "at", "kh", "bn", "mg", "kr", "ca", "gh", "ma", "md", "in", "gt", "at", "kh", "bn", "mg", "kr", "ca", "gh", "ma", "md",
"je", "pa", "ba", "mm", "ir", "gy", "mt", "ae", "es", "ng", "ls", "je", "pa", "ba", "mm", "ir", "gy", "mt", "ae", "es", "ng", "ls",
"ag", "pk", "bd", "kn", "mw", "ve", "hk", "cv", "hn", "tm", "us", "ag", "pk", "bd", "kn", "mw", "ve", "hk", "cv", "hn", "tm", "us",
"cz", "ly", "gb", "kz", "it", "bh", "sn", "fi", "co", "sx", "bm", "cz", "ly", "gb", "kz", "it", "bh", "sn", "fi", "co", "sx", "bm",
"fj", "cw", "st", "bw", "fr", "bb", "tg", "ci", "gd", "ne", "bj", "fj", "cw", "st", "bw", "fr", "bb", "tg", "ci", "gd", "ne", "bj",
"nz", "rs", "do", "cl", "lb", "nl", "re", "aw", "ug", "sv", "ar", "nz", "rs", "do", "cl", "lb", "nl", "re", "aw", "ug", "sv", "ar",
"jo", "bg", "jp", "rw", "py", "mn", "ec", "uz", "ro", "cu", "gu", "jo", "bg", "jp", "rw", "py", "mn", "ec", "uz", "ro", "cu", "gu",
"xk", "sy", "so", "zm", "tz", "ni", "sc", "my", "gf", "na", "zw", "xk", "sy", "so", "zm", "tz", "ni", "sc", "my", "gf", "na", "zw",
"la", "et", "ao", "ua", "om", "np", "mx", "mz", "dm", "ye", "gi", "la", "et", "ao", "ua", "om", "np", "mx", "mz", "dm", "ye", "gi",
"cr", "cm", "ph", "am", "th", "ch", "br", "sd", "ie", "bo", "bs", "cr", "cm", "ph", "am", "th", "ch", "br", "sd", "ie", "bo", "bs",
"tc", "vg", "pe", "sa", "dk", "tn", "ee", "jm", "lc", "pt", "qa", "tc", "vg", "pe", "sa", "dk", "tn", "ee", "jm", "lc", "pt", "qa",
"ge", "ps" "ge", "ps"
}; };
public static volatile String scriptResult; public static volatile String scriptResult;
// 初始化 ExecutorService // 初始化 ExecutorService
private void initializeExecutorService() { private void initializeExecutorService() {
if (executorService == null || executorService.isShutdown()) { if (executorService == null || executorService.isShutdown()) {
executorService = new ThreadPoolExecutor( executorService = new ThreadPoolExecutor(
1, // 核心线程数 1, // 核心线程数
1, // 最大线程数 1, // 最大线程数
0L, TimeUnit.MILLISECONDS, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(50), // 阻塞队列 new LinkedBlockingQueue<>(50), // 阻塞队列
new ThreadPoolExecutor.AbortPolicy() // 拒绝策略 new ThreadPoolExecutor.AbortPolicy() // 拒绝策略
); );
}
} }
}
/** /**
* 获取 Android 设备的 ANDROID_ID * 获取 Android 设备的 ANDROID_ID
* *
* @param context 应用上下文 * @param context 应用上下文
* @return 设备的 ANDROID_ID若无法获取则返回 null * @return 设备的 ANDROID_ID若无法获取则返回 null
*/ */
private String getAndroidId(Context context) { private String getAndroidId(Context context) {
// if (context == null) { // if (context == null) {
// LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "getAndroidId: Context cannot be null",null); // LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "getAndroidId: Context cannot be null",null);
// throw new IllegalArgumentException("Context cannot be null"); // throw new IllegalArgumentException("Context cannot be null");
@ -107,89 +108,89 @@ public class MainActivity extends AppCompatActivity {
// LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "getAndroidId: Failed to get ANDROID_ID",e); // LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "getAndroidId: Failed to get ANDROID_ID",e);
// return null; // return null;
// } // }
return "FyZqWrStUvOpKlMn"; return "FyZqWrStUvOpKlMn";
}
private static final int REQUEST_CODE_PERMISSIONS = 100;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LogFileUtil.initialize(this);
setContentView(R.layout.activity_main);
instance = new WeakReference<>(this);
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Initializing application", null);
initializeExecutorService();
System.setProperty("java.library.path", this.getApplicationInfo().nativeLibraryDir);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_CODE_STORAGE_PERMISSION
);
}
} else {
if (!Environment.isExternalStorageManager()) {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE);
}
}
if (!isNetworkAvailable(this)) {
Toast.makeText(this, "Network is not available", Toast.LENGTH_SHORT).show();
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "Network not available, closing app.", null);
finish();
} }
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Setting up work manager", null);
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(CheckAccessibilityWorker.class, 15, TimeUnit.MINUTES)
.build();
WorkManager.getInstance(this).enqueue(workRequest);
private static final int REQUEST_CODE_PERMISSIONS = 100; LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Setting up UI components", null);
Button runScriptButton = findViewById(R.id.run_script_button);
@Override if (runScriptButton != null) {
protected void onCreate(@Nullable Bundle savedInstanceState) { runScriptButton.setOnClickListener(v -> {
super.onCreate(savedInstanceState);
LogFileUtil.initialize(this);
setContentView(R.layout.activity_main);
instance = new WeakReference<>(this);
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Initializing application", null);
initializeExecutorService(); initializeExecutorService();
System.setProperty("java.library.path", this.getApplicationInfo().nativeLibraryDir);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_CODE_STORAGE_PERMISSION
);
}
} else {
if (!Environment.isExternalStorageManager()) {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE);
}
}
if (!isNetworkAvailable(this)) {
Toast.makeText(this, "Network is not available", Toast.LENGTH_SHORT).show();
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "Network not available, closing app.", null);
finish();
}
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Setting up work manager", null); executorService.submit(() -> {
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(CheckAccessibilityWorker.class, 15, TimeUnit.MINUTES) try {
.build(); TaskUtil.infoUpload(MainActivity.this, getAndroidId(MainActivity.this), "wsj.reader_sp");
WorkManager.getInstance(this).enqueue(workRequest); } catch (Exception e) {
e.printStackTrace();
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Setting up UI components", null); }
Button runScriptButton = findViewById(R.id.run_script_button); });
if (runScriptButton != null) { });
runScriptButton.setOnClickListener(v -> {
initializeExecutorService();
executorService.submit(() -> {
try {
TaskUtil.infoUpload(MainActivity.this, getAndroidId(MainActivity.this), "wsj.reader_sp");
} catch (Exception e) {
e.printStackTrace();
}
});
});
// runScriptButton.setOnClickListener(v -> AutoJsUtil.runAutojsScript(this)); // runScriptButton.setOnClickListener(v -> AutoJsUtil.runAutojsScript(this));
} else { } else {
LogFileUtil.logAndWrite(Log.WARN, "MainActivity", "Run Script Button not found", null); LogFileUtil.logAndWrite(Log.WARN, "MainActivity", "Run Script Button not found", null);
Toast.makeText(this, "Button not found", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Button not found", Toast.LENGTH_SHORT).show();
} }
Button connectButton = findViewById(R.id.connectVpnButton); Button connectButton = findViewById(R.id.connectVpnButton);
if (connectButton != null) { if (connectButton != null) {
connectButton.setOnClickListener(v -> startProxyVpn(this)); connectButton.setOnClickListener(v -> startProxyVpn(this));
} else { } else {
Toast.makeText(this, "Connect button not found", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Connect button not found", Toast.LENGTH_SHORT).show();
} }
Button disconnectButton = findViewById(R.id.disconnectVpnButton); Button disconnectButton = findViewById(R.id.disconnectVpnButton);
if (disconnectButton != null) { if (disconnectButton != null) {
disconnectButton.setOnClickListener(v -> ClashUtil.stopProxy(this)); disconnectButton.setOnClickListener(v -> ClashUtil.stopProxy(this));
} else { } else {
Toast.makeText(this, "Disconnect button not found", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Disconnect button not found", Toast.LENGTH_SHORT).show();
} }
Button switchVpnButton = findViewById(R.id.switchVpnButton); Button switchVpnButton = findViewById(R.id.switchVpnButton);
if (switchVpnButton != null) { if (switchVpnButton != null) {
switchVpnButton.setOnClickListener(v -> ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170")); switchVpnButton.setOnClickListener(v -> ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170"));
} else { } else {
Toast.makeText(this, "Disconnect button not found", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Disconnect button not found", Toast.LENGTH_SHORT).show();
} }
// Button modifyDeviceInfoButton = findViewById(R.id.modifyDeviceInfoButton); // Button modifyDeviceInfoButton = findViewById(R.id.modifyDeviceInfoButton);
// if (modifyDeviceInfoButton != null) { // if (modifyDeviceInfoButton != null) {
@ -198,101 +199,101 @@ public class MainActivity extends AppCompatActivity {
// Toast.makeText(this, "modifyDeviceInfo button not found", Toast.LENGTH_SHORT).show(); // Toast.makeText(this, "modifyDeviceInfo button not found", Toast.LENGTH_SHORT).show();
// } // }
Button resetDeviceInfoButton = findViewById(R.id.resetDeviceInfoButton); Button resetDeviceInfoButton = findViewById(R.id.resetDeviceInfoButton);
if (resetDeviceInfoButton != null) { if (resetDeviceInfoButton != null) {
resetDeviceInfoButton.setOnClickListener(v -> ChangeDeviceInfoUtil.resetChangedDeviceInfo(getPackageName(), this)); resetDeviceInfoButton.setOnClickListener(v -> ChangeDeviceInfoUtil.resetChangedDeviceInfo(getPackageName(), this));
} else { } else {
Toast.makeText(this, "resetDeviceInfo button not found", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "resetDeviceInfo button not found", Toast.LENGTH_SHORT).show();
} }
// 初始化 ChangeDeviceInfoUtil // 初始化 ChangeDeviceInfoUtil
String androidId = getAndroidId(this); String androidId = getAndroidId(this);
String taskId = UUID.randomUUID().toString(); String taskId = UUID.randomUUID().toString();
// ChangeDeviceInfoUtil.getAddDeviceInfo("US", 2); // ChangeDeviceInfoUtil.getAddDeviceInfo("US", 2);
// 获取输入框和按钮 // 获取输入框和按钮
Button executeButton = findViewById(R.id.execute_button); Button executeButton = findViewById(R.id.execute_button);
Button stopExecuteButton = findViewById(R.id.stop_execute_button); Button stopExecuteButton = findViewById(R.id.stop_execute_button);
// 设置按钮的点击事件 // 设置按钮的点击事件
if (executeButton != null) { if (executeButton != null) {
executeButton.setOnClickListener(v -> { executeButton.setOnClickListener(v -> {
executeButton.setEnabled(false); executeButton.setEnabled(false);
Toast.makeText(this, "任务正在执行", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "任务正在执行", Toast.LENGTH_SHORT).show();
executeLogic(androidId, taskId); executeLogic(androidId, taskId);
}); });
} }
if (stopExecuteButton != null) { if (stopExecuteButton != null) {
stopExecuteButton.setOnClickListener(v -> { stopExecuteButton.setOnClickListener(v -> {
if (executorService != null && !executorService.isShutdown()) { if (executorService != null && !executorService.isShutdown()) {
executorService.shutdownNow(); executorService.shutdownNow();
ClashUtil.stopProxy(this); ClashUtil.stopProxy(this);
AutoJsUtil.stopAutojsScript(this); AutoJsUtil.stopAutojsScript(this);
executeButton.setEnabled(true); executeButton.setEnabled(true);
}
});
} else {
Toast.makeText(this, "Stop button not found", Toast.LENGTH_SHORT).show();
} }
});
} else {
Toast.makeText(this, "Stop button not found", Toast.LENGTH_SHORT).show();
}
}
private void executeLogic(String androidId, String taskId) {
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeLogic: Start execution", null);
if (!isNetworkAvailable(this)) {
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "executeLogic: Network is not available!", null);
Toast.makeText(this, "网络不可用,请检查网络连接", Toast.LENGTH_SHORT).show();
return;
} }
private void executeLogic(String androidId, String taskId) { LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeLogic: Submitting job to executor", null);
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeLogic: Start execution", null); initializeExecutorService();
if (!isNetworkAvailable(this)) { executorService.submit(() -> {
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "executeLogic: Network is not available!", null); try {
Toast.makeText(this, "网络不可用,请检查网络连接", Toast.LENGTH_SHORT).show(); ChangeDeviceInfoUtil.getAddDeviceInfo("US", 2, (bigoDevice, afDevice) -> {
return; startProxyVpn(MainActivity.this);
} ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), MainActivity.this, bigoDevice, afDevice);
AutoJsUtil.runAutojsScript(this);
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeLogic: Submitting job to executor", null);
initializeExecutorService();
executorService.submit(() -> {
try {
ChangeDeviceInfoUtil.getAddDeviceInfo("US", 2, (bigoDevice, afDevice) -> {
if (startProxyVpn(MainActivity.this)){
ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), MainActivity.this, bigoDevice, afDevice);
AutoJsUtil.runAutojsScript(this);
}
});
AutoJsUtil.registerScriptResultReceiver(this);
while (isRunning) {
if (!isRunning) break;
try {
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script", null);
String currentScriptResult = scriptResultQueue.take();
ChangeDeviceInfoUtil.getAddDeviceInfo("US", 2, (bigoDevice, afDevice) -> {
try {
if (startProxyVpn(MainActivity.this)){
ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), MainActivity.this, bigoDevice, afDevice);
AutoJsUtil.runAutojsScript(this);
if (currentScriptResult != null && !TextUtils.isEmpty(currentScriptResult)) {
TaskUtil.execSaveTask(this, androidId, taskId, currentScriptResult);
infoUpload(this, androidId, currentScriptResult);
}
}
} catch (Exception e) {
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "changeDeviceInfo erro", e);
}
});
LogFileUtil.logAndWrite(android.util.Log.DEBUG, "MainActivity", "----发送result------;" + currentScriptResult, null);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "loop: Thread interrupted while waiting", e);
} catch(Exception e) {
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "loop erro", e);
}
}
} catch (Exception e) {
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "start executeLogic: Unexpected task error.", e);
}
}); });
} AutoJsUtil.registerScriptResultReceiver(this);
public static final LinkedBlockingQueue<String> scriptResultQueue = new LinkedBlockingQueue<>(1); while (isRunning) {
private volatile boolean isRunning = true; // 主线程运行状态 if (!isRunning) {
public static final Object taskLock = new Object(); // 任务逻辑锁 break;
}
try {
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script", null);
String currentScriptResult = scriptResultQueue.take();
ChangeDeviceInfoUtil.getAddDeviceInfo("US", 2, (bigoDevice, afDevice) -> {
try {
startProxyVpn(MainActivity.this);
ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), MainActivity.this, bigoDevice, afDevice);
AutoJsUtil.runAutojsScript(this);
if (currentScriptResult != null && !TextUtils.isEmpty(currentScriptResult)) {
TaskUtil.execSaveTask(this, androidId, taskId, currentScriptResult, IpUtil.fetchGeoInfo());
infoUpload(this, androidId, currentScriptResult);
}
} catch (Exception e) {
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "changeDeviceInfo erro", e);
}
});
LogFileUtil.logAndWrite(android.util.Log.DEBUG, "MainActivity", "----发送result------;" + currentScriptResult, null);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "loop: Thread interrupted while waiting", e);
} catch (Exception e) {
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "loop erro", e);
}
}
} catch (Exception e) {
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "start executeLogic: Unexpected task error.", e);
}
});
}
public static final LinkedBlockingQueue<String> scriptResultQueue = new LinkedBlockingQueue<>(1);
private volatile boolean isRunning = true; // 主线程运行状态
public static final Object taskLock = new Object(); // 任务逻辑锁
// public void executeSingleLogic() { // public void executeSingleLogic() {
//// LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Proxy not active, starting VPN",null); //// LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Proxy not active, starting VPN",null);
@ -300,138 +301,133 @@ public class MainActivity extends AppCompatActivity {
// LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Changing device info",null); // LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Changing device info",null);
// ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this); // ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this);
/// / LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script",null); /// / LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script",null);
/// ///
/// @return /// @return
// AutoJsUtil.runAutojsScript(this); // AutoJsUtil.runAutojsScript(this);
// } // }
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)) {
Toast.makeText(context, "Context must be an Activity", Toast.LENGTH_SHORT).show();
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Context is not an Activity.", null);
return false;
}
try {
ClashUtil.startProxy(context); // 在主线程中调用
ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170");
return ClashUtil.checkCountryIsUS();
} catch (Exception 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();
}
return false;
} }
@Override if (!(context instanceof Activity)) {
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { Toast.makeText(context, "Context must be an Activity", Toast.LENGTH_SHORT).show();
super.onRequestPermissionsResult(requestCode, permissions, grantResults); LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Context is not an Activity.", null);
if (requestCode == REQUEST_CODE_STORAGE_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show();
} else {
// 提示权限被拒绝同时允许用户重新授予权限
showPermissionExplanationDialog();
}
}
if (requestCode == REQUEST_CODE_PERMISSIONS) {
boolean allGranted = true;
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
allGranted = false;
break;
}
}
if (allGranted) {
// 所有权限已授予
startMyForegroundService();
} else {
Toast.makeText(this, "未授予必要权限,请检查设置", Toast.LENGTH_SHORT).show();
}
}
} }
try {
private void startMyForegroundService() { ClashUtil.startProxy(context); // 在主线程中调用
Intent serviceIntent = new Intent(this, MyAccessibilityService.class); ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170");
ContextCompat.startForegroundService(this, serviceIntent); } catch (Exception 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();
} }
}
@Override
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onRequestPermissionsResult(requestCode, permissions, grantResults);
super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_STORAGE_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
switch (requestCode) { Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show();
case ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE: } else {
handleStoragePermissionResult(resultCode); // 提示权限被拒绝同时允许用户重新授予权限
break; showPermissionExplanationDialog();
default: }
break; }
if (requestCode == REQUEST_CODE_PERMISSIONS) {
boolean allGranted = true;
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
allGranted = false;
break;
} }
} }
private void handleStoragePermissionResult(int resultCode) { if (allGranted) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && Environment.isExternalStorageManager()) { // 所有权限已授予
Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show(); startMyForegroundService();
} else { } else {
Toast.makeText(this, "请授予所有文件管理权限", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "未授予必要权限,请检查设置", Toast.LENGTH_SHORT).show();
finish(); }
}
} }
}
private void showPermissionExplanationDialog() { private void startMyForegroundService() {
AlertDialog.Builder builder = new AlertDialog.Builder(this); Intent serviceIntent = new Intent(this, MyAccessibilityService.class);
builder.setTitle("Permission Required") ContextCompat.startForegroundService(this, serviceIntent);
.setMessage("Storage Permission is required for the app to function. Please enable it in Settings.") }
.setPositiveButton("Go to Settings", (dialog, which) -> {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
})
.setNegativeButton("Cancel", (dialog, which) -> finish())
.show();
}
@Override
protected void onDestroy() {
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onDestroy: Cleaning up resources", null);
super.onDestroy();
instance.clear();
if (AutoJsUtil.scriptResultReceiver != null) {
unregisterReceiver(AutoJsUtil.scriptResultReceiver);
AutoJsUtil.scriptResultReceiver = null;
}
if (executorService != null) {
executorService.shutdown();
}
isRunning = false;
synchronized (taskLock) {
taskLock.notifyAll();
}
}
public static MainActivity getInstance() { @Override
return instance.get(); // 返回实例 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
} super.onActivityResult(requestCode, resultCode, data);
private boolean isNetworkAvailable(Context context) { switch (requestCode) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); case ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE:
if (connectivityManager != null) { handleStoragePermissionResult(resultCode);
Network network = connectivityManager.getActiveNetwork(); break;
if (network != null) { default:
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network); break;
return capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
}
}
return false;
} }
}
private void handleStoragePermissionResult(int resultCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && Environment.isExternalStorageManager()) {
Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "请授予所有文件管理权限", Toast.LENGTH_SHORT).show();
finish();
}
}
private void showPermissionExplanationDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Permission Required")
.setMessage("Storage Permission is required for the app to function. Please enable it in Settings.")
.setPositiveButton("Go to Settings", (dialog, which) -> {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
})
.setNegativeButton("Cancel", (dialog, which) -> finish())
.show();
}
@Override
protected void onDestroy() {
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onDestroy: Cleaning up resources", null);
super.onDestroy();
instance.clear();
if (AutoJsUtil.scriptResultReceiver != null) {
unregisterReceiver(AutoJsUtil.scriptResultReceiver);
AutoJsUtil.scriptResultReceiver = null;
}
if (executorService != null) {
executorService.shutdown();
}
isRunning = false;
synchronized (taskLock) {
taskLock.notifyAll();
}
}
public static MainActivity getInstance() {
return instance.get(); // 返回实例
}
private boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
Network network = connectivityManager.getActiveNetwork();
if (network != null) {
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
return capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
}
}
return false;
}
} }

View File

@ -133,53 +133,4 @@ public class ClashUtil {
} }
} }
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

@ -1,18 +1,12 @@
package com.example.studyapp.task; package com.example.studyapp.task;
import static androidx.core.content.PackageManagerCompat.LOG_TAG;
import android.content.Context; import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.example.studyapp.utils.FileUtils; import com.example.studyapp.utils.FileUtils;
import com.example.studyapp.utils.LogFileUtil; import com.example.studyapp.utils.LogFileUtil;
import com.example.studyapp.utils.ShellUtils; import com.example.studyapp.utils.ShellUtils;
import com.example.studyapp.utils.ZipUtils; import com.example.studyapp.utils.ZipUtils;
import com.google.android.gms.common.util.CollectionUtils;
import com.google.android.gms.common.util.MapUtils;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
@ -21,15 +15,10 @@ import java.io.FileInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.MediaType; import okhttp3.MediaType;
import okhttp3.MultipartBody; import okhttp3.MultipartBody;
@ -38,7 +27,6 @@ import okhttp3.Request;
import okhttp3.RequestBody; import okhttp3.RequestBody;
import okhttp3.Response; import okhttp3.Response;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -65,7 +53,7 @@ public class TaskUtil {
.readTimeout(30, TimeUnit.SECONDS) // 读取超时 .readTimeout(30, TimeUnit.SECONDS) // 读取超时
.build(); .build();
public static void postDeviceInfo(String androidId, String taskId,String packageName) { public static void postDeviceInfo(String androidId, String taskId,String packageName, String ipInfo) {
Log.i("TaskUtil", "postDeviceInfo called with androidId: " + androidId); Log.i("TaskUtil", "postDeviceInfo called with androidId: " + androidId);
if (okHttpClient == null) { if (okHttpClient == null) {
@ -99,6 +87,7 @@ public class TaskUtil {
.addQueryParameter("id", androidId) .addQueryParameter("id", androidId)
.addQueryParameter("taskId", taskId) .addQueryParameter("taskId", taskId)
.addQueryParameter("packageName", packageName) .addQueryParameter("packageName", packageName)
.addQueryParameter("deviceIp",ipInfo)
.build(); .build();
Log.d("TaskUtil", "Request URL: " + url.toString()); Log.d("TaskUtil", "Request URL: " + url.toString());
@ -783,7 +772,7 @@ public class TaskUtil {
return getDeviceInfoSync(androidId,taskId); return getDeviceInfoSync(androidId,taskId);
} }
public static void execSaveTask(Context context, String androidId, String taskId,String packName) { public static void execSaveTask(Context context, String androidId, String taskId,String packName, String ipInfo) {
if (context == null) { if (context == null) {
throw new IllegalArgumentException("Context or Package name cannot be null or empty"); throw new IllegalArgumentException("Context or Package name cannot be null or empty");
} }
@ -794,7 +783,7 @@ public class TaskUtil {
} }
try { try {
postDeviceInfo(androidId, taskId,packName); postDeviceInfo(androidId, taskId,packName,ipInfo);
} catch (Exception e) { } catch (Exception e) {
System.err.println("Error in execReloginTask: " + e.getMessage()); System.err.println("Error in execReloginTask: " + e.getMessage());
e.printStackTrace(); e.printStackTrace();

View File

@ -2,8 +2,14 @@ package com.example.studyapp.utils;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -46,6 +52,35 @@ public class IpUtil {
} }
} }
public static String fetchGeoInfo() {
Request request = new Request.Builder()
.url("https://ipv4.geojs.io/v1/ip/geo.json")
.build();
OkHttpClient client = new OkHttpClient();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
Log.e("ClashUtil", "OkHttp request unsuccessful: " + response.code());
return null;
}
ResponseBody responseBody = response.body();
if (responseBody == null) {
Log.e("ClashUtil", "Response body is null");
return null;
}
String jsonData = responseBody.string();
Log.i("ClashUtil", "Geo info: " + jsonData);
return jsonData;
} catch (IOException e) {
Log.e("ClashUtil", "OkHttp request failed: ", e);
return null;
}
}
public static String checkClientIp(String excludeCountry) public static String checkClientIp(String excludeCountry)
{ {
try { try {

View File

@ -131,14 +131,14 @@ public class TaskUtilTest {
// 运行上传方法 // 运行上传方法
String taskId = "asddasdasd"; String taskId = "asddasdasd";
TaskUtil.postDeviceInfo("b3d893cf9de3a85a", taskId, "com.example.studyapp"); //TaskUtil.postDeviceInfo("b3d893cf9de3a85a", taskId, "com.example.studyapp", ipInfo);
} }
@Test @Test
public void testGetDeviceInfoSync_Success() throws Exception { public void testGetDeviceInfoSync_Success() throws Exception {
// 运行上传方法 // 运行上传方法
TaskUtil.getDeviceInfoSync("b3d893cf9de3a85a"); //TaskUtil.getDeviceInfoSync("b3d893cf9de3a85a");
} }
// @Test // @Test