1
0
Fork 0
mirror of https://github.com/PiMaker/Teletun.git synced 2025-02-14 19:01:53 +00:00

Finally fixed the stupid thing

This commit is contained in:
Stefan Reiter 2017-01-11 11:49:21 +01:00
parent 93548f8a18
commit 9b9af64344

View file

@ -44,6 +44,7 @@ if i not in contacts:
# Print username # Print username
username = unicode(contacts[i]['print_name']) username = unicode(contacts[i]['print_name'])
peer_id = contacts[i]['peer_id']
print 'Connecting to partner: ' + username print 'Connecting to partner: ' + username
@ -75,6 +76,11 @@ tun.up()
up = True up = True
# Init stats
sent = 0
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 pass
@ -86,21 +92,23 @@ def main_loop_starter():
@coroutine @coroutine
def main_loop(): def main_loop():
global up global up
global received
try: 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 msg is not None and msg['event'] == unicode('message')\ if msg is not None and msg['event'] == unicode('message')\
and not msg['own'] and msg['sender']['name'] == username: and not msg['own'] and msg['sender']['peer_id'] == peer_id:
try: try:
# Decode data and write it to the tunnel # Decode data and write it to the tunnel
data = base64.b64decode(msg.text) data = base64.b64decode(msg.text)
received += len(data)
tun.write(data) tun.write(data)
except: except:
print msg.text print 'Invalid Base64-Data: ' + msg.text
except: except:
print 'Receiver stopped' print '!!! Receiver crashed!'
print 'TUN is up' print 'TUN is up'
@ -123,17 +131,22 @@ try:
# Continually read from the tunnel and write data to telegram in base64 # 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 # 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))) data = base64.b64encode(buf)
sent += len(data)
sender.msg(username, unicode(data))
except: except:
print 'Exiting...' print '(Interrupt/Crash)'
# Cleanup and stop application # Cleanup and stop application
up = False up = False
tun.down() tun.down()
receiver.stop() receiver.stop()
print 'Bye bye!' print 'Bytes sent via Telegram: ' + str(sent)
print 'Bytes received via Telegram: ' + str(received)
print '~~ Bye bye! ~~'
# Literally Overkill # Literally Overkill