mirror of
https://github.com/PiMaker/Teletun.git
synced 2025-03-09 15:40:14 +00:00
Initial commit
This commit is contained in:
parent
8cd1cc5166
commit
bfebb87e3e
2 changed files with 170 additions and 0 deletions
39
README
Normal file
39
README
Normal file
|
@ -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.
|
131
teletun.py
Executable file
131
teletun.py
Executable file
|
@ -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)
|
Loading…
Add table
Add a link
Reference in a new issue