From 33259cf04e73bd366e6d75506bc2efef914db373 Mon Sep 17 00:00:00 2001 From: Administrator Date: Wed, 9 Jul 2025 14:00:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=90=86=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/studyapp/MainActivity.java | 2 +- .../com/example/studyapp/proxy/ClashUtil.java | 47 ++++++++++++++----- .../com/example/studyapp/update/HttpUtil.kt | 4 ++ .../main/res/xml/network_security_config.xml | 18 +++---- 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/example/studyapp/MainActivity.java b/app/src/main/java/com/example/studyapp/MainActivity.java index 04aca55..9802798 100644 --- a/app/src/main/java/com/example/studyapp/MainActivity.java +++ b/app/src/main/java/com/example/studyapp/MainActivity.java @@ -387,7 +387,7 @@ public class MainActivity extends AppCompatActivity { try { ClashUtil.startProxy(context); 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) { LogFileUtil.logAndWrite(Log.ERROR, TAG, "startProxyVpn: Failed to start VPN", e); Toast.makeText(context, "Failed to start VPN: " + diff --git a/app/src/main/java/com/example/studyapp/proxy/ClashUtil.java b/app/src/main/java/com/example/studyapp/proxy/ClashUtil.java index 275276c..7d17d91 100644 --- a/app/src/main/java/com/example/studyapp/proxy/ClashUtil.java +++ b/app/src/main/java/com/example/studyapp/proxy/ClashUtil.java @@ -14,6 +14,8 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + import okhttp3.Call; import okhttp3.Callback; import okhttp3.HttpUrl; @@ -23,6 +25,8 @@ import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import okhttp3.ResponseBody; +import okhttp3.logging.HttpLoggingInterceptor; + import org.json.JSONException; import org.json.JSONObject; @@ -34,6 +38,18 @@ import org.json.JSONObject; * @Description: */ 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) { 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) { int port = getProxyPort(); - String url = "http://39.103.73.250/tt/test/testProxy.jsp?port="+port+"&country="+country; - OkHttpClient client = new OkHttpClient(); + // 安全构建 URL + 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() .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); + + // 使用 try-with-resources 确保 Response 自动关闭 + try (Response response = sharedClient.newCall(request).execute()) { + // 读取并记录响应内容 + 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 { - 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 (IOException e) { - LogFileUtil.logAndWrite(Log.ERROR, "ClashUtil", "switchProxyGroup: Failed to switch proxy", e); - System.out.println("Failed to switch proxy: " + e.getMessage()); + } catch (Exception e) { + LogFileUtil.logAndWrite(Log.ERROR, "ClashUtil", "switchProxyGroup: Unexpected error", e); } } diff --git a/app/src/main/java/com/example/studyapp/update/HttpUtil.kt b/app/src/main/java/com/example/studyapp/update/HttpUtil.kt index 9c64bb2..1d5ceac 100644 --- a/app/src/main/java/com/example/studyapp/update/HttpUtil.kt +++ b/app/src/main/java/com/example/studyapp/update/HttpUtil.kt @@ -6,6 +6,7 @@ import okhttp3.* import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.RequestBody.Companion.toRequestBody +import okhttp3.logging.HttpLoggingInterceptor import java.io.IOException import java.util.concurrent.TimeUnit @@ -17,6 +18,9 @@ object HttpUtils { .connectTimeout(15, TimeUnit.SECONDS) .readTimeout(15, TimeUnit.SECONDS) .writeTimeout(15, TimeUnit.SECONDS) + .addInterceptor(HttpLoggingInterceptor().apply { + level = HttpLoggingInterceptor.Level.BODY + }) .build() } diff --git a/app/src/main/res/xml/network_security_config.xml b/app/src/main/res/xml/network_security_config.xml index b5a1e74..eddd5b1 100644 --- a/app/src/main/res/xml/network_security_config.xml +++ b/app/src/main/res/xml/network_security_config.xml @@ -1,17 +1,13 @@ - - + + + + + + + - 47.236.153.142 - 8.211.204.20 127.0.0.1 - 8.217.137.25 - 47.238.96.231 - 192.168.30.80