MCPcopy Create free account
hub / github.com/LUX-Core/lux / Socks5Connection

Class Socks5Connection

test/functional/test_framework/socks5.py:54–123  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

52 return 'Socks5Command(%s,%s,%s,%s,%s,%s)' % (self.cmd, self.atyp, self.addr, self.port, self.username, self.password)
53
54class Socks5Connection(object):
55 def __init__(self, serv, conn, peer):
56 self.serv = serv
57 self.conn = conn
58 self.peer = peer
59
60 def handle(self):
61 """Handle socks5 request according to RFC192."""
62 try:
63 # Verify socks version
64 ver = recvall(self.conn, 1)[0]
65 if ver != 0x05:
66 raise IOError('Invalid socks version %i' % ver)
67 # Choose authentication method
68 nmethods = recvall(self.conn, 1)[0]
69 methods = bytearray(recvall(self.conn, nmethods))
70 method = None
71 if 0x02 in methods and self.serv.conf.auth:
72 method = 0x02 # username/password
73 elif 0x00 in methods and self.serv.conf.unauth:
74 method = 0x00 # unauthenticated
75 if method is None:
76 raise IOError('No supported authentication method was offered')
77 # Send response
78 self.conn.sendall(bytearray([0x05, method]))
79 # Read authentication (optional)
80 username = None
81 password = None
82 if method == 0x02:
83 ver = recvall(self.conn, 1)[0]
84 if ver != 0x01:
85 raise IOError('Invalid auth packet version %i' % ver)
86 ulen = recvall(self.conn, 1)[0]
87 username = str(recvall(self.conn, ulen))
88 plen = recvall(self.conn, 1)[0]
89 password = str(recvall(self.conn, plen))
90 # Send authentication response
91 self.conn.sendall(bytearray([0x01, 0x00]))
92
93 # Read connect request
94 (ver,cmd,rsv,atyp) = recvall(self.conn, 4)
95 if ver != 0x05:
96 raise IOError('Invalid socks version %i in connect request' % ver)
97 if cmd != Command.CONNECT:
98 raise IOError('Unhandled command %i in connect request' % cmd)
99
100 if atyp == AddressType.IPV4:
101 addr = recvall(self.conn, 4)
102 elif atyp == AddressType.DOMAINNAME:
103 n = recvall(self.conn, 1)[0]
104 addr = recvall(self.conn, n)
105 elif atyp == AddressType.IPV6:
106 addr = recvall(self.conn, 16)
107 else:
108 raise IOError('Unknown address type %i' % atyp)
109 port_hi,port_lo = recvall(self.conn, 2)
110 port = (port_hi << 8) | port_lo
111

Callers 1

runMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected