assignToHttpClientWithSecureOptions static method

void assignToHttpClientWithSecureOptions(
  1. HttpClient httpClient,
  2. List<ProxySettings> proxies, {
  3. dynamic host,
  4. SecurityContext? context,
  5. bool onBadCertificate(
    1. X509Certificate certificate
    )?,
  6. void keyLog(
    1. String line
    )?,
  7. List<String>? supportedProtocols,
})

Assign http client connection factory to proxy connection.

Applies host, context, onBadCertificate, keyLog and supportedProtocols to SecureSocket if connection is tls-over-http

Implementation

static void assignToHttpClientWithSecureOptions(
  HttpClient httpClient,
  List<ProxySettings> proxies,
  {
    dynamic host,
    SecurityContext? context,
    bool Function(X509Certificate certificate)? onBadCertificate,
    void Function(String line)? keyLog,
    List<String>? supportedProtocols,
  }
) {
  httpClient.connectionFactory =
    (uri, proxyHost, proxyPort) async {
      // Returns instance of SocksSocket which implements Socket
      final client = SocksTCPClient.connect(
        proxies,
        InternetAddress(uri.host, type: InternetAddressType.unix),
        uri.port,
      );

      // Secure connection after establishing Socks connection
      if(uri.scheme == 'https') {
        final Future<SecureSocket> secureClient;
        return ConnectionTask.fromSocket(secureClient = (await client).secure(
          uri.host,
          context: context,
          onBadCertificate: onBadCertificate,
          keyLog: keyLog,
          supportedProtocols: supportedProtocols,
        ), () async => (await secureClient).close().ignore(),);
      }

      // SocketConnectionTask implements ConnectionTask<Socket>
      return ConnectionTask.fromSocket(client,
        () async => (await client).close().ignore(),);
    };
}