Skip to content

Commit baf0507

Browse files
committed
Improve proxy initialization
Simplify and make the code in AppInit2 more clear. This provides a straightforward flow, gets rid of .count() (which makes it possible to override an earlier provided proxy option to nothing), as well as comments the different cases.
1 parent ebab5d3 commit baf0507

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/init.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -984,31 +984,36 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
984984
}
985985
}
986986

987-
proxyType addrProxy;
988-
bool fProxy = false;
989-
if (mapArgs.count("-proxy")) {
990-
addrProxy = proxyType(CService(mapArgs["-proxy"], 9050), GetBoolArg("-proxyrandomize", true));
987+
bool proxyRandomize = GetBoolArg("-proxyrandomize", true);
988+
// -proxy sets a proxy for all outgoing network traffic
989+
// -noproxy (or -proxy=0) as well as the empty string can be used to not set a proxy, this is the default
990+
std::string proxyArg = GetArg("-proxy", "");
991+
if (proxyArg != "" && proxyArg != "0") {
992+
proxyType addrProxy = proxyType(CService(proxyArg, 9050), proxyRandomize);
991993
if (!addrProxy.IsValid())
992-
return InitError(strprintf(_("Invalid -proxy address: '%s'"), mapArgs["-proxy"]));
994+
return InitError(strprintf(_("Invalid -proxy address: '%s'"), proxyArg));
993995

994996
SetProxy(NET_IPV4, addrProxy);
995997
SetProxy(NET_IPV6, addrProxy);
998+
SetProxy(NET_TOR, addrProxy);
996999
SetNameProxy(addrProxy);
997-
fProxy = true;
1000+
SetReachable(NET_TOR); // by default, -proxy sets onion as reachable, unless -noonion later
9981001
}
9991002

1000-
// -onion can override normal proxy, -noonion disables connecting to .onion entirely
1001-
if (!(mapArgs.count("-onion") && mapArgs["-onion"] == "0") &&
1002-
(fProxy || mapArgs.count("-onion"))) {
1003-
proxyType addrOnion;
1004-
if (!mapArgs.count("-onion"))
1005-
addrOnion = addrProxy;
1006-
else
1007-
addrOnion = proxyType(CService(mapArgs["-onion"], 9050), GetBoolArg("-proxyrandomize", true));
1008-
if (!addrOnion.IsValid())
1009-
return InitError(strprintf(_("Invalid -onion address: '%s'"), mapArgs["-onion"]));
1010-
SetProxy(NET_TOR, addrOnion);
1011-
SetReachable(NET_TOR);
1003+
// -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
1004+
// -noonion (or -onion=0) disables connecting to .onion entirely
1005+
// An empty string is used to not override the onion proxy (in which case it defaults to -proxy set above, or none)
1006+
std::string onionArg = GetArg("-onion", "");
1007+
if (onionArg != "") {
1008+
if (onionArg == "0") { // Handle -noonion/-onion=0
1009+
SetReachable(NET_TOR, false); // set onions as unreachable
1010+
} else {
1011+
proxyType addrOnion = proxyType(CService(onionArg, 9050), proxyRandomize);
1012+
if (!addrOnion.IsValid())
1013+
return InitError(strprintf(_("Invalid -onion address: '%s'"), onionArg));
1014+
SetProxy(NET_TOR, addrOnion);
1015+
SetReachable(NET_TOR);
1016+
}
10121017
}
10131018

10141019
// see Step 2: parameter interactions for more information about these

0 commit comments

Comments
 (0)