refactor(proxy): 重构代理工具类并添加地理位置信息获取功能- 移除了 ClashUtil 中的 checkCountryIsUS 方法
- 在 IpUtil 中添加了 fetchGeoInfo 方法,用于获取地理位置信息 - 更新了 MainActivity 中的代理启动逻辑 - 修改了 TaskUtil 中的设备信息上传逻辑,增加了 IP信息参数
This commit is contained in:
parent
11ae6347cd
commit
645c4b076a
|
@ -35,6 +35,7 @@ import com.example.studyapp.device.ChangeDeviceInfoUtil;
|
|||
import com.example.studyapp.proxy.ClashUtil;
|
||||
import com.example.studyapp.service.MyAccessibilityService;
|
||||
import com.example.studyapp.task.TaskUtil;
|
||||
import com.example.studyapp.utils.IpUtil;
|
||||
import com.example.studyapp.utils.LogFileUtil;
|
||||
import com.example.studyapp.worker.CheckAccessibilityWorker;
|
||||
|
||||
|
@ -250,28 +251,28 @@ public class MainActivity extends AppCompatActivity {
|
|||
executorService.submit(() -> {
|
||||
try {
|
||||
ChangeDeviceInfoUtil.getAddDeviceInfo("US", 2, (bigoDevice, afDevice) -> {
|
||||
if (startProxyVpn(MainActivity.this)){
|
||||
startProxyVpn(MainActivity.this);
|
||||
ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), MainActivity.this, bigoDevice, afDevice);
|
||||
AutoJsUtil.runAutojsScript(this);
|
||||
}
|
||||
});
|
||||
AutoJsUtil.registerScriptResultReceiver(this);
|
||||
|
||||
while (isRunning) {
|
||||
if (!isRunning) break;
|
||||
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)){
|
||||
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);
|
||||
TaskUtil.execSaveTask(this, androidId, taskId, currentScriptResult, IpUtil.fetchGeoInfo());
|
||||
infoUpload(this, androidId, currentScriptResult);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "changeDeviceInfo erro", e);
|
||||
}
|
||||
|
@ -305,28 +306,23 @@ public class MainActivity extends AppCompatActivity {
|
|||
/// @return
|
||||
// AutoJsUtil.runAutojsScript(this);
|
||||
// }
|
||||
private boolean startProxyVpn(Context context) {
|
||||
private void startProxyVpn(Context context) {
|
||||
if (!isNetworkAvailable(context)) {
|
||||
Toast.makeText(context, "Network is not available", Toast.LENGTH_SHORT).show();
|
||||
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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,18 +1,12 @@
|
|||
package com.example.studyapp.task;
|
||||
|
||||
import static androidx.core.content.PackageManagerCompat.LOG_TAG;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.example.studyapp.utils.FileUtils;
|
||||
import com.example.studyapp.utils.LogFileUtil;
|
||||
import com.example.studyapp.utils.ShellUtils;
|
||||
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.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
@ -21,15 +15,10 @@ import java.io.FileInputStream;
|
|||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.MultipartBody;
|
||||
|
@ -38,7 +27,6 @@ import okhttp3.Request;
|
|||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
|
@ -65,7 +53,7 @@ public class TaskUtil {
|
|||
.readTimeout(30, TimeUnit.SECONDS) // 读取超时
|
||||
.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);
|
||||
|
||||
if (okHttpClient == null) {
|
||||
|
@ -99,6 +87,7 @@ public class TaskUtil {
|
|||
.addQueryParameter("id", androidId)
|
||||
.addQueryParameter("taskId", taskId)
|
||||
.addQueryParameter("packageName", packageName)
|
||||
.addQueryParameter("deviceIp",ipInfo)
|
||||
.build();
|
||||
|
||||
Log.d("TaskUtil", "Request URL: " + url.toString());
|
||||
|
@ -783,7 +772,7 @@ public class TaskUtil {
|
|||
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) {
|
||||
throw new IllegalArgumentException("Context or Package name cannot be null or empty");
|
||||
}
|
||||
|
@ -794,7 +783,7 @@ public class TaskUtil {
|
|||
}
|
||||
|
||||
try {
|
||||
postDeviceInfo(androidId, taskId,packName);
|
||||
postDeviceInfo(androidId, taskId,packName,ipInfo);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error in execReloginTask: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
|
|
|
@ -2,8 +2,14 @@ package com.example.studyapp.utils;
|
|||
|
||||
import android.text.TextUtils;
|
||||
|
||||
import android.util.Log;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.json.JSONObject;
|
||||
|
||||
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)
|
||||
{
|
||||
try {
|
||||
|
|
|
@ -131,14 +131,14 @@ public class TaskUtilTest {
|
|||
|
||||
// 运行上传方法
|
||||
String taskId = "asddasdasd";
|
||||
TaskUtil.postDeviceInfo("b3d893cf9de3a85a", taskId, "com.example.studyapp");
|
||||
//TaskUtil.postDeviceInfo("b3d893cf9de3a85a", taskId, "com.example.studyapp", ipInfo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDeviceInfoSync_Success() throws Exception {
|
||||
|
||||
// 运行上传方法
|
||||
TaskUtil.getDeviceInfoSync("b3d893cf9de3a85a");
|
||||
//TaskUtil.getDeviceInfoSync("b3d893cf9de3a85a");
|
||||
}
|
||||
|
||||
// @Test
|
||||
|
|
Loading…
Reference in New Issue