mirror of
https://github.com/PiMaker/Teletun.git
synced 2025-03-09 15:40:14 +00:00
Merge pull request #1 from Akiiino/master
Add small changes, make code cleaner
This commit is contained in:
commit
d4f8a9872c
1 changed files with 50 additions and 63 deletions
113
teletun.py
113
teletun.py
|
@ -16,42 +16,37 @@ import signal
|
||||||
|
|
||||||
|
|
||||||
# Connect to telegram
|
# 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
|
# Retrieve contact list
|
||||||
contacts = {}
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
counter = 0
|
contacts = [c for c in sender.dialog_list()]
|
||||||
for c in sender.dialog_list():
|
for i, user in enumerate(contacts):
|
||||||
counter += 1
|
print(unicode(i) + ': \t' + unicode(user['print_name']))
|
||||||
contacts[counter] = c
|
|
||||||
print unicode(counter) + ': \t' + unicode(c['print_name'])
|
|
||||||
except ConnectionError:
|
except ConnectionError:
|
||||||
print 'Could not connect to telegram-cli. Start it by issuing "telegram-cli --json -P 4458" in a separate console.'
|
print('Could not connect to telegram-cli. Start it by issuing "telegram-cli --json -P 4458" in a separate console.')
|
||||||
exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Ask user to choose contact
|
# Ask user to choose contact
|
||||||
i = input('Telegram online, please enter contact to connect to (by number): ')
|
i = int(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)
|
|
||||||
|
|
||||||
|
|
||||||
# Print username
|
# Print username
|
||||||
username = unicode(contacts[i]['print_name'])
|
try:
|
||||||
peer_id = contacts[i]['peer_id']
|
username = unicode(contacts[i]['print_name'])
|
||||||
print 'Connecting to partner: ' + username
|
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)
|
||||||
|
|
||||||
# Create TUN device for network capture and injections
|
# 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
|
# Set IP address based on --server flag
|
||||||
|
@ -65,10 +60,10 @@ else:
|
||||||
tun.netmask = '255.255.255.0'
|
tun.netmask = '255.255.255.0'
|
||||||
tun.mtu = 1500
|
tun.mtu = 1500
|
||||||
|
|
||||||
print 'Address: ' + tun.addr
|
print('Address: ' + tun.addr)
|
||||||
print 'Dest.-Address: ' + tun.dstaddr
|
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
|
# Start TUN device
|
||||||
|
@ -83,7 +78,6 @@ received = 0
|
||||||
|
|
||||||
# Helper function that can be executed in a thread
|
# Helper function that can be executed in a thread
|
||||||
def main_loop_starter():
|
def main_loop_starter():
|
||||||
pass
|
|
||||||
receiver.start()
|
receiver.start()
|
||||||
# Start the receive loop
|
# Start the receive loop
|
||||||
receiver.message(main_loop())
|
receiver.message(main_loop())
|
||||||
|
@ -93,60 +87,53 @@ def main_loop_starter():
|
||||||
def main_loop():
|
def main_loop():
|
||||||
global up
|
global up
|
||||||
global received
|
global received
|
||||||
try:
|
while up:
|
||||||
while up:
|
# Receive message from telegram, this includes ALL messages
|
||||||
# Receive message from telegram, this includes ALL messages
|
msg = (yield)
|
||||||
msg = (yield)
|
# Check if it is an actual "message" message and if the sender is our peer
|
||||||
# Check if it is an actual "message" message and if the sender is our peer
|
if (
|
||||||
if msg is not None and msg['event'] == unicode('message')\
|
msg is not None and
|
||||||
and not msg['own'] and msg['sender']['peer_id'] == peer_id:
|
msg['event'] == unicode('message') and
|
||||||
try:
|
not msg['own'] and
|
||||||
# Decode data and write it to the tunnel
|
msg['sender']['peer_id'] == peer_id
|
||||||
data = base64.b64decode(msg.text)
|
):
|
||||||
received += len(data)
|
# Decode data and write it to the tunnel
|
||||||
tun.write(data)
|
data = base64.b64decode(msg.text)
|
||||||
except:
|
received += len(data)
|
||||||
print 'Invalid Base64-Data: ' + msg.text
|
tun.write(data)
|
||||||
except:
|
|
||||||
print '!!! Receiver crashed!'
|
|
||||||
|
|
||||||
|
|
||||||
print 'TUN is up'
|
print('TUN is up')
|
||||||
|
|
||||||
|
|
||||||
# Create the receive thread via our helper method
|
# Create the receive thread via our helper method
|
||||||
thread = threading.Thread(target=main_loop_starter)
|
thread = threading.Thread(target=main_loop_starter)
|
||||||
|
|
||||||
try:
|
# Start the thread for receiving
|
||||||
|
print('Connecting to peer...')
|
||||||
|
thread.start()
|
||||||
|
print('Connected! Sending Invitation!')
|
||||||
|
|
||||||
# Start the thread for receiving
|
# Send the invitation message
|
||||||
print 'Connecting to peer...'
|
sender.msg(username, unicode('Hello, I would like to establish a Layer 3 Tunnel with you! -teletun'))
|
||||||
thread.start()
|
|
||||||
print 'Connected! Sending Invitation!'
|
|
||||||
|
|
||||||
# Send the invitation message
|
while up:
|
||||||
sender.msg(username, unicode('Hello, I would like to establish a Layer 3 Tunnel with you! -teletun'))
|
# 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
|
||||||
while up:
|
buf = tun.read(tun.mtu)
|
||||||
# Continually read from the tunnel and write data to telegram in base64
|
data = base64.b64encode(buf)
|
||||||
# TODO: Telegram supports unicode, base64 can probably be replaced for something less overhead-inducing
|
sent += len(data)
|
||||||
buf = tun.read(tun.mtu)
|
sender.msg(username, unicode(data))
|
||||||
data = base64.b64encode(buf)
|
|
||||||
sent += len(data)
|
|
||||||
sender.msg(username, unicode(data))
|
|
||||||
|
|
||||||
except:
|
|
||||||
print '(Interrupt/Crash)'
|
|
||||||
|
|
||||||
# Cleanup and stop application
|
# Cleanup and stop application
|
||||||
up = False
|
up = False
|
||||||
tun.down()
|
tun.down()
|
||||||
receiver.stop()
|
receiver.stop()
|
||||||
|
|
||||||
print 'Bytes sent via Telegram: ' + str(sent)
|
print('Bytes sent via Telegram: ' + str(sent))
|
||||||
print 'Bytes received via Telegram: ' + str(received)
|
print('Bytes received via Telegram: ' + str(received))
|
||||||
|
|
||||||
print '~~ Bye bye! ~~'
|
print('~~ Bye bye! ~~')
|
||||||
|
|
||||||
# Literally Overkill
|
# Literally Overkill
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue