加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python写的Socks5协议代理服务器

发布时间:2020-12-16 20:47:41 所属栏目:Python 来源:网络整理
导读:直接上代码: #!/usr/bin/python # Filename s5.py # Python Dynamic Socks5 Proxy # Usage: python s5.py 1080 # Background Run: nohup python s5.py 1080 lt;= 0: break if remote in r: if sock.send(remote.recv(4096)) = 0: break def handle(self): t

直接上代码:

#!/usr/bin/python 
# Filename s5.py 
# Python Dynamic Socks5 Proxy 
# Usage: python s5.py 1080 
# Background Run: nohup python s5.py 1080 & 

import socket,sys,select,SocketServer,struct,time 

class ThreadingTCPServer(SocketServer.ThreadingMixIn,SocketServer.TCPServer): pass
class 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: break 
  def handle(self): 
    try: 
      pass # print 'from ',self.client_address nothing to do. 
      sock = self.connection 
      # 1. Version 
      sock.recv(262) 
      sock.send("x05x00"); 
      # 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 = "x05x00x00x01" 
      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 do to. 
        else: 
          reply = "x05x07x00x01" # Command not supported 
        local = remote.getsockname() 
        reply += socket.inet_aton(local[0]) + struct.pack(">H",local[1])
      except socket.error: 
        # Connection refused 
        reply = 'x05x05x00x01x00x00x00x00x00x00' 
      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()

您可能感兴趣的文章:

  • python实现简单的TCP代理服务器
  • Python实现简单的代理服务器
  • 仅用50行Python代码实现一个简单的代理服务器
  • Python实现TCP协议下的端口映射功能的脚本程序示例
  • Python实现的FTP通信客户端与服务器端功能示例
  • python快速建立超简单的web服务器的实现方法
  • Python搭建FTP服务器的方法示例
  • 尝试用最短的Python代码来实现服务器和代理服务器
  • python实现通过代理服务器访问远程url的方法
  • Python简单实现的代理服务器端口映射功能示例

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读