Repository: bitcoin/bitcoin
File: src/init.cpp (C++)

Code before:
        if (onionArg == "0") { // Handle -noonion/-onion=0
            onion_proxy = Proxy{};
            if (onlynet_used_with_onion) {
                return InitError(
                    _("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
                      "reaching the Tor network is explicitly forbidden: -onion=0"));
            }
        } else {
            if (IsUnixSocketPath(onionArg)) {
                onion_proxy = Proxy(onionArg, /*tor_stream_isolation=*/proxyRandomize);
            } else {
                const std::optional<CService> addr{Lookup(onionArg, 9050, fNameLookup)};
                if (!addr.has_value() || !addr->IsValid()) {
                    return InitError(strprintf(_("Invalid -onion address or hostname: '%s'"), onionArg));
                }

                onion_proxy = Proxy(addr.value(), /*tor_stream_isolation=*/proxyRandomize);
            }
        }
    }

    if (onion_proxy.IsValid()) {
        SetProxy(NET_ONION, onion_proxy);
    } else {
        // If -listenonion is set, then we will (try to) connect to the Tor control port
        // later from the torcontrol thread and may retrieve the onion proxy from there.
        const bool listenonion_disabled{!args.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION)};
        if (onlynet_used_with_onion && listenonion_disabled) {
            return InitError(
                _("Outbound connections restricted to Tor (-onlynet=onion) but the proxy for "
                  "reaching the Tor network is not provided: none of -proxy, -onion or "
                  "-listenonion is given"));
        }
        g_reachable_nets.Remove(NET_ONION);
    }

    for (const std::string& strAddr : args.GetArgs("-externalip")) {
        const std::optional<CService> addrLocal{Lookup(strAddr, GetListenPort(), fNameLookup)};
        if (addrLocal.has_value() && addrLocal->IsValid())
            AddLocal(addrLocal.value(), LOCAL_MANUAL);
        else
            return InitError(ResolveErrMsg("externalip", strAddr));
    }

#ifdef ENABLE_ZMQ
    g_zmq_notification_interface = CZMQNotificationInterface::Create(
        [&chainman = node.chainman](std::vector<uint8_t>& block, const CBlockIndex& index) {
            assert(chainman);
            return chainman->m_blockman.ReadRawBlock(block, WITH_LOCK(cs_main, return index.GetBlockPos()));
        });


Review comment (suggestion):
I'm not sure this logic is ideal. If you set an all-network proxy, but then disable it for IPv6, you might be expecting name resolution to use the proxy. Or maybe not.

Code after:
            }
            net_str = ToLower(param_value.substr(eq_pos + 1)); // e.g. 127.0.0.1:9050=ipv4 -> ipv4
        }

        Proxy proxy;
        if (!proxy_str.empty() && proxy_str != "0") {
            if (IsUnixSocketPath(proxy_str)) {
                proxy = Proxy{proxy_str, /*tor_stream_isolation=*/proxyRandomize};
            } else {
                const std::optional<CService> addr{Lookup(proxy_str, DEFAULT_TOR_SOCKS_PORT, fNameLookup)};
                if (!addr.has_value()) {
                    return InitError(strprintf(_("Invalid -proxy address or hostname: '%s'"), proxy_str));
                }
                proxy = Proxy{addr.value(), /*tor_stream_isolation=*/proxyRandomize};
            }
            if (!proxy.IsValid()) {
                return InitError(strprintf(_("Invalid -proxy address or hostname: '%s'"), proxy_str));
            }
        }

        if (net_str.empty()) { // For all networks.
            ipv4_proxy = ipv6_proxy = name_proxy = cjdns_proxy = onion_proxy = proxy;
        } else if (net_str == "ipv4") {
            ipv4_proxy = name_proxy = proxy;
        } else if (net_str == "ipv6") {
            ipv6_proxy = name_proxy = proxy;
        } else if (net_str == "tor" || net_str == "onion") {
            onion_proxy = proxy;
        } else if (net_str == "cjdns") {
            cjdns_proxy = proxy;
        } else {
            return InitError(strprintf(_("Unrecognized network in -proxy='%s': '%s'"), param_value, net_str));
        }
    }
    if (ipv4_proxy.IsValid()) {
        SetProxy(NET_IPV4, ipv4_proxy);
    }
    if (ipv6_proxy.IsValid()) {
        SetProxy(NET_IPV6, ipv6_proxy);
    }
    if (name_proxy.IsValid()) {
        SetNameProxy(name_proxy);
    }
    if (cjdns_proxy.IsValid()) {
        SetProxy(NET_CJDNS, cjdns_proxy);
    }

    const bool onlynet_used_with_onion{!onlynets.empty() && g_reachable_nets.Contains(NET_ONION)};

    // -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
    // -noonion (or -onion=0) disables connecting to .onion entirely