python socket5 代理
#!/usr/bin/python
# Filename
# Python Dynamic Socks5 Proxy
# Usage: python 1080
# Background Run: nohup python 1080 &
import socket, sys, select, SocketServer, struct, time
class ThreadingTCPServer(, ): pass
class Socks5Server():
def handle_tcp(self, sock, remote):
fdset = [sock, remote]
while True:
r, w, e = (fdset, [], [])
if sock in r:
if ((4096)) <= 0: break
if remote in r:
if ((4096)) <= 0: break
def handle(self):
try:
pass # print 'from ', self.client_address nothing to do.
sock =
# 1. Version
(262)
("\x05\x00");
# 2. Request
data = (4)
mode = ord(data[1])
addrtype = ord(data[3])
if addrtype == 1: # IPv4
addr = socket.inet_ntoa((4))
elif addrtype == 3: # Domain name
addr = (ord((1)[0]))
port = ('>H', (2))
reply = "\x05\x00\x00\x01"
try:
if mode == 1: # 1. Tcp connect
remote = (socket.AF_INET, socket.SOCK_STREAM)
((addr, port[0]))
pass # print 'To', addr, port[0] nothing do to.
else:
reply = "\x05\x07\x00\x01" # Command not supported
local = ()
reply += socket.inet_aton(local[0]) + (">H", local[1])
except :
# Connection refused
reply = '\x05\x05\x00\x01\x00\x00\x00\x00\x00\x00'
(reply)
# 3. Transfering
if reply[1] == '\x00': # Success
if mode == 1: # 1. Tcp connect
self.handle_tcp(sock, remote)
except :
pass #print 'error' nothing to do .
except IndexError:
pass
def main():
filename = [0];
if len()<2:
print 'usage: ' + filename + ' port'
()
socks_port = int([1]);
server = ThreadingTCPServer(('', socks_port), Socks5Server)
print 'bind port: %d' % socks_port + ' ok!'
server.serve_forever()
if __name__ == '__main__':
main()