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

Unable to Access HTTP Response from Tor Proxy due to ByteReaderException #9

Closed nns52k closed 4 months ago

nns52k commented 4 months ago

As mentioned in https://github.com/LacticWhale/socks_dart/issues/8, though RangeError now becomes ByteReaderException, we still cannot get the HTTP response from the Tor proxy.

Assume we have simple code like this:

import 'package:socks5_proxy/socks_client.dart';
import 'package:tor/tor.dart';

Future<void> main() async {
  ...
  final int port = await startTor();
  final HttpClient httpClient = createProxyClient(port);
  await accessInternetThroughTor(httpClient, destination: Uri.parse('https://icanhazip.com/'));
  ...
}
Future<int> startTor() async {
  await Tor.init();
  await Tor.instance.start();
  return Tor.instance.port;
}
HttpClient createProxyClient(int port) {
  final httpClient = HttpClient();
  SocksTCPClient.assignToHttpClient(httpClient, [ProxySettings(InternetAddress.loopbackIPv4, port)]);
  return httpClient;
}
Future<void> accessInternetThroughTor(HttpClient httpClient, {required Uri destination}) async {
  final HttpClientRequest request = await httpClient.getUrl(destination); // FIXME
  final HttpClientResponse response = await request.close();
  print('STATUS: ${response.statusCode}\nRESPONSE: ${await utf8.decodeStream(response)}');
}

The line commented with FIXME will throw an ByteReaderException. Whether or not we catch it, the variable request won't be valid, which means we cannot access the destination site (in this example https://icanhazip.com/) through Tor proxy.

Please note that this isn't necessary an issue on package:socks5_proxy. It may be an issue on package:tor, due to the fact that when we use an external Tor proxy instead, all codes work.

Also, it would be great if you could try package:tor yourself. You definitely will know more about this issue than we do.

nns52k commented 4 months ago

It's more like a bug in package Tor. I tried the same code on utopic_tor_onion_proxy instead of Foundation-Devices/tor: Tor plugin for Flutter and the code finally works.