proxy从headers取出,增加get和post方法用于重定向拦截

pull/395/head
Celeter 4 years ago
parent f7c870c4ee
commit 9952fb1663
  1. 32
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  2. 18
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  3. 65
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  4. 4
      app/src/main/java/io/legado/app/model/rss/RssParser.kt

@ -3,11 +3,11 @@ package io.legado.app.help
import android.util.Base64 import android.util.Base64
import androidx.annotation.Keep import androidx.annotation.Keep
import io.legado.app.constant.AppConst.dateFormat 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.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.EncoderUtils import io.legado.app.utils.*
import io.legado.app.utils.MD5Utils import org.jsoup.Connection
import io.legado.app.utils.htmlFormat import org.jsoup.Jsoup
import io.legado.app.utils.msg
import java.net.URLEncoder import java.net.URLEncoder
import java.util.* import java.util.*
@ -40,6 +40,30 @@ interface JsExtensions {
} }
} }
/**
* js实现重定向拦截,不能删
*/
fun get(urlStr: String, headers: Map<String, String>): 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<String, String>): Connection.Response {
return Jsoup.connect(urlStr)
.sslSocketFactory(SSLHelper.unsafeSSLSocketFactory)
.ignoreContentType(true)
.followRedirects(false)
.requestBody(body)
.headers(headers)
.method(Connection.Method.POST)
.execute()
}
/** /**
* js实现解码,不能删 * js实现解码,不能删
*/ */

