Directly on the code:
#!/usr/bin/python # Filename s5.py # python Dynamic Socks5 Proxy # usage:python s5.py # Background Run:nohup python s5.py & Import sockets, SYS, select, Socketserver, struct, Time class Threadingtcpserver (Socketserver.threadingmi XIn, Socketserver.tcpserver): Passclass socks5server (socketserver.streamrequesthandler): Def handle_tcp (self, sock, Remote): Fdset = [sock, remote] while True:r, W, E = Select.select (Fdset, [], []) if sock in R: If Remote.send (SOCK.RECV (4096)) <= 0:break if remote in R:if sock.send (REMOTE.RECV (4096)) <= 0:b Reak def handle (self): Try:pass # print "from", self.client_address nothing to do. sock = self.connection # 1. Version sock.recv (262) sock.send ("\x05\x00"); # 2. Request data = Self.rfile.read (4) mode = Ord (data[1]) AddrType = Ord (data[3]) if addrtype = = 1: # IPv4 addr = Socket.inet_ntoa (Self.rfile.read (4))elif AddrType = = 3: # Domain Name addr = Self.rfile.read (Ord (SOCK.RECV (1) [0])) port = Struct.unpack (' >h ', Self.rfile.read (2)) Reply = "\x05\x00\x00\x01" try:if mode = = 1: # 1. TCP Connect remote = Socket.socket (socket.af_inet, socket. Sock_stream) Remote.connect ((addr, port[0]) Pass # print ' to ', addr, port[0] nothing does to. else:reply = "\x05\x07\x00\x01" # Command not supported local = Remote.getsockname () Reply + = s Ocket.inet_aton (local[0]) + struct.pack (">h", local[1]) except Socket.error: # Connection refused Reply = ' \x05\x05\x00\x01\x00\x00\x00\x00\x00\x00 ' sock.send (Reply) # 3. transfering if reply[1] = = ' \x00 ': # Success if mode = = 1: # 1. TCP Connect self.handle_tcp (sock, remote) except Socket.error:pass #print ' error ' nothing to do. Except Indexerror:pass def main (): filename = sys.argv[0]; If Len (sys.argv) <2:print ' usage: ' + filename + ' port ' sys.exit () socks_port = Int (sys.argv[1]); Server = Threadingtcpserver ((' ', Socks_port), socks5server) print ' Bind port:%d '% socks_port + ' ok! ' Server.serve_forever () if __name__ = = ' __main__ ': Main ()