mirror of
https://github.com/PiMaker/Teletun.git
synced 2025-03-09 15:40:14 +00:00
Code cleanup
This commit is contained in:
parent
bfebb87e3e
commit
21847df5af
2 changed files with 37 additions and 32 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -87,3 +87,6 @@ ENV/
|
||||||
|
|
||||||
# Rope project settings
|
# Rope project settings
|
||||||
.ropeproject
|
.ropeproject
|
||||||
|
|
||||||
|
# JetBrains
|
||||||
|
.idea
|
||||||
|
|
66
teletun.py
66
teletun.py
|
@ -1,31 +1,37 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# (C) Stefan Reiter 2017
|
||||||
|
|
||||||
from pytun import TunTapDevice
|
from pytun import TunTapDevice
|
||||||
from pytg.sender import Sender
|
from pytg.sender import Sender
|
||||||
from pytg.receiver import Receiver
|
from pytg.receiver import Receiver
|
||||||
from pytg.utils import coroutine
|
from pytg.utils import coroutine
|
||||||
|
from pytg.exceptions import ConnectionError
|
||||||
import base64
|
import base64
|
||||||
import threading
|
import threading
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
|
||||||
import os
|
|
||||||
import signal
|
|
||||||
import psutil
|
|
||||||
|
|
||||||
|
|
||||||
|
# Connect to telegram
|
||||||
print 'Connecting to telegram...'
|
print 'Connecting to telegram...'
|
||||||
|
|
||||||
receiver = Receiver(host="localhost", port=4458)
|
receiver = Receiver(host="localhost", port=4458)
|
||||||
sender = Sender(host="localhost", port=4458)
|
sender = Sender(host="localhost", port=4458)
|
||||||
|
|
||||||
|
# Retrieve contact list
|
||||||
contacts = {}
|
contacts = {}
|
||||||
|
|
||||||
counter = 0
|
try:
|
||||||
for c in sender.dialog_list():
|
counter = 0
|
||||||
counter += 1
|
for c in sender.dialog_list():
|
||||||
contacts[counter] = c
|
counter += 1
|
||||||
print unicode(counter) + ': \t' + unicode(c['print_name'])
|
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)
|
||||||
|
|
||||||
|
# Ask user to choose contact
|
||||||
i = input('Telegram online, please enter contact to connect to (by number): ')
|
i = input('Telegram online, please enter contact to connect to (by number): ')
|
||||||
|
|
||||||
if i not in contacts:
|
if i not in contacts:
|
||||||
|
@ -33,15 +39,18 @@ if i not in contacts:
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
# Print username
|
||||||
username = unicode(contacts[i]['print_name'])
|
username = unicode(contacts[i]['print_name'])
|
||||||
print 'Connecting to partner: ' + username
|
print 'Connecting to partner: ' + username
|
||||||
|
|
||||||
|
|
||||||
|
# Create TUN device for network capture and injections
|
||||||
tun = TunTapDevice(name='teletun-device')
|
tun = TunTapDevice(name='teletun-device')
|
||||||
|
|
||||||
print tun.name + ' has been created, information follows:'
|
print tun.name + ' has been created, information follows:'
|
||||||
|
|
||||||
|
|
||||||
|
# Set IP address based on --server flag
|
||||||
if '--server' in sys.argv:
|
if '--server' in sys.argv:
|
||||||
tun.addr = '10.8.0.1'
|
tun.addr = '10.8.0.1'
|
||||||
tun.dstaddr = '10.8.0.2'
|
tun.dstaddr = '10.8.0.2'
|
||||||
|
@ -57,13 +66,17 @@ print 'Dest.-Address: ' + tun.dstaddr
|
||||||
print 'Netmask: ' + tun.netmask
|
print 'Netmask: ' + tun.netmask
|
||||||
print 'MTU: ' + str(tun.mtu)
|
print 'MTU: ' + str(tun.mtu)
|
||||||
|
|
||||||
|
|
||||||
|
# Start TUN device
|
||||||
tun.up()
|
tun.up()
|
||||||
up = True
|
up = True
|
||||||
|
|
||||||
|
|
||||||
|
# Helper function that can be executed in a thread
|
||||||
def main_loop_starter():
|
def main_loop_starter():
|
||||||
pass
|
pass
|
||||||
receiver.start()
|
receiver.start()
|
||||||
|
# Start the receive loop
|
||||||
receiver.message(main_loop())
|
receiver.message(main_loop())
|
||||||
|
|
||||||
|
|
||||||
|
@ -72,60 +85,49 @@ def main_loop():
|
||||||
global up
|
global up
|
||||||
try:
|
try:
|
||||||
while up:
|
while up:
|
||||||
|
# Receive message from telegram, this includes ALL messages
|
||||||
msg = (yield)
|
msg = (yield)
|
||||||
if msg is not None and msg['event'] == unicode('message') and not msg['own']:
|
# 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['from']['print_name'] == username:
|
||||||
try:
|
try:
|
||||||
|
# Decode data and write it to the tunnel
|
||||||
data = base64.b64decode(msg.text)
|
data = base64.b64decode(msg.text)
|
||||||
tun.write(data)
|
tun.write(data)
|
||||||
except:
|
except:
|
||||||
print msg.text
|
print msg.text
|
||||||
except:
|
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 'Receiver stopped'
|
||||||
|
|
||||||
|
|
||||||
print 'TUN is up'
|
print 'TUN is up'
|
||||||
|
|
||||||
|
|
||||||
|
# Create the receive thread via our helper method
|
||||||
thread = threading.Thread(target=main_loop_starter)
|
thread = threading.Thread(target=main_loop_starter)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
|
# Start the thread for receiving
|
||||||
print 'Connecting to peer...'
|
print 'Connecting to peer...'
|
||||||
thread.start()
|
thread.start()
|
||||||
print 'Connected! Sending Invitation!'
|
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'))
|
sender.msg(username, unicode('Hello, I would like to establish a Layer 3 Tunnel with you! -teletun'))
|
||||||
|
|
||||||
while True:
|
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)
|
buf = tun.read(tun.mtu)
|
||||||
sender.msg(username, unicode(base64.b64encode(buf)))
|
sender.msg(username, unicode(base64.b64encode(buf)))
|
||||||
|
|
||||||
except:
|
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...'
|
print 'Exiting...'
|
||||||
|
|
||||||
|
# Cleanup and stop application
|
||||||
up = False
|
up = False
|
||||||
tun.down()
|
tun.down()
|
||||||
|
|
||||||
receiver.stop()
|
receiver.stop()
|
||||||
|
|
||||||
print 'Bye bye!'
|
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