diff --git a/action_map.yml b/action_map.yml index 069c7c71..3d98e0c4 100644 --- a/action_map.yml +++ b/action_map.yml @@ -180,6 +180,14 @@ domain: help: Domain name to add nargs: '+' pattern: '^([a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)(\.[a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)*(\.[a-zA-Z]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)$' + -m: + full: --main + help: Is the main domain + action: store_true + -d: + full: --dyndns + help: Subscribe to the DynDNS service + action: store_true ### domain_remove() remove: diff --git a/yunohost_domain.py b/yunohost_domain.py index 10de4ae9..43903236 100644 --- a/yunohost_domain.py +++ b/yunohost_domain.py @@ -30,8 +30,10 @@ import re import shutil import json import yaml +import requests from urllib import urlopen from yunohost import YunoHostError, YunoHostLDAP, win_msg, colorize, validate, get_required_args +from yunohost_dyndns import dyndns_subscribe def domain_list(filter=None, limit=None, offset=None): @@ -66,12 +68,14 @@ def domain_list(filter=None, limit=None, offset=None): return { 'Domains': result_list } -def domain_add(domains, main=False): +def domain_add(domains, main=False, dyndns=False): """ Create a custom domain Keyword argument: domains -- Domain name to add + main -- Is the main domain + dyndns -- Subscribe to DynDNS """ with YunoHostLDAP() as yldap: @@ -88,6 +92,19 @@ def domain_add(domains, main=False): try: if domain in domain_list()['Domains']: continue except YunoHostError: pass + + # DynDNS domain + if dyndns and len(domain.split('.')) >= 3: + r = requests.get('http://dyndns.yunohost.org/domains') + dyndomains = json.loads(r.text) + dyndomain = '.'.join(domain.split('.')[1:]) + if dyndomain in dyndomains: + if os.path.exists('/etc/cron.d/yunohost-dyndns'): + raise YunoHostError(22, _("You already have a DynDNS domain")) + else: + dyndns_subscribe(domain=domain) + + # Commands ssl_dir = '/usr/share/yunohost/yunohost-config/ssl/yunoCA' ssl_domain_path = '/etc/yunohost/certs/'+ domain with open(ssl_dir +'/serial', 'r') as f: diff --git a/yunohost_dyndns.py b/yunohost_dyndns.py index cc30280e..2d4355ce 100644 --- a/yunohost_dyndns.py +++ b/yunohost_dyndns.py @@ -45,6 +45,10 @@ def dyndns_subscribe(subscribe_host="dyndns.yunohost.org", domain=None, key=None with open('/etc/yunohost/current_host', 'r') as f: domain = f.readline().rstrip() + # Verify if domain is available + if requests.get('http://'+ subscribe_host +'/test/'+ domain).status_code != 200: + raise YunoHostError(17, _("DynDNS domain is already taken")) + if key is None: if len(glob.glob('/etc/yunohost/dyndns/*.key')) == 0: os.makedirs('/etc/yunohost/dyndns') @@ -56,10 +60,6 @@ def dyndns_subscribe(subscribe_host="dyndns.yunohost.org", domain=None, key=None with open(key_file) as f: key = f.readline().strip().split(' ')[-1] - # Verify if domain is available - if requests.get('http://'+ subscribe_host +'/test/'+ domain).status_code != 200: - raise YunoHostError(17, _("Domain is already taken")) - # Send subscription r = requests.post('http://'+ subscribe_host +'/key/'+ base64.b64encode(key), data={ 'subdomain': domain }) if r.status_code != 201: