scoop:scoop.http.client:0.1.0
src/transport/proxy.scoop
Selected production source · src/transport/proxy.scoop
1
package scoop.http.client3
import scoop.asyncio.*4
import scoop.collections.emptyList5
import scoop.http.*6
import scoop.http.asyncio.*7
import scoop.platform.net.*8
import scoop.tls.*9
import scoop.tls.asyncio.openTlsClientContext11
internal suspend fun connectClientHttpProxy(call: ClientCall, entry: ClientPoolEntry, tcp: AsyncTcpStream,12
endpoint: HttpProxyEndpoint, forward: Boolean): ClientConnectResult<ClientConnection> {13
if (!endpoint.address.secure) return connectClientHttpProxyTransport(call, entry, tcp, endpoint, forward)14
val options = endpoint.tls15
val tls = openTlsClientContext(options.trust,16
TlsOptions(options.minimum, options.maximum, ["http/1.1".toByteArray()]), options.identity).use suspend { context ->17
connectClientTls(call, tcp, clientTlsPeer(endpoint.address.host), context, ClientObservedPhase.ProxyTls)18
}19
try {20
val selected: List<Byte>? = when (tls.connectionInfo.alpn) { case Some(val bytes) -> Some(bytes); case None -> None }21
selectHttpAlpn(selected, [HttpProtocol.Http1], true)22
requireClientCall(call)23
return connectClientHttpProxyTransport(call, entry, tls, endpoint, forward)24
} catch (error: Throwable) {25
try { tls.close() } catch (cleanup: Throwable) { throw ResourceUseException(error, cleanup) }26
throw error27
}28
}30
private suspend fun <T> connectClientHttpProxyTransport(call: ClientCall, entry: ClientPoolEntry, transport: T,31
endpoint: HttpProxyEndpoint, forward: Boolean): ClientConnectResult<ClientConnection> where T : AsyncByteStream, T : AsyncCloseable {32
if (forward) return ClientConnectResult.Connected(adoptClientConnection(call, entry, transport, HttpProtocol.Http1))33
return when (connectClientHttpTunnel(call, transport, endpoint)) {34
case Connected(val tunnel) -> ClientConnectResult.Connected(connectClientOrigin(call, entry, tunnel))35
case Authenticate(val session) -> ClientConnectResult.Authenticate(session)36
}37
}39
internal suspend fun connectClientSocks5(call: ClientCall, entry: ClientPoolEntry, tcp: AsyncTcpStream,40
endpoint: HttpSocks5Endpoint): ClientConnection {41
val origin = call.request.origin42
val destination = when (origin.host) {43
case Dns(val name) -> Socks5Address.Domain(name, origin.port)44
case Ipv4(val bytes) -> Socks5Address.Ip(SocketAddress(IpAddress.fromByteArray(bytes), origin.port))45
case Ipv6(val bytes) -> Socks5Address.Ip(SocketAddress(IpAddress.fromByteArray(bytes), origin.port))46
}47
observeClientPhase(call, ClientObservedPhase.ProxyHandshake, true)48
val deadline = startClientDeadline(call, call.scope!!, HttpTimeoutPhase.ProxyHandshake, call.timeouts.proxyHandshake)49
val tunnel = try {50
requireClientCall(call)51
finishClientNetworkStage(call, deadline,52
AsyncSocks5Tunnel.takeOwnership(tcp, destination, endpoint.credentials, endpoint.resolution))53
} catch (error: Throwable) { recordClientIoFailure(call, error); throw error }54
finally { stopClientDeadline(deadline); observeClientPhase(call, ClientObservedPhase.ProxyHandshake, false) }55
return connectClientOrigin(call, entry, tunnel)56
}58
internal fun clientProxyAuthorization(call: ClientCall, endpoint: HttpProxyEndpoint): Array<HttpField> {59
val explicit = endpoint.authorization60
if (clientPresent(explicit)) return [ClientAuthenticationScope.Proxy(endpoint).field(explicit!!)]61
val authentication = call.proxyAuthentication ?: return emptyList<HttpField>().toArray()62
val session = authentication.session ?: return emptyList<HttpField>().toArray()63
return [session.field!!]64
}