Skip to content

Commit 46a9a79

Browse files
committed
net: respect -onlynet= when making outbound connections
Do not make outbound connections to hosts which belong to a network which is restricted by `-onlynet`. This applies to hosts that are automatically chosen to connect to and to anchors. This does not apply to hosts given to `-connect`, `-addnode`, `addnode` RPC, dns seeds, `-seednodes`. Fixes bitcoin#13378 Fixes bitcoin#22647 Supersedes bitcoin#22651
1 parent 629c4ab commit 46a9a79

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

doc/i2p.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ logging` for more information.
6767

6868
Make outgoing connections only to I2P addresses. Incoming connections are not
6969
affected by this option. It can be specified multiple times to allow multiple
70-
network types, e.g. onlynet=ipv4, onlynet=ipv6, onlynet=onion, onlynet=i2p.
71-
72-
Warning: if you use -onlynet with values other than onion, and the -onion or
73-
-proxy option is set, then outgoing onion connections will still be made; use
74-
-noonion or -onion=0 to disable outbound onion connections in this case.
70+
network types, e.g. onlynet=onion, onlynet=i2p.
7571

7672
I2P support was added to Bitcoin Core in version 22.0 and there may be fewer I2P
7773
peers than Tor or IP ones. Therefore, using I2P alone without other networks may

doc/tor.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ outgoing connections, but more is possible.
5757
-onlynet=onion Make outgoing connections only to .onion addresses. Incoming
5858
connections are not affected by this option. This option can be
5959
specified multiple times to allow multiple network types, e.g.
60-
onlynet=ipv4, onlynet=ipv6, onlynet=onion, onlynet=i2p.
61-
Warning: if you use -onlynet with values other than onion, and
62-
the -onion or -proxy option is set, then outgoing onion
63-
connections will still be made; use -noonion or -onion=0 to
64-
disable outbound onion connections in this case.
60+
onlynet=onion, onlynet=i2p.
6561

6662
In a typical situation, this suffices to run behind a Tor proxy:
6763

src/init.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ void SetupServerArgs(ArgsManager& argsman)
437437
argsman.AddArg("-onion=<ip:port>", "Use separate SOCKS5 proxy to reach peers via Tor onion services, set -noonion to disable (default: -proxy)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
438438
argsman.AddArg("-i2psam=<ip:port>", "I2P SAM proxy to reach I2P peers and accept I2P connections (default: none)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
439439
argsman.AddArg("-i2pacceptincoming", "If set and -i2psam is also set then incoming I2P connections are accepted via the SAM proxy. If this is not set but -i2psam is set then only outgoing connections will be made to the I2P network. Ignored if -i2psam is not set. Listening for incoming I2P connections is done through the SAM proxy, not by binding to a local address and port (default: 1)", ArgsManager::ALLOW_BOOL, OptionsCategory::CONNECTION);
440-
argsman.AddArg("-onlynet=<net>", "Make outgoing connections only through network <net> (" + Join(GetNetworkNames(), ", ") + "). Incoming connections are not affected by this option. This option can be specified multiple times to allow multiple networks. Warning: if it is used with non-onion networks and the -onion or -proxy option is set, then outbound onion connections will still be made; use -noonion or -onion=0 to disable outbound onion connections in this case.", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
440+
argsman.AddArg("-onlynet=<net>", "Make automatic outgoing connections only through network <net> (" + Join(GetNetworkNames(), ", ") + "). Incoming connections are not affected by this option. This option can be specified multiple times to allow multiple networks.", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
441441
argsman.AddArg("-peerbloomfilters", strprintf("Support filtering of blocks and transaction with bloom filters (default: %u)", DEFAULT_PEERBLOOMFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
442442
argsman.AddArg("-peerblockfilters", strprintf("Serve compact block filters to peers per BIP 157 (default: %u)", DEFAULT_PEERBLOCKFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
443443
argsman.AddArg("-permitbaremultisig", strprintf("Relay non-P2SH multisig (default: %u)", DEFAULT_PERMIT_BAREMULTISIG), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
@@ -1238,6 +1238,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12381238
SetReachable(net, false);
12391239
}
12401240
}
1241+
// Automatic outbound connections are restricted to some networks (-onlynet)
1242+
// and the Tor network is not among the allowed ones.
1243+
const bool onion_restricted{!IsReachable(NET_ONION)};
12411244

12421245
// Check for host lookup allowed before parsing any network related parameters
12431246
fNameLookup = args.GetBoolArg("-dns", DEFAULT_NAME_LOOKUP);
@@ -1261,7 +1264,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12611264
SetProxy(NET_IPV6, addrProxy);
12621265
SetProxy(NET_ONION, addrProxy);
12631266
SetNameProxy(addrProxy);
1264-
SetReachable(NET_ONION, true); // by default, -proxy sets onion as reachable, unless -noonion later
1267+
if (!onion_restricted) {
1268+
SetReachable(NET_ONION, true); // by default, -proxy sets onion as reachable, unless -noonion later
1269+
}
12651270
}
12661271

12671272
// -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
@@ -1270,6 +1275,11 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12701275
std::string onionArg = args.GetArg("-onion", "");
12711276
if (onionArg != "") {
12721277
if (onionArg == "0") { // Handle -noonion/-onion=0
1278+
if (args.IsArgSet("-onlynet") && IsReachable(NET_ONION)) {
1279+
return InitError(
1280+
_("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
1281+
"reaching the Tor network is not provided (-onion=0)"));
1282+
}
12731283
SetReachable(NET_ONION, false);
12741284
} else {
12751285
CService onionProxy;
@@ -1280,7 +1290,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12801290
if (!addrOnion.IsValid())
12811291
return InitError(strprintf(_("Invalid -onion address or hostname: '%s'"), onionArg));
12821292
SetProxy(NET_ONION, addrOnion);
1283-
SetReachable(NET_ONION, true);
1293+
if (!onion_restricted) {
1294+
SetReachable(NET_ONION, true);
1295+
}
12841296
}
12851297
}
12861298

@@ -1791,7 +1803,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17911803
if (!Lookup(i2psam_arg, addr, 7656, fNameLookup) || !addr.IsValid()) {
17921804
return InitError(strprintf(_("Invalid -i2psam address or hostname: '%s'"), i2psam_arg));
17931805
}
1794-
SetReachable(NET_I2P, true);
17951806
SetProxy(NET_I2P, proxyType{addr});
17961807
} else {
17971808
SetReachable(NET_I2P, false);

src/torcontrol.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply&
379379
CService resolved(LookupNumeric("127.0.0.1", 9050));
380380
proxyType addrOnion = proxyType(resolved, true);
381381
SetProxy(NET_ONION, addrOnion);
382-
SetReachable(NET_ONION, true);
383382
}
384383

385384
// Finally - now create the service

0 commit comments

Comments
 (0)