-
Notifications
You must be signed in to change notification settings - Fork 0
/
asocks4a.py
74 lines (61 loc) · 1.69 KB
/
asocks4a.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python
import asyncore
import socket
import struct
def parse_socks(r):
if not r.startswith('\x04\x01'):
print "Unsupported command, closing"
reply = '\x00\x5b'
return reply, None
port = socket.ntohs(struct.unpack('<H',r[2:4])[0])
if r[4:8].startswith('\x00\x00\x00'):
user, host = r[8:].split('\x00')[:-1]
else:
host = socket.inet_ntoa(r[4:8])
user = r[8:]
if not len(user):
user = 'Anonymous'
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s2.connect((host,port))
except:
reply = '\x00\x5b' + r[2:4] + r[4:8]
s2.close()
return reply, None
s2.setblocking(0)
reply = '\x00\x5a' + r[2:4] + r[4:8]
return reply, s2
class Relay(asyncore.dispatcher_with_send):
def __init__(self, sockin, relayback=None):
asyncore.dispatcher_with_send.__init__(self, sockin)
#self.sockout = sockout
self.relayback = relayback
def handle_read(self):
data = self.recv(4096)
if data:
if self.relayback is None:
reply, s = parse_socks(data)
if s is not None:
self.relayback = Relay(s, self)
self.send(reply)
else:
self.send(reply)
self.close()
else:
self.relayback.send(data)
class Listener(asyncore.dispatcher_with_send):
def __init__(self, listener_addr):
asyncore.dispatcher_with_send.__init__(self)
self.server = None
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind(listener_addr)
self.listen(5)
#self.relays = []
def handle_accept(self):
conn, addr = self.accept()
#self.relays.append(Relay(conn))
rel = Relay(conn)
if __name__ == '__main__':
socks = Listener(('0.0.0.0', 9000))
asyncore.loop()