def main(argv): if len(argv) != 7: print >> sys.stderr, "Usage: ./vpntestclient.py <serv-addr> <serv-port> <chan-port-my> <chan-port-other> <client-id-filename> <server-id-filename>" sys.exit(1) serv_addr = argv[1] serv_port = int(argv[2]) myport = int(argv[3]) otherport = int(argv[4]) client_id_filename = argv[5] server_id_filename = argv[6] client_id = rsaid.from_file(client_id_filename) server_id = rsaid.from_file(server_id_filename) serv_chan = packetchannel.SecureChannelClient.connect(serv_addr, serv_port, client_id, server_id) data_chan = packetchannel.UDPChannel("localhost", myport, "localhost", otherport) try: while True: ready_chans = packetchannel.select([serv_chan, data_chan]) if serv_chan in ready_chans: pkt = serv_chan.recv() data_chan.send(pkt) if data_chan in ready_chans: pkt = data_chan.recv() serv_chan.send(pkt) except: serv_chan.close() data_chan.close()
def main(argv): if len(argv) != 5: print >> sys.stderr, "Usage: ./vpnserver.py <serv-addr> <serv-port> <server-id-filename> <clients-id-filename>" sys.exit(1) serv_addr = argv[1] serv_port = int(argv[2]) server_id_filename = argv[3] client_id_filenames = argv[4] server_id = rsaid.from_file(server_id_filename) client_ids = set(rsaid.from_file(client_id_filenames)) server = packetchannel.SecureChannelServer(serv_addr, serv_port, server_id, client_ids) clients = set() routing_unit = HubUnit() try: while True: ready_comms = packetchannel.select(list(clients) + [server]) for c in ready_comms: if c == server: print "new vpn connection accepted!" client = server.accept() clients.add(client) routing_unit.attach_client(client) else: try: pkt = c.recv() dst = routing_unit.route_packet(c, 0, 0) for d in dst: d.send(pkt) except packetchannel.ChannelClosedException, e: print "vpn connection closed!" routing_unit.detach_client(c) clients.remove(c) c.close() except KeyboardInterrupt: pass server.close() for c in clients: c.close()