From 283e01db8fa3c2322670f465427710cbbb300640 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 24 Feb 2019 01:58:33 +0100 Subject: [PATCH 1/5] Don't make a whole HTTP request every dyndns update... --- src/yunohost/dyndns.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/yunohost/dyndns.py b/src/yunohost/dyndns.py index 2f8d63135..53486241a 100644 --- a/src/yunohost/dyndns.py +++ b/src/yunohost/dyndns.py @@ -333,7 +333,8 @@ def _guess_current_dyndns_domain(dyn_host): """ # Retrieve the first registered domain - for path in glob.iglob('/etc/yunohost/dyndns/K*.private'): + paths = list(glob.iglob('/etc/yunohost/dyndns/K*.private')) + for path in paths: match = RE_DYNDNS_PRIVATE_KEY_MD5.match(path) if not match: match = RE_DYNDNS_PRIVATE_KEY_SHA512.match(path) @@ -343,7 +344,9 @@ def _guess_current_dyndns_domain(dyn_host): # Verify if domain is registered (i.e., if it's available, skip # current domain beause that's not the one we want to update..) - if _dyndns_available(dyn_host, _domain): + # If there's only 1 such key found, then avoid doing the request + # for nothing (that's very probably the one we want to find ...) + if len(paths) > 1 and _dyndns_available(dyn_host, _domain): continue else: return (_domain, path) From 3f8f328fae8967a6294495484c7c197e8497947d Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 24 Feb 2019 02:04:41 +0100 Subject: [PATCH 2/5] Delete the key if subscribing failed, to avoid confusion later trying to detect registered domains --- src/yunohost/dyndns.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/yunohost/dyndns.py b/src/yunohost/dyndns.py index 53486241a..090ee3901 100644 --- a/src/yunohost/dyndns.py +++ b/src/yunohost/dyndns.py @@ -144,7 +144,8 @@ def dyndns_subscribe(operation_logger, subscribe_host="dyndns.yunohost.org", dom 'dnssec-keygen -a hmac-sha512 -b 512 -r /dev/urandom -n USER %s' % domain) os.system('chmod 600 /etc/yunohost/dyndns/*.key /etc/yunohost/dyndns/*.private') - key_file = glob.glob('/etc/yunohost/dyndns/*.key')[0] + private_file = glob.glob('/etc/yunohost/dyndns/*%s*.private' % domain)[0] + key_file = glob.glob('/etc/yunohost/dyndns/*%s*.key' % domain)[0] with open(key_file) as f: key = f.readline().strip().split(' ', 6)[-1] @@ -153,8 +154,12 @@ def dyndns_subscribe(operation_logger, subscribe_host="dyndns.yunohost.org", dom try: r = requests.post('https://%s/key/%s?key_algo=hmac-sha512' % (subscribe_host, base64.b64encode(key)), data={'subdomain': domain}, timeout=30) except requests.ConnectionError: + os.system("rm %s" % private_file) + os.system("rm %s" % key_file) raise YunohostError('no_internet_connection') if r.status_code != 201: + os.system("rm %s" % private_file) + os.system("rm %s" % key_file) try: error = json.loads(r.text)['error'] except: From 9657387746137d0a4e1adfefd2757759325951a7 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 24 Feb 2019 02:10:24 +0100 Subject: [PATCH 3/5] Prevent calling dyndns_subscribe if already subscribed --- src/yunohost/dyndns.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/yunohost/dyndns.py b/src/yunohost/dyndns.py index 090ee3901..36509b0e4 100644 --- a/src/yunohost/dyndns.py +++ b/src/yunohost/dyndns.py @@ -119,6 +119,9 @@ def dyndns_subscribe(operation_logger, subscribe_host="dyndns.yunohost.org", dom subscribe_host -- Dynette HTTP API to subscribe to """ + if len(glob.glob('/etc/yunohost/dyndns/*.key')) != 0 or os.path.exists('/etc/cron.d/yunohost-dyndns'): + raise YunohostError('domain_dyndns_already_subscribed') + if domain is None: domain = _get_maindomain() operation_logger.related_to.append(('domain', domain)) From e6f1845448c86c32a379353c8c0a70797135a52b Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Fri, 1 Mar 2019 01:40:52 +0100 Subject: [PATCH 4/5] Add -f to rm --- src/yunohost/dyndns.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/yunohost/dyndns.py b/src/yunohost/dyndns.py index 36509b0e4..ddc285e8d 100644 --- a/src/yunohost/dyndns.py +++ b/src/yunohost/dyndns.py @@ -157,12 +157,12 @@ def dyndns_subscribe(operation_logger, subscribe_host="dyndns.yunohost.org", dom try: r = requests.post('https://%s/key/%s?key_algo=hmac-sha512' % (subscribe_host, base64.b64encode(key)), data={'subdomain': domain}, timeout=30) except requests.ConnectionError: - os.system("rm %s" % private_file) - os.system("rm %s" % key_file) + os.system("rm -f %s" % private_file) + os.system("rm -f %s" % key_file) raise YunohostError('no_internet_connection') if r.status_code != 201: - os.system("rm %s" % private_file) - os.system("rm %s" % key_file) + os.system("rm -f %s" % private_file) + os.system("rm -f %s" % key_file) try: error = json.loads(r.text)['error'] except: From f37a6b43e1ab4e163877b6afdd3226009cfd70a2 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Mon, 4 Mar 2019 17:09:31 +0100 Subject: [PATCH 5/5] More general exception to handle other cases (e.g. SSL errors, ...) --- src/yunohost/dyndns.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/yunohost/dyndns.py b/src/yunohost/dyndns.py index ddc285e8d..2dadcef52 100644 --- a/src/yunohost/dyndns.py +++ b/src/yunohost/dyndns.py @@ -156,10 +156,10 @@ def dyndns_subscribe(operation_logger, subscribe_host="dyndns.yunohost.org", dom # Send subscription try: r = requests.post('https://%s/key/%s?key_algo=hmac-sha512' % (subscribe_host, base64.b64encode(key)), data={'subdomain': domain}, timeout=30) - except requests.ConnectionError: + except Exception as e: os.system("rm -f %s" % private_file) os.system("rm -f %s" % key_file) - raise YunohostError('no_internet_connection') + raise YunohostError('dyndns_registration_failed', error=str(e)) if r.status_code != 201: os.system("rm -f %s" % private_file) os.system("rm -f %s" % key_file)