Guest User

Untitled

a guest
Sep 23rd, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from pyrogram import Client
  3. from Crypto.Cipher import AES
  4. from base64 import b64encode, b64decode
  5. from pytun import TunTapDevice
  6. import threading
  7.  
  8. tun = TunTapDevice(name='teleproxy')
  9. tun.addr = '10.8.0.1'
  10. tun.dstaddr = '10.8.0.2'
  11. tun.netmask = '255.255.255.0'
  12. tun.mtu = 1500
  13. tun.up()
  14.  
  15. started = False
  16. api_id = 1
  17. api_hash = "sdfsdf"
  18. app = Client("proxy", api_id, api_hash)
  19.  
  20. peer_id = 2131233
  21. key = b'Sixteen byte key'
  22.  
  23. def send_message(data):
  24. print(1, data)
  25. cipher = AES.new(key, AES.MODE_EAX)
  26. nonce = cipher.nonce
  27. ciphertext, tag = cipher.encrypt_and_digest(data)
  28. text = nonce + tag + ciphertext
  29. del nonce, tag, ciphertext, data
  30. text = b64encode(text).decode()
  31. app.send_message(peer_id, text)
  32.  
  33. def listen_tunnel(tun):
  34. while True:
  35. read = tun.read(tun.mtu)
  36. print(0, read)
  37. send_thread = threading.Thread(target=send_message, args=(read, ))
  38. send_thread.start()
  39.  
  40. @app.on_message()
  41. async def hello(client, message):
  42. if message['chat']['id'] == peer_id and message['from_user']['id'] == peer_id:
  43. text = b64decode(message['text'])
  44. nonce = text[:16]
  45. tag = text[16:32]
  46. cipher = AES.new(key, AES.MODE_EAX, nonce=nonce)
  47. plaindata = cipher.decrypt(text[32:])
  48. print(plaindata)
  49.  
  50. try:
  51. cipher.verify(tag)
  52. print("The message is authentic:", plaindata)
  53. except ValueError:
  54. raise Exception("Key incorrect or message corrupted")
  55.  
  56. tun.write(plaindata)
  57.  
  58. tunnel = threading.Thread(target=listen_tunnel, args=(tun, ))
  59. tunnel.start()
  60. app.run()
Add Comment
Please, Sign In to add comment