Skip to content

Commit 94d1784

Browse files
committed
Merge bitcoin/bitcoin#24991: init: allow startup with -onlynet=onion -listenonion=1
2d0b4e4 init: allow startup with -onlynet=onion -listenonion=1 (Vasil Dimov) Pull request description: 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 ACKs for top commit: mzumsande: Tested ACK 2d0b4e4 MarcoFalke: ACK 2d0b4e4 🕸 Tree-SHA512: d1d18e07a8a40a47b7f00c31cb291a3d3a9b24eeb28c5e4720d5df4997f488583a3a010d46902b4b600d2ed1136a368e1051c133847ae165e0748b8167603dc3
2 parents a361c6c + 2d0b4e4 commit 94d1784

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
@@ -1324,13 +1324,20 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13241324
onion_proxy = addrProxy;
13251325
}
13261326

1327+
const bool onlynet_used_with_onion{args.IsArgSet("-onlynet") && IsReachable(NET_ONION)};
1328+
13271329
// -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
13281330
// -noonion (or -onion=0) disables connecting to .onion entirely
13291331
// An empty string is used to not override the onion proxy (in which case it defaults to -proxy set above, or none)
13301332
std::string onionArg = args.GetArg("-onion", "");
13311333
if (onionArg != "") {
13321334
if (onionArg == "0") { // Handle -noonion/-onion=0
13331335
onion_proxy = Proxy{};
1336+
if (onlynet_used_with_onion) {
1337+
return InitError(
1338+
_("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
1339+
"reaching the Tor network is explicitly forbidden: -onion=0"));
1340+
}
13341341
} else {
13351342
CService addr;
13361343
if (!Lookup(onionArg, addr, 9050, fNameLookup) || !addr.IsValid()) {
@@ -1343,11 +1350,14 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13431350
if (onion_proxy.IsValid()) {
13441351
SetProxy(NET_ONION, onion_proxy);
13451352
} else {
1346-
if (args.IsArgSet("-onlynet") && IsReachable(NET_ONION)) {
1353+
// If -listenonion is set, then we will (try to) connect to the Tor control port
1354+
// later from the torcontrol thread and may retrieve the onion proxy from there.
1355+
const bool listenonion_disabled{!args.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION)};
1356+
if (onlynet_used_with_onion && listenonion_disabled) {
13471357
return InitError(
13481358
_("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
1349-
"reaching the Tor network is not provided (no -proxy= and no -onion= given) or "
1350-
"it is explicitly forbidden (-onion=0)"));
1359+
"reaching the Tor network is not provided: none of -proxy, -onion or "
1360+
"-listenonion is given"));
13511361
}
13521362
SetReachable(NET_ONION, false);
13531363
}

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)