def create_tuntap(self, name, address, mtu, channel): self.tap = tuntap.Tun(name=name) self.tap.open() #channel.on("message")(self.tap.fd.write) @channel.on("message") def on_message(message): self.tap.fd.write(message) def tun_reader(): data = self.tap.fd.read(self.tap.mtu) channel_state = self.channel.transport.transport.state if data and channel_state == "connected": channel.send(data) loop = asyncio.get_event_loop() loop.add_reader(self.tap.fd, tun_reader) self.tap.up() ip = IPRoute() index = ip.link_lookup(ifname=name)[0] ip.addr('add', index=index, address=address, mask=24) ip.link("set", index=index, mtu=mtu)
if __name__ == '__main__': parser = argparse.ArgumentParser(description='Data channels with copy-and-paste signaling') parser.add_argument('role', choices=['offer', 'answer']) parser.add_argument('--verbose', '-v', action='count') parser.add_argument('--persist', '-p', action='count') parser.add_argument('--mode', '-m', choices=['tap', 'tun'], default="tap") args = parser.parse_args() if args.verbose and args.verbose > 1: logging.basicConfig(level=logging.DEBUG) elif args.verbose: logging.basicConfig(level=logging.INFO) tap = tuntap.Tun(name="revpn-%s" % args.role, mode=args.mode, persist=args.persist) tap.open() tap.disconnected() tap.up() pc = create_pc() signaling = CopyAndPasteSignaling() if args.role == 'offer': coro = run_offer(pc, tap) else: coro = run_answer(pc, tap) # run event loop loop = asyncio.get_event_loop() done = loop.run_until_complete(coro)
tap.up() print('tap interface up') return done if __name__ == '__main__': parser = argparse.ArgumentParser(description='VPN over data channel') parser.add_argument('role', choices=['offer', 'answer']) parser.add_argument('--verbose', '-v', action='count') add_signaling_arguments(parser) args = parser.parse_args() if args.verbose: logging.basicConfig(level=logging.DEBUG) tap = tuntap.Tun(name="revpn-%s" % args.role) signaling = create_signaling(args) pc = create_pc() if args.role == 'offer': coro = run_offer(pc, signaling, tap) else: coro = run_answer(pc, signaling, tap) # run event loop loop = asyncio.get_event_loop() try: done = loop.run_until_complete(coro) loop.run_until_complete(done.wait()) except KeyboardInterrupt: pass