From 215afb49934f0cbac03d88e983055d5db3570567 Mon Sep 17 00:00:00 2001 From: cektom Date: Sun, 16 Feb 2025 17:30:09 +0100 Subject: [PATCH] Added multihost support for cloudflare service --- .../ddclient/lib/account/cloudflare.py | 153 +++++++++--------- 1 file changed, 79 insertions(+), 74 deletions(-) diff --git a/dns/ddclient/src/opnsense/scripts/ddclient/lib/account/cloudflare.py b/dns/ddclient/src/opnsense/scripts/ddclient/lib/account/cloudflare.py index 6620e2431c..1c12c5d7c6 100755 --- a/dns/ddclient/src/opnsense/scripts/ddclient/lib/account/cloudflare.py +++ b/dns/ddclient/src/opnsense/scripts/ddclient/lib/account/cloudflare.py @@ -98,88 +98,93 @@ def execute(self): "Account %s ZoneID for %s %s" % (self.description, self.settings.get('zone'), zone_id) ) - # Get record ID - req_opts = { - 'url': f"{req_opts['url']}/{zone_id}/dns_records", - 'params': { - 'name': self.settings.get('hostnames'), - 'type': recordType - }, - 'headers': req_opts['headers'] - } - response = requests.get(**req_opts) - try: - payload = response.json() - except requests.exceptions.JSONDecodeError: - payload = {} - if 'success' not in payload: - syslog.syslog( + + # Update each hostname + for hostname in self.settings.get("hostnames", "").split(","): + + req_opts = { + 'url': f"{url}/{zone_id}/dns_records", + 'params': { + 'name': hostname, + 'type': recordType + }, + 'headers': req_opts['headers'] + } + response = requests.get(**req_opts) + try: + payload = response.json() + except requests.exceptions.JSONDecodeError: + payload = {} + if 'success' not in payload: + syslog.syslog( + syslog.LOG_ERR, + "Account %s error parsing JSON response [RecordID] for hostname %s Response: %s" % (self.description, hostname, response.text) + ) + return + if not payload.get('success', False): + syslog.syslog( syslog.LOG_ERR, - "Account %s error parsing JSON response [RecordID] %s" % (self.description, response.text) + "Account %s error receiving RecordID [%s] for host %s" % ( + self.description, json.dumps(payload.get('errors', {})), hostname + ) ) - return - if not payload.get('success', False): - syslog.syslog( - syslog.LOG_ERR, - "Account %s error receiving RecordID [%s]" % ( - self.description, json.dumps(payload.get('errors', {})) - ) - ) - return False + continue - if len(payload['result']) == 0: - syslog.syslog( - syslog.LOG_ERR, "Account %s error locating hostname %s [%s]" % ( - self.description, self.settings.get('hostnames'), recordType + if len(payload['result']) == 0: + syslog.syslog( + syslog.LOG_ERR, "Account %s error locating hostname %s [%s]" % ( + self.description, hostname, recordType + ) ) - ) - return False + continue - record_id = payload['result'][0]['id'] - if self.is_verbose: - syslog.syslog( - syslog.LOG_NOTICE, - "Account %s RecordID for %s %s" % (self.description, self.settings.get('hostnames'), record_id) - ) + record_id = payload['result'][0]['id'] + if self.is_verbose: + syslog.syslog( + syslog.LOG_NOTICE, + "Account %s RecordID for %s %s" % (self.description, hostname, record_id) + ) - # Send IP address update - req_opts = { - 'url': f"{req_opts['url']}/{record_id}", - 'json': { - 'type': recordType, - 'name': self.settings.get('hostnames'), - 'content': str(self.current_address), - }, - 'headers': req_opts['headers'] - } - response = requests.patch(**req_opts) - try: - payload = response.json() - except requests.exceptions.JSONDecodeError: - payload = {} - if 'success' not in payload: - syslog.syslog( - syslog.LOG_ERR, - "Account %s error parsing JSON response [UpdateIP] %s" % (self.description, response.text) + # Send IP address update + req_opts = { + 'url': f"{req_opts['url']}/{record_id}", + 'json': { + 'type': recordType, + 'name': hostname, + 'content': str(self.current_address), + }, + 'headers': req_opts['headers'] + } + response = requests.patch(**req_opts) + try: + payload = response.json() + except requests.exceptions.JSONDecodeError: + payload = {} + if 'success' not in payload: + syslog.syslog( + syslog.LOG_ERR, + "Account %s error parsing JSON response [UpdateIP] for hostname %s Response: %s" % (self.description, hostname, response.text) + ) + continue + if payload.get('success', False): + syslog.syslog( + syslog.LOG_NOTICE, + "Account %s successfully updated hostname %s to IP %s [%s]" % ( + self.description, + hostname, + self.current_address, + payload.get('result', {}).get('content', '') + ) ) - return False - if payload.get('success', False): - syslog.syslog( - syslog.LOG_NOTICE, - "Account %s set new ip %s [%s]" % ( - self.description, - self.current_address, - payload.get('result', {}).get('content', '') + else: + syslog.syslog( + syslog.LOG_ERR, + "Account %s failed to set new ip %s for hostname %s [%s]" % (self.description, self.current_address, hostname, response.text) ) - ) - - self.update_state(address=self.current_address) - return True - else: - syslog.syslog( - syslog.LOG_ERR, - "Account %s failed to set new ip %s [%s]" % (self.description, self.current_address, response.text) - ) + continue + self.update_state(address=self.current_address) + return True + return False