LacticWhale / socks_dart

Socks5 server and client with ability to catch packet in both direction, chain proxies and more.
MIT License
14 stars 4 forks source link

Can it be used together with Xray? #14

Open wanliyunyan opened 1 week ago

wanliyunyan commented 1 week ago

I have a bold idea to use it together with Xray.Route WebView traffic to port 10808, start Xray to listen on port 10808. When WebView accesses http://**, Xray reports an error: 'rejected proxy/socks: failed to read address > common/protocol: invalid domain name: **' Do you know what the problem might be? I'm sure the Xray configuration is correct because it works fine when I use hev-socks5-tunnel.I set up a SOCKS5 server using https://github.com/Lozy/danted, and this Flutter code also works fine.

  InAppWebView(
    AppWebViewSettings.shouldInterceptRequestSetting,
    ......
  )

  static Future<WebResourceResponse?> shouldInterceptRequestSetting(
      InAppWebViewController controller,
      WebResourceRequest request,
      ) async {
    try {
      final client = HttpClient();

      final proxies = [
        ProxySettings(InternetAddress("127.0.0.1"), 10808),
      ];

      SocksTCPClient.assignToHttpClient(
        client,
        proxies,
      );

      HttpClientRequest httpRequest = await client.getUrl(Uri.parse(request.url.toString()));

      request.headers!.forEach((key, value) {
        httpRequest.headers.set(key, value);
      });

      final httpResponse = await httpRequest.close();

      BytesBuilder builder = BytesBuilder();
      await for (var chunk in httpResponse) {
        builder.add(chunk);
      }
      Uint8List responseBytes = builder.takeBytes();

      return WebResourceResponse(
        contentType: httpResponse.headers.contentType?.mimeType ?? "text/html",
        contentEncoding: httpResponse.headers.value("content-encoding") ?? "gzip",
        data: responseBytes,
        statusCode: httpResponse.statusCode,
        reasonPhrase: httpResponse.reasonPhrase,
      );
    } catch (e) {
      print('HttpClient request failed: $e');
      return null;
    }
  }
LacticWhale commented 1 week ago

To clarify:

wanliyunyan commented 1 week ago

yes,I use Xray as my local SOCKS server. I only use socks client from this package,and start Xray to listen on port 10808.

  @override
  void initState() {
    super.initState();
    loadingWebView();
  }

  Future<void> loadingWebView() async {
    final client = HttpClient();

    SocksTCPClient.assignToHttpClient(
      client, [ProxySettings(InternetAddress("127.0.0.1"), 10808)]   // to connect local Xray socks5 server 
    );

    try {
      var url = Uri.parse("https://www.baidu.com/");
      HttpClientRequest httpRequest = await client.getUrl(url);
      HttpClientResponse httpResponse = await httpRequest.close();
      String responseBody = await httpResponse.transform(utf8.decoder).join();
      print('Response: $responseBody');

    } catch (e) {
      print('error: $e');
    } finally {
      client.close();
    }
  }

The local Xray server works well with hev-socks5-tunnel.

curl -x socks5h://127.0.0.1:10808 https://www.baidu.com      also works well

      final client = HttpClient()
        ..findProxy = (_) {
          return 'PROXY localhost:10808';
        };   // also works well
LacticWhale commented 1 week ago

Oh I see the issue now. Can you run your code using lookup branch?

     socks5_proxy:
       git:
         url: git://github.com/lacticwhale/socks_dart.git
         ref: lookup

And add tryLookup: true here:

    SocksTCPClient.assignToHttpClient(
      client, [ProxySettings(InternetAddress("127.0.0.1"), 10808)],   // to connect local Xray socks5 server 
      tryLookup: true,
    );

If it helped I will polish this feature and put into next release.

wanliyunyan commented 1 week ago
  socks5_proxy:
    git:
      url: https://github.com/lacticwhale/socks_dart.git
      ref: lookup

works well,thank you

LacticWhale commented 1 week ago

Ok I will do a better interface and release update soon.

The problem was that some socks5 proxy don't do DNS lookup, they usually noted as socks5h.