Python写的Socks5协议代理服务器

作者:junjie 时间:2022-03-16 21:22:44 

直接上代码:


#!/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()
标签:Python,Socks5,代理服务器
0
投稿

猜你喜欢

  • Python Django基础二之URL路由系统

    2021-05-12 11:50:07
  • 浅谈tensorflow中张量的提取值和赋值

    2022-06-23 19:20:55
  • python3第三方爬虫库BeautifulSoup4安装教程

    2023-05-10 21:11:57
  • Python中的列表知识点汇总

    2021-06-01 05:00:50
  • Python实战之实现获取动态图表

    2023-10-31 14:03:34
  • Python中pandas dataframe删除一行或一列:drop函数详解

    2021-07-09 16:46:47
  • python中的五种异常处理机制介绍

    2022-05-05 13:02:01
  • python正则表达式中的括号匹配问题

    2023-07-26 01:36:24
  • Python下Fabric的简单部署方法

    2023-11-08 07:17:11
  • Python OpenCV形态学运算示例详解

    2022-03-04 12:23:39
  • JavaScript基于ChatGPT API实现划词翻译浏览器脚本

    2024-04-22 12:52:27
  • Python 基于FIR实现Hilbert滤波器求信号包络详解

    2023-07-13 01:31:47
  • Python实现账号密码输错三次即锁定功能简单示例

    2023-07-22 07:40:21
  • perl获取日期与时间的实例代码

    2023-03-30 23:57:01
  • Python正则表达式匹配ip地址实例

    2023-06-13 15:06:12
  • Python 异步等待任务集合

    2022-08-14 17:23:22
  • 利用python解决mysql视图导入导出依赖的问题

    2023-10-28 07:27:38
  • 浅析PyCharm 的初始设置(知道)

    2022-12-04 05:41:26
  • 让ThinkPHP支持大小写url地址访问的方法

    2023-11-17 02:28:37
  • Mysql DateTime 查询问题解析

    2024-01-23 06:00:53
  • asp之家 网络编程 m.aspxhome.com