Mercurial > hg > anonet-resdb
view contrib/quicktun.socks4a/quicktun.socks4a @ 1076:f55d73333527 draft
added some information for sorbatol
author | d3v11 <d3v11@d3v11.ano> |
---|---|
date | Tue, 12 Mar 2013 03:35:10 +0000 |
parents | 497e6255939e |
children |
line wrap: on
line source
#!/usr/bin/env python import subprocess, signal, socket, select, fcntl, time, sys, os def exit(sn,sf): try: tun.terminate () except: pass signal.signal (signal.SIGHUP, exit) signal.signal (signal.SIGINT, exit) signal.signal (signal.SIGTERM,exit) DEBUG = os.getenv ('DEBUG' ,'1' ) TUN_MODE = os.getenv ('TUN_MODE' ,'1' ) PROTOCOL = os.getenv ('PROTOCOL' ,'raw' ) INTERFACE = os.getenv ('INTERFACE' ,'ppp-socks') REMOTE_FLOAT = int(os.getenv ('REMOTE_FLOAT' ,'1' )) LOCAL_ADDRESS = os.getenv ('LOCAL_ADDRESS' ,'127.0.0.1') LOCAL_PORT = int(os.getenv ('LOCAL_PORT' ,'2998' )) REMOTE_ADDRESS = os.getenv ('REMOTE_ADDRESS','127.0.0.2') REMOTE_PORT = int(os.getenv ('REMOTE_PORT' ,'2998' )) DST_ADDRESS = os.getenv ('DST_ADDRESS' ,'127.0.0.3') DST_PORT = os.getenv ('DST_PORT' ,'2998' ) SOCKS_ADDRESS = os.getenv ('SOCKS_ADDRESS' ,'127.0.0.1') SOCKS_PORT = os.getenv ('SOCKS_PORT' ,'9050' ) udp = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) udp.setsockopt (socket.SOL_SOCKET,socket.SO_REUSEADDR,1) udp.setblocking (0) udp.bind ((REMOTE_ADDRESS,REMOTE_PORT)) udp_poll = select.poll () udp_poll.register (udp.fileno(),select.POLLIN|select.POLLPRI) if REMOTE_FLOAT==0: os.environ.pop ('REMOTE_FLOAT') tun = subprocess.Popen ( ['socat','STDIO','SOCKS4A:'+SOCKS_ADDRESS+':' +DST_ADDRESS+':'+DST_PORT+',SOCKSPORT=' +SOCKS_PORT], stdout = subprocess.PIPE, stdin = subprocess.PIPE, ) tun_stdout = tun.stdout.fileno () tun_stdin = tun.stdin.fileno () tun_poll = select.poll () tun_poll.register ( tun.stdout.fileno(),select.POLLIN|select.POLLPRI) if REMOTE_FLOAT==1: tun = subprocess.Popen ( ['socat','TCP-LISTEN:'+str(LOCAL_PORT) +',BIND='+LOCAL_ADDRESS+',REUSEADDR', 'UDP-CONNECT:'+LOCAL_ADDRESS+':'+str( LOCAL_PORT)], stdout = subprocess.PIPE, stdin = subprocess.PIPE, ) tun_stdout = tun.stdout.fileno () tun_stdin = tun.stdin.fileno () tun_poll = select.poll () tun_poll.register ( tun.stdout.fileno(),select.POLLIN|select.POLLPRI) fcntl.fcntl (tun_stdin,fcntl.F_SETFL,fcntl.fcntl (tun_stdin,fcntl.F_GETFL)&~os.O_NONBLOCK|os.O_NONBLOCK) fcntl.fcntl (tun_stdout,fcntl.F_SETFL,fcntl.fcntl (tun_stdout,fcntl.F_GETFL)&~os.O_NONBLOCK|os.O_NONBLOCK) time.sleep (4) tun = subprocess.Popen ( [ '/usr/sbin/quicktun.'+PROTOCOL, ], stdout = subprocess.PIPE, stdin = subprocess.PIPE, ) recvq = str () sendq = str () while 1: if len(udp_poll.poll(128))>0: buffer = str () buffer = udp.recv (1024) if len(buffer)==0: break sendq = sendq + buffer if len(tun_poll.poll(128))>0: buffer = str () buffer = os.read (tun_stdout,1024) if len(buffer)==0: break recvq = recvq + buffer if len(sendq)>0: try: sendq = sendq [os.write(tun_stdin,sendq[:1024]):] except OSError as ex: if ex.errno!=11: break if len(recvq)>0: try: recvq = recvq [udp.sendto(recvq[:1024], (LOCAL_ADDRESS,LOCAL_PORT)):] except OSError as ex: if ex.errno!=11: break if len(sendq)>65536*128 or len(recvq)>65536*128: break tun.terminate ()