Skip to content

Commit 848b116

Browse files
committed
Merge bitcoin/bitcoin#22834: net: respect -onlynet= when making outbound connections
0eea83a scripted-diff: rename `proxyType` to `Proxy` (Vasil Dimov) e53a850 net: respect -onlynet= when making outbound connections (Vasil Dimov) Pull request description: 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, `-seednode`. Fixes bitcoin/bitcoin#13378 Fixes bitcoin/bitcoin#22647 Supersedes bitcoin/bitcoin#22651 ACKs for top commit: naumenkogs: utACK 0eea83a prayank23: reACK bitcoin/bitcoin@0eea83a jonatack: ACK 0eea83a code review, rebased to master, debug built, and did some manual testing with various config options on signet Tree-SHA512: 37d68b449dd6d2715843fc84d85f48fa2508be40ea105a7f4a28443b318d0b6bd39e3b2ca2a6186f2913836adf08d91038a8b142928e1282130f39ac81aa741b
2 parents 024b8e1 + 0eea83a commit 848b116

File tree

14 files changed

+72
-52
lines changed

14 files changed

+72
-52
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/release-notes.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ Updated settings
153153
E.g. `-maxuploadtarget=500g`. No whitespace, +- or fractions allowed.
154154
Default is `M` if no suffix provided. (#23249)
155155

156+
- If `-proxy=` is given together with `-noonion` then the provided proxy will
157+
not be set as a proxy for reaching the Tor network. So it will not be
158+
possible to open manual connections to the Tor network for example with the
159+
`addnode` RPC. To mimic the old behavior use `-proxy=` together with
160+
`-onlynet=` listing all relevant networks except `onion`. (#22834)
161+
156162
Tools and Utilities
157163
-------------------
158164

doc/tor.md

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

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

src/init.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ void SetupServerArgs(ArgsManager& argsman)
462462
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);
463463
argsman.AddArg("-i2psam=<ip:port>", "I2P SAM proxy to reach I2P peers and accept I2P connections (default: none)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
464464
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_ANY, OptionsCategory::CONNECTION);
465-
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);
465+
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);
466466
argsman.AddArg("-peerbloomfilters", strprintf("Support filtering of blocks and transaction with bloom filters (default: %u)", DEFAULT_PEERBLOOMFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
467467
argsman.AddArg("-peerblockfilters", strprintf("Serve compact block filters to peers per BIP 157 (default: %u)", DEFAULT_PEERBLOCKFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
468468
argsman.AddArg("-permitbaremultisig", strprintf("Relay non-P2SH multisig (default: %u)", DEFAULT_PERMIT_BAREMULTISIG), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
@@ -1317,27 +1317,27 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13171317
// Check for host lookup allowed before parsing any network related parameters
13181318
fNameLookup = args.GetBoolArg("-dns", DEFAULT_NAME_LOOKUP);
13191319

1320+
Proxy onion_proxy;
1321+
13201322
bool proxyRandomize = args.GetBoolArg("-proxyrandomize", DEFAULT_PROXYRANDOMIZE);
13211323
// -proxy sets a proxy for all outgoing network traffic
13221324
// -noproxy (or -proxy=0) as well as the empty string can be used to not set a proxy, this is the default
13231325
std::string proxyArg = args.GetArg("-proxy", "");
1324-
SetReachable(NET_ONION, false);
13251326
if (proxyArg != "" && proxyArg != "0") {
13261327
CService proxyAddr;
13271328
if (!Lookup(proxyArg, proxyAddr, 9050, fNameLookup)) {
13281329
return InitError(strprintf(_("Invalid -proxy address or hostname: '%s'"), proxyArg));
13291330
}
13301331

1331-
proxyType addrProxy = proxyType(proxyAddr, proxyRandomize);
1332+
Proxy addrProxy = Proxy(proxyAddr, proxyRandomize);
13321333
if (!addrProxy.IsValid())
13331334
return InitError(strprintf(_("Invalid -proxy address or hostname: '%s'"), proxyArg));
13341335

13351336
SetProxy(NET_IPV4, addrProxy);
13361337
SetProxy(NET_IPV6, addrProxy);
1337-
SetProxy(NET_ONION, addrProxy);
13381338
SetProxy(NET_CJDNS, addrProxy);
13391339
SetNameProxy(addrProxy);
1340-
SetReachable(NET_ONION, true); // by default, -proxy sets onion as reachable, unless -noonion later
1340+
onion_proxy = addrProxy;
13411341
}
13421342

13431343
// -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
@@ -1346,18 +1346,26 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
13461346
std::string onionArg = args.GetArg("-onion", "");
13471347
if (onionArg != "") {
13481348
if (onionArg == "0") { // Handle -noonion/-onion=0
1349-
SetReachable(NET_ONION, false);
1349+
onion_proxy = Proxy{};
13501350
} else {
1351-
CService onionProxy;
1352-
if (!Lookup(onionArg, onionProxy, 9050, fNameLookup)) {
1351+
CService addr;
1352+
if (!Lookup(onionArg, addr, 9050, fNameLookup) || !addr.IsValid()) {
13531353
return InitError(strprintf(_("Invalid -onion address or hostname: '%s'"), onionArg));
13541354
}
1355-
proxyType addrOnion = proxyType(onionProxy, proxyRandomize);
1356-
if (!addrOnion.IsValid())
1357-
return InitError(strprintf(_("Invalid -onion address or hostname: '%s'"), onionArg));
1358-
SetProxy(NET_ONION, addrOnion);
1359-
SetReachable(NET_ONION, true);
1355+
onion_proxy = Proxy{addr, proxyRandomize};
1356+
}
1357+
}
1358+
1359+
if (onion_proxy.IsValid()) {
1360+
SetProxy(NET_ONION, onion_proxy);
1361+
} else {
1362+
if (args.IsArgSet("-onlynet") && IsReachable(NET_ONION)) {
1363+
return InitError(
1364+
_("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
1365+
"reaching the Tor network is not provided (no -proxy= and no -onion= given) or "
1366+
"it is explicitly forbidden (-onion=0)"));
13601367
}
1368+
SetReachable(NET_ONION, false);
13611369
}
13621370

13631371
for (const std::string& strAddr : args.GetArgs("-externalip")) {
@@ -1752,8 +1760,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17521760
if (!Lookup(i2psam_arg, addr, 7656, fNameLookup) || !addr.IsValid()) {
17531761
return InitError(strprintf(_("Invalid -i2psam address or hostname: '%s'"), i2psam_arg));
17541762
}
1755-
SetReachable(NET_I2P, true);
1756-
SetProxy(NET_I2P, proxyType{addr});
1763+
SetProxy(NET_I2P, Proxy{addr});
17571764
} else {
17581765
SetReachable(NET_I2P, false);
17591766
}

src/interfaces/node.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CNodeStats;
2727
class Coin;
2828
class RPCTimerInterface;
2929
class UniValue;
30-
class proxyType;
30+
class Proxy;
3131
enum class SynchronizationState;
3232
enum class TransactionError;
3333
struct CNodeStateStats;
@@ -101,7 +101,7 @@ class Node
101101
virtual void mapPort(bool use_upnp, bool use_natpmp) = 0;
102102

103103
//! Get proxy.
104-
virtual bool getProxy(Network net, proxyType& proxy_info) = 0;
104+
virtual bool getProxy(Network net, Proxy& proxy_info) = 0;
105105

106106
//! Get number of connections.
107107
virtual size_t getNodeCount(ConnectionDirection flags) = 0;

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
449449
// Connect
450450
bool connected = false;
451451
std::unique_ptr<Sock> sock;
452-
proxyType proxy;
452+
Proxy proxy;
453453
CAddress addr_bind;
454454
assert(!addr_bind.IsValid());
455455

@@ -2559,7 +2559,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
25592559
return false;
25602560
}
25612561

2562-
proxyType i2p_sam;
2562+
Proxy i2p_sam;
25632563
if (GetProxy(NET_I2P, i2p_sam)) {
25642564
m_i2p_sam_session = std::make_unique<i2p::sam::Session>(gArgs.GetDataDirNet() / "i2p_private_key",
25652565
i2p_sam.proxy, &interruptNet);

src/netbase.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131

3232
// Settings
3333
static Mutex g_proxyinfo_mutex;
34-
static proxyType proxyInfo[NET_MAX] GUARDED_BY(g_proxyinfo_mutex);
35-
static proxyType nameProxy GUARDED_BY(g_proxyinfo_mutex);
34+
static Proxy proxyInfo[NET_MAX] GUARDED_BY(g_proxyinfo_mutex);
35+
static Proxy nameProxy GUARDED_BY(g_proxyinfo_mutex);
3636
int nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
3737
bool fNameLookup = DEFAULT_NAME_LOOKUP;
3838

@@ -605,7 +605,7 @@ bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nT
605605
return true;
606606
}
607607

608-
bool SetProxy(enum Network net, const proxyType &addrProxy) {
608+
bool SetProxy(enum Network net, const Proxy &addrProxy) {
609609
assert(net >= 0 && net < NET_MAX);
610610
if (!addrProxy.IsValid())
611611
return false;
@@ -614,7 +614,7 @@ bool SetProxy(enum Network net, const proxyType &addrProxy) {
614614
return true;
615615
}
616616

617-
bool GetProxy(enum Network net, proxyType &proxyInfoOut) {
617+
bool GetProxy(enum Network net, Proxy &proxyInfoOut) {
618618
assert(net >= 0 && net < NET_MAX);
619619
LOCK(g_proxyinfo_mutex);
620620
if (!proxyInfo[net].IsValid())
@@ -623,15 +623,15 @@ bool GetProxy(enum Network net, proxyType &proxyInfoOut) {
623623
return true;
624624
}
625625

626-
bool SetNameProxy(const proxyType &addrProxy) {
626+
bool SetNameProxy(const Proxy &addrProxy) {
627627
if (!addrProxy.IsValid())
628628
return false;
629629
LOCK(g_proxyinfo_mutex);
630630
nameProxy = addrProxy;
631631
return true;
632632
}
633633

634-
bool GetNameProxy(proxyType &nameProxyOut) {
634+
bool GetNameProxy(Proxy &nameProxyOut) {
635635
LOCK(g_proxyinfo_mutex);
636636
if(!nameProxy.IsValid())
637637
return false;
@@ -653,7 +653,7 @@ bool IsProxy(const CNetAddr &addr) {
653653
return false;
654654
}
655655

656-
bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed)
656+
bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed)
657657
{
658658
// first connect to proxy server
659659
if (!ConnectSocketDirectly(proxy.proxy, sock, nTimeout, true)) {

src/netbase.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ static inline bool operator&(ConnectionDirection a, ConnectionDirection b) {
4545
return (underlying(a) & underlying(b));
4646
}
4747

48-
class proxyType
48+
class Proxy
4949
{
5050
public:
51-
proxyType(): randomize_credentials(false) {}
52-
explicit proxyType(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), randomize_credentials(_randomize_credentials) {}
51+
Proxy(): randomize_credentials(false) {}
52+
explicit Proxy(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), randomize_credentials(_randomize_credentials) {}
5353

5454
bool IsValid() const { return proxy.IsValid(); }
5555

@@ -73,8 +73,8 @@ enum Network ParseNetwork(const std::string& net);
7373
std::string GetNetworkName(enum Network net);
7474
/** Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE. */
7575
std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
76-
bool SetProxy(enum Network net, const proxyType &addrProxy);
77-
bool GetProxy(enum Network net, proxyType &proxyInfoOut);
76+
bool SetProxy(enum Network net, const Proxy &addrProxy);
77+
bool GetProxy(enum Network net, Proxy &proxyInfoOut);
7878
bool IsProxy(const CNetAddr &addr);
7979
/**
8080
* Set the name proxy to use for all connections to nodes specified by a
@@ -92,9 +92,9 @@ bool IsProxy(const CNetAddr &addr);
9292
* server in common use (most notably Tor) actually implements UDP
9393
* support, and a DNS resolver is beyond the scope of this project.
9494
*/
95-
bool SetNameProxy(const proxyType &addrProxy);
95+
bool SetNameProxy(const Proxy &addrProxy);
9696
bool HaveNameProxy();
97-
bool GetNameProxy(proxyType &nameProxyOut);
97+
bool GetNameProxy(Proxy &nameProxyOut);
9898

9999
using DNSLookupFn = std::function<std::vector<CNetAddr>(const std::string&, bool)>;
100100
extern DNSLookupFn g_dns_lookup;
@@ -219,7 +219,7 @@ bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nT
219219
*
220220
* @returns Whether or not the operation succeeded.
221221
*/
222-
bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed);
222+
bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed);
223223

224224
/** Disable or enable blocking-mode for a socket */
225225
bool SetSocketNonBlocking(const SOCKET& hSocket, bool fNonBlocking);

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class NodeImpl : public Node
113113
}
114114
bool shutdownRequested() override { return ShutdownRequested(); }
115115
void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); }
116-
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
116+
bool getProxy(Network net, Proxy& proxy_info) override { return GetProxy(net, proxy_info); }
117117
size_t getNodeCount(ConnectionDirection flags) override
118118
{
119119
return m_context->connman ? m_context->connman->GetNodeCount(flags) : 0;

src/qt/clientmodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ void ClientModel::unsubscribeFromCoreSignals()
329329

330330
bool ClientModel::getProxyInfo(std::string& ip_port) const
331331
{
332-
proxyType ipv4, ipv6;
332+
Proxy ipv4, ipv6;
333333
if (m_node.getProxy((Network) 1, ipv4) && m_node.getProxy((Network) 2, ipv6)) {
334334
ip_port = ipv4.proxy.ToStringIPPort();
335335
return true;

0 commit comments

Comments
 (0)