def test(self): loop = sevent.instance() def run(): loop.start() self.proxy_thread = threading.Thread(target=run) self.proxy_thread.setDaemon(True) self.proxy_thread.start() yield self._execute_test_connection_closing() yield self._execute_test_connection_closed() yield self._execute_test_remote_closing() yield self._execute_test_pool_closing()
while True: conn = await server.accept() status = {"recv_len": 0, "send_len": 0, "last_time": time.time(), "check_recv_len": 0, "check_send_len": 0} sevent.current().call_async(tcp_proxy, conns, conn, args.proxy_type, proxy_host, proxy_port, noproxy_hosts, status) conns[id(conn)] = (conn, status) def main(argv): parser = argparse.ArgumentParser(description='simple http and socks5 proxy forward to http or socks5 uplink proxy') parser.add_argument('-b', dest='bind', default="0.0.0.0", help='local bind host (default: 0.0.0.0)') parser.add_argument('-p', dest='port', default=8088, type=int, help='local bind port (default: 8088)') parser.add_argument('-t', dest='timeout', default=7200, type=int, help='no read/write timeout (default: 7200)') parser.add_argument('-T', dest='proxy_type', default="http", choices=("http", "socks5"), help='proxy type (default: http)') parser.add_argument('-P', dest='proxy_host', default="127.0.0.1:8088", help='proxy host, accept format [proxy_host:proxy_port] (default: 127.0.0.1:8088)') parser.add_argument('-N', dest='noproxy_hosts', default="*", help='noproxy hosts, accept format [host,*host,host*] (default: *)') args = parser.parse_args(args=argv) config_signal() server = create_server((args.bind, args.port)) logging.info("listen server at %s:%d", args.bind, args.port) sevent.run(tcp_accept, server, args) if __name__ == '__main__': logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)1.1s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') try: main(sys.argv[1:]) sevent.instance().start() except KeyboardInterrupt: exit(0)
def main(argv): parser = argparse.ArgumentParser(description='tcp reverse port forward') parser.add_argument('-c', dest='is_client_mode', nargs='?', const=True, default=False, type=bool, help='is client mode (defualt: False)') parser.add_argument('-k', dest='key', default='', type=str, help='auth key (defualt: "")') parser.add_argument( '-b', dest='bind_host', default="0.0.0.0", help='server and client mode local bind host (default: 0.0.0.0)') parser.add_argument( '-p', dest='bind_port', default=0, type=int, help='server and client mode local bind port (default: 8089)') parser.add_argument( '-r', dest='listen_host', default="0.0.0.0", help='server mode reverse server listen host (default: 0.0.0.0)') parser.add_argument( '-l', dest='listen_port', default=8088, type=int, help='server mode reverse server listen port (default: 8088)') parser.add_argument( '-H', dest='connect_host', default="127.0.0.1", help= 'client mode reverse client connect server host (default: 127.0.0.1)') parser.add_argument( '-P', dest='connect_port', default=8088, type=int, help='client mode reverse client connect server port (default: 8088)') parser.add_argument( '-f', dest='forward_host', default="", help= 'server and client mode forward host , accept format [remote_host:remote_port] (default: )' ) parser.add_argument( '-T', dest='proxy_type', default="", choices=("raw", "http", "socks5", "redirect"), help='server and client mode local listen proxy type (default: raw)') parser.add_argument('-t', dest='timeout', default=7200, type=int, help='no read/write timeout (default: 7200)') args = parser.parse_args(args=argv) config_signal() if not args.forward_host: forward_address = None else: forward_info = args.forward_host.split(":") if len(forward_info) == 1: if not forward_info[0].isdigit(): forward_address = (forward_info[0], 8088) else: forward_address = ("127.0.0.1", int(forward_info[0])) else: forward_address = (forward_info[0], int(forward_info[1])) if not args.is_client_mode: remote_server = create_server((args.listen_host, args.listen_port)) local_server = create_server((args.bind_host, args.bind_port or 8089)) logging.info("listen %s %d -> %s:%d", args.bind_host, args.bind_port or 8089, args.listen_host, args.listen_port) sevent.instance().call_async( server_run_server, remote_server, forward_address if forward_address else ("127.0.0.1", 80), sevent.utils.ensure_bytes(args.key), args.proxy_type, conns, status, server_handle_remote_connect) sevent.instance().call_async(server_run_server, local_server, forward_address, sevent.utils.ensure_bytes(args.key), args.proxy_type, conns, status, server_handle_local_connect) else: if args.bind_port: local_server = create_server((args.bind_host, args.bind_port)) logging.info("listen %s %d", args.bind_host, args.bind_port) sevent.instance().call_async( client_run_server, local_server, (args.connect_host, args.connect_port), sevent.utils.ensure_bytes(args.key), args.proxy_type, conns) logging.info("connect %s:%d -> %s", args.connect_host, args.connect_port, forward_address) sevent.instance().call_async( client_run_connect, (args.connect_host, args.connect_port), forward_address if forward_address else ("127.0.0.1", 80), sevent.utils.ensure_bytes(args.key), conns, status) sevent.current().call_async(check_timeout, conns, status, args.timeout)
import sys import sevent loop = sevent.instance() def on_connection(s, conn): print 'on_connection' conn.on('data', on_data) conn.on('end', on_end) conn.on('close', on_close) conn.on('error', on_error) def on_data(s, data): print 'on_data' sys.stdout.write(data) s.write('HTTP/1.0 200 OK\r\nHost: 127.0.0.1\r\nConnection: Close\r\n\r\nHello world!\r\n') s.end() def on_end(s): print 'on_end' def on_close(s): print 'on_close' def on_error(s, e): print 'on_error'
import sys import sevent import logging logging.basicConfig(level=logging.DEBUG) loop = sevent.instance() def on_connect(s): print 'on_connect' s.write('GET / HTTP/1.0\r\nHost: www.baidu.com\r\nConnection: Close\r\n\r\n') def on_data(s, data): print 'on_data' sys.stdout.write(data) def on_end(s): print 'on_end' def on_close(s): print 'on_close' global loop loop.stop() def on_error(s, e): print 'on_error'