def handle(sock, addr): print 'new client:', addr socket = pwrtls.wrap_socket(sock, server_side=True, **state) socket.do_handshake() print 'remote longpub', socket.remote_longpub.encode('hex') forward(socket, sys.stdout) print 'client gone', addr socket.close()
def main(): parser = argparse.ArgumentParser(description='pwrcall nacl test.') parser.add_argument('action', help='connect/listen', choices=['connect', 'listen', 'c', 'l']) parser.add_argument('--state', dest='state', help='path to state file', default='pwr.state') parser.add_argument('--sock', dest='sock', help='where to connect / what to bind', required=True) parser.add_argument('--rpub', dest='rpub', help='remove public key for verification') args = parser.parse_args() state = pwrtls.state_file(args.state) fdnonblock(sys.stdin.fileno()) fdnonblock(sys.stdout.fileno()) if args.rpub: args.rpub = args.rpub.decode('hex') if args.action[0] == 'c': ip, port = args.sock.split(':', 1) port = int(port) socket = gevent.socket.create_connection((ip, port)) socket = pwrtls.wrap_socket(socket, **state) socket.do_handshake() print 'remote longpub', socket.remote_longpub.encode('hex') g1 = gevent.spawn(forward, sys.stdin, socket) forward(socket, sys.stdout) print 'server gone' socket.close() elif args.action[0] == 'l': if ':' in args.sock: ip, port = args.sock.split(':', 1) else: ip, port = '0.0.0.0', args.sock port = int(port) lsocket = gevent.socket.socket() lsocket.setsockopt(gevent.socket.SOL_SOCKET, gevent.socket.SO_REUSEADDR, 1) lsocket.bind((ip, port)) lsocket.listen(1) socket, addr = lsocket.accept() lsocket.close() print 'new client:', addr socket = pwrtls.wrap_socket(socket, server_side=True, **state) socket.do_handshake() print 'remote longpub', socket.remote_longpub.encode('hex') g1 = gevent.spawn(forward, sys.stdin, socket) forward(socket, sys.stdout) print 'client gone', addr socket.close() elif args.action[0] == 's': if ':' in args.sock: ip, port = args.sock.split(':', 1) else: ip, port = '0.0.0.0', args.sock port = int(port) def handle(sock, addr): print 'new client:', addr socket = pwrtls.wrap_socket(sock, server_side=True, **state) socket.do_handshake() print 'remote longpub', socket.remote_longpub.encode('hex') forward(socket, sys.stdout) print 'client gone', addr socket.close() server = gevent.server.StreamServer((ip, port), handle) server.serve_forever() return 0