python:socksv4_proxy
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
| — | python:socksv4_proxy [2011/05/20 13:50] (current) – created tkbletsc | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | This is a simple [[http:// | ||
| + | < | ||
| + | # | ||
| + | import sys | ||
| + | import socket | ||
| + | import struct | ||
| + | import thread | ||
| + | from optparse import OptionParser | ||
| + | |||
| + | # Very simple multi-threaded SOCKSv4(a) proxy | ||
| + | # by Tyler Bletsch (Tyler.Bletsch at gmail dot com) | ||
| + | |||
| + | parser = OptionParser(" | ||
| + | |||
| + | parser.add_option(" | ||
| + | parser.add_option(" | ||
| + | parser.add_option(" | ||
| + | parser.add_option(" | ||
| + | |||
| + | (options, args) = parser.parse_args() | ||
| + | |||
| + | buffer_size = options.buffer_size | ||
| + | listen_port = options.listen_port | ||
| + | listen_interface = options.listen_interface | ||
| + | connect_interface = options.connect_interface | ||
| + | |||
| + | def readstr(s): | ||
| + | r='' | ||
| + | while 1: | ||
| + | c = s.recv(1) | ||
| + | if not c: return None | ||
| + | if c == ' | ||
| + | r += c | ||
| + | return r | ||
| + | |||
| + | def tunnel(src, | ||
| + | while 1: | ||
| + | try: | ||
| + | r = src.recv(buffer_size) | ||
| + | except Exception as e: | ||
| + | print "Recv error: %s" % e | ||
| + | src.close() | ||
| + | dest.close() | ||
| + | break | ||
| + | |||
| + | if not r: | ||
| + | src.close() | ||
| + | dest.close() | ||
| + | break | ||
| + | |||
| + | try: | ||
| + | dest.send(r) | ||
| + | except Exception as e: | ||
| + | print "Send error: %s" % e | ||
| + | src.close() | ||
| + | dest.close() | ||
| + | break | ||
| + | |||
| + | |||
| + | svr = socket.socket(socket.AF_INET, | ||
| + | svr.bind((listen_interface, | ||
| + | svr.listen(32) | ||
| + | |||
| + | SOCKS4_REQUEST_FORMAT = ' | ||
| + | SOCKS4_RESPONSE_OKAY = struct.pack(' | ||
| + | SOCKS4_RESPONSE_FAIL = struct.pack(' | ||
| + | |||
| + | print " | ||
| + | |||
| + | while 1: | ||
| + | initiator, initiator_addr = svr.accept() | ||
| + | sys.stdout.write(' | ||
| + | |||
| + | data = initiator.recv(struct.calcsize(SOCKS4_REQUEST_FORMAT)) | ||
| + | |||
| + | if not data: | ||
| + | print " | ||
| + | break | ||
| + | |||
| + | (socks_ver, | ||
| + | (target_addr_int, | ||
| + | target_addr = socket.inet_ntoa(target_addr) | ||
| + | username = readstr(initiator) | ||
| + | |||
| + | if socks_ver!=4: | ||
| + | print " | ||
| + | initiator.send(SOCKS4_RESPONSE_FAIL) | ||
| + | initiator.close() | ||
| + | continue | ||
| + | |||
| + | if command!=1: | ||
| + | print " | ||
| + | initiator.send(SOCKS4_RESPONSE_FAIL) | ||
| + | initiator.close() | ||
| + | continue | ||
| + | |||
| + | is_socks4a = target_addr_int> | ||
| + | if is_socks4a: | ||
| + | target_addr = readstr(initiator) | ||
| + | |||
| + | sys.stdout.write(" | ||
| + | |||
| + | target = socket.socket(socket.AF_INET, | ||
| + | if connect_interface: | ||
| + | try: | ||
| + | target.connect((target_addr, | ||
| + | except Exception as e: | ||
| + | initiator.send(SOCKS4_RESPONSE_FAIL) | ||
| + | initiator.close() | ||
| + | print "FAIL: %s" % e | ||
| + | continue | ||
| + | if not target: | ||
| + | initiator.send(SOCKS4_RESPONSE_FAIL) | ||
| + | initiator.close() | ||
| + | print " | ||
| + | continue | ||
| + | |||
| + | print " | ||
| + | initiator.send(SOCKS4_RESPONSE_OKAY) | ||
| + | thread.start_new_thread(tunnel, | ||
| + | thread.start_new_thread(tunnel, | ||
| + | |||
| + | |||
| + | </ | ||
python/socksv4_proxy.txt · Last modified: by tkbletsc
