diff --git a/.gitignore b/.gitignore index 894a44c..4ad6467 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,5 @@ venv.bak/ # mypy .mypy_cache/ + +.idea/ \ No newline at end of file diff --git a/README.md b/README.md index 7bdf090..e3d915a 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,9 @@ certbot certonly --authenticator certbot-dns-hostker:dns-hostker --certbot-dns-h ``` certbot_dns_hostker:dns_hostker_email=[your email] certbot_dns_hostker:dns_hostker_token=[your token] -``` \ No newline at end of file +``` + +### Help +#### Why I can't run successful? + +f-string is a feature in Python3.6+, please ensure your python version. \ No newline at end of file diff --git a/certbot_dns_hostker/dns_hostker.py b/certbot_dns_hostker/dns_hostker.py index 162abb6..48b1ab8 100644 --- a/certbot_dns_hostker/dns_hostker.py +++ b/certbot_dns_hostker/dns_hostker.py @@ -7,11 +7,23 @@ from certbot import errors from certbot import interfaces from certbot.plugins import dns_common +from ker import Ker +from ker import HostkerRequestError logger = logging.getLogger(__name__) ACCOUNT_URL = 'https://www.hostker.com/' +def sub_domain_to_TLD(domain): + domain_list = domain.split('.')[-2:] + return '.'.join(domain_list) + +def validate_domain_to_record(domain): + domain_list = domain.split('.')[:-2] + if len(domain_list) > 0 and domain_list[-1] == '*': + domain_list = domain_list[:-1] + return '.'.join(domain_list) + @zope.interface.implementer(interfaces.IAuthenticator) @zope.interface.provider(interfaces.IPluginFactory) class Authenticator(dns_common.DNSAuthenticator): @@ -45,12 +57,14 @@ def _setup_credentials(self): # add txt record def _perform(self, domain, validation_name, validation): - record_name = validation_name.split(f'.{domain}')[0] + record_name = validate_domain_to_record(validation_name) + domain = sub_domain_to_TLD(domain) self._get_hostker_client().add_txt_record(domain, record_name, validation, self.ttl) # delete txt record def _cleanup(self, domain, validation_name, validation): - record_name = validation_name.split(f'.{domain}')[0] + record_name = validate_domain_to_record(validation_name) + domain = sub_domain_to_TLD(domain) self._get_hostker_client().del_txt_record(domain, record_name) def _get_hostker_client(self): @@ -60,57 +74,33 @@ class HostkerClient: """Encapsulates all communication with the Hostker API""" def __init__(self, email, token): - self.email = email - self.token = token + self.ker = Ker(email, token) def add_txt_record(self, domain, record_name, record_content, record_ttl): - params = { - 'domain': domain, - 'header': record_name, - 'type': 'TXT', - 'data': record_content, - 'ttl': record_ttl, - 'email': self.email, - 'token': self.token, - } - - r = requests.post('https://i.hostker.com/api/dnsAddRecord', data=params, headers={ - 'Content-Type': 'application/x-www-form-urlencoded' - }) - result = r.json() - if result['success'] == 0: - raise errors.PluginError(result['errorMessage']) + try: + self.ker.dns.add(domain=domain, header=record_name, record_type='TXT', data=record_content, ttl=record_ttl) + except HostkerRequestError as err: + raise errors.PluginError(str(err)) else: logger.debug('Successfully add TXT record') def del_txt_record(self, domain, record_name): record_ids = self._get_record_ids(domain, record_name) - for id in record_ids: - r = requests.post('https://i.hostker.com/api/dnsDeleteRecord', data={ - 'email': self.email, - 'token': self.token, - 'id': id - }, headers={ - 'Content-Type': 'application/x-www-form-urlencoded' - }) - result = r.json() - if result['success'] == 0: - raise errors.PluginError(f'error when delete record: {result["errorMessage"]}') + for unique_id in record_ids: + try: + self.ker.dns.delete(unique_id) + except HostkerRequestError as err: + raise errors.PluginError(str(err)) else: logger.debug('Successfully remove TXT record') def _get_record_ids(self, domain, record_name): - r = requests.post('https://i.hostker.com/api/dnsGetRecords', data={ - 'email': self.email, - 'token': self.token, - 'domain': domain - }, headers={ - 'Content-Type': 'application/x-www-form-urlencoded' - }) - result = r.json() - if result['success'] == 0: - raise errors.PluginError(result['errorMessage']) - logger.debug(result) - records = list(filter(lambda record: record['header']==record_name and record['type'] == 'TXT', result['records'])) - return [record['id'] for record in records] + try: + result = self.ker.dns.list(domain) + except HostkerRequestError as err: + raise errors.PluginError(str(err)) + else: + logger.debug(result) + records = list(filter(lambda record: record['header']==record_name and record['type'] == 'TXT', result['records'])) + return [record['id'] for record in records] diff --git a/requirements.txt b/requirements.txt index 660f14a..10a824b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,4 @@ zope.event==4.4 zope.hookable==4.2.0 zope.interface==4.6.0 zope.proxy==4.3.2 +ker.py===0.0.2 \ No newline at end of file diff --git a/setup.py b/setup.py index 35a061f..33b2f12 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='certbot-dns-hostker', - version='0.0.1', + version='0.0.2', description='Certbot for Hostker DNS', author='SkyAo', author_email='csvwolf@qq.com',