|
|
@ -1,14 +1,8 @@ |
|
|
|
package io.legado.app.help.http |
|
|
|
package io.legado.app.help.http |
|
|
|
|
|
|
|
|
|
|
|
import io.legado.app.constant.AppConst |
|
|
|
|
|
|
|
import io.legado.app.help.AppConfig |
|
|
|
|
|
|
|
import io.legado.app.help.http.api.HttpGetApi |
|
|
|
|
|
|
|
import io.legado.app.utils.NetworkUtils |
|
|
|
|
|
|
|
import kotlinx.coroutines.suspendCancellableCoroutine |
|
|
|
import kotlinx.coroutines.suspendCancellableCoroutine |
|
|
|
import okhttp3.* |
|
|
|
import okhttp3.* |
|
|
|
import retrofit2.Retrofit |
|
|
|
import retrofit2.Retrofit |
|
|
|
import rxhttp.wrapper.param.RxHttp |
|
|
|
|
|
|
|
import rxhttp.wrapper.param.toText |
|
|
|
|
|
|
|
import java.net.InetSocketAddress |
|
|
|
import java.net.InetSocketAddress |
|
|
|
import java.net.Proxy |
|
|
|
import java.net.Proxy |
|
|
|
import java.util.concurrent.TimeUnit |
|
|
|
import java.util.concurrent.TimeUnit |
|
|
@ -41,72 +35,48 @@ object HttpHelper { |
|
|
|
builder.build() |
|
|
|
builder.build() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
suspend fun simpleGetAsync(url: String): String { |
|
|
|
|
|
|
|
val str = RxHttp.get(url) |
|
|
|
|
|
|
|
.addHeader(AppConst.UA_NAME, AppConfig.userAgent) |
|
|
|
|
|
|
|
.toText() |
|
|
|
|
|
|
|
return str.await() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
suspend fun simpleGetBytesAsync(url: String): ByteArray? { |
|
|
|
|
|
|
|
NetworkUtils.getBaseUrl(url)?.let { baseUrl -> |
|
|
|
|
|
|
|
return getByteRetrofit(baseUrl) |
|
|
|
|
|
|
|
.create(HttpGetApi::class.java) |
|
|
|
|
|
|
|
.getMapByteAsync(url, mapOf(), mapOf(Pair(AppConst.UA_NAME, AppConfig.userAgent))) |
|
|
|
|
|
|
|
.body() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return null |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline fun <reified T> getApiService( |
|
|
|
inline fun <reified T> getApiService( |
|
|
|
baseUrl: String, |
|
|
|
baseUrl: String, |
|
|
|
encode: String? = null, |
|
|
|
encode: String? = null, |
|
|
|
proxy: String? = null |
|
|
|
proxy: String? = null |
|
|
|
): T { |
|
|
|
): T { |
|
|
|
return if (proxy.isNullOrEmpty()) { |
|
|
|
return getRetrofit(baseUrl, encode, proxy).create(T::class.java) |
|
|
|
getRetrofit(baseUrl, encode).create(T::class.java) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
getRetrofitWithProxy(baseUrl, encode, proxy).create(T::class.java) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline fun <reified T> getBytesApiService(baseUrl: String): T { |
|
|
|
|
|
|
|
return getByteRetrofit(baseUrl).create(T::class.java) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun getRetrofit(baseUrl: String, encode: String? = null): Retrofit { |
|
|
|
fun getRetrofit( |
|
|
|
|
|
|
|
baseUrl: String, |
|
|
|
|
|
|
|
encode: String? = null, |
|
|
|
|
|
|
|
proxy: String? = null |
|
|
|
|
|
|
|
): Retrofit { |
|
|
|
return Retrofit.Builder().baseUrl(baseUrl) |
|
|
|
return Retrofit.Builder().baseUrl(baseUrl) |
|
|
|
//增加返回值为字符串的支持(以实体类返回) |
|
|
|
//增加返回值为字符串的支持(以实体类返回) |
|
|
|
.addConverterFactory(EncodeConverter(encode)) |
|
|
|
.addConverterFactory(EncodeConverter(encode)) |
|
|
|
.client(client) |
|
|
|
.client(getProxyClient(proxy)) |
|
|
|
.build() |
|
|
|
.build() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun getRetrofitWithProxy( |
|
|
|
fun getProxyClient(proxy: String? = null): OkHttpClient { |
|
|
|
baseUrl: String, |
|
|
|
if (proxy.isNullOrBlank()) { |
|
|
|
encode: String? = null, |
|
|
|
return client |
|
|
|
proxy: String? = null |
|
|
|
} |
|
|
|
): Retrofit { |
|
|
|
|
|
|
|
val r = Regex("(http|socks4|socks5)://(.*):(\\d{2,5})(@.*@.*)?") |
|
|
|
val r = Regex("(http|socks4|socks5)://(.*):(\\d{2,5})(@.*@.*)?") |
|
|
|
val ms = proxy?.let { r.findAll(it) } |
|
|
|
val ms = r.findAll(proxy) |
|
|
|
val group = ms?.first() |
|
|
|
val group = ms.first() |
|
|
|
var type = "direct" //直接连接 |
|
|
|
val type: String //直接连接 |
|
|
|
var host = "127.0.0.1" //代理服务器hostname |
|
|
|
val host: String //代理服务器hostname |
|
|
|
var port = 1080 //代理服务器port |
|
|
|
val port: Int //代理服务器port |
|
|
|
var username = "" //代理服务器验证用户名 |
|
|
|
var username = "" //代理服务器验证用户名 |
|
|
|
var password = "" //代理服务器验证密码 |
|
|
|
var password = "" //代理服务器验证密码 |
|
|
|
if (group != null) { |
|
|
|
type = if (group.groupValues[1] == "http") { |
|
|
|
type = if (group.groupValues[1] == "http") { |
|
|
|
"http" |
|
|
|
"http" |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
"socks" |
|
|
|
"socks" |
|
|
|
} |
|
|
|
} |
|
|
|
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 != "") { |
|
|
@ -125,18 +95,7 @@ object HttpHelper { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return Retrofit.Builder().baseUrl(baseUrl) |
|
|
|
return builder.build() |
|
|
|
//增加返回值为字符串的支持(以实体类返回) |
|
|
|
|
|
|
|
.addConverterFactory(EncodeConverter(encode)) |
|
|
|
|
|
|
|
.client(builder.build()) |
|
|
|
|
|
|
|
.build() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun getByteRetrofit(baseUrl: String): Retrofit { |
|
|
|
|
|
|
|
return Retrofit.Builder().baseUrl(baseUrl) |
|
|
|
|
|
|
|
.addConverterFactory(ByteConverter()) |
|
|
|
|
|
|
|
.client(client) |
|
|
|
|
|
|
|
.build() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun getHeaderInterceptor(): Interceptor { |
|
|
|
private fun getHeaderInterceptor(): Interceptor { |
|
|
|