zoukankan      html  css  js  c++  java
  • 用Python写socks5服务器端

    用Python写socks5服务器端 « Xiaoxia[PG]

    用Python写socks5服务器端

    参考自RFC1928: http://xiaoxia.org/?p=2672

    直接运行这个程序就给本机建立了一个socks5的代理服务器。

    代码如下:

    1. import socket, sys, select, SocketServer, struct, time  
    2.   
    3. class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): pass  
    4. class Socks5Server(SocketServer.StreamRequestHandler):  
    5.     def handle_tcp(self, sock, remote):  
    6.         fdset = [sock, remote]  
    7.         while True:  
    8.             r, w, e = select.select(fdset, [], [])  
    9.             if sock in r:  
    10.                 if remote.send(sock.recv(4096)) <= 0break  
    11.             if remote in r:  
    12.                 if sock.send(remote.recv(4096)) <= 0break  
    13.     def handle(self):  
    14.         try:  
    15.             print 'socks connection from 'self.client_address  
    16.             sock = self.connection  
    17.             # 1. Version  
    18.             sock.recv(262)  
    19.             sock.send(b"\x05\x00");  
    20.             # 2. Request  
    21.             data = self.rfile.read(4)  
    22.             mode = ord(data[1])  
    23.             addrtype = ord(data[3])  
    24.             if addrtype == 1:       # IPv4  
    25.                 addr = socket.inet_ntoa(self.rfile.read(4))  
    26.             elif addrtype == 3:     # Domain name  
    27.                 addr = self.rfile.read(ord(sock.recv(1)[0]))  
    28.             port = struct.unpack('>H'self.rfile.read(2))  
    29.             reply = b"\x05\x00\x00\x01"  
    30.             try:  
    31.                 if mode == 1:  # 1. Tcp connect  
    32.                     remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
    33.                     remote.connect((addr, port[0]))  
    34.                     print 'Tcp connect to', addr, port[0]  
    35.                 else:  
    36.                     reply = b"\x05\x07\x00\x01" # Command not supported  
    37.                 local = remote.getsockname()  
    38.                 reply += socket.inet_aton(local[0]) + struct.pack(">H", local[1])  
    39.             except socket.error:  
    40.                 # Connection refused  
    41.                 reply = '\x05\x05\x00\x01\x00\x00\x00\x00\x00\x00'  
    42.             sock.send(reply)  
    43.             # 3. Transfering  
    44.             if reply[1] == '\x00':  # Success  
    45.                 if mode == 1:    # 1. Tcp connect  
    46.                     self.handle_tcp(sock, remote)  
    47.         except socket.error:  
    48.             print 'socket error'  
    49. def main():  
    50.     server = ThreadingTCPServer((''1080), Socks5Server)  
    51.     server.serve_forever()  
    52. if __name__ == '__main__':  
    53.     main()  

    已经修正Google SyntaxHighlighter无法正确显示Python代码的问题。问题出自shBrushPython.js中定义regexList数组的时候末尾多了一个","。删去就行了。神奇的是,只有IEcore才报错……

  • 相关阅读:
    杭电1171 Big Event in HDU(母函数+多重背包解法)
    怎样设计接口?
    未将对象引用设置到对象的实例--可能出现的问题总结
    開始Unity3D的学习之旅
    介绍一款轻量级js控件:easy.js
    Mustache 使用心得总结
    (ArcGIS API For Silverlight )QueryTask 跨层查询,和监控完整的查询!
    非常基本的SQL 内外连接
    Myeclipse它显示了一个目录的结构,而不是包
    Duanxx的Design abroad: C++矩阵运算库Eigen 概要
  • 原文地址:https://www.cnblogs.com/lexus/p/2379103.html
Copyright © 2011-2022 走看看