2016-03-19 20:58:06 +01:00
|
|
|
#!/usr/bin/env python3
|
2019-02-21 02:03:13 +01:00
|
|
|
# Copyright (c) 2015-2019 The Bitcoin Core developers
|
2015-04-17 19:26:03 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2019-08-30 17:28:43 +02:00
|
|
|
"""Test lbrycrdd with different proxy configuration.
|
2016-03-19 20:58:06 +01:00
|
|
|
|
2015-04-17 19:26:03 +02:00
|
|
|
Test plan:
|
2019-08-30 17:28:43 +02:00
|
|
|
- Start lbrycrdd's with different proxy configurations
|
2015-04-17 19:26:03 +02:00
|
|
|
- Use addnode to initiate connections
|
|
|
|
- Verify that proxies are connected to, and the right connection command is given
|
2019-08-30 17:28:43 +02:00
|
|
|
- Proxy configurations to test on lbrycrdd side:
|
2015-04-17 19:26:03 +02:00
|
|
|
- `-proxy` (proxy everything)
|
|
|
|
- `-onion` (proxy just onions)
|
|
|
|
- `-proxyrandomize` Circuit randomization
|
|
|
|
- Proxy configurations to test on proxy side,
|
|
|
|
- support no authentication (other proxy)
|
|
|
|
- support no authentication + user/pass authentication (Tor)
|
|
|
|
- proxy on IPv6
|
|
|
|
|
|
|
|
- Create various proxies (as threads)
|
2019-08-30 17:28:43 +02:00
|
|
|
- Create lbrycrdds that connect to them
|
|
|
|
- Manipulate the lbrycrdds using addnode (onetry) an observe effects
|
2015-04-17 19:26:03 +02:00
|
|
|
|
|
|
|
addnode connect to IPv4
|
|
|
|
addnode connect to IPv6
|
|
|
|
addnode connect to onion
|
|
|
|
addnode connect to generic DNS name
|
2017-01-18 00:34:40 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
import socket
|
|
|
|
import os
|
|
|
|
|
|
|
|
from test_framework.socks5 import Socks5Configuration, Socks5Command, Socks5Server, AddressType
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import (
|
|
|
|
PORT_MIN,
|
|
|
|
PORT_RANGE,
|
|
|
|
assert_equal,
|
|
|
|
)
|
|
|
|
from test_framework.netutil import test_ipv6_local
|
2015-04-17 19:26:03 +02:00
|
|
|
|
2016-11-13 22:34:09 +01:00
|
|
|
RANGE_BEGIN = PORT_MIN + 2 * PORT_RANGE # Start after p2p and rpc ports
|
|
|
|
|
2015-12-02 18:12:23 +01:00
|
|
|
class ProxyTest(BitcoinTestFramework):
|
2017-08-24 17:11:56 +02:00
|
|
|
def set_test_params(self):
|
|
|
|
self.num_nodes = 4
|
2019-02-11 19:56:19 +01:00
|
|
|
self.setup_clean_chain = True
|
2017-08-24 17:11:56 +02:00
|
|
|
|
2017-03-17 23:36:39 +01:00
|
|
|
def setup_nodes(self):
|
2016-02-09 12:37:05 +01:00
|
|
|
self.have_ipv6 = test_ipv6_local()
|
2015-04-17 19:26:03 +02:00
|
|
|
# Create two proxies on different ports
|
|
|
|
# ... one unauthenticated
|
|
|
|
self.conf1 = Socks5Configuration()
|
2016-11-13 22:34:09 +01:00
|
|
|
self.conf1.addr = ('127.0.0.1', RANGE_BEGIN + (os.getpid() % 1000))
|
2015-04-17 19:26:03 +02:00
|
|
|
self.conf1.unauth = True
|
|
|
|
self.conf1.auth = False
|
|
|
|
# ... one supporting authenticated and unauthenticated (Tor)
|
|
|
|
self.conf2 = Socks5Configuration()
|
2016-11-13 22:34:09 +01:00
|
|
|
self.conf2.addr = ('127.0.0.1', RANGE_BEGIN + 1000 + (os.getpid() % 1000))
|
2015-04-17 19:26:03 +02:00
|
|
|
self.conf2.unauth = True
|
|
|
|
self.conf2.auth = True
|
2016-02-09 12:37:05 +01:00
|
|
|
if self.have_ipv6:
|
|
|
|
# ... one on IPv6 with similar configuration
|
|
|
|
self.conf3 = Socks5Configuration()
|
|
|
|
self.conf3.af = socket.AF_INET6
|
2016-11-13 22:34:09 +01:00
|
|
|
self.conf3.addr = ('::1', RANGE_BEGIN + 2000 + (os.getpid() % 1000))
|
2016-02-09 12:37:05 +01:00
|
|
|
self.conf3.unauth = True
|
|
|
|
self.conf3.auth = True
|
|
|
|
else:
|
2017-03-17 23:36:39 +01:00
|
|
|
self.log.warning("Testing without local IPv6 support")
|
2015-04-17 19:26:03 +02:00
|
|
|
|
|
|
|
self.serv1 = Socks5Server(self.conf1)
|
|
|
|
self.serv1.start()
|
|
|
|
self.serv2 = Socks5Server(self.conf2)
|
|
|
|
self.serv2.start()
|
2016-02-09 12:37:05 +01:00
|
|
|
if self.have_ipv6:
|
|
|
|
self.serv3 = Socks5Server(self.conf3)
|
|
|
|
self.serv3.start()
|
2015-04-17 19:26:03 +02:00
|
|
|
|
|
|
|
# Note: proxies are not used to connect to local nodes
|
|
|
|
# this is because the proxy to use is based on CService.GetNetwork(), which return NET_UNROUTABLE for localhost
|
2016-02-09 12:37:05 +01:00
|
|
|
args = [
|
2018-05-10 17:28:27 +02:00
|
|
|
['-listen', '-proxy=%s:%i' % (self.conf1.addr),'-proxyrandomize=1'],
|
|
|
|
['-listen', '-proxy=%s:%i' % (self.conf1.addr),'-onion=%s:%i' % (self.conf2.addr),'-proxyrandomize=0'],
|
|
|
|
['-listen', '-proxy=%s:%i' % (self.conf2.addr),'-proxyrandomize=1'],
|
2016-02-09 12:37:05 +01:00
|
|
|
[]
|
|
|
|
]
|
|
|
|
if self.have_ipv6:
|
2017-02-24 18:39:33 +01:00
|
|
|
args[3] = ['-listen', '-proxy=[%s]:%i' % (self.conf3.addr),'-proxyrandomize=0', '-noonion']
|
2017-08-24 17:37:25 +02:00
|
|
|
self.add_nodes(self.num_nodes, extra_args=args)
|
2017-06-09 22:35:17 +02:00
|
|
|
self.start_nodes()
|
2015-04-17 19:26:03 +02:00
|
|
|
|
2015-06-15 12:44:56 +02:00
|
|
|
def node_test(self, node, proxies, auth, test_onion=True):
|
2015-04-17 19:26:03 +02:00
|
|
|
rv = []
|
|
|
|
# Test: outgoing IPv4 connection through node
|
|
|
|
node.addnode("15.61.23.23:1234", "onetry")
|
|
|
|
cmd = proxies[0].queue.get()
|
2019-02-19 23:43:44 +01:00
|
|
|
assert isinstance(cmd, Socks5Command)
|
2019-08-30 17:28:43 +02:00
|
|
|
# Note: lbrycrdd's SOCKS5 implementation only sends atyp DOMAINNAME, even if connecting directly to IPv4/IPv6
|
2015-04-17 19:26:03 +02:00
|
|
|
assert_equal(cmd.atyp, AddressType.DOMAINNAME)
|
2016-04-10 16:54:28 +02:00
|
|
|
assert_equal(cmd.addr, b"15.61.23.23")
|
2015-04-17 19:26:03 +02:00
|
|
|
assert_equal(cmd.port, 1234)
|
|
|
|
if not auth:
|
|
|
|
assert_equal(cmd.username, None)
|
|
|
|
assert_equal(cmd.password, None)
|
|
|
|
rv.append(cmd)
|
|
|
|
|
2016-02-09 12:37:05 +01:00
|
|
|
if self.have_ipv6:
|
|
|
|
# Test: outgoing IPv6 connection through node
|
|
|
|
node.addnode("[1233:3432:2434:2343:3234:2345:6546:4534]:5443", "onetry")
|
|
|
|
cmd = proxies[1].queue.get()
|
2019-02-19 23:43:44 +01:00
|
|
|
assert isinstance(cmd, Socks5Command)
|
2019-08-30 17:28:43 +02:00
|
|
|
# Note: lbrycrdd's SOCKS5 implementation only sends atyp DOMAINNAME, even if connecting directly to IPv4/IPv6
|
2016-02-09 12:37:05 +01:00
|
|
|
assert_equal(cmd.atyp, AddressType.DOMAINNAME)
|
2016-04-10 16:54:28 +02:00
|
|
|
assert_equal(cmd.addr, b"1233:3432:2434:2343:3234:2345:6546:4534")
|
2016-02-09 12:37:05 +01:00
|
|
|
assert_equal(cmd.port, 5443)
|
|
|
|
if not auth:
|
|
|
|
assert_equal(cmd.username, None)
|
|
|
|
assert_equal(cmd.password, None)
|
|
|
|
rv.append(cmd)
|
2015-04-17 19:26:03 +02:00
|
|
|
|
2015-06-15 12:44:56 +02:00
|
|
|
if test_onion:
|
|
|
|
# Test: outgoing onion connection through node
|
|
|
|
node.addnode("bitcoinostk4e4re.onion:8333", "onetry")
|
|
|
|
cmd = proxies[2].queue.get()
|
2019-02-19 23:43:44 +01:00
|
|
|
assert isinstance(cmd, Socks5Command)
|
2015-06-15 12:44:56 +02:00
|
|
|
assert_equal(cmd.atyp, AddressType.DOMAINNAME)
|
2016-04-10 16:54:28 +02:00
|
|
|
assert_equal(cmd.addr, b"bitcoinostk4e4re.onion")
|
2015-06-15 12:44:56 +02:00
|
|
|
assert_equal(cmd.port, 8333)
|
|
|
|
if not auth:
|
|
|
|
assert_equal(cmd.username, None)
|
|
|
|
assert_equal(cmd.password, None)
|
|
|
|
rv.append(cmd)
|
2015-04-17 19:26:03 +02:00
|
|
|
|
|
|
|
# Test: outgoing DNS name connection through node
|
|
|
|
node.addnode("node.noumenon:8333", "onetry")
|
|
|
|
cmd = proxies[3].queue.get()
|
2019-02-19 23:43:44 +01:00
|
|
|
assert isinstance(cmd, Socks5Command)
|
2015-04-17 19:26:03 +02:00
|
|
|
assert_equal(cmd.atyp, AddressType.DOMAINNAME)
|
2016-04-10 16:54:28 +02:00
|
|
|
assert_equal(cmd.addr, b"node.noumenon")
|
2015-04-17 19:26:03 +02:00
|
|
|
assert_equal(cmd.port, 8333)
|
|
|
|
if not auth:
|
|
|
|
assert_equal(cmd.username, None)
|
|
|
|
assert_equal(cmd.password, None)
|
|
|
|
rv.append(cmd)
|
|
|
|
|
|
|
|
return rv
|
|
|
|
|
|
|
|
def run_test(self):
|
|
|
|
# basic -proxy
|
|
|
|
self.node_test(self.nodes[0], [self.serv1, self.serv1, self.serv1, self.serv1], False)
|
|
|
|
|
|
|
|
# -proxy plus -onion
|
|
|
|
self.node_test(self.nodes[1], [self.serv1, self.serv1, self.serv2, self.serv1], False)
|
|
|
|
|
|
|
|
# -proxy plus -onion, -proxyrandomize
|
|
|
|
rv = self.node_test(self.nodes[2], [self.serv2, self.serv2, self.serv2, self.serv2], True)
|
|
|
|
# Check that credentials as used for -proxyrandomize connections are unique
|
|
|
|
credentials = set((x.username,x.password) for x in rv)
|
2016-02-09 12:37:05 +01:00
|
|
|
assert_equal(len(credentials), len(rv))
|
2015-04-17 19:26:03 +02:00
|
|
|
|
2016-02-09 12:37:05 +01:00
|
|
|
if self.have_ipv6:
|
|
|
|
# proxy on IPv6 localhost
|
|
|
|
self.node_test(self.nodes[3], [self.serv3, self.serv3, self.serv3, self.serv3], False, False)
|
2015-06-15 12:44:56 +02:00
|
|
|
|
|
|
|
def networks_dict(d):
|
|
|
|
r = {}
|
|
|
|
for x in d['networks']:
|
|
|
|
r[x['name']] = x
|
|
|
|
return r
|
|
|
|
|
|
|
|
# test RPC getnetworkinfo
|
|
|
|
n0 = networks_dict(self.nodes[0].getnetworkinfo())
|
|
|
|
for net in ['ipv4','ipv6','onion']:
|
|
|
|
assert_equal(n0[net]['proxy'], '%s:%i' % (self.conf1.addr))
|
|
|
|
assert_equal(n0[net]['proxy_randomize_credentials'], True)
|
|
|
|
assert_equal(n0['onion']['reachable'], True)
|
|
|
|
|
|
|
|
n1 = networks_dict(self.nodes[1].getnetworkinfo())
|
|
|
|
for net in ['ipv4','ipv6']:
|
|
|
|
assert_equal(n1[net]['proxy'], '%s:%i' % (self.conf1.addr))
|
|
|
|
assert_equal(n1[net]['proxy_randomize_credentials'], False)
|
|
|
|
assert_equal(n1['onion']['proxy'], '%s:%i' % (self.conf2.addr))
|
|
|
|
assert_equal(n1['onion']['proxy_randomize_credentials'], False)
|
|
|
|
assert_equal(n1['onion']['reachable'], True)
|
2018-04-16 11:13:07 +02:00
|
|
|
|
2015-06-15 12:44:56 +02:00
|
|
|
n2 = networks_dict(self.nodes[2].getnetworkinfo())
|
|
|
|
for net in ['ipv4','ipv6','onion']:
|
|
|
|
assert_equal(n2[net]['proxy'], '%s:%i' % (self.conf2.addr))
|
|
|
|
assert_equal(n2[net]['proxy_randomize_credentials'], True)
|
|
|
|
assert_equal(n2['onion']['reachable'], True)
|
|
|
|
|
2016-02-09 12:37:05 +01:00
|
|
|
if self.have_ipv6:
|
|
|
|
n3 = networks_dict(self.nodes[3].getnetworkinfo())
|
|
|
|
for net in ['ipv4','ipv6']:
|
|
|
|
assert_equal(n3[net]['proxy'], '[%s]:%i' % (self.conf3.addr))
|
|
|
|
assert_equal(n3[net]['proxy_randomize_credentials'], False)
|
|
|
|
assert_equal(n3['onion']['reachable'], False)
|
2015-06-15 12:44:56 +02:00
|
|
|
|
2015-04-17 19:26:03 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
ProxyTest().main()
|