From 9952fb16636c5cbd6cd78a97e46f233e029c55da Mon Sep 17 00:00:00 2001 From: Celeter Date: Wed, 23 Sep 2020 09:21:19 +0800 Subject: [PATCH] =?UTF-8?q?proxy=E4=BB=8Eheaders=E5=8F=96=E5=87=BA?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0get=E5=92=8Cpost=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E9=87=8D=E5=AE=9A=E5=90=91=E6=8B=A6=E6=88=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/JsExtensions.kt | 32 +++++++-- .../io/legado/app/help/http/HttpHelper.kt | 18 +++-- .../app/model/analyzeRule/AnalyzeUrl.kt | 65 +++++-------------- .../java/io/legado/app/model/rss/RssParser.kt | 4 +- 4 files changed, 59 insertions(+), 60 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index 201f2e62a..46bb029a1 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -3,11 +3,11 @@ package io.legado.app.help import android.util.Base64 import androidx.annotation.Keep import io.legado.app.constant.AppConst.dateFormat +import io.legado.app.help.http.SSLHelper import io.legado.app.model.analyzeRule.AnalyzeUrl -import io.legado.app.utils.EncoderUtils -import io.legado.app.utils.MD5Utils -import io.legado.app.utils.htmlFormat -import io.legado.app.utils.msg +import io.legado.app.utils.* +import org.jsoup.Connection +import org.jsoup.Jsoup import java.net.URLEncoder import java.util.* @@ -40,6 +40,30 @@ interface JsExtensions { } } + /** + * js实现重定向拦截,不能删 + */ + fun get(urlStr: String, headers: Map): Connection.Response { + return Jsoup.connect(urlStr) + .sslSocketFactory(SSLHelper.unsafeSSLSocketFactory) + .ignoreContentType(true) + .followRedirects(false) + .headers(headers) + .method(Connection.Method.GET) + .execute() + } + + fun post(urlStr: String, body: String, headers: Map): Connection.Response { + return Jsoup.connect(urlStr) + .sslSocketFactory(SSLHelper.unsafeSSLSocketFactory) + .ignoreContentType(true) + .followRedirects(false) + .requestBody(body) + .headers(headers) + .method(Connection.Method.POST) + .execute() + } + /** * js实现解码,不能删 */ 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 6364e9ed8..1d9252164 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 @@ -48,7 +48,11 @@ object HttpHelper { return null } - fun getBytes(url: String, queryMap: Map, headers: Map): ByteArray? { + fun getBytes( + url: String, + queryMap: Map, + headers: Map + ): ByteArray? { NetworkUtils.getBaseUrl(url)?.let { baseUrl -> return getByteRetrofit(baseUrl) .create(HttpGetApi::class.java) @@ -78,16 +82,16 @@ object HttpHelper { return null } - inline fun getApiService(baseUrl: String, encode: String? = null): T { - return getRetrofit(baseUrl, encode).create(T::class.java) - } - - inline fun getApiServiceWithProxy( + inline fun getApiService( baseUrl: String, encode: String? = null, proxy: String? = null ): T { - return getRetrofitWithProxy(baseUrl, encode, proxy).create(T::class.java) + return if (proxy.isNullOrEmpty()) { + getRetrofit(baseUrl, encode).create(T::class.java) + } else { + getRetrofitWithProxy(baseUrl, encode, proxy).create(T::class.java) + } } inline fun getBytesApiService(baseUrl: String): T { 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 067690afb..7323c145d 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 @@ -67,10 +67,6 @@ class AnalyzeUrl( } headerMapF?.let { headerMap.putAll(it) - if (it.containsKey("proxy")) { - proxy = it["proxy"] - headerMap.remove("proxy") - } } //替换参数 analyzeJs(key, page, speakText, speakSpeed, book) @@ -191,6 +187,7 @@ class AnalyzeUrl( val option = GSON.fromJsonObject(urlArray[1]) option?.let { _ -> option.method?.let { if (it.equals("POST", true)) method = RequestMethod.POST } + option.proxy?.let { proxy = it } option.headers?.let { headers -> if (headers is Map<*, *>) { headers.forEach { entry -> @@ -304,19 +301,19 @@ class AnalyzeUrl( method == RequestMethod.POST -> { if (fieldMap.isNotEmpty()) { HttpHelper - .getApiService(baseUrl, charset) + .getApiService(baseUrl, charset, proxy) .postMap(url, fieldMap, headerMap) } else { HttpHelper - .getApiService(baseUrl, charset) + .getApiService(baseUrl, charset, proxy) .postBody(url, requestBody!!, headerMap) } } fieldMap.isEmpty() -> HttpHelper - .getApiService(baseUrl, charset) + .getApiService(baseUrl, charset, proxy) .get(url, headerMap) else -> HttpHelper - .getApiService(baseUrl, charset) + .getApiService(baseUrl, charset, proxy) .getMap(url, fieldMap, headerMap) } } @@ -343,51 +340,24 @@ 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) - .getAsync(url, headerMap) - + .getApiService(baseUrl, charset, proxy) + .postMapAsync(url, fieldMap, headerMap) } else { HttpHelper - .getApiServiceWithProxy(baseUrl, charset, proxy) - .getAsync(url, headerMap) + .getApiService(baseUrl, charset, proxy) + .postBodyAsync(url, requestBody!!, headerMap) } - + } + fieldMap.isEmpty() -> { + HttpHelper + .getApiService(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) - } - + HttpHelper + .getApiService(baseUrl, charset, proxy) + .getMapAsync(url, fieldMap, headerMap) } } return Res(NetworkUtils.getUrl(res), res.body()) @@ -448,6 +418,7 @@ class AnalyzeUrl( val webView: Any?, val headers: Any?, val body: Any?, + val proxy: String? ) } diff --git a/app/src/main/java/io/legado/app/model/rss/RssParser.kt b/app/src/main/java/io/legado/app/model/rss/RssParser.kt index 09cd756bb..e6667164e 100644 --- a/app/src/main/java/io/legado/app/model/rss/RssParser.kt +++ b/app/src/main/java/io/legado/app/model/rss/RssParser.kt @@ -117,11 +117,11 @@ object RssParser { private fun getImageUrl(input: String): String? { var url: String? = null - val patternImg = "()".toPattern() + val patternImg = "(]*>)".toPattern() val matcherImg = patternImg.matcher(input) if (matcherImg.find()) { val imgTag = matcherImg.group(1) - val patternLink = "src\\s*=\\s*\"(.+?)\"".toPattern() + val patternLink = "src\\s*=\\s*\"([^\"]+)\"".toPattern() val matcherLink = patternLink.matcher(imgTag!!) if (matcherLink.find()) { url = matcherLink.group(1)!!.trim()