1
0
Fork 0
mirror of https://github.com/PiMaker/Teletun.git synced 2025-02-13 02:11:54 +00:00
Teletun/teletun.py

142 lines
3.4 KiB
Python
Raw Normal View History

2017-01-10 17:07:59 +00:00
#!/usr/bin/env python
2017-01-11 08:42:55 +00:00
# (C) Stefan Reiter 2017
2017-01-10 17:07:59 +00:00
from pytun import TunTapDevice
from pytg.sender import Sender
from pytg.receiver import Receiver
from pytg.utils import coroutine
2017-01-11 08:42:55 +00:00
from pytg.exceptions import ConnectionError
2017-01-10 17:07:59 +00:00
import base64
import threading
import sys
2017-01-11 10:17:41 +00:00
import psutil
import os
import signal
2017-01-10 17:07:59 +00:00
2017-01-11 08:42:55 +00:00
# Connect to telegram
2017-01-10 17:07:59 +00:00
print 'Connecting to telegram...'
receiver = Receiver(host="localhost", port=4458)
sender = Sender(host="localhost", port=4458)
2017-01-11 08:42:55 +00:00
# Retrieve contact list
2017-01-10 17:07:59 +00:00
contacts = {}
2017-01-11 08:42:55 +00:00
try:
counter = 0
for c in sender.dialog_list():
counter += 1
contacts[counter] = c
print unicode(counter) + ': \t' + unicode(c['print_name'])
except ConnectionError:
print 'Could not connect to telegram-cli. Start it by issuing "telegram-cli --json -P 4458" in a separate console.'
exit(1)
2017-01-10 17:07:59 +00:00
2017-01-11 08:42:55 +00:00
# Ask user to choose contact
2017-01-10 17:07:59 +00:00
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)
2017-01-11 08:42:55 +00:00
# Print username
2017-01-10 17:07:59 +00:00
username = unicode(contacts[i]['print_name'])
print 'Connecting to partner: ' + username
2017-01-11 08:42:55 +00:00
# Create TUN device for network capture and injections
2017-01-10 17:07:59 +00:00
tun = TunTapDevice(name='teletun-device')
print tun.name + ' has been created, information follows:'
2017-01-11 08:42:55 +00:00
# Set IP address based on --server flag
2017-01-10 17:07:59 +00:00
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)
2017-01-11 08:42:55 +00:00
# Start TUN device
2017-01-10 17:07:59 +00:00
tun.up()
up = True
2017-01-11 08:42:55 +00:00
# Helper function that can be executed in a thread
2017-01-10 17:07:59 +00:00
def main_loop_starter():
pass
receiver.start()
2017-01-11 08:42:55 +00:00
# Start the receive loop
2017-01-10 17:07:59 +00:00
receiver.message(main_loop())
@coroutine
def main_loop():
global up
try:
while up:
2017-01-11 08:42:55 +00:00
# Receive message from telegram, this includes ALL messages
2017-01-10 17:07:59 +00:00
msg = (yield)
2017-01-11 08:42:55 +00:00
# Check if it is an actual "message" message and if the sender is our peer
if msg is not None and msg['event'] == unicode('message')\
2017-01-11 10:17:41 +00:00
and not msg['own'] and msg['sender']['name'] == username:
2017-01-10 17:07:59 +00:00
try:
2017-01-11 08:42:55 +00:00
# Decode data and write it to the tunnel
2017-01-10 17:07:59 +00:00
data = base64.b64decode(msg.text)
tun.write(data)
except:
print msg.text
except:
print 'Receiver stopped'
print 'TUN is up'
2017-01-11 08:42:55 +00:00
# Create the receive thread via our helper method
2017-01-10 17:07:59 +00:00
thread = threading.Thread(target=main_loop_starter)
try:
2017-01-11 08:42:55 +00:00
# Start the thread for receiving
2017-01-10 17:07:59 +00:00
print 'Connecting to peer...'
thread.start()
print 'Connected! Sending Invitation!'
2017-01-11 08:42:55 +00:00
# Send the invitation message
2017-01-10 17:07:59 +00:00
sender.msg(username, unicode('Hello, I would like to establish a Layer 3 Tunnel with you! -teletun'))
2017-01-11 08:42:55 +00:00
while up:
# Continually read from the tunnel and write data to telegram in base64
# TODO: Telegram supports unicode, base64 can probably be replaced for something less overhead-inducing
2017-01-10 17:07:59 +00:00
buf = tun.read(tun.mtu)
sender.msg(username, unicode(base64.b64encode(buf)))
except:
print 'Exiting...'
2017-01-11 08:42:55 +00:00
# Cleanup and stop application
2017-01-10 17:07:59 +00:00
up = False
tun.down()
receiver.stop()
print 'Bye bye!'
2017-01-11 10:17:41 +00:00
# Literally Overkill
current_process = psutil.Process()
os.kill(current_process.pid, signal.SIGKILL)