5 Dummy Socks5 server for testing. 7 from __future__
import print_function, division, unicode_literals
8 import socket, threading, Queue
22 '''Receive n bytes from a socket, or fail''' 27 raise IOError(
'Unexpected end of stream')
34 '''Proxy configuration''' 37 self.
af = socket.AF_INET
42 '''Information about an incoming socks5 command''' 43 def __init__(self, cmd, atyp, addr, port, username, password):
51 return 'Socks5Command(%s,%s,%s,%s,%s,%s)' % (self.
cmd, self.
atyp, self.
addr, self.
port, self.
username, self.
password)
61 Handle socks5 request according to RFC1928 67 raise IOError(
'Invalid socks version %i' % ver)
72 if 0x02
in methods
and self.
serv.conf.auth:
74 elif 0x00
in methods
and self.
serv.conf.unauth:
77 raise IOError(
'No supported authentication method was offered')
79 self.
conn.sendall(bytearray([0x05, method]))
86 raise IOError(
'Invalid auth packet version %i' % ver)
92 self.
conn.sendall(bytearray([0x01, 0x00]))
97 raise IOError(
'Invalid socks version %i in connect request' % ver)
98 if cmd != Command.CONNECT:
99 raise IOError(
'Unhandled command %i in connect request' % cmd)
101 if atyp == AddressType.IPV4:
103 elif atyp == AddressType.DOMAINNAME:
106 elif atyp == AddressType.IPV6:
109 raise IOError(
'Unknown address type %i' % atyp)
111 port = (port_hi << 8) | port_lo
114 self.
conn.sendall(bytearray([0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
116 cmdin =
Socks5Command(cmd, atyp, addr, port, username, password)
117 self.
serv.queue.put(cmdin)
118 print(
'Proxy: ', cmdin)
120 except Exception
as e:
121 traceback.print_exc(file=sys.stderr)
122 self.
serv.queue.put(e)
129 self.
s = socket.socket(conf.af)
130 self.
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
131 self.
s.bind(conf.addr)
139 (sockconn, peer) = self.
s.accept()
142 thread = threading.Thread(
None, conn.handle)
149 self.
thread = threading.Thread(
None, self.
run)
156 s = socket.socket(self.
conf.af)
157 s.connect(self.
conf.addr)
def __init__(self, cmd, atyp, addr, port, username, password)
def __init__(self, serv, conn, peer)