Skip to content

Commit

Permalink
Merge pull request #1 from csvwolf/dev
Browse files Browse the repository at this point in the history
v0.0.2
  • Loading branch information
csvwolf authored Oct 14, 2019
2 parents e25ba6c + 3de2394 commit 08af6d9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 46 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ venv.bak/

# mypy
.mypy_cache/

.idea/
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
```
```

### Help
#### Why I can't run successful?

f-string is a feature in Python3.6+, please ensure your python version.
78 changes: 34 additions & 44 deletions certbot_dns_hostker/dns_hostker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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]
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 08af6d9

Please sign in to comment.