Socks服务器

socks


import socketserver, struct

class MyTCPHandler(socketserver.BaseRequestHandler):

def handle(self):

self.request.recv(3)

self.request.send(b'\x05\x00')

data = self.request.recv(200)

host = data[5:-2]
port = struct.unpack('H', data[-2:])[0]

self.request.send(b'\x05\x00\x00\x01\x00\x00\x00\x00\x10\x10')

buffer = []

while True:
d = self.request.recv(2048)
buffer.append(d)
if len(d) < 2048:
break

data = b''.join(buffer)

print(data)

self.request.sendall(b'HTTP/1.1 200 OK\r\n\r\nhello')

print(data)

if __name__ == "__main__":
HOST, PORT = "0.0.0.0", 9999
server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler)
server.serve_forever()