支持在请求头里设置代理

pull/349/head
gedoor 4 years ago
parent 7af642ed14
commit 0e48bca78f
  1. 29
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt

@ -4,11 +4,7 @@ import io.legado.app.constant.AppConst
import io.legado.app.help.http.api.HttpGetApi import io.legado.app.help.http.api.HttpGetApi
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.ConnectionSpec import okhttp3.*
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Protocol
import okhttp3.Credentials
import retrofit2.Retrofit import retrofit2.Retrofit
import java.net.InetSocketAddress import java.net.InetSocketAddress
import java.net.Proxy import java.net.Proxy
@ -111,19 +107,14 @@ object HttpHelper {
encode: String? = null, encode: String? = null,
proxy: String? = null proxy: String? = null
): Retrofit { ): Retrofit {
val r = val r = Regex("(http|socks4|socks5)://(.*):(\\d{2,5})(@.*@.*)?")
Regex(
"""
(http|socks4|socks5)://(.*):(\d{2,5})(@.*@.*)?
""".trimIndent()
)
val ms = proxy?.let { r.findAll(it) }; val ms = proxy?.let { r.findAll(it) };
val group = ms?.first() val group = ms?.first()
var type: String = "direct" //直接连接 var type = "direct" //直接连接
var host: String = "127.0.0.1" //代理服务器hostname var host = "127.0.0.1" //代理服务器hostname
var port: Int = 1080 //代理服务器port var port = 1080 //代理服务器port
var username: String = "" //代理服务器验证用户名 var username = "" //代理服务器验证用户名
var password: String = "" //代理服务器验证密码 var password = "" //代理服务器验证密码
if (group != null) { if (group != null) {
type = if (group.groupValues[1] == "http") { type = if (group.groupValues[1] == "http") {
"http" "http"
@ -133,11 +124,11 @@ object HttpHelper {
host = group.groupValues[2] host = group.groupValues[2]
port = group.groupValues[3].toInt() port = group.groupValues[3].toInt()
if (group.groupValues[4] != "") { if (group.groupValues[4] != "") {
username = group.groupValues[4].split("@")[1]; username = group.groupValues[4].split("@")[1]
password = group.groupValues[4].split("@")[2]; password = group.groupValues[4].split("@")[2]
} }
} }
val builder = client.newBuilder(); val builder = client.newBuilder()
if (type != "direct" && host != "") { if (type != "direct" && host != "") {
if (type == "http") { if (type == "http") {
builder.proxy(Proxy(Proxy.Type.HTTP, InetSocketAddress(host, port))); builder.proxy(Proxy(Proxy.Type.HTTP, InetSocketAddress(host, port)));

Loading…
Cancel
Save