代理切换
This commit is contained in:
parent
2ddd9a5f80
commit
b8acabc545
|
@ -1,5 +1,6 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
|
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||||
<component name="GradleSettings">
|
<component name="GradleSettings">
|
||||||
<option name="linkedExternalProjectsSettings">
|
<option name="linkedExternalProjectsSettings">
|
||||||
<GradleProjectSettings>
|
<GradleProjectSettings>
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="jbr-21" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="jbr-21" project-jdk-type="JavaSDK">
|
||||||
|
|
|
@ -203,9 +203,10 @@ public class MainActivity extends AppCompatActivity {
|
||||||
LogFileUtil.logAndWrite(Log.INFO, TAG, "onCreate: Setting up UI components", null);
|
LogFileUtil.logAndWrite(Log.INFO, TAG, "onCreate: Setting up UI components", null);
|
||||||
String androidId = getAndroidId(this);
|
String androidId = getAndroidId(this);
|
||||||
String taskId = UUID.randomUUID().toString();
|
String taskId = UUID.randomUUID().toString();
|
||||||
|
|
||||||
setupButton(R.id.run_script_button, v -> executeRunScript(androidId));
|
setupButton(R.id.run_script_button, v -> executeRunScript(androidId));
|
||||||
setupButton(R.id.connectVpnButton, v -> startProxyVpn(this));
|
setupButton(R.id.connectVpnButton, v ->executorService.submit(() -> {
|
||||||
|
startProxyVpn(MainActivity.this);
|
||||||
|
}));
|
||||||
setupButton(R.id.disconnectVpnButton, v -> ClashUtil.stopProxy(this));
|
setupButton(R.id.disconnectVpnButton, v -> ClashUtil.stopProxy(this));
|
||||||
setupButton(R.id.switchVpnButton, v -> {
|
setupButton(R.id.switchVpnButton, v -> {
|
||||||
ClashUtil.switchProxyGroup("GLOBAL", switchCountry(), "http://127.0.0.1:6170");
|
ClashUtil.switchProxyGroup("GLOBAL", switchCountry(), "http://127.0.0.1:6170");
|
||||||
|
@ -383,13 +384,10 @@ public class MainActivity extends AppCompatActivity {
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Network is not available.", null);
|
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Network is not available.", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
ClashUtil.startProxy(context);
|
ClashUtil.startProxy(context);
|
||||||
ClashUtil.switchProxyGroup("GLOBAL", switchCountry(), "http://127.0.0.1:6170");
|
ClashUtil.switchProxyWithPort(switchCountry());
|
||||||
|
ClashUtil.switchProxyGroup("GLOBAL", "my-socks5-proxy", "http://127.0.0.1:6170");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, TAG, "startProxyVpn: Failed to start VPN", e);
|
LogFileUtil.logAndWrite(Log.ERROR, TAG, "startProxyVpn: Failed to start VPN", e);
|
||||||
Toast.makeText(context, "Failed to start VPN: " +
|
Toast.makeText(context, "Failed to start VPN: " +
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
package com.example.studyapp
|
|
||||||
|
|
||||||
class ScriptRepository {
|
|
||||||
}
|
|
|
@ -31,8 +31,10 @@ public class AutoJsUtil {
|
||||||
// 检查脚本文件
|
// 检查脚本文件
|
||||||
LogFileUtil.logAndWrite(android.util.Log.INFO, "AutoJsUtil", "-------脚本运行开始:--------" + count++,null);
|
LogFileUtil.logAndWrite(android.util.Log.INFO, "AutoJsUtil", "-------脚本运行开始:--------" + count++,null);
|
||||||
File scriptDir = new File(Environment.getExternalStorageDirectory(), "script");//todo
|
File scriptDir = new File(Environment.getExternalStorageDirectory(), "script");//todo
|
||||||
scriptDir.delete();
|
File oldFile = new File(scriptDir, "main.js");
|
||||||
// File scriptFile = new File(scriptDir, "main.js");
|
if (oldFile.exists()){
|
||||||
|
oldFile.delete();
|
||||||
|
}
|
||||||
File scriptFile = downloadCodeFile("main.js", scriptDir);
|
File scriptFile = downloadCodeFile("main.js", scriptDir);
|
||||||
if (scriptFile == null || !scriptFile.exists()) {
|
if (scriptFile == null || !scriptFile.exists()) {
|
||||||
runOnUiThread(() -> Toast.makeText(context, "下载脚本文件失败", Toast.LENGTH_SHORT).show());
|
runOnUiThread(() -> Toast.makeText(context, "下载脚本文件失败", Toast.LENGTH_SHORT).show());
|
||||||
|
|
|
@ -4,9 +4,14 @@ import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.os.Environment;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
import com.example.studyapp.utils.LogFileUtil;
|
import com.example.studyapp.utils.LogFileUtil;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import okhttp3.Call;
|
import okhttp3.Call;
|
||||||
|
@ -85,6 +90,50 @@ public class ClashUtil {
|
||||||
context.unregisterReceiver(clashStatusReceiver);
|
context.unregisterReceiver(clashStatusReceiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int getProxyPort(){
|
||||||
|
File scriptDir = new File(Environment.getExternalStorageDirectory(), "script");
|
||||||
|
File portFile = new File(scriptDir, "ip.port.json");
|
||||||
|
StringBuilder text = new StringBuilder();
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(portFile))) {
|
||||||
|
String line;
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
text.append(line).append('\n');
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e("TAG", "getProxyPort: ", e);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int port = -1;
|
||||||
|
try{
|
||||||
|
JSONObject config = new JSONObject(text.toString());
|
||||||
|
config.optInt("port", -1);
|
||||||
|
}catch (Exception e){
|
||||||
|
Log.e("TAG", "getProxyPort: ", e);
|
||||||
|
}
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void switchProxyWithPort(String country) {
|
||||||
|
int port = getProxyPort();
|
||||||
|
String url = "http://39.103.73.250/tt/test/testProxy.jsp?port="+port+"&country="+country;
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.build();
|
||||||
|
try {
|
||||||
|
Response response = client.newCall(request).execute();
|
||||||
|
if (response.isSuccessful() && response.body() != null) {
|
||||||
|
LogFileUtil.logAndWrite(Log.INFO, "ClashUtil", "switchProxyGroup: Switch proxy response", null);
|
||||||
|
} else {
|
||||||
|
LogFileUtil.logAndWrite(Log.ERROR, "ClashUtil", "switchProxyGroup: Response is not successful or body is null", null);
|
||||||
|
}
|
||||||
|
response.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
LogFileUtil.logAndWrite(Log.ERROR, "ClashUtil", "switchProxyGroup: Failed to switch proxy", e);
|
||||||
|
System.out.println("Failed to switch proxy: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void switchProxyGroup(String groupName, String proxyName, String controllerUrl) {
|
public static void switchProxyGroup(String groupName, String proxyName, String controllerUrl) {
|
||||||
if (groupName == null || groupName.trim().isEmpty() || proxyName == null || proxyName.trim().isEmpty()) {
|
if (groupName == null || groupName.trim().isEmpty() || proxyName == null || proxyName.trim().isEmpty()) {
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "ClashUtil", "switchProxyGroup: Invalid arguments", null);
|
LogFileUtil.logAndWrite(Log.ERROR, "ClashUtil", "switchProxyGroup: Invalid arguments", null);
|
||||||
|
|
Loading…
Reference in New Issue