Python写的Socks5协议代理服务器

yipeiwu_com4年前服务器

直接上代码:

#!/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("\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 do to. 
        else: 
          reply = "\x05\x07\x00\x01" # Command not supported 
        local = remote.getsockname() 
        reply += socket.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()

相关文章

在IIS服务器上以CGI方式运行Python脚本的教程

在IIS服务器上以CGI方式运行Python脚本的教程

由于接触到Python Web开发,正好把最简单的CGI方式研究了一下,话说在Windows下配置Python的Web开发还真的蛮麻烦的,Linux下配置倒挺容易,正好微软有技术文章《U...

Python实现根据指定端口探测服务器/模块部署的方法

本文实例讲述了Python实现根据指定端口探测服务器/模块部署的方法,非常具有实用价值。分享给大家供大家参考借鉴。 有些时候,在维护过程中,服务器数量非常多。应用模块部署在不同服务器上。...

Pycharm保存不能自动同步到远程服务器的解决方法

Pycharm保存不能自动同步到远程服务器的解决方法

Deployment已经设置了远程服务,Pycharm也已经取消自动保存,确保Ctrl+S可以触发,可是依旧不能自动同步到远程服务器。捣鼓了半天发现在Delployment的mappin...

Python实现简易版的Web服务器(推荐)

Python实现简易版的Web服务器(推荐)

下面给大家介绍python实现简易版的web服务器,具体内容详情大家通过本文学习吧! 1、请自行了解HTTP协议 /post/133883.htm(点击跳转) 2、创建Socket服务...

paramiko模块安装和使用(远程登录服务器)

一:简介 由paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。 由于使用的是python这样的能够跨平台运行的语言,所以所...