def handle(self): logger.info("client connected from %s:%i" % self.client_address) data = self.request.recv(len(MAGIC_WORD)) if data != MAGIC_WORD: logger.warning("bad magic word for %s:%i" % self.client_address) return logger.info("handshaked") self.request.send(MAGIC_WORD) proxy(tun.fd, self.request)
def main(args): tun = Tun(device=args.device, ip=args.ip, peer_ip=args.peer_ip) tun.open() sock = socket() server_ip = gethostbyname(args.server) logger.info("%s resolved to %s", args.server, server_ip) try: sock.connect((server_ip, args.port)) logger.info("connected to %s:%i" % (server_ip, args.port)) sock.send(MAGIC_WORD) data = sock.recv(len(MAGIC_WORD)) if data != MAGIC_WORD: logger.warning("Handshake failed") sys.exit(2) logger.info("Connection with %s:%i established" % (server_ip, args.port)) gateway = get_default_gateway() if args.down: atexit.register( on_down, args.down, server_ip=server_ip, restore_gateway=gateway if args.default_gateway else None) call(['route', 'delete', server_ip + '/32']) check_call(['route', 'add', server_ip + '/32', gateway]) if args.default_gateway: logger.info("set default gateway") call(['route', 'delete', 'default']) check_call(['route', 'add', 'default', args.peer_ip]) if args.up: logger.info("Run up script") check_call(args.up) proxy(tun.fd, sock) except KeyboardInterrupt: logger.warning("Stopped by user")
def main(args): tun = Tun(device=args.device, ip=args.ip, peer_ip=args.peer_ip) tun.open() sock = socket() server_ip = gethostbyname(args.server) logger.info("%s resolved to %s", args.server, server_ip) try: sock.connect((server_ip, args.port)) logger.info("connected to %s:%i" % (server_ip, args.port)) sock.send(MAGIC_WORD) data = sock.recv(len(MAGIC_WORD)) if data != MAGIC_WORD: logger.warning("Handshake failed") sys.exit(2) logger.info("Connection with %s:%i established" % (server_ip, args.port)) gateway = get_default_gateway() if args.down: atexit.register(on_down, args.down, server_ip=server_ip, restore_gateway=gateway if args.default_gateway else None) call(['route', 'delete', server_ip+'/32']) check_call(['route', 'add', server_ip+'/32', gateway]) if args.default_gateway: logger.info("set default gateway") call(['route', 'delete', 'default']) check_call(['route', 'add', 'default', args.peer_ip]) if args.up: logger.info("Run up script") check_call(args.up) proxy(tun.fd, sock) except KeyboardInterrupt: logger.warning("Stopped by user")