implement tor killswitch

comment update
This commit is contained in:
sneurlax 2023-09-08 10:50:50 -05:00
parent cf27dd9252
commit d317bc5e8b

View file

@ -158,55 +158,71 @@ class ElectrumX {
} }
void _checkRpcClient() { void _checkRpcClient() {
// If we're supposed to use Tor...
if (_prefs.useTor) { if (_prefs.useTor) {
// But Tor isn't enabled...
if (!_torService.enabled) { if (!_torService.enabled) {
throw Exception("Tor is not enabled"); // And the killswitch isn't set...
} if (!_prefs.torKillswitch) {
// Then we'll just proceed and connect to ElectrumX through clearnet at the bottom of this function.
final proxyInfo = _torService.proxyInfo; Logging.instance.log(
"Tor preference set but Tor is not enabled, killswitch not set, connecting to ElectrumX through clearnet",
if (currentFailoverIndex == -1) { level: LogLevel.Warning,
_rpcClient ??= JsonRPC( );
host: host, } else {
port: port, // ... But if the killswitch is set, then we throw an exception.
useSSL: useSSL, throw Exception(
connectionTimeout: connectionTimeoutForSpecialCaseJsonRPCClients, "Tor preference and killswitch set but Tor is not enabled, not connecting to ElectrumX");
proxyInfo: proxyInfo, }
);
} else { } else {
_rpcClient ??= JsonRPC( // Get the proxy info from the TorService.
host: failovers![currentFailoverIndex].address, final proxyInfo = _torService.proxyInfo;
port: failovers![currentFailoverIndex].port,
useSSL: failovers![currentFailoverIndex].useSSL,
connectionTimeout: connectionTimeoutForSpecialCaseJsonRPCClients,
proxyInfo: proxyInfo,
);
}
if (_rpcClient!.proxyInfo != proxyInfo) { if (currentFailoverIndex == -1) {
_rpcClient!.proxyInfo = proxyInfo; _rpcClient ??= JsonRPC(
_rpcClient!.disconnect( host: host,
reason: "Tor proxyInfo does not match current info", port: port,
); useSSL: useSSL,
connectionTimeout: connectionTimeoutForSpecialCaseJsonRPCClients,
proxyInfo: proxyInfo,
);
} else {
_rpcClient ??= JsonRPC(
host: failovers![currentFailoverIndex].address,
port: failovers![currentFailoverIndex].port,
useSSL: failovers![currentFailoverIndex].useSSL,
connectionTimeout: connectionTimeoutForSpecialCaseJsonRPCClients,
proxyInfo: proxyInfo,
);
}
if (_rpcClient!.proxyInfo != proxyInfo) {
_rpcClient!.proxyInfo = proxyInfo;
_rpcClient!.disconnect(
reason: "Tor proxyInfo does not match current info",
);
}
return;
} }
}
if (currentFailoverIndex == -1) {
_rpcClient ??= JsonRPC(
host: host,
port: port,
useSSL: useSSL,
connectionTimeout: connectionTimeoutForSpecialCaseJsonRPCClients,
proxyInfo: null,
);
} else { } else {
if (currentFailoverIndex == -1) { _rpcClient ??= JsonRPC(
_rpcClient ??= JsonRPC( host: failovers![currentFailoverIndex].address,
host: host, port: failovers![currentFailoverIndex].port,
port: port, useSSL: failovers![currentFailoverIndex].useSSL,
useSSL: useSSL, connectionTimeout: connectionTimeoutForSpecialCaseJsonRPCClients,
connectionTimeout: connectionTimeoutForSpecialCaseJsonRPCClients, proxyInfo: null,
proxyInfo: null, );
);
} else {
_rpcClient ??= JsonRPC(
host: failovers![currentFailoverIndex].address,
port: failovers![currentFailoverIndex].port,
useSSL: failovers![currentFailoverIndex].useSSL,
connectionTimeout: connectionTimeoutForSpecialCaseJsonRPCClients,
proxyInfo: null,
);
}
} }
} }