Skip to content

Commit 88f9d31

Browse files
committed
add support for multiple destination ports
1 parent 05164a1 commit 88f9d31

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

README

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Usage:
55
Set the socks_proxy os environment variable.
66
Import it before anything else that will be using sockets.
77
If the environment variable isn't set, then no proxying will occur
8+
socks_proxy="<DESTPORT1,DESTPORT2,...,DESTPORTN:>[username[:password]@]<PROXYHOST:><PROXYPORT>"
89

910

1011
Caveats:
@@ -14,7 +15,4 @@ user/pass auth has not been tested
1415
if socks_proxy env variable is set, all socket connections on that port will use it
1516

1617

17-
Todo:
18-
Add support for multiple ports/proxies
19-
socks_proxy="<DESTPORT:>[username[:password]@]<PROXYHOST:><PROXYPORT> socks_proxy=<DESTPORT:>[username[:password]@]<PROXYHOST:><PROXYPORT> ..."
20-
socks_proxy="<DESTPORT1,DESTPORT2,...,DESTPORTN:>[username[:password]@]<PROXYHOST:><PROXYPORT>"
18+

monkey_sockets_socks5.py

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,77 @@
88

99

1010
def _split_proxy(uri, port):
11+
# <DESTPORT1,DESTPORT2,...,DESTPORTN:>[username[:password]@]<PROXYHOST:><PROXYPORT>
1112
split_auth = uri.split("@")
1213
if uri == "":
1314
split_uri = []
1415
elif len(split_auth) == 2:
1516
split_first = split_auth[0].split(":")
1617
split_second = split_auth[1].split(":")
1718
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])]
1920
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])]
2122
else:
2223
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])]
2428
if len(split_uri) != 5:
2529
split_uri = None
26-
elif split_uri[0] != port:
30+
elif port not in [int(p) for p in split_uri[0].split(",")]:
2731
split_uri = None
32+
else:
33+
# we only care about one port
34+
split_uri[0] = port
2835
return split_uri
2936

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+
3082

3183
# CAVEATS:
3284
# only supports ipv4
@@ -35,7 +87,7 @@ def _split_proxy(uri, port):
3587
# if socks_proxy env variable is set, all socket connections on that port will use it
3688
class Socks5Socket(socket.socket):
3789
def connect(self, address):
38-
# socks_proxy=<DESTPORT:>[username[:password]@]<PROXYHOST:><PROXYPORT>
90+
# socks_proxy=<DESTPORT1,DESTPORT2,...,DESTPORTN:>[username[:password]@]<PROXYHOST:><PROXYPORT>
3991
socks_proxy = _split_proxy(os.getenv("socks_proxy",""), address[1])
4092

4193
if not socks_proxy:

0 commit comments

Comments
 (0)