Python-written SOCKS5 protocol proxy Server

Source: Internet
Author: User
Tags ord
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 ()
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.