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

RangeError (RangeError: _readBytes has fewer bytes than expected.) #41

Open
nns52k opened this issue Apr 30, 2024 · 2 comments
Open

RangeError (RangeError: _readBytes has fewer bytes than expected.) #41

nns52k opened this issue Apr 30, 2024 · 2 comments

Comments

@nns52k
Copy link

nns52k commented Apr 30, 2024

To recreate this bug, we need to import socks5_proxy and tor:

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

Assume we have these 2 code lines in function main:

  final int port = await startTor();
  final HttpClient httpClient = createProxyClient(port);

The functions startTor and createProxyClient are implemented as follows:

Future<int> startTor() async {
  await Tor.init();
  final DateTime t1 = DateTime.now();
  await Tor.instance.start();
  print('It took ${DateTime.now().difference(t1).inSeconds} seconds to connect Tor network.');
  return Tor.instance.port;
}

HttpClient createProxyClient(int port) {
  final httpClient = HttpClient();
  SocksTCPClient.assignToHttpClientWithSecureOptions(
    httpClient,
    [ProxySettings(InternetAddress.loopbackIPv4, port)],
    onBadCertificate: (X509Certificate certificate) {
      return true;
    },
  );
  return httpClient;
}

To use the httpClient variable, we have these 2 invocations in main:

  await accessInternetThroughTor(httpClient, destination: Uri.parse('https://icanhazip.com/'));
  await accessInternetThroughTor(httpClient, destination: Uri.parse('https://duckduckgogg42xjoc72x3sjasowoarfbgcmvfimaftt6twagswzczad.onion/'));

The function accessInternetThroughTor is implemented as follows:

Future<void> accessInternetThroughTor(HttpClient httpClient, {required Uri destination}) async {
  final HttpClientRequest request = await httpClient.getUrl(destination); // FIXME: RangeError (RangeError: _readBytes has fewer bytes than expected.)
  final HttpClientResponse response = await request.close();
  print('STATUS: ${response.statusCode}\nRESPONSE: ${await utf8.decodeStream(response)}');
}

It looks like a legit way to use Tor proxy, but it will lead to RangeError (see the comment in accessInternetThroughTor above).

If we run an external Tor process in background listensing TCP port, say 9050, and we pass the port number to the createProxyClient invocation, all these codes work. No RangeError would appear. I've tried this on Linux, building the Flutter app as a Linux GUI application, and have this observation.

On Android the same error happens, if we use the port number returned from startTor ( See the code above).

I don't know it's a bug in package socks5_proxy or tor. When use an external Tor, all codes work. So it looks like a bug in package tor. On the other hand, the RangeError is thrown from package socks5_proxy.

@sneurlax
Copy link
Collaborator

Also, version 2.0 of https://github.com/LacticWhale/socks_dart has since been released and this issue needs re-assessing in light of that update.

@sneurlax
Copy link
Collaborator

sneurlax commented Oct 3, 2024

@nns52k care to try again?

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