From d52724d0396e59b6f2e82ff03ebf1c81fba45420 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Mon, 30 Aug 2021 14:33:29 +0200 Subject: [PATCH] merge bitcoin#22834: respect -onlynet= when making outbound connections --- doc/i2p.md | 6 +----- doc/release-notes-22834.md | 8 +++++++ doc/tor.md | 6 +----- src/init.cpp | 37 +++++++++++++++++++------------- src/interfaces/node.h | 4 ++-- src/net.cpp | 4 ++-- src/netbase.cpp | 14 ++++++------ src/netbase.h | 16 +++++++------- src/node/interfaces.cpp | 2 +- src/qt/clientmodel.cpp | 2 +- src/qt/optionsdialog.cpp | 4 ++-- src/rpc/net.cpp | 2 +- src/torcontrol.cpp | 19 ++++++++++++++-- test/functional/feature_proxy.py | 2 +- 14 files changed, 74 insertions(+), 52 deletions(-) create mode 100644 doc/release-notes-22834.md diff --git a/doc/i2p.md b/doc/i2p.md index 9ce825ff5f..d779054a56 100644 --- a/doc/i2p.md +++ b/doc/i2p.md @@ -60,11 +60,7 @@ logging` for more information. Make outgoing connections only to I2P addresses. Incoming connections are not affected by this option. It can be specified multiple times to allow multiple -network types, e.g. onlynet=ipv4, onlynet=ipv6, onlynet=onion, onlynet=i2p. - -Warning: if you use -onlynet with values other than onion, and the -onion or --proxy option is set, then outgoing onion connections will still be made; use --noonion or -onion=0 to disable outbound onion connections in this case. +network types, e.g. onlynet=onion, onlynet=i2p. I2P support was added to Dash Core in version 20.0 and there may be fewer I2P peers than Tor or IP ones. Therefore, using I2P alone without other networks may diff --git a/doc/release-notes-22834.md b/doc/release-notes-22834.md new file mode 100644 index 0000000000..23fa5d6a80 --- /dev/null +++ b/doc/release-notes-22834.md @@ -0,0 +1,8 @@ +Updated settings +---------------- + +- If `-proxy=` is given together with `-noonion` then the provided proxy will + not be set as a proxy for reaching the Tor network. So it will not be + possible to open manual connections to the Tor network for example with the + `addnode` RPC. To mimic the old behavior use `-proxy=` together with + `-onlynet=` listing all relevant networks except `onion`. \ No newline at end of file diff --git a/doc/tor.md b/doc/tor.md index fdfb4c59eb..c044e0b278 100644 --- a/doc/tor.md +++ b/doc/tor.md @@ -51,11 +51,7 @@ outgoing connections, but more is possible. -onlynet=onion Make outgoing connections only to .onion addresses. Incoming connections are not affected by this option. This option can be specified multiple times to allow multiple network types, e.g. - onlynet=ipv4, onlynet=ipv6, onlynet=onion, onlynet=i2p. - Warning: if you use -onlynet with values other than onion, and - the -onion or -proxy option is set, then outgoing onion - connections will still be made; use -noonion or -onion=0 to - disable outbound onion connections in this case. + onlynet=onion, onlynet=i2p. An example how to start the client if the Tor proxy is running on local host on port 9050 and only allows .onion nodes to connect: diff --git a/src/init.cpp b/src/init.cpp index e0a12d117f..981dbb072f 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -584,7 +584,7 @@ void SetupServerArgs(NodeContext& node) argsman.AddArg("-onion=", "Use separate SOCKS5 proxy to reach peers via Tor onion services, set -noonion to disable (default: -proxy)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); argsman.AddArg("-i2psam=", "I2P SAM proxy to reach I2P peers and accept I2P connections (default: none)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); 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); - argsman.AddArg("-onlynet=", "Make outgoing connections only through network (" + 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); + argsman.AddArg("-onlynet=", "Make automatic outgoing connections only through network (" + 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); argsman.AddArg("-peerblockfilters", strprintf("Serve compact block filters to peers per BIP 157 (default: %u)", DEFAULT_PEERBLOCKFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); argsman.AddArg("-peerbloomfilters", strprintf("Support filtering of blocks and transaction with bloom filters (default: %u)", DEFAULT_PEERBLOOMFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION); argsman.AddArg("-peertimeout=", 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 // Check for host lookup allowed before parsing any network related parameters fNameLookup = args.GetBoolArg("-dns", DEFAULT_NAME_LOOKUP); + Proxy onion_proxy; + bool proxyRandomize = args.GetBoolArg("-proxyrandomize", DEFAULT_PROXYRANDOMIZE); // -proxy sets a proxy for all outgoing network traffic // -noproxy (or -proxy=0) as well as the empty string can be used to not set a proxy, this is the default std::string proxyArg = args.GetArg("-proxy", ""); - SetReachable(NET_ONION, false); if (proxyArg != "" && proxyArg != "0") { CService proxyAddr; if (!Lookup(proxyArg, proxyAddr, 9050, fNameLookup)) { return InitError(strprintf(_("Invalid -proxy address or hostname: '%s'"), proxyArg)); } - proxyType addrProxy = proxyType(proxyAddr, proxyRandomize); + Proxy addrProxy = Proxy(proxyAddr, proxyRandomize); if (!addrProxy.IsValid()) return InitError(strprintf(_("Invalid -proxy address or hostname: '%s'"), proxyArg)); SetProxy(NET_IPV4, addrProxy); SetProxy(NET_IPV6, addrProxy); - SetProxy(NET_ONION, addrProxy); SetProxy(NET_CJDNS, addrProxy); SetNameProxy(addrProxy); - SetReachable(NET_ONION, true); // by default, -proxy sets onion as reachable, unless -noonion later + onion_proxy = addrProxy; } // -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses @@ -1826,20 +1826,28 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc std::string onionArg = args.GetArg("-onion", ""); if (onionArg != "") { if (onionArg == "0") { // Handle -noonion/-onion=0 - SetReachable(NET_ONION, false); + onion_proxy = Proxy{}; } else { - CService onionProxy; - if (!Lookup(onionArg, onionProxy, 9050, fNameLookup)) { + CService addr; + if (!Lookup(onionArg, addr, 9050, fNameLookup) || !addr.IsValid()) { return InitError(strprintf(_("Invalid -onion address or hostname: '%s'"), onionArg)); } - proxyType addrOnion = proxyType(onionProxy, proxyRandomize); - if (!addrOnion.IsValid()) - return InitError(strprintf(_("Invalid -onion address or hostname: '%s'"), onionArg)); - SetProxy(NET_ONION, addrOnion); - SetReachable(NET_ONION, true); + onion_proxy = Proxy{addr, proxyRandomize}; } } + if (onion_proxy.IsValid()) { + SetProxy(NET_ONION, onion_proxy); + } else { + if (args.IsArgSet("-onlynet") && IsReachable(NET_ONION)) { + return InitError( + _("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for " + "reaching the Tor network is not provided (no -proxy= and no -onion= given) or " + "it is explicitly forbidden (-onion=0)")); + } + SetReachable(NET_ONION, false); + } + for (const std::string& strAddr : args.GetArgs("-externalip")) { CService addrLocal; if (Lookup(strAddr, addrLocal, GetListenPort(), fNameLookup) && addrLocal.IsValid()) @@ -2539,8 +2547,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc if (!Lookup(i2psam_arg, addr, 7656, fNameLookup) || !addr.IsValid()) { return InitError(strprintf(_("Invalid -i2psam address or hostname: '%s'"), i2psam_arg)); } - SetReachable(NET_I2P, true); - SetProxy(NET_I2P, proxyType{addr}); + SetProxy(NET_I2P, Proxy{addr}); } else { SetReachable(NET_I2P, false); } diff --git a/src/interfaces/node.h b/src/interfaces/node.h index 374e4d0a95..46b6f64317 100644 --- a/src/interfaces/node.h +++ b/src/interfaces/node.h @@ -32,7 +32,7 @@ class CNodeStats; class Coin; class RPCTimerInterface; class UniValue; -class proxyType; +class Proxy; struct bilingual_str; enum class SynchronizationState; struct CNodeStateStats; @@ -174,7 +174,7 @@ public: virtual void mapPort(bool use_upnp, bool use_natpmp) = 0; //! Get proxy. - virtual bool getProxy(Network net, proxyType& proxy_info) = 0; + virtual bool getProxy(Network net, Proxy& proxy_info) = 0; //! Get number of connections. virtual size_t getNodeCount(ConnectionDirection flags) = 0; diff --git a/src/net.cpp b/src/net.cpp index baeef8781c..c267e2dbfd 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -492,7 +492,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo // Connect bool connected = false; std::unique_ptr sock; - proxyType proxy; + Proxy proxy; CAddress addr_bind; assert(!addr_bind.IsValid()); std::unique_ptr i2p_transient_session; @@ -3397,7 +3397,7 @@ bool CConnman::Start(CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_met return false; } - proxyType i2p_sam; + Proxy i2p_sam; if (GetProxy(NET_I2P, i2p_sam) && connOptions.m_i2p_accept_incoming) { m_i2p_sam_session = std::make_unique(GetDataDir() / "i2p_private_key", i2p_sam.proxy, &interruptNet); diff --git a/src/netbase.cpp b/src/netbase.cpp index 74ab7b6318..544f575da0 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -30,8 +30,8 @@ // Settings static Mutex g_proxyinfo_mutex; -static proxyType proxyInfo[NET_MAX] GUARDED_BY(g_proxyinfo_mutex); -static proxyType nameProxy GUARDED_BY(g_proxyinfo_mutex); +static Proxy proxyInfo[NET_MAX] GUARDED_BY(g_proxyinfo_mutex); +static Proxy nameProxy GUARDED_BY(g_proxyinfo_mutex); int nConnectTimeout = DEFAULT_CONNECT_TIMEOUT; bool fNameLookup = DEFAULT_NAME_LOOKUP; @@ -604,7 +604,7 @@ bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nT return true; } -bool SetProxy(enum Network net, const proxyType &addrProxy) { +bool SetProxy(enum Network net, const Proxy &addrProxy) { assert(net >= 0 && net < NET_MAX); if (!addrProxy.IsValid()) return false; @@ -613,7 +613,7 @@ bool SetProxy(enum Network net, const proxyType &addrProxy) { return true; } -bool GetProxy(enum Network net, proxyType &proxyInfoOut) { +bool GetProxy(enum Network net, Proxy &proxyInfoOut) { assert(net >= 0 && net < NET_MAX); LOCK(g_proxyinfo_mutex); if (!proxyInfo[net].IsValid()) @@ -622,7 +622,7 @@ bool GetProxy(enum Network net, proxyType &proxyInfoOut) { return true; } -bool SetNameProxy(const proxyType &addrProxy) { +bool SetNameProxy(const Proxy &addrProxy) { if (!addrProxy.IsValid()) return false; LOCK(g_proxyinfo_mutex); @@ -630,7 +630,7 @@ bool SetNameProxy(const proxyType &addrProxy) { return true; } -bool GetNameProxy(proxyType &nameProxyOut) { +bool GetNameProxy(Proxy &nameProxyOut) { LOCK(g_proxyinfo_mutex); if(!nameProxy.IsValid()) return false; @@ -652,7 +652,7 @@ bool IsProxy(const CNetAddr &addr) { return false; } -bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed) +bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed) { // first connect to proxy server if (!ConnectSocketDirectly(proxy.proxy, sock, nTimeout, true)) { diff --git a/src/netbase.h b/src/netbase.h index f40f0a5b31..3d98f9cb73 100644 --- a/src/netbase.h +++ b/src/netbase.h @@ -49,11 +49,11 @@ static inline bool operator&(ConnectionDirection a, ConnectionDirection b) { return (underlying(a) & underlying(b)); } -class proxyType +class Proxy { public: - proxyType(): randomize_credentials(false) {} - explicit proxyType(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), randomize_credentials(_randomize_credentials) {} + Proxy(): randomize_credentials(false) {} + explicit Proxy(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), randomize_credentials(_randomize_credentials) {} bool IsValid() const { return proxy.IsValid(); } @@ -77,8 +77,8 @@ enum Network ParseNetwork(const std::string& net); std::string GetNetworkName(enum Network net); /** Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE. */ std::vector GetNetworkNames(bool append_unroutable = false); -bool SetProxy(enum Network net, const proxyType &addrProxy); -bool GetProxy(enum Network net, proxyType &proxyInfoOut); +bool SetProxy(enum Network net, const Proxy &addrProxy); +bool GetProxy(enum Network net, Proxy &proxyInfoOut); bool IsProxy(const CNetAddr &addr); /** * Set the name proxy to use for all connections to nodes specified by a @@ -96,9 +96,9 @@ bool IsProxy(const CNetAddr &addr); * server in common use (most notably Tor) actually implements UDP * support, and a DNS resolver is beyond the scope of this project. */ -bool SetNameProxy(const proxyType &addrProxy); +bool SetNameProxy(const Proxy &addrProxy); bool HaveNameProxy(); -bool GetNameProxy(proxyType &nameProxyOut); +bool GetNameProxy(Proxy &nameProxyOut); using DNSLookupFn = std::function(const std::string&, bool)>; extern DNSLookupFn g_dns_lookup; @@ -223,7 +223,7 @@ bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nT * * @returns Whether or not the operation succeeded. */ -bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed); +bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed); /** Enable non-blocking mode for a socket */ bool SetSocketNonBlocking(const SOCKET& hSocket); diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index b4be01cde2..c3f725291b 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -341,7 +341,7 @@ public: } bool shutdownRequested() override { return ShutdownRequested(); } void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); } - bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); } + bool getProxy(Network net, Proxy& proxy_info) override { return GetProxy(net, proxy_info); } size_t getNodeCount(ConnectionDirection flags) override { return m_context->connman ? m_context->connman->GetNodeCount(flags) : 0; diff --git a/src/qt/clientmodel.cpp b/src/qt/clientmodel.cpp index 9bca270f46..a20453b360 100644 --- a/src/qt/clientmodel.cpp +++ b/src/qt/clientmodel.cpp @@ -381,7 +381,7 @@ void ClientModel::unsubscribeFromCoreSignals() bool ClientModel::getProxyInfo(std::string& ip_port) const { - proxyType ipv4, ipv6; + Proxy ipv4, ipv6; if (m_node.getProxy((Network) 1, ipv4) && m_node.getProxy((Network) 2, ipv6)) { ip_port = ipv4.proxy.ToStringIPPort(); return true; diff --git a/src/qt/optionsdialog.cpp b/src/qt/optionsdialog.cpp index d5dec22d6b..1431342697 100644 --- a/src/qt/optionsdialog.cpp +++ b/src/qt/optionsdialog.cpp @@ -485,7 +485,7 @@ void OptionsDialog::updateProxyValidationState() void OptionsDialog::updateDefaultProxyNets() { - proxyType proxy; + Proxy proxy; std::string strProxy; QString strDefaultProxyGUI; @@ -567,7 +567,7 @@ QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) cons Q_UNUSED(pos); // Validate the proxy CService serv(LookupNumeric(input.toStdString(), DEFAULT_GUI_PROXY_PORT)); - proxyType addrProxy = proxyType(serv, true); + Proxy addrProxy = Proxy(serv, true); if (addrProxy.IsValid()) return QValidator::Acceptable; diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index a1a1d40035..1143612a04 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -586,7 +586,7 @@ static UniValue GetNetworksInfo() for (int n = 0; n < NET_MAX; ++n) { enum Network network = static_cast(n); if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue; - proxyType proxy; + Proxy proxy; UniValue obj(UniValue::VOBJ); GetProxy(network, proxy); obj.pushKV("name", GetNetworkName(network)); diff --git a/src/torcontrol.cpp b/src/torcontrol.cpp index 0f98a2f2ed..4463e1d728 100644 --- a/src/torcontrol.cpp +++ b/src/torcontrol.cpp @@ -374,9 +374,24 @@ void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply& // if -onion isn't set to something else. if (gArgs.GetArg("-onion", "") == "") { CService resolved(LookupNumeric("127.0.0.1", 9050)); - proxyType addrOnion = proxyType(resolved, true); + Proxy addrOnion = Proxy(resolved, true); SetProxy(NET_ONION, addrOnion); - SetReachable(NET_ONION, true); + + const auto onlynets = gArgs.GetArgs("-onlynet"); + + const bool onion_allowed_by_onlynet{ + !gArgs.IsArgSet("-onlynet") || + std::any_of(onlynets.begin(), onlynets.end(), [](const auto& n) { + return ParseNetwork(n) == NET_ONION; + })}; + + if (onion_allowed_by_onlynet) { + // If NET_ONION is reachable, then the below is a noop. + // + // If NET_ONION is not reachable, then none of -proxy or -onion was given. + // Since we are here, then -torcontrol and -torpassword were given. + SetReachable(NET_ONION, true); + } } // Finally - now create the service diff --git a/test/functional/feature_proxy.py b/test/functional/feature_proxy.py index 5276b8fd36..d5f3777cad 100755 --- a/test/functional/feature_proxy.py +++ b/test/functional/feature_proxy.py @@ -280,7 +280,7 @@ class ProxyTest(BitcoinTestFramework): n3 = networks_dict(self.nodes[3].getnetworkinfo()) assert_equal(NETWORKS, n3.keys()) for net in NETWORKS: - if net == NET_I2P: + if net == NET_I2P or net == NET_ONION: expected_proxy = '' else: expected_proxy = f'[{self.conf3.addr[0]}]:{self.conf3.addr[1]}'