You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
5.2 KiB

  1. import requests
  2. import json
  3. import socket
  4. class GNUnetRest:
  5. def __init__(self, host="localhost", port="7776"):
  6. self.host = host
  7. self.port = port
  8. self.base_url = "http://{}:{}/".format(host, port)
  9. self.identity = self.base_url + "identity/"
  10. self.identity_name = self.identity + "name/"
  11. self.identity_hash = self.identity + "pubkey/"
  12. self.identity_subsystem = self.identity + "subsystem/"
  13. self.identity_namestore = self.identity_subsystem + "namestore/"
  14. self.namestore = self.base_url + "namestore/"
  15. self.gns = self.base_url + "gns/"
  16. def is_port_open(self):
  17. # check if port is open
  18. # not the best solution, it should better check if REST responds \
  19. # and not only if port is listening
  20. testsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  21. testlocation = (self.host, int(self.port))
  22. testresult = testsocket.connect_ex(testlocation)
  23. testsocket.close()
  24. if testresult == 0:
  25. return True
  26. else:
  27. return False
  28. # Identity Section
  29. def identity_list(self):
  30. """Returns a list of all identities or tuple in case of error."""
  31. r = requests.get(self.identity)
  32. return r.json() if r.ok else (False, r.reason)
  33. def fetch_identity_keyhash(self, identity_name):
  34. """Returns the hash of the public key of an identity by name."""
  35. r = requests.get(self.identity_hash + identity_name)
  36. return r.json()['name'] if r.ok else (False, r.reason)
  37. def fetch_identity_name(self, key_hash):
  38. """Returns the name of an identity specified by public key."""
  39. r = requests.get(self.identity_name + key_hash)
  40. return r.json()['pubkey'] if r.ok else (False, r.reason)
  41. def fetch_identity_namestore(self):
  42. """Returns the default identity of subsystem 'namestore'."""
  43. r = requests.get(self.identity_namestore)
  44. return r.json()['name'] if r.ok else (False, r.reason)
  45. def create_identity(self, identity_name):
  46. """Creates Identy, returns True if successful, tuple if not."""
  47. r = requests.post(self.identity, {"name": identity_name})
  48. return r.ok if r.ok else (False, r.reason)
  49. def change_identity_name(self, identity_name, new_identity_name):
  50. """Changes name of identity.
  51. Returns True if successful, tuple if not.
  52. """
  53. r = requests.put(
  54. self.identity_name + identity_name,
  55. {"newname": new_identity_name}
  56. )
  57. return True if r.ok else (False, r.reason)
  58. def delete_identity(self, identity_name):
  59. """Deletes Identity, returns True if successful, tuple if not."""
  60. r = requests.delete(self.identity_name + identity_name)
  61. return True if r.ok else (False, r.reason)
  62. # Namestore Section
  63. def set_namestore_identity(self, identity_name):
  64. """Sets the default Identity for subsystem 'namestore'.
  65. Returns True if successful tuple if not.
  66. """
  67. r = requests.put(
  68. self.identity_subsystem + identity_name,
  69. {"subsystem": "namestore"})
  70. return True if r.ok else (False, r.reason)
  71. def list_namestore_entries(self, zone):
  72. """Returns a list of all namestore entries."""
  73. r = requests.get(self.namestore + zone)
  74. return r.json() if r.ok else (False, r.reason)
  75. def add_namestore_entry(self, zone, record_name, value, record_type):
  76. """Adds a namestore entry, returns true if successful, tuple if not."""
  77. data = {
  78. 'value': value,
  79. 'record_type': record_type,
  80. 'record_name': record_name,
  81. "expiration_time": "1d",
  82. "private": False,
  83. "relative_expiration": False,
  84. "supplemental": False,
  85. "shadow": False,
  86. }
  87. r = requests.post(self.namestore + zone, data)
  88. return True if r.ok else (False, r.reason)
  89. def change_namestore_entry(self, zone, record_name, value, record_type):
  90. """Changes a namestore entry,
  91. returns true if successful, tuple if not.
  92. """
  93. data = {
  94. 'value': value,
  95. 'record_type': record_type,
  96. 'record_name': record_name,
  97. }
  98. r = requests.put(self.namestore + zone, data)
  99. return True if r.ok else (False, r.reason)
  100. def delete_namestore_entry(self, zone, record_name):
  101. """Deletes a record_namestore entry.
  102. Returns true if successful, tuple if not.
  103. """
  104. r = requests.delete(self.namestore + zone + "/" + record_name)
  105. return True if r.ok else (False, r.reason)
  106. # GNS section
  107. def list_gns_record(self, zone, record_name):
  108. """Returns a list of values and record types by record_name."""
  109. r = requests.get(self.gns + record_name + "." + zone)
  110. return r.json() if r.ok else (False, r.reason)
  111. def list_gns_record_type(self, zone, record_name, record_type):
  112. """Returns a list of values by record name and record type."""
  113. r = requests.get(
  114. self.gns + record_name + "." + zone + "?record_type=" + record_type
  115. )
  116. return r.json() if r.ok else (False, r.reason)