user.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from connection.broadcast import BroadcastClient
  2. from connection.socket import SocketClient
  3. import time
  4. import config as cfg
  5. from .userParser import UserParser
  6. import secrets
  7. from Utils import *
  8. import requests
  9. from tls.decrypt import AES_GCM
  10. from Utils import *
  11. import threading
  12. import sys
  13. class User:
  14. def __init__(self, data, server, broadcast, broadcastPort):
  15. time.sleep(1)
  16. print("user")
  17. self.onlineOracles = []
  18. self.id = "PADRA"
  19. self.server = server
  20. self.data = data
  21. self.dataLen = len(data) // 2
  22. self.parser = UserParser(self)
  23. self.broadcast = BroadcastClient(
  24. broadcast=broadcast,
  25. broadcastPort=broadcastPort,
  26. parser=self.parser,
  27. id=self.id,
  28. )
  29. self.keyCryptor = secrets.token_hex(16)
  30. self.keyConnector = secrets.token_hex(16)
  31. self.cryData = None
  32. self.conData = None
  33. self.doneThread = threading.Thread(target=self.done)
  34. def run(self):
  35. self.broadcast.send(
  36. self.parser.requestOracleBroadcast(self.id), self.broadcast.addr
  37. )
  38. while len(self.onlineOracles) < cfg.numberOfOracles:
  39. l = len(self.onlineOracles)
  40. yBackPrint(
  41. "Warning",
  42. "We need min {cfg.numberOfOracles} Oracles. Waiting for mor Oracle. we have now {l} online Oracles",
  43. )
  44. time.sleep(0.5)
  45. self.broadcast.send(
  46. self.parser.requestOracleBroadcast(self.id), self.broadcast.addr
  47. )
  48. gBackPrint(
  49. "now we have min {cfg.numberOfOracles} Oracles. List of All Online Oracles",
  50. self.onlineOracles,
  51. )
  52. worker = []
  53. # Socket creation
  54. for i in range(cfg.numberOfOracles):
  55. worker.append(
  56. SocketClient(
  57. "127.0.0.1", int(self.onlineOracles[i][1]), self.parser, None
  58. )
  59. )
  60. # send rule to each oracle
  61. for i in range(cfg.numberOfOracles):
  62. data = self.parser.socketConnecting(i)
  63. worker[i].id = i
  64. worker[i].send(data)
  65. time.sleep(1)
  66. worker[0].send(
  67. self.parser.oracleInitialConnector(
  68. self.server,
  69. worker[1].Host,
  70. worker[2].Host,
  71. self.dataLen,
  72. self.keyConnector,
  73. )
  74. )
  75. worker[1].send(
  76. self.parser.oracleInitialCryptor(
  77. (worker[0].Host, worker[0].port + 1),
  78. self.server,
  79. self.dataLen,
  80. self.keyCryptor,
  81. )
  82. )
  83. worker[2].send(
  84. self.parser.oracleInitialBearer(
  85. (worker[0].Host, worker[0].port + 1), self.data
  86. )
  87. )
  88. def done(self):
  89. while not( self.conData and self.cryData):
  90. time.sleep(1)
  91. cry = self.parser.toJSON(self.cryData)
  92. key= cry["key"]
  93. iv= cry["iv"]
  94. con = self.parser.toJSON(self.conData)
  95. cipher= con["cipher"]
  96. dec = AES_GCM(int(key, 16),int(iv, 16))
  97. p = dec.decrypt(bytes.fromhex(cipher))
  98. gPrint("DATA IS",p)
  99. sys.exit()
  100. def cryptorDone(self,data):
  101. gPrint("cryptorDone", data["data"])
  102. d = data["data"]
  103. r = requests.get(d)
  104. self.cryData = self.parser.decrypt(self.keyCryptor,r.text)
  105. # self.cryData = r.text
  106. print(self.cryData)
  107. self.doneThread.start()
  108. def connectorDone(self,data):
  109. gPrint("connectorDone", data["data"])
  110. d = data["data"]
  111. r = requests.get(d)
  112. self.conData = self.parser.decrypt(self.keyConnector,r.text)
  113. # self.conData = r.text
  114. print(self.conData)
  115. # TODO response from Oracle in decrypt with
  116. """ if __name__ == '__main__':
  117. master_key = 0xb9b8887ee 6e1e8b78e2aaa2a0c522c13
  118. iv = 0xacf448fa5d3c87b08f549c5e
  119. data = b"\x00"
  120. my_gcm = AES_GCM(master_key, iv)
  121. my_gcm.decrypt(data)
  122. """