Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can it be used together with Xray? #14

Open
wanliyunyan opened this issue Nov 1, 2024 · 5 comments
Open

Can it be used together with Xray? #14

wanliyunyan opened this issue Nov 1, 2024 · 5 comments

Comments

@wanliyunyan
Copy link

wanliyunyan commented Nov 1, 2024

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
Copy link
Owner

To clarify:

  • Issue happens while using other socks server?
  • You are only using socks client from this package? Or do you also run socks server using this package?

@wanliyunyan
Copy link
Author

wanliyunyan commented Nov 4, 2024

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
Copy link
Owner

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
Copy link
Author

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

works well,thank you

@LacticWhale
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants