Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 142 lines (106 sloc) 3.44 KB
#!/usr/bin/env python
# (C) Stefan Reiter 2017
from pytun import TunTapDevice
from pytg.sender import Sender
from pytg.receiver import Receiver
from pytg.utils import coroutine
from pytg.exceptions import ConnectionError
import base64
import threading
import sys
import psutil
import os
import signal
# Connect to telegram
print('Connecting to telegram...')
receiver = Receiver(host="localhost", port=4458)
sender = Sender(host="localhost", port=4458)
# Retrieve contact list
contacts = [c for c in sender.dialog_list()]
for i, user in enumerate(contacts):
print(unicode(i) + ': \t' + unicode(user['print_name']))
except ConnectionError:
print('Could not connect to telegram-cli. Start it by issuing "telegram-cli --json -P 4458" in a separate console.')
# Ask user to choose contact
i = int(input('Telegram online, please enter contact to connect to (by number): '))
# Print username
username = unicode(contacts[i]['print_name'])
peer_id = contacts[i]['peer_id']
print('Connecting to partner: ' + username)
except IndexError:
print('Please enter a number in the above range!')
# Create TUN device for network capture and injections
tun = TunTapDevice(name='teletun-device')
print( + ' has been created, information follows:')
# Set IP address based on --server flag
if '--server' in sys.argv:
tun.addr = ''
tun.dstaddr = ''
tun.addr = ''
tun.dstaddr = ''
tun.netmask = ''
tun.mtu = 1500
print('Address: ' + tun.addr)
print('Dest.-Address: ' + tun.dstaddr)
print('Netmask: ' + tun.netmask)
print('MTU: ' + str(tun.mtu))
# Start TUN device
up = True
# Init stats
sent = 0
received = 0
# Helper function that can be executed in a thread
def main_loop_starter():
# Start the receive loop
def main_loop():
global up
global received
while up:
# Receive message from telegram, this includes ALL messages
msg = (yield)
# 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') and
not msg['own'] and
msg['sender']['peer_id'] == peer_id
# Decode data and write it to the tunnel
data = base64.b64decode(msg.text)
received += len(data)
print('TUN is up')
# Create the receive thread via our helper method
thread = threading.Thread(target=main_loop_starter)
# Start the thread for receiving
print('Connecting to peer...')
print('Connected! Sending Invitation!')
# Send the invitation message
sender.msg(username, unicode('Hello, I would like to establish a Layer 3 Tunnel with you! -teletun'))
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
buf =
data = base64.b64encode(buf)
sent += len(data)
sender.msg(username, unicode(data))
# Cleanup and stop application
up = False
print('Bytes sent via Telegram: ' + str(sent))
print('Bytes received via Telegram: ' + str(received))
print('~~ Bye bye! ~~')
# Literally Overkill
current_process = psutil.Process()
os.kill(, signal.SIGKILL)
You can’t perform that action at this time.