dart-lang / web_socket_channel

StreamChannel wrappers for WebSockets.
https://pub.dev/packages/web_socket_channel
BSD 3-Clause "New" or "Revised" License
412 stars 107 forks source link

WebSocket support custom sni host,when use doh #374

Open weatherfish opened 1 month ago

weatherfish commented 1 month ago

In someplace, the dns server is not work well, so we need use doh (DNS OVER HTTP)to resolve first. but when I repace the hsot to ip and call like that, server recieved error host

  Uri wsUri = Uri.parse(webSocketUrl);
  if (useDOH) {
    final domain = wsUri.host;
    if (domain.isNotEmpty && !CliNetUtils.isIpV4Address(domain) && !CliNetUtils.isIPv6(domain)) {
      final ip = await CliNetDns().adjustIp(domain);
      if (ip?.isNotEmpty ?? false) {
        webSocketUrl = '${wsUri.scheme}://$ip:${wsUri.port}${wsUri.path}';
        headers = {'Host': domain};
      }
    }
  }
  HttpClient client = HttpClient();
  client.badCertificateCallback = (X509Certificate cr, String host, int port) {
    return true;
  };

  WebSocket channel = await WebSocket.connect(webSocketUrl, customClient: client, headers: headers);

the headers params not gived to tls,so when tls check it return

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: WebSocketChannelException: 
HandshakeException: Handshake error in client (OS Error: SSLV3_ALERT_HANDSHAKE_FAILURE(tls_record.cc:592)
HANDSHAKE_FAILURE_ON_CLIENT_HELLO(handshake.cc:644))