|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# Copyright (c) 2015 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +import socket |
| 6 | +import traceback, sys |
| 7 | +from binascii import hexlify |
| 8 | +import time, os |
| 9 | + |
| 10 | +from socks5 import Socks5Configuration, Socks5Command, Socks5Server, AddressType |
| 11 | +from test_framework import BitcoinTestFramework |
| 12 | +from util import * |
| 13 | +''' |
| 14 | +Test plan: |
| 15 | +- Start bitcoind's with different proxy configurations |
| 16 | +- Use addnode to initiate connections |
| 17 | +- Verify that proxies are connected to, and the right connection command is given |
| 18 | +- Proxy configurations to test on bitcoind side: |
| 19 | + - `-proxy` (proxy everything) |
| 20 | + - `-onion` (proxy just onions) |
| 21 | + - `-proxyrandomize` Circuit randomization |
| 22 | +- Proxy configurations to test on proxy side, |
| 23 | + - support no authentication (other proxy) |
| 24 | + - support no authentication + user/pass authentication (Tor) |
| 25 | + - proxy on IPv6 |
| 26 | +
|
| 27 | +- Create various proxies (as threads) |
| 28 | +- Create bitcoinds that connect to them |
| 29 | +- Manipulate the bitcoinds using addnode (onetry) an observe effects |
| 30 | +
|
| 31 | +addnode connect to IPv4 |
| 32 | +addnode connect to IPv6 |
| 33 | +addnode connect to onion |
| 34 | +addnode connect to generic DNS name |
| 35 | +''' |
| 36 | + |
| 37 | +class ProxyTest(BitcoinTestFramework): |
| 38 | + def __init__(self): |
| 39 | + # Create two proxies on different ports |
| 40 | + # ... one unauthenticated |
| 41 | + self.conf1 = Socks5Configuration() |
| 42 | + self.conf1.addr = ('127.0.0.1', 13000 + (os.getpid() % 1000)) |
| 43 | + self.conf1.unauth = True |
| 44 | + self.conf1.auth = False |
| 45 | + # ... one supporting authenticated and unauthenticated (Tor) |
| 46 | + self.conf2 = Socks5Configuration() |
| 47 | + self.conf2.addr = ('127.0.0.1', 14000 + (os.getpid() % 1000)) |
| 48 | + self.conf2.unauth = True |
| 49 | + self.conf2.auth = True |
| 50 | + # ... one on IPv6 with similar configuration |
| 51 | + self.conf3 = Socks5Configuration() |
| 52 | + self.conf3.af = socket.AF_INET6 |
| 53 | + self.conf3.addr = ('::1', 15000 + (os.getpid() % 1000)) |
| 54 | + self.conf3.unauth = True |
| 55 | + self.conf3.auth = True |
| 56 | + |
| 57 | + self.serv1 = Socks5Server(self.conf1) |
| 58 | + self.serv1.start() |
| 59 | + self.serv2 = Socks5Server(self.conf2) |
| 60 | + self.serv2.start() |
| 61 | + self.serv3 = Socks5Server(self.conf3) |
| 62 | + self.serv3.start() |
| 63 | + |
| 64 | + def setup_nodes(self): |
| 65 | + # Note: proxies are not used to connect to local nodes |
| 66 | + # this is because the proxy to use is based on CService.GetNetwork(), which return NET_UNROUTABLE for localhost |
| 67 | + return start_nodes(4, self.options.tmpdir, extra_args=[ |
| 68 | + ['-listen', '-debug=net', '-debug=proxy', '-proxy=%s:%i' % (self.conf1.addr),'-proxyrandomize=1'], |
| 69 | + ['-listen', '-debug=net', '-debug=proxy', '-proxy=%s:%i' % (self.conf1.addr),'-onion=%s:%i' % (self.conf2.addr),'-proxyrandomize=0'], |
| 70 | + ['-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'] |
| 72 | + ]) |
| 73 | + |
| 74 | + def node_test(self, node, proxies, auth): |
| 75 | + rv = [] |
| 76 | + # Test: outgoing IPv4 connection through node |
| 77 | + node.addnode("15.61.23.23:1234", "onetry") |
| 78 | + cmd = proxies[0].queue.get() |
| 79 | + assert(isinstance(cmd, Socks5Command)) |
| 80 | + # Note: bitcoind's SOCKS5 implementation only sends atyp DOMAINNAME, even if connecting directly to IPv4/IPv6 |
| 81 | + assert_equal(cmd.atyp, AddressType.DOMAINNAME) |
| 82 | + assert_equal(cmd.addr, "15.61.23.23") |
| 83 | + assert_equal(cmd.port, 1234) |
| 84 | + if not auth: |
| 85 | + assert_equal(cmd.username, None) |
| 86 | + assert_equal(cmd.password, None) |
| 87 | + rv.append(cmd) |
| 88 | + |
| 89 | + # Test: outgoing IPv6 connection through node |
| 90 | + node.addnode("[1233:3432:2434:2343:3234:2345:6546:4534]:5443", "onetry") |
| 91 | + cmd = proxies[1].queue.get() |
| 92 | + assert(isinstance(cmd, Socks5Command)) |
| 93 | + # Note: bitcoind's SOCKS5 implementation only sends atyp DOMAINNAME, even if connecting directly to IPv4/IPv6 |
| 94 | + assert_equal(cmd.atyp, AddressType.DOMAINNAME) |
| 95 | + assert_equal(cmd.addr, "1233:3432:2434:2343:3234:2345:6546:4534") |
| 96 | + assert_equal(cmd.port, 5443) |
| 97 | + if not auth: |
| 98 | + assert_equal(cmd.username, None) |
| 99 | + assert_equal(cmd.password, None) |
| 100 | + rv.append(cmd) |
| 101 | + |
| 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) |
| 113 | + |
| 114 | + # Test: outgoing DNS name connection through node |
| 115 | + node.addnode("node.noumenon:8333", "onetry") |
| 116 | + cmd = proxies[3].queue.get() |
| 117 | + assert(isinstance(cmd, Socks5Command)) |
| 118 | + assert_equal(cmd.atyp, AddressType.DOMAINNAME) |
| 119 | + assert_equal(cmd.addr, "node.noumenon") |
| 120 | + assert_equal(cmd.port, 8333) |
| 121 | + if not auth: |
| 122 | + assert_equal(cmd.username, None) |
| 123 | + assert_equal(cmd.password, None) |
| 124 | + rv.append(cmd) |
| 125 | + |
| 126 | + return rv |
| 127 | + |
| 128 | + def run_test(self): |
| 129 | + # basic -proxy |
| 130 | + self.node_test(self.nodes[0], [self.serv1, self.serv1, self.serv1, self.serv1], False) |
| 131 | + |
| 132 | + # -proxy plus -onion |
| 133 | + self.node_test(self.nodes[1], [self.serv1, self.serv1, self.serv2, self.serv1], False) |
| 134 | + |
| 135 | + # -proxy plus -onion, -proxyrandomize |
| 136 | + rv = self.node_test(self.nodes[2], [self.serv2, self.serv2, self.serv2, self.serv2], True) |
| 137 | + # Check that credentials as used for -proxyrandomize connections are unique |
| 138 | + credentials = set((x.username,x.password) for x in rv) |
| 139 | + assert_equal(len(credentials), 4) |
| 140 | + |
| 141 | + # proxy on IPv6 localhost |
| 142 | + self.node_test(self.nodes[3], [self.serv3, self.serv3, self.serv3, self.serv3], False) |
| 143 | + |
| 144 | +if __name__ == '__main__': |
| 145 | + ProxyTest().main() |
| 146 | + |
0 commit comments