Skip to content

Commit d52724d

Browse files
committed
merge bitcoin#22834: respect -onlynet= when making outbound connections
1 parent f9d1a9a commit d52724d

File tree

14 files changed

+74
-52
lines changed

14 files changed

+74
-52
lines changed

doc/i2p.md

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

6161
Make outgoing connections only to I2P addresses. Incoming connections are not
6262
affected by this option. It can be specified multiple times to allow multiple
63-
network types, e.g. onlynet=ipv4, onlynet=ipv6, onlynet=onion, onlynet=i2p.
64-
65-
Warning: if you use -onlynet with values other than onion, and the -onion or
66-
-proxy option is set, then outgoing onion connections will still be made; use
67-
-noonion or -onion=0 to disable outbound onion connections in this case.
63+
network types, e.g. onlynet=onion, onlynet=i2p.
6864

6965
I2P support was added to Dash Core in version 20.0 and there may be fewer I2P
7066
peers than Tor or IP ones. Therefore, using I2P alone without other networks may

doc/release-notes-22834.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Updated settings
2+
----------------
3+
4+
- If `-proxy=` is given together with `-noonion` then the provided proxy will
5+
not be set as a proxy for reaching the Tor network. So it will not be
6+
possible to open manual connections to the Tor network for example with the
7+
`addnode` RPC. To mimic the old behavior use `-proxy=` together with
8+
`-onlynet=` listing all relevant networks except `onion`.

doc/tor.md

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

6056
An example how to start the client if the Tor proxy is running on local host on
6157
port 9050 and only allows .onion nodes to connect:

