mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-11-17 09:47:37 +00:00
parent
ed99bdc169
commit
5cccd3e4a3
2 changed files with 73 additions and 172 deletions
|
@ -38,7 +38,6 @@ class JsonRPC {
|
||||||
final _JsonRPCRequestQueue _requestQueue = _JsonRPCRequestQueue();
|
final _JsonRPCRequestQueue _requestQueue = _JsonRPCRequestQueue();
|
||||||
Socket? _socket;
|
Socket? _socket;
|
||||||
SOCKSSocket? _socksSocket;
|
SOCKSSocket? _socksSocket;
|
||||||
SOCKS5Proxy? _socksProxy;
|
|
||||||
StreamSubscription<Uint8List>? _subscription;
|
StreamSubscription<Uint8List>? _subscription;
|
||||||
|
|
||||||
void _dataHandler(List<int> data) {
|
void _dataHandler(List<int> data) {
|
||||||
|
@ -86,17 +85,8 @@ class JsonRPC {
|
||||||
_socket!.write('${req.jsonRequest}\r\n');
|
_socket!.write('${req.jsonRequest}\r\n');
|
||||||
}
|
}
|
||||||
if (_socksSocket != null) {
|
if (_socksSocket != null) {
|
||||||
print('writing to _socksSocket: ${req.jsonRequest}');
|
|
||||||
print(req.jsonRequest);
|
|
||||||
print(req);
|
|
||||||
_socksSocket!.write('${req.jsonRequest}\r\n');
|
_socksSocket!.write('${req.jsonRequest}\r\n');
|
||||||
}
|
}
|
||||||
if (_socksProxy != null) {
|
|
||||||
print('writing to _socksProxy: ${req.jsonRequest}');
|
|
||||||
print(req.jsonRequest);
|
|
||||||
print(req);
|
|
||||||
_socksProxy!.write('${req.jsonRequest}\r\n');
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO different timeout length?
|
// TODO different timeout length?
|
||||||
req.initiateTimeout(
|
req.initiateTimeout(
|
||||||
|
@ -119,19 +109,12 @@ class JsonRPC {
|
||||||
"JsonRPC request: opening socket $host:$port",
|
"JsonRPC request: opening socket $host:$port",
|
||||||
level: LogLevel.Info,
|
level: LogLevel.Info,
|
||||||
);
|
);
|
||||||
// await connect();
|
await connect();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// if (_socksSocket == null) {
|
if (_socksSocket == null) {
|
||||||
// Logging.instance.log(
|
|
||||||
// "JsonRPC request: opening SOCKS socket to $host:$port",
|
|
||||||
// level: LogLevel.Info,
|
|
||||||
// );
|
|
||||||
// await connect();
|
|
||||||
// }
|
|
||||||
if (_socksProxy == null) {
|
|
||||||
Logging.instance.log(
|
Logging.instance.log(
|
||||||
"JsonRPC request: opening SOCKS proxy to $host:$port",
|
"JsonRPC request: opening SOCKS socket to $host:$port",
|
||||||
level: LogLevel.Info,
|
level: LogLevel.Info,
|
||||||
);
|
);
|
||||||
await connect();
|
await connect();
|
||||||
|
@ -202,163 +185,82 @@ class JsonRPC {
|
||||||
timeout: connectionTimeout,
|
timeout: connectionTimeout,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
_subscription = _socket!.listen(
|
|
||||||
_dataHandler,
|
|
||||||
onError: _errorHandler,
|
|
||||||
onDone: _doneHandler,
|
|
||||||
cancelOnError: true,
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
if (_socksProxy == null) {
|
if (proxyInfo == null) {
|
||||||
print(1111111);
|
// TODO await tor / make sure it's running
|
||||||
_socksProxy = SOCKS5Proxy();
|
proxyInfo = (
|
||||||
// TODO check if null
|
host: InternetAddress.loopbackIPv4.address,
|
||||||
await _socksProxy!.connect();
|
port: TorService.sharedInstance.port
|
||||||
print(222222);
|
|
||||||
// TODO check if null
|
|
||||||
await _socksProxy!.connectTo('bitcoincash.stackwallet.com', 50002);
|
|
||||||
|
|
||||||
print(333333);
|
|
||||||
|
|
||||||
// TODO check if null
|
|
||||||
_subscription = _socksProxy!.socket.listen(
|
|
||||||
_dataHandler,
|
|
||||||
onError: _errorHandler,
|
|
||||||
onDone: _doneHandler,
|
|
||||||
cancelOnError: true,
|
|
||||||
);
|
);
|
||||||
} else {
|
Logging.instance.log(
|
||||||
print('0000000');
|
"ElectrumX.connect(): no tor proxy info, read $proxyInfo",
|
||||||
|
level: LogLevel.Warning);
|
||||||
}
|
}
|
||||||
// if (proxyInfo == null) {
|
// TODO connect to proxy socket...
|
||||||
// // TODO await tor / make sure it's running
|
|
||||||
// proxyInfo = (
|
// TODO implement ssl over tor
|
||||||
// host: InternetAddress.loopbackIPv4.address,
|
// if (useSSL) {
|
||||||
// port: TorService.sharedInstance.port
|
// _socket = await SecureSocket.connect(
|
||||||
// );
|
// host,
|
||||||
// Logging.instance.log(
|
// port,
|
||||||
// "ElectrumX.connect(): no tor proxy info, read $proxyInfo",
|
// timeout: connectionTimeout,
|
||||||
// level: LogLevel.Warning);
|
// onBadCertificate: (_) => true,
|
||||||
// }
|
// ); // TODO do not automatically trust bad certificates
|
||||||
// // TODO connect to proxy socket...
|
// final _client = SocksSocket.protected(_socket, type);
|
||||||
//
|
|
||||||
// // TODO implement ssl over tor
|
|
||||||
// // if (useSSL) {
|
|
||||||
// // _socket = await SecureSocket.connect(
|
|
||||||
// // host,
|
|
||||||
// // port,
|
|
||||||
// // timeout: connectionTimeout,
|
|
||||||
// // onBadCertificate: (_) => true,
|
|
||||||
// // ); // TODO do not automatically trust bad certificates
|
|
||||||
// // final _client = SocksSocket.protected(_socket, type);
|
|
||||||
// // } else {
|
|
||||||
// final sock = await RawSocket.connect(
|
|
||||||
// InternetAddress.loopbackIPv4, proxyInfo!.port);
|
|
||||||
//
|
|
||||||
// if (_socksSocket == null) {
|
|
||||||
// Logging.instance.log(
|
|
||||||
// "JsonRPC.connect(): creating SOCKS socket at $proxyInfo",
|
|
||||||
// level: LogLevel.Info);
|
|
||||||
// _socksSocket = SOCKSSocket(sock);
|
|
||||||
// if (_socksSocket == null) {
|
|
||||||
// Logging.instance.log(
|
|
||||||
// "JsonRPC.connect(): failed to create SOCKS socket at $proxyInfo",
|
|
||||||
// level: LogLevel.Error);
|
|
||||||
// throw Exception(
|
|
||||||
// "JsonRPC.connect(): failed to create SOCKS socket at $proxyInfo");
|
|
||||||
// } else {
|
|
||||||
// Logging.instance.log(
|
|
||||||
// "JsonRPC.connect(): created SOCKS socket at $proxyInfo",
|
|
||||||
// level: LogLevel.Info);
|
|
||||||
// }
|
|
||||||
// } else {
|
// } else {
|
||||||
// // TODO also check if sock == previous sock, eg. if RawSocket is different
|
final sock = await RawSocket.connect(
|
||||||
// Logging.instance.log(
|
InternetAddress.loopbackIPv4, proxyInfo!.port);
|
||||||
// "JsonRPC.connect(): using pre-existing SOCKS socket at $proxyInfo",
|
|
||||||
// level: LogLevel.Info);
|
if (_socksSocket == null) {
|
||||||
// }
|
Logging.instance.log(
|
||||||
//
|
"JsonRPC.connect(): creating SOCKS socket at $proxyInfo",
|
||||||
// try {
|
level: LogLevel.Info);
|
||||||
// Logging.instance.log(
|
_socksSocket = SOCKSSocket(sock);
|
||||||
// "JsonRPC.connect(): connecting to $host:$port over SOCKS socket at $proxyInfo...",
|
if (_socksSocket == null) {
|
||||||
// level: LogLevel.Info);
|
Logging.instance.log(
|
||||||
// if (!isIpAddress(host)) {
|
"JsonRPC.connect(): failed to create SOCKS socket at $proxyInfo",
|
||||||
// await _socksSocket!.connect("$host:$port");
|
level: LogLevel.Error);
|
||||||
// } else {
|
throw Exception(
|
||||||
// await _socksSocket!.connectIp(InternetAddress(host), port);
|
"JsonRPC.connect(): failed to create SOCKS socket at $proxyInfo");
|
||||||
// }
|
} else {
|
||||||
// Logging.instance.log(
|
Logging.instance.log(
|
||||||
// "JsonRPC.connect(): connected to $host:$port over SOCKS socket at $proxyInfo",
|
"JsonRPC.connect(): created SOCKS socket at $proxyInfo",
|
||||||
// level: LogLevel.Info);
|
level: LogLevel.Info);
|
||||||
// } catch (e) {
|
}
|
||||||
// Logging.instance.log(
|
} else {
|
||||||
// "JsonRPC.connect(): failed to connect to $host over tor proxy at $proxyInfo, $e",
|
// TODO also check if sock == previous sock, eg. if RawSocket is different
|
||||||
// level: LogLevel.Error);
|
Logging.instance.log(
|
||||||
// throw Exception(
|
"JsonRPC.connect(): using pre-existing SOCKS socket at $proxyInfo",
|
||||||
// "JsonRPC.connect(): failed to connect to tor proxy, $e");
|
level: LogLevel.Info);
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Logging.instance.log(
|
||||||
|
"JsonRPC.connect(): connecting to $host:$port over SOCKS socket at $proxyInfo...",
|
||||||
|
level: LogLevel.Info);
|
||||||
|
if (!isIpAddress(host)) {
|
||||||
|
await _socksSocket!.connect("$host:$port");
|
||||||
|
} else {
|
||||||
|
await _socksSocket!.connectIp(InternetAddress(host), port);
|
||||||
|
}
|
||||||
|
Logging.instance.log(
|
||||||
|
"JsonRPC.connect(): connected to $host:$port over SOCKS socket at $proxyInfo",
|
||||||
|
level: LogLevel.Info);
|
||||||
|
} catch (e) {
|
||||||
|
Logging.instance.log(
|
||||||
|
"JsonRPC.connect(): failed to connect to $host over tor proxy at $proxyInfo, $e",
|
||||||
|
level: LogLevel.Error);
|
||||||
|
throw Exception(
|
||||||
|
"JsonRPC.connect(): failed to connect to tor proxy, $e");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class SOCKS5Proxy {
|
_subscription = _socket!.listen(
|
||||||
final String host;
|
_dataHandler,
|
||||||
final int port;
|
onError: _errorHandler,
|
||||||
|
onDone: _doneHandler,
|
||||||
late Socket _socks5Socket;
|
cancelOnError: true,
|
||||||
Socket get socket => _socks5Socket;
|
);
|
||||||
|
|
||||||
SOCKS5Proxy({String? host, int? port})
|
|
||||||
: host = host ?? InternetAddress.loopbackIPv4.address,
|
|
||||||
port = port ?? TorService.sharedInstance.port;
|
|
||||||
|
|
||||||
Future<void> connect() async {
|
|
||||||
_socks5Socket = await Socket.connect(host, port);
|
|
||||||
|
|
||||||
// Greeting and method selection
|
|
||||||
_socks5Socket.add([0x05, 0x01, 0x00]);
|
|
||||||
|
|
||||||
// Wait for server response
|
|
||||||
var response = await _socks5Socket.first;
|
|
||||||
if (response[1] != 0x00) {
|
|
||||||
throw Exception('Failed to connect to SOCKS5 proxy.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is just a basic example for domain-based addresses.
|
|
||||||
Future<void> connectTo(String domain, int port) async {
|
|
||||||
// Command, Reserved, Address Type, Address, Port
|
|
||||||
var request = [
|
|
||||||
0x05,
|
|
||||||
0x01,
|
|
||||||
0x00,
|
|
||||||
0x03,
|
|
||||||
domain.length,
|
|
||||||
...domain.codeUnits,
|
|
||||||
(port >> 8) & 0xFF,
|
|
||||||
port & 0xFF
|
|
||||||
];
|
|
||||||
|
|
||||||
_socks5Socket.add(request);
|
|
||||||
print(444444);
|
|
||||||
|
|
||||||
// Wait for server response
|
|
||||||
// var response = await _socks5Socket.first;
|
|
||||||
// if (response[1] != 0x00) {
|
|
||||||
// throw Exception('Failed to connect to target through SOCKS5 proxy.');
|
|
||||||
// }
|
|
||||||
// print(response);
|
|
||||||
print(55555);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Converts [object] to a String by invoking [Object.toString] and
|
|
||||||
/// sends the encoding of the result to the socket.
|
|
||||||
void write(Object? object) {
|
|
||||||
if (object == null) return;
|
|
||||||
|
|
||||||
List<int> data = utf8.encode(object.toString());
|
|
||||||
_socks5Socket.add(data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -275,7 +275,6 @@ class SOCKSSocket {
|
||||||
break;
|
break;
|
||||||
case RawSocketEvent.write:
|
case RawSocketEvent.write:
|
||||||
// TODO: Handle this case.
|
// TODO: Handle this case.
|
||||||
|
|
||||||
Logging.instance.log(
|
Logging.instance.log(
|
||||||
"SOCKSSocket._start(): unhandled event RawSocketEvent.write",
|
"SOCKSSocket._start(): unhandled event RawSocketEvent.write",
|
||||||
level: LogLevel.Warning);
|
level: LogLevel.Warning);
|
||||||
|
|
Loading…
Reference in a new issue