用Python写socks5服务器端

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

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

代码如下:

view plaincopy to clipboardprint?
  1. importsocket,sys,select,SocketServer,struct,time
  2. classThreadingTCPServer(SocketServer.ThreadingMixIn,SocketServer.TCPServer):pass
  3. classSocks5Server(SocketServer.StreamRequestHandler):
  4. defhandle_tcp(self,sock,remote):
  5. fdset=[sock,remote]
  6. whileTrue:
  7. r,w,e=select.select(fdset,[],[])
  8. ifsockinr:
  9. ifremote.send(sock.recv(4096))<=0:break
  10. ifremoteinr:
  11. ifsock.send(remote.recv(4096))<=0:break
  12. defhandle(self):
  13. try:
  14. print'socksconnectionfrom',self.client_address
  15. sock=self.connection
  16. #1.Version
  17. sock.recv(262)
  18. sock.send(b"\x05\x00");
  19. #2.Request
  20. data=self.rfile.read(4)
  21. mode=ord(data[1])
  22. addrtype=ord(data[3])
  23. ifaddrtype==1:#IPv4
  24. addr=socket.inet_ntoa(self.rfile.read(4))
  25. elifaddrtype==3:#Domainname
  26. addr=self.rfile.read(ord(sock.recv(1)[0]))
  27. port=struct.unpack('>H',self.rfile.read(2))
  28. reply=b"\x05\x00\x00\x01"
  29. try:
  30. ifmode==1:#1.Tcpconnect
  31. remote=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  32. remote.connect((addr,port[0]))
  33. print'Tcpconnectto',addr,port[0]
  34. else:
  35. reply=b"\x05\x07\x00\x01"#Commandnotsupported
  36. local=remote.getsockname()
  37. reply+=socket.inet_aton(local[0])+struct.pack(">H",local[1])
  38. exceptsocket.error:
  39. #Connectionrefused
  40. reply='\x05\x05\x00\x01\x00\x00\x00\x00\x00\x00'
  41. sock.send(reply)
  42. #3.Transfering
  43. ifreply[1]=='\x00':#Success
  44. ifmode==1:#1.Tcpconnect
  45. self.handle_tcp(sock,remote)
  46. exceptsocket.error:
  47. print'socketerror'
  48. defmain():
  49. server=ThreadingTCPServer(('',1080),Socks5Server)
  50. server.serve_forever()
  51. if__name__=='__main__':
  52. main()
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:
print 'socks connection from ', self.client_address
sock = self.connection
# 1. Version
sock.recv(262)
sock.send(b"\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 = b"\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]))
print 'Tcp connect to', addr, port[0]
else:
reply = b"\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:
print 'socket error'
def main():
server = ThreadingTCPServer(('', 1080), Socks5Server)
server.serve_forever()
if __name__ == '__main__':
main()

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