src/init.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ void SetupServerArgs(NodeContext& node)
584584
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);
585585
argsman.AddArg("-i2psam=<ip:port>", "I2P SAM proxy to reach I2P peers and accept I2P connections (default: none)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
586586
argsman.AddArg("-i2pacceptincoming", strprintf("Whether to accept inbound I2P connections (default: %i). Ignored if -i2psam is not set. Listening for inbound I2P connections is done through the SAM proxy, not by binding to a local address and port.", DEFAULT_I2P_ACCEPT_INCOMING), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
587-
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);
587+
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);
588588
argsman.AddArg("-peerblockfilters", strprintf("Serve compact block filters to peers per BIP 157 (default: %u)", DEFAULT_PEERBLOCKFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
589589
argsman.AddArg("-peerbloomfilters", strprintf("Support filtering of blocks and transaction with bloom filters (default: %u)", DEFAULT_PEERBLOOMFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
590590
argsman.AddArg("-peertimeout=<n>", strprintf("Specify a p2p connection timeout delay in seconds. After connecting to a peer, wait this amount of time before considering disconnection based on inactivity (minimum: 1, default: %d)", DEFAULT_PEER_CONNECT_TIMEOUT), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
@@ -1797,27 +1797,27 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
17971797
// Check for host lookup allowed before parsing any network related parameters
17981798
fNameLookup = args.GetBoolArg("-dns", DEFAULT_NAME_LOOKUP);
17991799

1800+
Proxy onion_proxy;
1801+
18001802
bool proxyRandomize = args.GetBoolArg("-proxyrandomize", DEFAULT_PROXYRANDOMIZE);
18011803
// -proxy sets a proxy for all outgoing network traffic
18021804
// -noproxy (or -proxy=0) as well as the empty string can be used to not set a proxy, this is the default
18031805
std::string proxyArg = args.GetArg("-proxy", "");
1804-
SetReachable(NET_ONION, false);
18051806
if (proxyArg != "" && proxyArg != "0") {
18061807
CService proxyAddr;
18071808
if (!Lookup(proxyArg, proxyAddr, 9050, fNameLookup)) {
18081809
return InitError(strprintf(_("Invalid -proxy address or hostname: '%s'"), proxyArg));
18091810
}
18101811

1811-
proxyType addrProxy = proxyType(proxyAddr, proxyRandomize);
1812+
Proxy addrProxy = Proxy(proxyAddr, proxyRandomize);
18121813
if (!addrProxy.IsValid())
18131814
return InitError(strprintf(_("Invalid -proxy address or hostname: '%s'"), proxyArg));
18141815

18151816
SetProxy(NET_IPV4, addrProxy);
18161817
SetProxy(NET_IPV6, addrProxy);
1817-
SetProxy(NET_ONION, addrProxy);
18181818
SetProxy(NET_CJDNS, addrProxy);
18191819
SetNameProxy(addrProxy);
1820-
SetReachable(NET_ONION, true); // by default, -proxy sets onion as reachable, unless -noonion later
1820+
onion_proxy = addrProxy;
18211821
}
18221822

18231823
// -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
@@ -1826,18 +1826,26 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
18261826
std::string onionArg = args.GetArg("-onion", "");
18271827
if (onionArg != "") {
18281828
if (onionArg == "0") { // Handle -noonion/-onion=0
1829-
SetReachable(NET_ONION, false);
1829+
onion_proxy = Proxy{};
18301830
} else {
1831-
CService onionProxy;
1832-
if (!Lookup(onionArg, onionProxy, 9050, fNameLookup)) {
1831+
CService addr;
1832+
if (!Lookup(onionArg, addr, 9050, fNameLookup) || !addr.IsValid()) {
18331833
return InitError(strprintf(_("Invalid -onion address or hostname: '%s'"), onionArg));
18341834
}
1835-
proxyType addrOnion = proxyType(onionProxy, proxyRandomize);
1836-
if (!addrOnion.IsValid())
1837-
return InitError(strprintf(_("Invalid -onion address or hostname: '%s'"), onionArg));
1838-
SetProxy(NET_ONION, addrOnion);
1839-
SetReachable(NET_ONION, true);
1835+
onion_proxy = Proxy{addr, proxyRandomize};
1836+
}
1837+
}
1838+
1839+
if (onion_proxy.IsValid()) {
1840+
SetProxy(NET_ONION, onion_proxy);
1841+
} else {
1842+
if (args.IsArgSet("-onlynet") && IsReachable(NET_ONION)) {
1843+
return InitError(
1844+
_("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
1845+
"reaching the Tor network is not provided (no -proxy= and no -onion= given) or "
1846+
"it is explicitly forbidden (-onion=0)"));
18401847
}
1848+
SetReachable(NET_ONION, false);
18411849
}
18421850

18431851
for (const std::string& strAddr : args.GetArgs("-externalip")) {
@@ -2539,8 +2547,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
25392547
if (!Lookup(i2psam_arg, addr, 7656, fNameLookup) || !addr.IsValid()) {
25402548
return InitError(strprintf(_("Invalid -i2psam address or hostname: '%s'"), i2psam_arg));
25412549
}
2542-
SetReachable(NET_I2P, true);
2543-
SetProxy(NET_I2P, proxyType{addr});
2550+
SetProxy(NET_I2P, Proxy{addr});
25442551
} else {
25452552
SetReachable(NET_I2P, false);
25462553
}

src/interfaces/node.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CNodeStats;
3232
class Coin;
3333
class RPCTimerInterface;
3434
class UniValue;
35-
class proxyType;
35+
class Proxy;
3636
struct bilingual_str;
3737
enum class SynchronizationState;
3838
struct CNodeStateStats;
@@ -174,7 +174,7 @@ class Node
174174
virtual void mapPort(bool use_upnp, bool use_natpmp) = 0;
175175

176176
//! Get proxy.
177-
virtual bool getProxy(Network net, proxyType& proxy_info) = 0;
177+
virtual bool getProxy(Network net, Proxy& proxy_info) = 0;
178178

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

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
492492
// Connect
493493
bool connected = false;
494494
std::unique_ptr<Sock> sock;
495-
proxyType proxy;
495+
Proxy proxy;
496496
CAddress addr_bind;
497497
assert(!addr_bind.IsValid());
498498
std::unique_ptr<i2p::sam::Session> i2p_transient_session;
@@ -3397,7 +3397,7 @@ bool CConnman::Start(CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_met
33973397
return false;
33983398
}
33993399

3400-
proxyType i2p_sam;
3400+
Proxy i2p_sam;
34013401
if (GetProxy(NET_I2P, i2p_sam) && connOptions.m_i2p_accept_incoming) {
34023402
m_i2p_sam_session = std::make_unique<i2p::sam::Session>(GetDataDir() / "i2p_private_key",
34033403
i2p_sam.proxy, &interruptNet);

src/netbase.cpp

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

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

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

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

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

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

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

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

src/netbase.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ static inline bool operator&(ConnectionDirection a, ConnectionDirection b) {
4949
return (underlying(a) & underlying(b));
5050
}
5151

52-
class proxyType
52+
class Proxy
5353
{
5454
public:
55-
proxyType(): randomize_credentials(false) {}
56-
explicit proxyType(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), randomize_credentials(_randomize_credentials) {}
55+
Proxy(): randomize_credentials(false) {}
56+
explicit Proxy(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), randomize_credentials(_randomize_credentials) {}
5757

5858
bool IsValid() const { return proxy.IsValid(); }
5959

@@ -77,8 +77,8 @@ enum Network ParseNetwork(const std::string& net);
7777
std::string GetNetworkName(enum Network net);
7878
/** Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE. */
7979
std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
80-
bool SetProxy(enum Network net, const proxyType &addrProxy);
81-
bool GetProxy(enum Network net, proxyType &proxyInfoOut);
80+
bool SetProxy(enum Network net, const Proxy &addrProxy);
81+
bool GetProxy(enum Network net, Proxy &proxyInfoOut);
8282
bool IsProxy(const CNetAddr &addr);
8383
/**
8484
* Set the name proxy to use for all connections to nodes specified by a
@@ -96,9 +96,9 @@ bool IsProxy(const CNetAddr &addr);
9696
* server in common use (most notably Tor) actually implements UDP
9797
* support, and a DNS resolver is beyond the scope of this project.
9898
*/
99-
bool SetNameProxy(const proxyType &addrProxy);
99+
bool SetNameProxy(const Proxy &addrProxy);
100100
bool HaveNameProxy();
101-
bool GetNameProxy(proxyType &nameProxyOut);
101+
bool GetNameProxy(Proxy &nameProxyOut);
102102

103103
using DNSLookupFn = std::function<std::vector<CNetAddr>(const std::string&, bool)>;
104104
extern DNSLookupFn g_dns_lookup;
@@ -223,7 +223,7 @@ bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nT
223223
*
224224
* @returns Whether or not the operation succeeded.
225225
*/
226-
bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed);
226+
bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed);
227227

228228
/** Enable non-blocking mode for a socket */
229229
bool SetSocketNonBlocking(const SOCKET& hSocket);

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ class NodeImpl : public Node
341341
}
342342
bool shutdownRequested() override { return ShutdownRequested(); }
343343
void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); }
344-
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
344+
bool getProxy(Network net, Proxy& proxy_info) override { return GetProxy(net, proxy_info); }
345345
size_t getNodeCount(ConnectionDirection flags) override
346346
{
347347
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
@@ -381,7 +381,7 @@ void ClientModel::unsubscribeFromCoreSignals()
381381

382382
bool ClientModel::getProxyInfo(std::string& ip_port) const
383383
{
384-
proxyType ipv4, ipv6;
384+
Proxy ipv4, ipv6;
385385
if (m_node.getProxy((Network) 1, ipv4) && m_node.getProxy((Network) 2, ipv6)) {
386386
ip_port = ipv4.proxy.ToStringIPPort();
387387
return true;

0 commit comments

Comments
 (0)