user.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. class User:
  9. def __init__(self, data, server, broadcast, broadcastPort):
  10. time.sleep(1)
  11. print("user")
  12. self.onlineOracles = []
  13. self.id = "PADRA"
  14. self.server = server
  15. self.data = data
  16. self.dataLen = len(data) // 2
  17. self.parser = UserParser(self)
  18. self.broadcast = BroadcastClient(
  19. broadcast=broadcast,
  20. broadcastPort=broadcastPort,
  21. parser=self.parser,
  22. id=self.id,
  23. )
  24. self.keyCryptor = secrets.token_hex(16)
  25. self.keyConnector = secrets.token_hex(16)
  26. def run(self):
  27. self.broadcast.send(
  28. self.parser.requestOracleBroadcast(self.id), self.broadcast.addr
  29. )
  30. while len(self.onlineOracles) < cfg.numberOfOracles:
  31. l = len(self.onlineOracles)
  32. yBackPrint(
  33. "Warning",
  34. "We need min {cfg.numberOfOracles} Oracles. Waiting for mor Oracle. we have now {l} online Oracles",
  35. )
  36. time.sleep(0.5)
  37. self.broadcast.send(
  38. self.parser.requestOracleBroadcast(self.id), self.broadcast.addr
  39. )
  40. gBackPrint(
  41. "now we have min {cfg.numberOfOracles} Oracles. List of All Online Oracles",
  42. self.onlineOracles,
  43. )
  44. worker = []
  45. # Socket creation
  46. for i in range(cfg.numberOfOracles):
  47. worker.append(
  48. SocketClient(
  49. "127.0.0.1", int(self.onlineOracles[i][1]), self.parser, None
  50. )
  51. )
  52. # send rule to each oracle
  53. for i in range(cfg.numberOfOracles):
  54. data = self.parser.socketConnecting(i)
  55. worker[i].id = i
  56. worker[i].send(data)
  57. time.sleep(1)
  58. worker[0].send(
  59. self.parser.oracleInitialConnector(
  60. self.server,
  61. worker[1].Host,
  62. worker[2].Host,
  63. self.dataLen,
  64. self.keyConnector,
  65. )
  66. )
  67. worker[1].send(
  68. self.parser.oracleInitialCryptor(
  69. (worker[0].Host, worker[0].port + 1),
  70. self.server,
  71. self.dataLen,
  72. self.keyCryptor,
  73. )
  74. )
  75. worker[2].send(
  76. self.parser.oracleInitialBearer(
  77. (worker[0].Host, worker[0].port + 1), self.data
  78. )
  79. )
  80. # TODO response from Oracle in decrypt with
  81. """ if __name__ == '__main__':
  82. master_key = 0xb9b8887ee 6e1e8b78e2aaa2a0c522c13
  83. iv = 0xacf448fa5d3c87b08f549c5e
  84. data = b"\x00"
  85. my_gcm = AES_GCM(master_key, iv)
  86. my_gcm.decrypt(data)
  87. """