From 7b9b42f9145e98ef2b9f5dcd03c09cc3ff412c51 Mon Sep 17 00:00:00 2001 From: SkyAo Date: Tue, 8 Oct 2019 12:37:08 +0800 Subject: [PATCH 1/3] feat: support SLD ssl --- certbot_dns_hostker/dns_hostker.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/certbot_dns_hostker/dns_hostker.py b/certbot_dns_hostker/dns_hostker.py index 162abb6..a1eede6 100644 --- a/certbot_dns_hostker/dns_hostker.py +++ b/certbot_dns_hostker/dns_hostker.py @@ -12,6 +12,12 @@ ACCOUNT_URL = 'https://www.hostker.com/' +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 +51,12 @@ 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) 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) self._get_hostker_client().del_txt_record(domain, record_name) def _get_hostker_client(self): From 8dc962557347ca81a1999b9c3fccbb8040891f78 Mon Sep 17 00:00:00 2001 From: SkyAo Date: Tue, 8 Oct 2019 12:50:54 +0800 Subject: [PATCH 2/3] feat: use ker.py sdk --- README.md | 7 +++- certbot_dns_hostker/dns_hostker.py | 63 ++++++++++-------------------- requirements.txt | 1 + 3 files changed, 28 insertions(+), 43 deletions(-) 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 a1eede6..83a337d 100644 --- a/certbot_dns_hostker/dns_hostker.py +++ b/certbot_dns_hostker/dns_hostker.py @@ -7,11 +7,14 @@ 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 validate_domain_to_record(domain): domain_list = domain.split('.')[:-2] if len(domain_list) > 0 and domain_list[-1] == '*': @@ -66,57 +69,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..9e0f204 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.1 \ No newline at end of file From 3de23943643b33c16a2262b5dbf721ff03872030 Mon Sep 17 00:00:00 2001 From: SkyAo Date: Mon, 14 Oct 2019 21:40:49 +0800 Subject: [PATCH 3/3] fix: support sub domain register --- .gitignore | 2 ++ certbot_dns_hostker/dns_hostker.py | 5 +++++ requirements.txt | 2 +- setup.py | 2 +- 4 files changed, 9 insertions(+), 2 deletions(-) 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/certbot_dns_hostker/dns_hostker.py b/certbot_dns_hostker/dns_hostker.py index 83a337d..48b1ab8 100644 --- a/certbot_dns_hostker/dns_hostker.py +++ b/certbot_dns_hostker/dns_hostker.py @@ -14,6 +14,9 @@ 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] @@ -55,11 +58,13 @@ def _setup_credentials(self): # add txt record def _perform(self, domain, validation_name, validation): 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 = 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): diff --git a/requirements.txt b/requirements.txt index 9e0f204..10a824b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,4 +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.1 \ No newline at end of file +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',