fix translator when using internal proxy

This commit is contained in:
luvletter2333 2021-08-28 23:54:09 +08:00
parent 87eab8f0df
commit 3cb50b03be
No known key found for this signature in database
GPG Key ID: BFD68B892BECC1D8
5 changed files with 37 additions and 1 deletions

View File

@ -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 {

View File

@ -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 : HttpRequest> T.applyProxy(): T {
SharedConfig.getActiveSocks5Proxy()?.let { setProxy(it) }
return this
}
val String.code2Locale: Locale by receiveLazy<String, Locale> {
val args = replace('-', '_').split('_')

View File

@ -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");

View File

@ -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()

View File

@ -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()