Skip to content

Commit 32f8fda

Browse files
committed
merge bitcoin#24991: allow startup with -onlynet=onion -listenonion=1
1 parent e67ed92 commit 32f8fda

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/init.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -1821,13 +1821,20 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
18211821
onion_proxy = addrProxy;
18221822
}
18231823

1824+
const bool onlynet_used_with_onion{args.IsArgSet("-onlynet") && IsReachable(NET_ONION)};
1825+
18241826
// -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
18251827
// -noonion (or -onion=0) disables connecting to .onion entirely
18261828
// An empty string is used to not override the onion proxy (in which case it defaults to -proxy set above, or none)
18271829
std::string onionArg = args.GetArg("-onion", "");
18281830
if (onionArg != "") {
18291831
if (onionArg == "0") { // Handle -noonion/-onion=0
18301832
onion_proxy = Proxy{};
1833+
if (onlynet_used_with_onion) {
1834+
return InitError(
1835+
_("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
1836+
"reaching the Tor network is explicitly forbidden: -onion=0"));
1837+
}
18311838
} else {
18321839
CService addr;
18331840
if (!Lookup(onionArg, addr, 9050, fNameLookup) || !addr.IsValid()) {
@@ -1840,11 +1847,14 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
18401847
if (onion_proxy.IsValid()) {
18411848
SetProxy(NET_ONION, onion_proxy);
18421849
} else {
1843-
if (args.IsArgSet("-onlynet") && IsReachable(NET_ONION)) {
1850+
// If -listenonion is set, then we will (try to) connect to the Tor control port
1851+
// later from the torcontrol thread and may retrieve the onion proxy from there.
1852+
const bool listenonion_disabled{!args.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION)};
1853+
if (onlynet_used_with_onion && listenonion_disabled) {
18441854
return InitError(
18451855
_("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
1846-
"reaching the Tor network is not provided (no -proxy= and no -onion= given) or "
1847-
"it is explicitly forbidden (-onion=0)"));
1856+
"reaching the Tor network is not provided: none of -proxy, -onion or "
1857+
"-listenonion is given"));
18481858
}
18491859
SetReachable(NET_ONION, false);
18501860
}

test/functional/feature_proxy.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -328,20 +328,27 @@ def networks_dict(d):
328328
msg = "Error: Invalid -i2psam address or hostname: 'def:xyz'"
329329
self.nodes[1].assert_start_raises_init_error(expected_msg=msg)
330330

331+
self.log.info("Test passing -onlynet=onion with -onion=0/-noonion raises expected init error")
331332
msg = (
332333
"Error: Outbound connections restricted to Tor (-onlynet=onion) but "
333-
"the proxy for reaching the Tor network is not provided (no -proxy= "
334-
"and no -onion= given) or it is explicitly forbidden (-onion=0)"
334+
"the proxy for reaching the Tor network is explicitly forbidden: -onion=0"
335335
)
336-
self.log.info("Test passing -onlynet=onion without -proxy or -onion raises expected init error")
337-
self.nodes[1].extra_args = ["-onlynet=onion"]
338-
self.nodes[1].assert_start_raises_init_error(expected_msg=msg)
339-
340-
self.log.info("Test passing -onlynet=onion with -onion=0/-noonion raises expected init error")
341336
for arg in ["-onion=0", "-noonion"]:
342337
self.nodes[1].extra_args = ["-onlynet=onion", arg]
343338
self.nodes[1].assert_start_raises_init_error(expected_msg=msg)
344339

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

0 commit comments

Comments
 (0)