代理切换
This commit is contained in:
parent
b8acabc545
commit
33259cf04e
|
@ -387,7 +387,7 @@ public class MainActivity extends AppCompatActivity {
|
||||||
try {
|
try {
|
||||||
ClashUtil.startProxy(context);
|
ClashUtil.startProxy(context);
|
||||||
ClashUtil.switchProxyWithPort(switchCountry());
|
ClashUtil.switchProxyWithPort(switchCountry());
|
||||||
ClashUtil.switchProxyGroup("GLOBAL", "my-socks5-proxy", "http://127.0.0.1:6170");
|
ClashUtil.switchProxyGroup("PROXY", "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: " +
|
||||||
|
|
|
@ -14,6 +14,8 @@ import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import okhttp3.Call;
|
import okhttp3.Call;
|
||||||
import okhttp3.Callback;
|
import okhttp3.Callback;
|
||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
|
@ -23,6 +25,8 @@ import okhttp3.Request;
|
||||||
import okhttp3.RequestBody;
|
import okhttp3.RequestBody;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
import okhttp3.ResponseBody;
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
@ -34,6 +38,18 @@ import org.json.JSONObject;
|
||||||
* @Description:
|
* @Description:
|
||||||
*/
|
*/
|
||||||
public class ClashUtil {
|
public class ClashUtil {
|
||||||
|
private static final HttpLoggingInterceptor httpLoggingInterceptor= new HttpLoggingInterceptor();
|
||||||
|
|
||||||
|
private static final OkHttpClient sharedClient ;
|
||||||
|
|
||||||
|
static {
|
||||||
|
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||||
|
sharedClient = new OkHttpClient.Builder()
|
||||||
|
.connectTimeout(10, TimeUnit.SECONDS) // 连接超时
|
||||||
|
.readTimeout(30, TimeUnit.SECONDS) // 读取超时
|
||||||
|
.addInterceptor(httpLoggingInterceptor)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
public static void startProxy(Context context) {
|
public static void startProxy(Context context) {
|
||||||
Intent intent = new Intent("com.github.kr328.clash.intent.action.SESSION_CREATE");
|
Intent intent = new Intent("com.github.kr328.clash.intent.action.SESSION_CREATE");
|
||||||
|
@ -115,22 +131,31 @@ public class ClashUtil {
|
||||||
|
|
||||||
public static void switchProxyWithPort(String country) {
|
public static void switchProxyWithPort(String country) {
|
||||||
int port = getProxyPort();
|
int port = getProxyPort();
|
||||||
String url = "http://39.103.73.250/tt/test/testProxy.jsp?port="+port+"&country="+country;
|
// 安全构建 URL
|
||||||
OkHttpClient client = new OkHttpClient();
|
HttpUrl url = HttpUrl.parse("http://39.103.73.250/tt/test/testProxy.jsp")
|
||||||
|
.newBuilder()
|
||||||
|
.addQueryParameter("port", port+"")
|
||||||
|
.addQueryParameter("country", country)
|
||||||
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
try {
|
|
||||||
Response response = client.newCall(request).execute();
|
// 使用 try-with-resources 确保 Response 自动关闭
|
||||||
if (response.isSuccessful() && response.body() != null) {
|
try (Response response = sharedClient.newCall(request).execute()) {
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "ClashUtil", "switchProxyGroup: Switch proxy response", null);
|
// 读取并记录响应内容
|
||||||
|
String responseBody = response.body() != null ? response.body().string() : "Empty response body";
|
||||||
|
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
LogFileUtil.logAndWrite(Log.INFO, "ClashUtil",
|
||||||
|
"switchProxyGroup: Success | Status: " + response.code() + " | Response: " + responseBody, null);
|
||||||
} else {
|
} else {
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "ClashUtil", "switchProxyGroup: Response is not successful or body is null", null);
|
LogFileUtil.logAndWrite(Log.ERROR, "ClashUtil",
|
||||||
|
"switchProxyGroup: Failed | Status: " + response.code() + " | Response: " + responseBody, null);
|
||||||
}
|
}
|
||||||
response.close();
|
} catch (Exception e) {
|
||||||
} catch (IOException e) {
|
LogFileUtil.logAndWrite(Log.ERROR, "ClashUtil", "switchProxyGroup: Unexpected error", e);
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "ClashUtil", "switchProxyGroup: Failed to switch proxy", e);
|
|
||||||
System.out.println("Failed to switch proxy: " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ import okhttp3.*
|
||||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||||
import okhttp3.RequestBody.Companion.toRequestBody
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
@ -17,6 +18,9 @@ object HttpUtils {
|
||||||
.connectTimeout(15, TimeUnit.SECONDS)
|
.connectTimeout(15, TimeUnit.SECONDS)
|
||||||
.readTimeout(15, TimeUnit.SECONDS)
|
.readTimeout(15, TimeUnit.SECONDS)
|
||||||
.writeTimeout(15, TimeUnit.SECONDS)
|
.writeTimeout(15, TimeUnit.SECONDS)
|
||||||
|
.addInterceptor(HttpLoggingInterceptor().apply {
|
||||||
|
level = HttpLoggingInterceptor.Level.BODY
|
||||||
|
})
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,13 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<!--
|
|
||||||
Sample data extraction rules file; uncomment and customize as necessary.
|
|
||||||
See https://developer.android.com/about/versions/12/backup-restore#xml-changes
|
|
||||||
for details.
|
|
||||||
-->
|
|
||||||
<!-- res/xml/network_security_config.xml -->
|
|
||||||
<network-security-config>
|
<network-security-config>
|
||||||
|
<base-config cleartextTrafficPermitted="true">
|
||||||
|
<trust-anchors>
|
||||||
|
<certificates src="system" />
|
||||||
|
<certificates src="user" />
|
||||||
|
</trust-anchors>
|
||||||
|
</base-config>
|
||||||
|
|
||||||
<domain-config cleartextTrafficPermitted="true">
|
<domain-config cleartextTrafficPermitted="true">
|
||||||
<domain includeSubdomains="true">47.236.153.142</domain>
|
|
||||||
<domain includeSubdomains="true">8.211.204.20</domain>
|
|
||||||
<domain includeSubdomains="true">127.0.0.1</domain>
|
<domain includeSubdomains="true">127.0.0.1</domain>
|
||||||
<domain includeSubdomains="true">8.217.137.25</domain>
|
|
||||||
<domain includeSubdomains="true">47.238.96.231</domain>
|
|
||||||
<domain includeSubdomains="true">192.168.30.80</domain>
|
|
||||||
</domain-config>
|
</domain-config>
|
||||||
</network-security-config>
|
</network-security-config>
|
||||||
|
|
Loading…
Reference in New Issue