From bfebb87e3ed64481485bbe321bbf1856e4985d08 Mon Sep 17 00:00:00 2001 From: Stefan Reiter Date: Tue, 10 Jan 2017 18:07:59 +0100 Subject: [PATCH] Initial commit --- README | 39 ++++++++++++++++ teletun.py | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 README create mode 100755 teletun.py diff --git a/README b/README new file mode 100644 index 0000000..370e176 --- /dev/null +++ b/README @@ -0,0 +1,39 @@ +# Teletun - IP over Telegram + +Have you ever wanted to transfer IP traffic over an instant messenger? Wanted to test the capabilities of Telegram? + +What do you mean by "no", of course you have! So here you go, take it and *have fun*! + +# Install instructions + +Install the [telegram-cli](https://github.com/vysheng/tg) package (using snap for example). + +Run an instance of telegram-cli using the following line: + +``` +telegram-cli --json -P 4458 +``` + +(Make sure that `-P` is capital, half an hour of debugging was wasted on that already) + +Install dependencies: + +``` +pip install python-pytun pytg +``` + +Download the python script. Run the python script. Pray to the gods for mercy. + +# Performance + +Not that it really matters (you didn't think you're going to use this in anything serious, did you?), but actually, performance is not too bad. + +Bandwidth is quite limited of course, but you can get a Ping as low as 100-150 ms. I guess they didn't lie when they called it *Instant* Messenger. + +# MIT License + +(C) Stefan Reiter 2017 + +Full license text can be found in the file called LICENSE in this Repositories root. + +But please do me the favor and don't ever use this thing. It's quite horrible. diff --git a/teletun.py b/teletun.py new file mode 100755 index 0000000..704572d --- /dev/null +++ b/teletun.py @@ -0,0 +1,131 @@ +#!/usr/bin/env python + +from pytun import TunTapDevice +from pytg.sender import Sender +from pytg.receiver import Receiver +from pytg.utils import coroutine +import base64 +import threading +import sys +import traceback +import os +import signal +import psutil + + +print 'Connecting to telegram...' + +receiver = Receiver(host="localhost", port=4458) +sender = Sender(host="localhost", port=4458) + +contacts = {} + +counter = 0 +for c in sender.dialog_list(): + counter += 1 + contacts[counter] = c + print unicode(counter) + ': \t' + unicode(c['print_name']) + +i = input('Telegram online, please enter contact to connect to (by number): ') + +if i not in contacts: + print 'Please enter a number in the above range!' + exit(1) + + +username = unicode(contacts[i]['print_name']) +print 'Connecting to partner: ' + username + + +tun = TunTapDevice(name='teletun-device') + +print tun.name + ' has been created, information follows:' + + +if '--server' in sys.argv: + tun.addr = '10.8.0.1' + tun.dstaddr = '10.8.0.2' +else: + tun.addr = '10.8.0.2' + tun.dstaddr = '10.8.0.1' + +tun.netmask = '255.255.255.0' +tun.mtu = 1500 + +print 'Address: ' + tun.addr +print 'Dest.-Address: ' + tun.dstaddr +print 'Netmask: ' + tun.netmask +print 'MTU: ' + str(tun.mtu) + +tun.up() +up = True + + +def main_loop_starter(): + pass + receiver.start() + receiver.message(main_loop()) + + +@coroutine +def main_loop(): + global up + try: + while up: + msg = (yield) + if msg is not None and msg['event'] == unicode('message') and not msg['own']: + try: + data = base64.b64decode(msg.text) + tun.write(data) + except: + print msg.text + except: + exc_type, exc_value, exc_traceback = sys.exc_info() + lines = traceback.format_exception(exc_type, exc_value, exc_traceback) + print ''.join('!! ' + line for line in lines) + print 'Receiver stopped' + + +print 'TUN is up' + + +thread = threading.Thread(target=main_loop_starter) + +try: + + print 'Connecting to peer...' + thread.start() + print 'Connected! Sending Invitation!' + + sender.msg(username, unicode('Hello, I would like to establish a Layer 3 Tunnel with you! -teletun')) + + while True: + buf = tun.read(tun.mtu) + sender.msg(username, unicode(base64.b64encode(buf))) + +except: + exc_type, exc_value, exc_traceback = sys.exc_info() + lines = traceback.format_exception(exc_type, exc_value, exc_traceback) + print ''.join('!! ' + line for line in lines) + print 'Exiting...' + +up = False +tun.down() + +receiver.stop() + +print 'Bye bye!' + + +# Literally Overkill + +current_process = psutil.Process() +children = current_process.children(recursive=True) +for child in children: + os.kill(child.pid, signal.SIGKILL) + +children = current_process.children(recursive=True) +for child in children: + os.kill(child.pid, signal.SIGKILL) + +os.kill(current_process.pid, signal.SIGKILL)