Key.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from hub import methods, Global
  2. class Key(object):
  3. @classmethod
  4. def create_key(cls, **params):
  5. """
  6. 生成key
  7. """
  8. # --- check ---
  9. key_path = '/home/server/key'
  10. if methods.is_file(key_path):
  11. methods.remove_file(key_path)
  12. # --- get serial ---
  13. ssh = Global.SSHClient('172.18.0.1', 22, 'server', 'server')
  14. serial = ssh.run_command("cat /sys/firmware/devicetree/base/serial-number").strip('\x00')
  15. # --- generate salt ---
  16. salt = methods.now_ts()
  17. salt = methods.reverse(str(salt))
  18. salt = 0
  19. # --- generate key ---
  20. keys = methods.text_to_b64(f"{serial}|{salt}")
  21. # --- storage env_key ---
  22. methods.run_command(f"echo \"{keys}\" > {key_path}", callback=True)
  23. return dict(code=0, data=keys)
  24. @classmethod
  25. def verify_key(cls, **params):
  26. """
  27. 验证key
  28. """
  29. # --- check ---
  30. if not methods.is_file('/home/server/key'):
  31. return dict(code=1, is_verified=False)
  32. # --- get key ---
  33. keys = methods.read_text('/home/server/key')
  34. keys = methods.b64_to_text(keys)
  35. # --- check again ---
  36. if not keys:
  37. return dict(code=2, is_verified=False)
  38. if len(keys.split('|')) != 2:
  39. return dict(code=3, is_verified=False)
  40. serial_1, _ = keys.split('|')
  41. # --- check again ---
  42. ssh = Global.SSHClient('172.18.0.1', 22, 'server', 'server')
  43. serial_2 = ssh.run_command("cat /sys/firmware/devicetree/base/serial-number").strip('\x00')
  44. if serial_1 != serial_2:
  45. return dict(code=4, is_verified=False)
  46. return dict(code=0, is_verified=True)