Skip to content

Commit 2d0b4e4

Browse files
committed
init: allow startup with -onlynet=onion -listenonion=1
It does not make sense to specify `-onlynet=onion` without providing a Tor proxy (even if other `-onlynet=...` are given). This is checked during startup. However, it was forgotten that a Tor proxy can also be retrieved from "Tor control" to which we connect if `-listenonion=1`. So, the full Tor proxy retrieval logic is: 1. get it from `-onion` 2. get it from `-proxy` 3. if `-listenonion=1`, then connect to "Tor control" and get the proxy from there (was forgotten before this change) Fixes bitcoin/bitcoin#24980
1 parent 5291933 commit 2d0b4e4

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/init.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,13 +1307,20 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13071307
onion_proxy = addrProxy;
13081308
}
13091309

1310+
const bool onlynet_used_with_onion{args.IsArgSet("-onlynet") && IsReachable(NET_ONION)};
1311+
13101312
// -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
13111313
// -noonion (or -onion=0) disables connecting to .onion entirely
13121314
// An empty string is used to not override the onion proxy (in which case it defaults to -proxy set above, or none)
13131315
std::string onionArg = args.GetArg("-onion", "");
13141316
if (onionArg != "") {
13151317
if (onionArg == "0") { // Handle -noonion/-onion=0
13161318
onion_proxy = Proxy{};
1319+
if (onlynet_used_with_onion) {
1320+
return InitError(
1321+
_("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
1322+
"reaching the Tor network is explicitly forbidden: -onion=0"));
1323+
}
13171324
} else {
13181325
CService addr;
13191326
if (!Lookup(onionArg, addr, 9050, fNameLookup) || !addr.IsValid()) {
@@ -1326,11 +1333,14 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13261333
if (onion_proxy.IsValid()) {
13271334
SetProxy(NET_ONION, onion_proxy);
13281335
} else {
1329-
if (args.IsArgSet("-onlynet") && IsReachable(NET_ONION)) {
1336+
// If -listenonion is set, then we will (try to) connect to the Tor control port
1337+
// later from the torcontrol thread and may retrieve the onion proxy from there.
1338+
const bool listenonion_disabled{!args.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION)};
1339+
if (onlynet_used_with_onion && listenonion_disabled) {
13301340
return InitError(
13311341
_("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
1332-
"reaching the Tor network is not provided (no -proxy= and no -onion= given) or "
1333-
"it is explicitly forbidden (-onion=0)"));
1342+
"reaching the Tor network is not provided: none of -proxy, -onion or "
1343+
"-listenonion is given"));
13341344
}
13351345
SetReachable(NET_ONION, false);
13361346
}

test/functional/feature_proxy.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,20 +332,27 @@ def networks_dict(d):
332332
msg = "Error: Invalid -i2psam address or hostname: 'def:xyz'"
333333
self.nodes[1].assert_start_raises_init_error(expected_msg=msg)
334334

335+
self.log.info("Test passing -onlynet=onion with -onion=0/-noonion raises expected init error")
335336
msg = (
336337
"Error: Outbound connections restricted to Tor (-onlynet=onion) but "
337-
"the proxy for reaching the Tor network is not provided (no -proxy= "
338-
"and no -onion= given) or it is explicitly forbidden (-onion=0)"
338+
"the proxy for reaching the Tor network is explicitly forbidden: -onion=0"
339339
)
340-
self.log.info("Test passing -onlynet=onion without -proxy or -onion raises expected init error")
341-
self.nodes[1].extra_args = ["-onlynet=onion"]
342-
self.nodes[1].assert_start_raises_init_error(expected_msg=msg)
343-
344-
self.log.info("Test passing -onlynet=onion with -onion=0/-noonion raises expected init error")
345340
for arg in ["-onion=0", "-noonion"]:
346341
self.nodes[1].extra_args = ["-onlynet=onion", arg]
347342
self.nodes[1].assert_start_raises_init_error(expected_msg=msg)
348343

344+
self.log.info("Test passing -onlynet=onion without -proxy, -onion or -listenonion raises expected init error")
345+
self.nodes[1].extra_args = ["-onlynet=onion", "-listenonion=0"]
346+
msg = (
347+
"Error: Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
348+
"reaching the Tor network is not provided: none of -proxy, -onion or -listenonion is given"
349+
)
350+
self.nodes[1].assert_start_raises_init_error(expected_msg=msg)
351+
352+
self.log.info("Test passing -onlynet=onion without -proxy or -onion but with -listenonion=1 is ok")
353+
self.start_node(1, extra_args=["-onlynet=onion", "-listenonion=1"])
354+
self.stop_node(1)
355+
349356
self.log.info("Test passing unknown network to -onlynet raises expected init error")
350357
self.nodes[1].extra_args = ["-onlynet=abc"]
351358
msg = "Error: Unknown network specified in -onlynet: 'abc'"

0 commit comments

Comments
 (0)