diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index 9aa17e9c1..79abae41f 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -8,7 +8,10 @@ import okhttp3.ConnectionSpec import okhttp3.Interceptor import okhttp3.OkHttpClient import okhttp3.Protocol +import okhttp3.Credentials import retrofit2.Retrofit +import java.net.InetSocketAddress +import java.net.Proxy import java.util.concurrent.TimeUnit import kotlin.coroutines.resume @@ -83,6 +86,14 @@ object HttpHelper { return getRetrofit(baseUrl, encode).create(T::class.java) } + inline fun getApiServiceWithProxy( + baseUrl: String, + encode: String? = null, + proxy: String? = null + ): T { + return getRetrofitWithProxy(baseUrl, encode, proxy).create(T::class.java) + } + inline fun getBytesApiService(baseUrl: String): T { return getByteRetrofit(baseUrl).create(T::class.java) } @@ -95,6 +106,61 @@ object HttpHelper { .build() } + fun getRetrofitWithProxy( + baseUrl: String, + encode: String? = null, + proxy: String? = null + ): Retrofit { + val r = + Regex( + """ + (http|socks4|socks5)://(.*):(\d{2,5})(@.*@.*)? + """.trimIndent() + ) + val ms = proxy?.let { r.findAll(it) }; + val group = ms?.first() + var type: String = "direct" //直接连接 + var host: String = "127.0.0.1" //代理服务器hostname + var port: Int = 1080 //代理服务器port + var username: String = "" //代理服务器验证用户名 + var password: String = "" //代理服务器验证密码 + if (group != null) { + type = if (group.groupValues[1] == "http") { + "http" + } else { + "socks" + } + host = group.groupValues[2] + port = group.groupValues[3].toInt() + if (group.groupValues[4] != "") { + username = group.groupValues[4].split("@")[1]; + password = group.groupValues[4].split("@")[2]; + } + } + val builder = client.newBuilder(); + if (type != "direct" && host != "") { + if (type == "http") { + builder.proxy(Proxy(Proxy.Type.HTTP, InetSocketAddress(host, port))); + } else { + builder.proxy(Proxy(Proxy.Type.SOCKS, InetSocketAddress(host, port))); + } + if (username != "" && password != "") { + builder.proxyAuthenticator { _, response -> //设置代理服务器账号密码 + val credential: String = Credentials.basic(username, password) + response.request().newBuilder() + .header("Proxy-Authorization", credential) + .build() + } + } + + } + return Retrofit.Builder().baseUrl(baseUrl) + //增加返回值为字符串的支持(以实体类返回) + .addConverterFactory(EncodeConverter(encode)) + .client(builder.build()) + .build() + } + fun getByteRetrofit(baseUrl: String): Retrofit { return Retrofit.Builder().baseUrl(baseUrl) .addConverterFactory(ByteConverter()) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index e892bf073..97a9149a0 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -59,12 +59,19 @@ class AnalyzeUrl( private var requestBody: RequestBody? = null private var method = RequestMethod.GET private val splitUrlRegex = Regex(",\\s*(?=\\{)") + private var proxy: String? = null init { baseUrl?.let { this.baseUrl = it.split(splitUrlRegex, 1)[0] } - headerMapF?.let { headerMap.putAll(it) } + headerMapF?.let { + headerMap.putAll(it) + if (it.containsKey("proxy")) { + proxy = it["proxy"]; + headerMap.remove("proxy") + } + } //替换参数 analyzeJs(key, page, speakText, speakSpeed, book) replaceKeyPageJs(key, page, speakText, speakSpeed, book) @@ -329,21 +336,52 @@ class AnalyzeUrl( val res = when { method == RequestMethod.POST -> { if (fieldMap.isNotEmpty()) { + if (proxy == null) { + HttpHelper + .getApiService(baseUrl, charset) + .postMapAsync(url, fieldMap, headerMap) + } else { + HttpHelper + .getApiServiceWithProxy(baseUrl, charset, proxy) + .postMapAsync(url, fieldMap, headerMap) + } + } else { + if (proxy == null) { + HttpHelper + .getApiService(baseUrl, charset) + .postBodyAsync(url, requestBody!!, headerMap) + } else { + HttpHelper + .getApiServiceWithProxy(baseUrl, charset, proxy) + .postBodyAsync(url, requestBody!!, headerMap) + } + } + } + fieldMap.isEmpty() -> { + if (proxy == null) { HttpHelper - .getApiService(baseUrl, charset) - .postMapAsync(url, fieldMap, headerMap) + .getApiService(baseUrl, charset) + .getAsync(url, headerMap) + } else { HttpHelper - .getApiService(baseUrl, charset) - .postBodyAsync(url, requestBody!!, headerMap) + .getApiServiceWithProxy(baseUrl, charset, proxy) + .getAsync(url, headerMap) } + + } + else -> { + if (proxy == null) { + HttpHelper + .getApiService(baseUrl, charset) + .getMapAsync(url, fieldMap, headerMap) + } else { + HttpHelper + .getApiServiceWithProxy(baseUrl, charset, proxy) + .getMapAsync(url, fieldMap, headerMap) + } + } - fieldMap.isEmpty() -> HttpHelper - .getApiService(baseUrl, charset) - .getAsync(url, headerMap) - else -> HttpHelper - .getApiService(baseUrl, charset) - .getMapAsync(url, fieldMap, headerMap) } return Res(NetworkUtils.getUrl(res), res.body()) }