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-08-06 21:20:35 +00:00
|
|
|
print('Connecting to telegram...')
|
2017-01-10 17:07:59 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-01-11 08:42:55 +00:00
|
|
|
try:
|
2017-08-06 21:20:35 +00:00
|
|
|
contacts = [c for c in sender.dialog_list()]
|
|
|
|
for i, user in enumerate(contacts):
|
|
|
|
print(unicode(i) + ': \t' + unicode(user['print_name']))
|
2017-01-11 08:42:55 +00:00
|
|
|
except ConnectionError:
|
2017-08-06 21:20:35 +00:00
|
|
|
print('Could not connect to telegram-cli. Start it by issuing "telegram-cli --json -P 4458" in a separate console.')
|
|
|
|
sys.exit(1)
|
2017-01-10 17:07:59 +00:00
|
|
|
|
2017-01-11 08:42:55 +00:00
|
|
|
# Ask user to choose contact
|
2017-08-06 21:20:35 +00:00
|
|
|
i = int(input('Telegram online, please enter contact to connect to (by number): '))
|
2017-01-10 17:07:59 +00:00
|
|
|
|
2017-01-11 08:42:55 +00:00
|
|
|
# Print username
|
2017-08-06 21:20:35 +00:00
|
|
|
try:
|
|
|
|
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!')
|
|
|
|
sys.exit(1)
|
2017-01-10 17:07:59 +00:00
|
|
|
|
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')
|
|
|
|
|
2017-08-06 21:20:35 +00:00
|
|
|
print(tun.name + ' has been created, information follows:')
|
2017-01-10 17:07:59 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-08-06 21:20:35 +00:00
|
|
|
print('Address: ' + tun.addr)
|
|
|
|
print('Dest.-Address: ' + tun.dstaddr)
|
|
|
|
print('Netmask: ' + tun.netmask)
|
|
|
|
print('MTU: ' + str(tun.mtu))
|
2017-01-10 17:07:59 +00:00
|
|
|
|
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 10:49:21 +00:00
|
|
|
# Init stats
|
|
|
|
sent = 0
|
|
|
|
received = 0
|
|
|
|
|
|
|
|
|
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():
|
|
|
|
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
|
2017-01-11 10:49:21 +00:00
|
|
|
global received
|
2017-08-06 21:20:35 +00:00
|
|
|
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)
|
|
|
|
tun.write(data)
|
2017-01-10 17:07:59 +00:00
|
|
|
|
|
|
|
|
2017-08-06 21:20:35 +00:00
|
|
|
print('TUN is up')
|
2017-01-10 17:07:59 +00:00
|
|
|
|
|
|
|
|
2017-08-06 21:20:35 +00:00
|
|
|
# Create the receive thread via our helper method
|
|
|
|
thread = threading.Thread(target=main_loop_starter)
|
2017-01-10 17:07:59 +00:00
|
|
|
|
2017-08-06 21:20:35 +00:00
|
|
|
# Start the thread for receiving
|
|
|
|
print('Connecting to peer...')
|
|
|
|
thread.start()
|
|
|
|
print('Connected! Sending Invitation!')
|
2017-01-10 17:07:59 +00:00
|
|
|
|
2017-08-06 21:20:35 +00:00
|
|
|
# Send the invitation message
|
|
|
|
sender.msg(username, unicode('Hello, I would like to establish a Layer 3 Tunnel with you! -teletun'))
|
2017-01-10 17:07:59 +00:00
|
|
|
|
2017-08-06 21:20:35 +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
|
|
|
|
buf = tun.read(tun.mtu)
|
|
|
|
data = base64.b64encode(buf)
|
|
|
|
sent += len(data)
|
|
|
|
sender.msg(username, unicode(data))
|
2017-01-10 17:07:59 +00:00
|
|
|
|
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()
|
|
|
|
|
2017-08-06 21:20:35 +00:00
|
|
|
print('Bytes sent via Telegram: ' + str(sent))
|
|
|
|
print('Bytes received via Telegram: ' + str(received))
|
2017-01-11 10:49:21 +00:00
|
|
|
|
2017-08-06 21:20:35 +00:00
|
|
|
print('~~ Bye bye! ~~')
|
2017-01-11 10:17:41 +00:00
|
|
|
|
|
|
|
# Literally Overkill
|
|
|
|
|
|
|
|
current_process = psutil.Process()
|
|
|
|
os.kill(current_process.pid, signal.SIGKILL)
|