@ -48,7 +48,11 @@ object HttpHelper {
return null return null
} }
fun getBytes(url: String, queryMap: Map<String, String>, headers: Map<String, String>): ByteArray? { fun getBytes(
url: String,
queryMap: Map<String, String>,
headers: Map<String, String>
): ByteArray? {
NetworkUtils.getBaseUrl(url)?.let { baseUrl -> NetworkUtils.getBaseUrl(url)?.let { baseUrl ->
return getByteRetrofit(baseUrl) return getByteRetrofit(baseUrl)
.create(HttpGetApi::class.java) .create(HttpGetApi::class.java)
@ -78,16 +82,16 @@ object HttpHelper {
return null return null
} }
inline fun <reified T> getApiService(baseUrl: String, encode: String? = null): T { inline fun <reified T> getApiService(
return getRetrofit(baseUrl, encode).create(T::class.java)
}
inline fun <reified T> getApiServiceWithProxy(
baseUrl: String, baseUrl: String,
encode: String? = null, encode: String? = null,
proxy: String? = null proxy: String? = null
): T { ): 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 <reified T> getBytesApiService(baseUrl: String): T { inline fun <reified T> getBytesApiService(baseUrl: String): T {

@ -67,10 +67,6 @@ class AnalyzeUrl(
} }
headerMapF?.let { headerMapF?.let {
headerMap.putAll(it) headerMap.putAll(it)
if (it.containsKey("proxy")) {
proxy = it["proxy"]
headerMap.remove("proxy")
}
} }
//替换参数 //替换参数
analyzeJs(key, page, speakText, speakSpeed, book) analyzeJs(key, page, speakText, speakSpeed, book)
@ -191,6 +187,7 @@ class AnalyzeUrl(
val option = GSON.fromJsonObject<UrlOption>(urlArray[1]) val option = GSON.fromJsonObject<UrlOption>(urlArray[1])
option?.let { _ -> option?.let { _ ->
option.method?.let { if (it.equals("POST", true)) method = RequestMethod.POST } option.method?.let { if (it.equals("POST", true)) method = RequestMethod.POST }
option.proxy?.let { proxy = it }
option.headers?.let { headers -> option.headers?.let { headers ->
if (headers is Map<*, *>) { if (headers is Map<*, *>) {
headers.forEach { entry -> headers.forEach { entry ->
@ -304,19 +301,19 @@ class AnalyzeUrl(
method == RequestMethod.POST -> { method == RequestMethod.POST -> {
if (fieldMap.isNotEmpty()) { if (fieldMap.isNotEmpty()) {
HttpHelper HttpHelper
.getApiService<HttpPostApi>(baseUrl, charset) .getApiService<HttpPostApi>(baseUrl, charset, proxy)
.postMap(url, fieldMap, headerMap) .postMap(url, fieldMap, headerMap)
} else { } else {
HttpHelper HttpHelper
.getApiService<HttpPostApi>(baseUrl, charset) .getApiService<HttpPostApi>(baseUrl, charset, proxy)
.postBody(url, requestBody!!, headerMap) .postBody(url, requestBody!!, headerMap)
} }
} }
fieldMap.isEmpty() -> HttpHelper fieldMap.isEmpty() -> HttpHelper
.getApiService<HttpGetApi>(baseUrl, charset) .getApiService<HttpGetApi>(baseUrl, charset, proxy)
.get(url, headerMap) .get(url, headerMap)
else -> HttpHelper else -> HttpHelper
.getApiService<HttpGetApi>(baseUrl, charset) .getApiService<HttpGetApi>(baseUrl, charset, proxy)
.getMap(url, fieldMap, headerMap) .getMap(url, fieldMap, headerMap)
} }
} }
@ -343,51 +340,24 @@ class AnalyzeUrl(
val res = when { val res = when {
method == RequestMethod.POST -> { method == RequestMethod.POST -> {
if (fieldMap.isNotEmpty()) { if (fieldMap.isNotEmpty()) {
if (proxy == null) {
HttpHelper
.getApiService<HttpPostApi>(baseUrl, charset)
.postMapAsync(url, fieldMap, headerMap)
} else {
HttpHelper
.getApiServiceWithProxy<HttpPostApi>(baseUrl, charset, proxy)
.postMapAsync(url, fieldMap, headerMap)
}
} else {
if (proxy == null) {
HttpHelper
.getApiService<HttpPostApi>(baseUrl, charset)
.postBodyAsync(url, requestBody!!, headerMap)
} else {
HttpHelper
.getApiServiceWithProxy<HttpPostApi>(baseUrl, charset, proxy)
.postBodyAsync(url, requestBody!!, headerMap)
}
}
}
fieldMap.isEmpty() -> {
if (proxy == null) {
HttpHelper HttpHelper
.getApiService<HttpGetApi>(baseUrl, charset) .getApiService<HttpPostApi>(baseUrl, charset, proxy)
.getAsync(url, headerMap) .postMapAsync(url, fieldMap, headerMap)
} else { } else {
HttpHelper HttpHelper
.getApiServiceWithProxy<HttpGetApi>(baseUrl, charset, proxy) .getApiService<HttpPostApi>(baseUrl, charset, proxy)
.getAsync(url, headerMap) .postBodyAsync(url, requestBody!!, headerMap)
} }
}
fieldMap.isEmpty() -> {
HttpHelper
.getApiService<HttpGetApi>(baseUrl, charset, proxy)
.getAsync(url, headerMap)
} }
else -> { else -> {
if (proxy == null) { HttpHelper
HttpHelper .getApiService<HttpGetApi>(baseUrl, charset, proxy)
.getApiService<HttpGetApi>(baseUrl, charset) .getMapAsync(url, fieldMap, headerMap)
.getMapAsync(url, fieldMap, headerMap)
} else {
HttpHelper
.getApiServiceWithProxy<HttpGetApi>(baseUrl, charset, proxy)
.getMapAsync(url, fieldMap, headerMap)
}
} }
} }
return Res(NetworkUtils.getUrl(res), res.body()) return Res(NetworkUtils.getUrl(res), res.body())
@ -448,6 +418,7 @@ class AnalyzeUrl(
val webView: Any?, val webView: Any?,
val headers: Any?, val headers: Any?,
val body: Any?, val body: Any?,
val proxy: String?
) )
} }

@ -117,11 +117,11 @@ object RssParser {
private fun getImageUrl(input: String): String? { private fun getImageUrl(input: String): String? {
var url: String? = null var url: String? = null
val patternImg = "(<img .*?>)".toPattern() val patternImg = "(<img [^>]*>)".toPattern()
val matcherImg = patternImg.matcher(input) val matcherImg = patternImg.matcher(input)
if (matcherImg.find()) { if (matcherImg.find()) {
val imgTag = matcherImg.group(1) val imgTag = matcherImg.group(1)
val patternLink = "src\\s*=\\s*\"(.+?)\"".toPattern() val patternLink = "src\\s*=\\s*\"([^\"]+)\"".toPattern()
val matcherLink = patternLink.matcher(imgTag!!) val matcherLink = patternLink.matcher(imgTag!!)
if (matcherLink.find()) { if (matcherLink.find()) {
url = matcherLink.group(1)!!.trim() url = matcherLink.group(1)!!.trim()

Loading…
Cancel
Save