Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import json
  2. import logging
  3. import time
  4. from certbot import errors
  5. from certbot_pdns.pdnsapi import PdnsApi
  6. logger = logging.getLogger(__name__)
  7. class PdnsApiAuthenticator:
  8. api = None
  9. zones = None
  10. axfr_time = None
  11. def find_best_matching_zone(self, domain):
  12. if domain is None or domain == "":
  13. return None
  14. for zone in self.zones:
  15. if zone['name'] == domain + ".":
  16. return zone
  17. return self.find_best_matching_zone(domain[domain.index(".") + 1:]) if "." in domain else None
  18. def find_soa(self, zone):
  19. for rrset in zone["rrsets"]:
  20. if rrset["type"] == "SOA":
  21. return rrset, rrset["records"][0]
  22. return None
  23. def flush_zone(self, zone_name):
  24. res = self.api.flush_zone_cache(zone_name)
  25. if res is None or "result" not in res or res["result"] != "Flushed cache.":
  26. raise errors.PluginError("Bad return from PDNS API when flushing cache: %s" % res)
  27. def notify_zone(self, zone_name):
  28. res = self.api.notify_zone(zone_name)
  29. if res is None or "result" not in res or res["result"] != "Notification queued":
  30. raise errors.PluginError("Bad return from PDNS API when notifying: %s" % res)
  31. def update_soa(self, zone_name):
  32. zone = self.api.get_zone(zone_name)
  33. if zone is None or "error" in zone:
  34. raise errors.PluginError("Bad return from PDNS API when getting zone %s: %s" % (zone_name, zone))
  35. rrset, soa = self.find_soa(zone)
  36. split = soa["content"].split(" ")
  37. split[2] = str(int(split[2]) + 1)
  38. soa["content"] = ' '.join(split)
  39. res = self.api.replace_record(zone_name, zone_name, rrset["type"], rrset["ttl"], soa["content"],
  40. soa["disabled"], False)
  41. if res is not None:
  42. raise errors.PluginError("Bad return from PDNS API when updating SOA: %s" % res)
  43. def prepare(self, conf_path):
  44. self.api = PdnsApi()
  45. with open(conf_path) as f:
  46. config = json.load(f)
  47. self.api.set_api_key(config["api-key"])
  48. self.api.set_base_url(config["base-url"])
  49. self.axfr_time = config["axfr-time"]
  50. self.zones = self.api.list_zones()
  51. # print(self.zones)
  52. # raw_input('Press <ENTER> to continue')
  53. if self.zones is None or "error" in self.zones:
  54. raise errors.PluginError("Could not list zones %s" % self.zones)
  55. def perform_single(self, achall, response, validation):
  56. domain = achall.domain
  57. token = validation.encode()
  58. zone = self.find_best_matching_zone(domain)
  59. if zone is None:
  60. raise errors.PluginError("Could not find zone for %s" % domain)
  61. logger.debug("Found zone %s for domain %s" % (zone["name"], domain))
  62. res = self.api.replace_record(zone["name"], "_acme-challenge." + domain + ".", "TXT", 1, "\"" + token + "\"", False, False)
  63. if res is not None:
  64. raise errors.PluginError("Bad return from PDNS API when adding record: %s" % res)
  65. return response
  66. def perform_notify(self, zone):
  67. logger.info("Notifying zone %s..." % zone["name"])
  68. self.update_soa(zone["name"])
  69. self.flush_zone(zone["name"])
  70. self.notify_zone(zone["name"])
  71. def wait_for_propagation(self, achalls):
  72. # TODO search zones authoritative servers and check for TXT record on each of them
  73. # raw_input('Press <ENTER> to continue')
  74. logger.info("Waiting %i seconds..." % self.axfr_time)
  75. time.sleep(self.axfr_time)
  76. def cleanup(self, achall):
  77. domain = achall.domain
  78. zone = self.find_best_matching_zone(domain)
  79. if zone is None:
  80. return
  81. res = self.api.delete_record(zone["name"], "_acme-challenge." + domain + ".", "TXT", 1, None, False, False)
  82. if res is not None:
  83. raise errors.PluginError("Bad return from PDNS API when deleting record: %s" % res)
  84. self.update_soa(zone["name"])
  85. self.flush_zone(zone["name"])
  86. self.notify_zone(zone["name"])