From 3cb50b03be87b253d132bbf66ece8317937a6d6e Mon Sep 17 00:00:00 2001 From: luvletter2333 Date: Sat, 28 Aug 2021 23:54:09 +0800 Subject: [PATCH] fix translator when using internal proxy --- .../org/telegram/messenger/SharedConfig.java | 17 +++++++++++++++++ .../nekomimi/nekogram/transtale/Translator.kt | 7 +++++++ .../transtale/deepl/DeepLTranslatorRaw.java | 9 ++++++++- .../transtale/source/GoogleAppTranslator.kt | 3 +++ .../transtale/source/YandexTranslator.kt | 2 ++ 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java b/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java index d83da7b71..f9a55ea31 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java @@ -42,6 +42,8 @@ import org.telegram.ui.SwipeGestureSettingsView; import java.io.File; import java.io.RandomAccessFile; +import java.net.InetSocketAddress; +import java.net.Proxy; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; @@ -1013,6 +1015,21 @@ public class SharedConfig { private static boolean proxyListLoaded; public static ProxyInfo currentProxy; + public static Proxy getActiveSocks5Proxy() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) + return null; + // https://stackoverflow.com/questions/36205896/how-to-use-httpurlconnection-over-socks-proxy-on-android + // Android did not support socks proxy natively(using HURL) on devices previous than Marshmallow + // Hutool use HttpURLConnection too + if (!(currentProxy instanceof ExternalSocks5Proxy) || currentProxy instanceof WsProxy) + return null; + final ExternalSocks5Proxy proxy = (ExternalSocks5Proxy) currentProxy; + if (!proxy.isStarted()) + return null; + FileLog.w("Return socks5 proxy: " + currentProxy.toString() + " port:" + currentProxy.port); + return new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(currentProxy.address, currentProxy.port)); + } + public static void saveConfig() { synchronized (sync) { try { diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/Translator.kt b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/Translator.kt index 4a55e3d5e..a8b5cacb5 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/Translator.kt +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/Translator.kt @@ -3,9 +3,11 @@ package tw.nekomimi.nekogram.transtale import android.view.View import cn.hutool.core.util.ArrayUtil import cn.hutool.core.util.StrUtil +import cn.hutool.http.HttpRequest import org.apache.commons.lang3.LocaleUtils import org.telegram.messenger.LocaleController import org.telegram.messenger.R +import org.telegram.messenger.SharedConfig import tw.nekomimi.nekogram.NekoConfig import tw.nekomimi.nekogram.PopupBuilder import tw.nekomimi.nekogram.cc.CCConverter @@ -16,6 +18,11 @@ import tw.nekomimi.nekogram.utils.receive import tw.nekomimi.nekogram.utils.receiveLazy import java.util.* +fun T.applyProxy(): T { + SharedConfig.getActiveSocks5Proxy()?.let { setProxy(it) } + return this +} + val String.code2Locale: Locale by receiveLazy { val args = replace('-', '_').split('_') diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/deepl/DeepLTranslatorRaw.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/deepl/DeepLTranslatorRaw.java index 6bbfe4543..23055568f 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/deepl/DeepLTranslatorRaw.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/deepl/DeepLTranslatorRaw.java @@ -10,6 +10,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; +import java.net.Proxy; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Date; @@ -21,6 +22,7 @@ import java.util.regex.Pattern; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.telegram.messenger.SharedConfig; public class DeepLTranslatorRaw { private int id = (new Random()).nextInt(10000) * 10000 + 1; @@ -178,7 +180,12 @@ public class DeepLTranslatorRaw { private String request(String url, String body) throws IOException { InputStream httpConnectionStream = null; URL downloadUrl = new URL(url); - HttpURLConnection httpConnection = (HttpURLConnection)downloadUrl.openConnection(); + HttpURLConnection httpConnection; + final Proxy proxy = SharedConfig.getActiveSocks5Proxy(); + if (proxy != null) + httpConnection = (HttpURLConnection) downloadUrl.openConnection(proxy); + else + httpConnection = (HttpURLConnection) downloadUrl.openConnection(); httpConnection.addRequestProperty("Connection", "keep-alive"); httpConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4147.105 Safari/537.36"); httpConnection.addRequestProperty("Content-Type", "application/json"); diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/GoogleAppTranslator.kt b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/GoogleAppTranslator.kt index 5f12063c6..476d82857 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/GoogleAppTranslator.kt +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/GoogleAppTranslator.kt @@ -7,6 +7,8 @@ import org.telegram.messenger.R import tw.nekomimi.nekogram.NekoConfig import tw.nekomimi.nekogram.transtale.TransUtils import tw.nekomimi.nekogram.transtale.Translator +import tw.nekomimi.nekogram.transtale.applyProxy +import tw.nekomimi.nekogram.utils.applyIf object GoogleAppTranslator : Translator { @@ -28,6 +30,7 @@ object GoogleAppTranslator : Translator { val response = cn.hutool.http.HttpUtil .createGet(url) + .applyIf(NekoConfig.translationProvider != 2) { applyProxy() } .header("User-Agent", "GoogleTranslate/6.14.0.04.343003216 (Linux; U; Android 10; Redmi K20 Pro)") .execute() diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/YandexTranslator.kt b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/YandexTranslator.kt index 05ef93aae..ad3f3c424 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/YandexTranslator.kt +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/YandexTranslator.kt @@ -4,6 +4,7 @@ import cn.hutool.core.lang.UUID import cn.hutool.http.HttpUtil import org.json.JSONObject import tw.nekomimi.nekogram.transtale.Translator +import tw.nekomimi.nekogram.transtale.applyProxy import tw.nekomimi.nekogram.utils.applyUserAgent object YandexTranslator : Translator { @@ -16,6 +17,7 @@ object YandexTranslator : Translator { val response = HttpUtil.createPost("https://translate.yandex.net/api/v1/tr.json/translate?srv=android&uuid=$uuid&id=$uuid2-9-0") .applyUserAgent() + .applyProxy() .form("text", query) .form("lang", if (from == "auto") to else "$from-$to") .execute()