update HttpHelper.kt

pull/540/head
gedoor 4 years ago
parent 0386d638af
commit 42065080ec
  1. 32
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt

@ -6,6 +6,7 @@ import okhttp3.*
import java.io.IOException
import java.net.InetSocketAddress
import java.net.Proxy
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
@ -13,6 +14,10 @@ import kotlin.coroutines.resumeWithException
@Suppress("unused")
object HttpHelper {
private val proxyClientCache: ConcurrentHashMap<String, OkHttpClient> by lazy {
ConcurrentHashMap()
}
val client: OkHttpClient by lazy {
val specs = arrayListOf(
@ -55,31 +60,30 @@ object HttpHelper {
})
}
/**
* 缓存代理okHttp
*/
fun getProxyClient(proxy: String? = null): OkHttpClient {
if (proxy.isNullOrBlank()) {
return client
}
proxyClientCache[proxy]?.let {
return it
}
val r = Regex("(http|socks4|socks5)://(.*):(\\d{2,5})(@.*@.*)?")
val ms = r.findAll(proxy)
val group = ms.first()
val type: String //直接连接
val host: String //代理服务器hostname
val port: Int //代理服务器port
var username = "" //代理服务器验证用户名
var password = "" //代理服务器验证密码
type = if (group.groupValues[1] == "http") {
"http"
} else {
"socks"
}
host = group.groupValues[2]
port = group.groupValues[3].toInt()
val type = if (group.groupValues[1] == "http") "http" else "socks"
val host = group.groupValues[2]
val 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 != "") {
val builder = client.newBuilder()
if (type == "http") {
builder.proxy(Proxy(Proxy.Type.HTTP, InetSocketAddress(host, port)))
} else {
@ -93,9 +97,11 @@ object HttpHelper {
.build()
}
}
val proxyClient = builder.build()
proxyClientCache[proxy] = proxyClient
return proxyClient
}
return builder.build()
return client
}
private fun getHeaderInterceptor(): Interceptor {

Loading…
Cancel
Save