def connectPTLS(self, host, port, statepath=None): logging.info('Connecting to, {0}:{1}'.format(host, port)) if not statepath: raise NodeException('PTLS needs statepath!') c = gevent.socket.create_connection((host, port)) c = pwrtls.wrap_socket(c, **pwrtls.state_file(statepath)) c.do_handshake() return self._new_conn(c, (host, port))
def connectPTLS(self, host, port, statepath=None): logging.info('Connecting to, {0}:{1}'.format(host, port)) if not statepath: raise NodeException('PTLS needs statepath!') c = gevent.socket.create_connection((host, port)) c = pwrtls.wrap_socket(c, **pwrtls.state_file(statepath)) c.do_handshake() return self._new_conn(c, (host, port))
def handle(socket, addr): socket = pwrtls.wrap_socket(socket, server_side=True, **pwrtls.state_file(statepath)) try: socket.do_handshake() except Exception as e: logging.warn('PTLS Client handshake failure. Closing') else: self._new_conn(socket, addr)
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
def handle(socket, addr): socket = pwrtls.wrap_socket(socket, server_side=True, **pwrtls.state_file(statepath)) try: socket.do_handshake() except Exception as e: logging.warn('PTLS Client handshake failure. Closing') else: self._new_conn(socket, addr)