mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-14 17:53:05 +08:00
feat(partnerctl-fetch): support fetching raw data via https proxy
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package work.slhaf.partner.ctl.support
|
package work.slhaf.partner.ctl.support
|
||||||
|
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import java.net.InetSocketAddress
|
||||||
|
import java.net.ProxySelector
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
import java.net.http.*
|
import java.net.http.*
|
||||||
import java.time.Duration
|
import java.time.Duration
|
||||||
@@ -8,8 +10,28 @@ import java.time.Duration
|
|||||||
private val httpClient: HttpClient = HttpClient.newBuilder()
|
private val httpClient: HttpClient = HttpClient.newBuilder()
|
||||||
.connectTimeout(Duration.ofSeconds(20))
|
.connectTimeout(Duration.ofSeconds(20))
|
||||||
.followRedirects(HttpClient.Redirect.NEVER)
|
.followRedirects(HttpClient.Redirect.NEVER)
|
||||||
|
.apply {
|
||||||
|
proxySelectorFromEnv()?.let(::proxy)
|
||||||
|
}
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
|
private fun proxySelectorFromEnv(): ProxySelector? {
|
||||||
|
val proxyText = System.getenv("HTTPS_PROXY")
|
||||||
|
?: System.getenv("https_proxy")
|
||||||
|
?: return null
|
||||||
|
|
||||||
|
val proxyUri = URI.create(proxyText)
|
||||||
|
val host = proxyUri.host
|
||||||
|
?: throw IllegalArgumentException("Invalid HTTPS_PROXY host: $proxyText")
|
||||||
|
|
||||||
|
val port = proxyUri.port
|
||||||
|
if (port == -1) {
|
||||||
|
throw IllegalArgumentException("HTTPS_PROXY must include port: $proxyText")
|
||||||
|
}
|
||||||
|
|
||||||
|
return ProxySelector.of(InetSocketAddress(host, port))
|
||||||
|
}
|
||||||
|
|
||||||
fun fetchText(url: String): String {
|
fun fetchText(url: String): String {
|
||||||
var lastError: Exception? = null
|
var lastError: Exception? = null
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user