2023-12-07 16:44:07 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:socks5_proxy/socks.dart';
|
|
|
|
import 'package:tor/tor.dart';
|
|
|
|
|
|
|
|
class ProxyWrapper {
|
|
|
|
// Private constructor
|
|
|
|
ProxyWrapper._privateConstructor();
|
|
|
|
|
|
|
|
// Static private instance of Tor
|
|
|
|
static final ProxyWrapper _instance = ProxyWrapper._privateConstructor();
|
|
|
|
|
|
|
|
HttpClient? _client;
|
|
|
|
|
|
|
|
bool started = false;
|
|
|
|
|
|
|
|
// Factory method to get the singleton instance of TorSingleton
|
|
|
|
static ProxyWrapper get instance => _instance;
|
|
|
|
|
2023-12-11 19:34:34 +00:00
|
|
|
static int get port => Tor.instance.port;
|
|
|
|
|
2023-12-12 20:29:06 +00:00
|
|
|
static bool get enabled => Tor.instance.enabled;
|
|
|
|
|
|
|
|
|
2023-12-07 16:44:07 +00:00
|
|
|
// Method to get or create the Tor instance
|
2023-12-11 19:34:34 +00:00
|
|
|
Future<HttpClient> getProxyInstance({int? portOverride}) async {
|
2023-12-07 16:44:07 +00:00
|
|
|
if (!started) {
|
|
|
|
started = true;
|
|
|
|
_client = HttpClient();
|
|
|
|
|
|
|
|
// Assign connection factory.
|
|
|
|
SocksTCPClient.assignToHttpClient(_client!, [
|
|
|
|
ProxySettings(
|
|
|
|
InternetAddress.loopbackIPv4,
|
2023-12-11 19:34:34 +00:00
|
|
|
portOverride ?? Tor.instance.port,
|
2023-12-07 16:44:07 +00:00
|
|
|
password: null,
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
return _client!;
|
|
|
|
}
|
|
|
|
}
|