Skip to content

Commit 40400d5

Browse files
committed
Merge pull request #6272
edbdf88 tests: Extend RPC proxy tests (Wladimir J. van der Laan) baf0507 Improve proxy initialization (Wladimir J. van der Laan)
2 parents 0abfa8a + edbdf88 commit 40400d5

File tree

2 files changed

+71
-32
lines changed

2 files changed

+71
-32
lines changed

qa/rpc-tests/proxy_test.py

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ def setup_nodes(self):
6868
['-listen', '-debug=net', '-debug=proxy', '-proxy=%s:%i' % (self.conf1.addr),'-proxyrandomize=1'],
6969
['-listen', '-debug=net', '-debug=proxy', '-proxy=%s:%i' % (self.conf1.addr),'-onion=%s:%i' % (self.conf2.addr),'-proxyrandomize=0'],
7070
['-listen', '-debug=net', '-debug=proxy', '-proxy=%s:%i' % (self.conf2.addr),'-proxyrandomize=1'],
71-
['-listen', '-debug=net', '-debug=proxy', '-proxy=[%s]:%i' % (self.conf3.addr),'-proxyrandomize=0']
71+
['-listen', '-debug=net', '-debug=proxy', '-proxy=[%s]:%i' % (self.conf3.addr),'-proxyrandomize=0', '-noonion']
7272
])
7373

74-
def node_test(self, node, proxies, auth):
74+
def node_test(self, node, proxies, auth, test_onion=True):
7575
rv = []
7676
# Test: outgoing IPv4 connection through node
7777
node.addnode("15.61.23.23:1234", "onetry")
@@ -99,17 +99,18 @@ def node_test(self, node, proxies, auth):
9999
assert_equal(cmd.password, None)
100100
rv.append(cmd)
101101

102-
# Test: outgoing onion connection through node
103-
node.addnode("bitcoinostk4e4re.onion:8333", "onetry")
104-
cmd = proxies[2].queue.get()
105-
assert(isinstance(cmd, Socks5Command))
106-
assert_equal(cmd.atyp, AddressType.DOMAINNAME)
107-
assert_equal(cmd.addr, "bitcoinostk4e4re.onion")
108-
assert_equal(cmd.port, 8333)
109-
if not auth:
110-
assert_equal(cmd.username, None)
111-
assert_equal(cmd.password, None)
112-
rv.append(cmd)
102+
if test_onion:
103+
# Test: outgoing onion connection through node
104+
node.addnode("bitcoinostk4e4re.onion:8333", "onetry")
105+
cmd = proxies[2].queue.get()
106+
assert(isinstance(cmd, Socks5Command))
107+
assert_equal(cmd.atyp, AddressType.DOMAINNAME)
108+
assert_equal(cmd.addr, "bitcoinostk4e4re.onion")
109+
assert_equal(cmd.port, 8333)
110+
if not auth:
111+
assert_equal(cmd.username, None)
112+
assert_equal(cmd.password, None)
113+
rv.append(cmd)
113114

114115
# Test: outgoing DNS name connection through node
115116
node.addnode("node.noumenon:8333", "onetry")
@@ -139,8 +140,41 @@ def run_test(self):
139140
assert_equal(len(credentials), 4)
140141

141142
# proxy on IPv6 localhost
142-
self.node_test(self.nodes[3], [self.serv3, self.serv3, self.serv3, self.serv3], False)
143+
self.node_test(self.nodes[3], [self.serv3, self.serv3, self.serv3, self.serv3], False, False)
144+
145+
def networks_dict(d):
146+
r = {}
147+
for x in d['networks']:
148+
r[x['name']] = x
149+
return r
150+
151+
# test RPC getnetworkinfo
152+
n0 = networks_dict(self.nodes[0].getnetworkinfo())
153+
for net in ['ipv4','ipv6','onion']:
154+
assert_equal(n0[net]['proxy'], '%s:%i' % (self.conf1.addr))
155+
assert_equal(n0[net]['proxy_randomize_credentials'], True)
156+
assert_equal(n0['onion']['reachable'], True)
157+
158+
n1 = networks_dict(self.nodes[1].getnetworkinfo())
159+
for net in ['ipv4','ipv6']:
160+
assert_equal(n1[net]['proxy'], '%s:%i' % (self.conf1.addr))
161+
assert_equal(n1[net]['proxy_randomize_credentials'], False)
162+
assert_equal(n1['onion']['proxy'], '%s:%i' % (self.conf2.addr))
163+
assert_equal(n1['onion']['proxy_randomize_credentials'], False)
164+
assert_equal(n1['onion']['reachable'], True)
143165

166+
n2 = networks_dict(self.nodes[2].getnetworkinfo())
167+
for net in ['ipv4','ipv6','onion']:
168+
assert_equal(n2[net]['proxy'], '%s:%i' % (self.conf2.addr))
169+
assert_equal(n2[net]['proxy_randomize_credentials'], True)
170+
assert_equal(n2['onion']['reachable'], True)
171+
172+
n3 = networks_dict(self.nodes[3].getnetworkinfo())
173+
for net in ['ipv4','ipv6']:
174+
assert_equal(n3[net]['proxy'], '[%s]:%i' % (self.conf3.addr))
175+
assert_equal(n3[net]['proxy_randomize_credentials'], False)
176+
assert_equal(n3['onion']['reachable'], False)
177+
144178
if __name__ == '__main__':
145179
ProxyTest().main()
146180

src/init.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -987,31 +987,36 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
987987
}
988988
}
989989

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

997999
SetProxy(NET_IPV4, addrProxy);
9981000
SetProxy(NET_IPV6, addrProxy);
1001+
SetProxy(NET_TOR, addrProxy);
9991002
SetNameProxy(addrProxy);
1000-
fProxy = true;
1003+
SetReachable(NET_TOR); // by default, -proxy sets onion as reachable, unless -noonion later
10011004
}
10021005

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

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

0 commit comments

Comments
 (0)