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. 49
      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 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<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实现解码,不能删
*/

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

@ -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<UrlOption>(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<HttpPostApi>(baseUrl, charset)
.getApiService<HttpPostApi>(baseUrl, charset, proxy)
.postMap(url, fieldMap, headerMap)
} else {
HttpHelper
.getApiService<HttpPostApi>(baseUrl, charset)
.getApiService<HttpPostApi>(baseUrl, charset, proxy)
.postBody(url, requestBody!!, headerMap)
}
}
fieldMap.isEmpty() -> HttpHelper
.getApiService<HttpGetApi>(baseUrl, charset)
.getApiService<HttpGetApi>(baseUrl, charset, proxy)
.get(url, headerMap)
else -> HttpHelper
.getApiService<HttpGetApi>(baseUrl, charset)
.getApiService<HttpGetApi>(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<HttpPostApi>(baseUrl, charset)
.getApiService<HttpPostApi>(baseUrl, charset, proxy)
.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)
.getApiService<HttpPostApi>(baseUrl, charset, proxy)
.postBodyAsync(url, requestBody!!, headerMap)
}
}
}
fieldMap.isEmpty() -> {
if (proxy == null) {
HttpHelper
.getApiService<HttpGetApi>(baseUrl, charset)
.getAsync(url, headerMap)
} else {
HttpHelper
.getApiServiceWithProxy<HttpGetApi>(baseUrl, charset, proxy)
.getApiService<HttpGetApi>(baseUrl, charset, proxy)
.getAsync(url, headerMap)
}
}
else -> {
if (proxy == null) {
HttpHelper
.getApiService<HttpGetApi>(baseUrl, charset)
.getApiService<HttpGetApi>(baseUrl, charset, proxy)
.getMapAsync(url, fieldMap, headerMap)
} else {
HttpHelper
.getApiServiceWithProxy<HttpGetApi>(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?
)
}

@ -117,11 +117,11 @@ object RssParser {
private fun getImageUrl(input: String): String? {
var url: String? = null
val patternImg = "(<img .*?>)".toPattern()
val patternImg = "(<img [^>]*>)".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()

Loading…
Cancel
Save