8
8
9
9
10
10
def _split_proxy (uri , port ):
11
+ # <DESTPORT1,DESTPORT2,...,DESTPORTN:>[username[:password]@]<PROXYHOST:><PROXYPORT>
11
12
split_auth = uri .split ("@" )
12
13
if uri == "" :
13
14
split_uri = []
14
15
elif len (split_auth ) == 2 :
15
16
split_first = split_auth [0 ].split (":" )
16
17
split_second = split_auth [1 ].split (":" )
17
18
if len (split_first ) == 3 :
18
- split_uri = [ int ( split_first [ 0 ])] + split_first [ 1 :] + [split_second [0 ], int (split_second [1 ])]
19
+ split_uri = split_first + [split_second [0 ], int (split_second [1 ])]
19
20
else :
20
- split_uri = [ int ( split_first [ 0 ])] + split_first [ 1 :] + ["" ] + [split_second [0 ], int (split_second [1 ])]
21
+ split_uri = split_first + ["" ] + [split_second [0 ], int (split_second [1 ])]
21
22
else :
22
23
split_small = split_auth [0 ].split (":" )
23
- split_uri = [int (split_small [0 ])] + ["" ] + ["" ] + [split_small [1 ]] + [int (split_small [2 ])]
24
+ if len (split_small ) != 3 :
25
+ split_uri = []
26
+ else :
27
+ split_uri = [split_small [0 ],"" ,"" ] + [split_small [1 ]] + [int (split_small [2 ])]
24
28
if len (split_uri ) != 5 :
25
29
split_uri = None
26
- elif split_uri [0 ] != port :
30
+ elif port not in [ int ( p ) for p in split_uri [0 ]. split ( "," )] :
27
31
split_uri = None
32
+ else :
33
+ # we only care about one port
34
+ split_uri [0 ] = port
28
35
return split_uri
29
36
37
+ def _test_split_proxy ():
38
+ # multi-port
39
+ # user exists
40
+ # password exists (force user exists)
41
+ # port in port list
42
+ # port not in port list
43
+
44
+ # positive tests
45
+ print "positive"
46
+ ports = [
47
+ ["80:" ,[80 ]],
48
+ ["80,90:" ,[80 ,90 ]],
49
+ ["80,90,100:" ,[80 ,90 ,100 ]],
50
+ ]
51
+ auths = [
52
+ ["user:pass@" ,["user" ,"pass" ]],
53
+ ["user@" ,["user" ,"" ]],
54
+ ["" ,["" ,"" ]],
55
+ ]
56
+ hosts = [
57
+ ["host:100" ,["host" ,100 ]],
58
+ ]
59
+
60
+ for port in ports :
61
+ for expected_port in port [1 ]:
62
+ for auth in auths :
63
+ for host in hosts :
64
+ teststring = str (port [0 ]) + auth [0 ] + host [0 ]
65
+ testlist = [expected_port ] + auth [1 ] + host [1 ]
66
+ print teststring ,testlist ,_split_proxy (teststring ,expected_port )
67
+ assert testlist == _split_proxy (teststring ,expected_port )
68
+ print
69
+
70
+
71
+ # negative tests
72
+ print "negative"
73
+ print "80,90:host:100" ,100
74
+ assert None == _split_proxy ("80,90:host:100" ,100 )
75
+ print "80:host:100" ,100
76
+ assert None == _split_proxy ("80:host:100" ,100 )
77
+ print "80,90:100" ,80
78
+ assert None == _split_proxy ("80,90:100" ,80 )
79
+ print "80:100" ,80
80
+ assert None == _split_proxy ("80:100" ,80 )
81
+
30
82
31
83
# CAVEATS:
32
84
# only supports ipv4
@@ -35,7 +87,7 @@ def _split_proxy(uri, port):
35
87
# if socks_proxy env variable is set, all socket connections on that port will use it
36
88
class Socks5Socket (socket .socket ):
37
89
def connect (self , address ):
38
- # socks_proxy=<DESTPORT :>[username[:password]@]<PROXYHOST:><PROXYPORT>
90
+ # socks_proxy=<DESTPORT1,DESTPORT2,...,DESTPORTN :>[username[:password]@]<PROXYHOST:><PROXYPORT>
39
91
socks_proxy = _split_proxy (os .getenv ("socks_proxy" ,"" ), address [1 ])
40
92
41
93
if not socks_proxy :
0 commit comments