From afea741bfa9c821c510b107e6ebd23911b63ca93 Mon Sep 17 00:00:00 2001 From: usmannasir Date: Sat, 5 Oct 2024 21:47:16 +0500 Subject: [PATCH] add dns_cyberpanel custom api for acme ssl --- cli/cyberPanel.py | 8 ++++++- dns/dnsManager.py | 8 +++---- install/dns_cyberpanel.sh | 50 +++++++++++++++++++++++++++++++++++++++ install/install.py | 11 +++++++++ plogical/sslUtilities.py | 1 - plogical/sslv2.py | 6 ++++- plogical/upgrade.py | 13 ++++++++++ 7 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 install/dns_cyberpanel.sh diff --git a/cli/cyberPanel.py b/cli/cyberPanel.py index 0e3c51f8b..e2a15f118 100755 --- a/cli/cyberPanel.py +++ b/cli/cyberPanel.py @@ -313,7 +313,13 @@ class cyberPanel: def createDNSRecord(self, virtualHostName, name, recordType, value, priority, ttl): try: - zone = DNS.getZoneObject(virtualHostName) + import tldextract + + no_cache_extract = tldextract.TLDExtract(cache_dir=None) + extractDomain = no_cache_extract(virtualHostName) + topLevelDomain = extractDomain.domain + '.' + extractDomain.suffix + + zone = DNS.getZoneObject(topLevelDomain) DNS.createDNSRecord(zone, name, recordType, value, int(priority), int(ttl)) self.printStatus(1, 'None') except BaseException as msg: diff --git a/dns/dnsManager.py b/dns/dnsManager.py index 7569804b0..ded8c2edd 100755 --- a/dns/dnsManager.py +++ b/dns/dnsManager.py @@ -710,7 +710,7 @@ class DNSManager: try: zones = cf.zones.get(params=params) - except CloudFlare.CloudFlareAPIError as e: + except BaseException as e: final_json = json.dumps({'status': 0, 'fetchStatus': 0, 'error_message': str(e), "data": '[]'}) return HttpResponse(final_json) @@ -749,7 +749,7 @@ class DNSManager: try: dns_records = cf.zones.dns_records.get(zone_id, params={'per_page':50, 'type':fetchType}) - except CloudFlare.exceptions.CloudFlareAPIError as e: + except BaseException as e: final_json = json.dumps({'status': 0, 'fetchStatus': 0, 'error_message': str(e), "data": '[]'}) return HttpResponse(final_json) @@ -816,7 +816,7 @@ class DNSManager: try: zones = cf.zones.get(params=params) - except CloudFlare.CloudFlareAPIError as e: + except BaseException as e: final_json = json.dumps({'status': 0, 'delete_status': 0, 'error_message': str(e), "data": '[]'}) return HttpResponse(final_json) @@ -868,7 +868,7 @@ class DNSManager: try: zones = cf.zones.get(params=params) - except CloudFlare.CloudFlareAPIError as e: + except BaseException as e: final_json = json.dumps({'status': 0, 'delete_status': 0, 'error_message': str(e), "data": '[]'}) return HttpResponse(final_json) diff --git a/install/dns_cyberpanel.sh b/install/dns_cyberpanel.sh new file mode 100644 index 000000000..234361786 --- /dev/null +++ b/install/dns_cyberpanel.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env sh +# shellcheck disable=SC2034 +dns_myapi_info='CyberPanel script for ACME to add records to PDNS + A sample custom DNS API script. +Domains: example.com +Site: github.com/acmesh-official/acme.sh/wiki/DNS-API-Dev-Guide +Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_duckdns +Options: + MYAPI_Token API Token. Get API Token from https://example.com/api/. Optional. +Issues: github.com/usmannasir/cyberpanel +Author: Neil Pang +' + +# This file name is "dns_myapi.sh" +# So, here must be a method dns_myapi_add() +# Which will be called by acme.sh to add the txt record to your API system. +# Returns 0 means success, otherwise error. + +######## Public functions ##################### + +# Please Read this guide first: https://github.com/acmesh-official/acme.sh/wiki/DNS-API-Dev-Guide + +# Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" +dns_cyberpanel_add() { + fulldomain=$1 + txtvalue=$2 + _info "Using myapi" + _debug fulldomain "$fulldomain" + _debug txtvalue "$txtvalue" + _info "cyberpanel createDNSRecord --domainName $fulldomain --name $fulldomain --recordType TXT --value $txtvalue --priority 0 --ttl 3600" + + cyberpanel createDNSRecord --domainName $fulldomain --name $fulldomain --recordType TXT --value $txtvalue --priority 0 --ttl 3600 + + return 0 +} + +# Usage: fulldomain txtvalue +# Remove the txt record after validation. +dns_cyberpanel_rm() { + fulldomain=$1 + txtvalue=$2 + _info "Using myapi" + _debug fulldomain "$fulldomain" + _debug txtvalue "$txtvalue" + + return 0 +} + +#################### Private functions below ################################## +# You can add private helper functions here if needed \ No newline at end of file diff --git a/install/install.py b/install/install.py index b0b908b8a..e9ed3a87d 100755 --- a/install/install.py +++ b/install/install.py @@ -2583,6 +2583,16 @@ vmail writeToFile.close() + def installDNS_CyberPanelACMEFile(self): + + os.chdir(self.cwd) + + filePath = '/root/.acme.sh/dns_cyberpanel.sh' + shutil.copy('dns_cyberpanel.sh', filePath) + + command = f'chmod +x {filePath}' + preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR) + def main(): parser = argparse.ArgumentParser(description='CyberPanel Installer') parser.add_argument('publicip', help='Please enter public IP for your VPS or dedicated server.') @@ -2734,6 +2744,7 @@ def main(): checks.setupPort() checks.setupPythonWSGI() checks.setupLSCPDDaemon() + checks.installDNS_CyberPanelACMEFile() if args.redis is not None: checks.installRedis() diff --git a/plogical/sslUtilities.py b/plogical/sslUtilities.py index 6c8b67095..d77dd8e14 100755 --- a/plogical/sslUtilities.py +++ b/plogical/sslUtilities.py @@ -220,7 +220,6 @@ context /.well-known/acme-challenge { except BaseException as msg: return 0, str(msg) - @staticmethod def installSSLForDomain(virtualHostName, adminEmail='example@example.org'): diff --git a/plogical/sslv2.py b/plogical/sslv2.py index 7da67e928..a55434698 100755 --- a/plogical/sslv2.py +++ b/plogical/sslv2.py @@ -361,6 +361,8 @@ class sslUtilities: if zone['name'] == topLevelDomain: if zone['status'] == 'active': return 1, None + else: + logging.CyberCPLogFileWriter.writeToFile(f'zone is not active in cf: {zone["name"]}') return 0, 'Zone not found in Cloudflare' @@ -387,6 +389,8 @@ class sslUtilities: result = socket.getaddrinfo(f'cptest.{topLevelDomain}', None, socket.AF_INET)[0] + logging.CyberCPLogFileWriter.writeToFile(f'PDNS Result: {str(result)}.') + # Return the IP address as a string if result[4][0] == ACLManager.GetServerIP(): return 1, None @@ -426,7 +430,7 @@ class sslUtilities: CyberPanel_Check, message = sslUtilities.FindIfDomainInPowerDNS(virtualHostName) if CyberPanel_Check: - DNS_TO_USE = 'dns_pdns' + DNS_TO_USE = 'dns_cyberpanel' else: return 0, 'Domain is not active in any of the configured DNS provider.' diff --git a/plogical/upgrade.py b/plogical/upgrade.py index 79f204614..323a3e5c9 100755 --- a/plogical/upgrade.py +++ b/plogical/upgrade.py @@ -3594,6 +3594,8 @@ pm.max_spare_servers = 3 command = 'chmod +x /usr/local/CyberCP/public/imunifyav/bin/execute.py' Upgrade.executioner(command, command, 1) + Upgrade.installDNS_CyberPanelACMEFile() + Upgrade.stdOut("Upgrade Completed.") ### remove log file path incase its there @@ -3842,6 +3844,17 @@ pm.max_spare_servers = 3 else: print("Quotas can not be enabled continue to use chhtr.") + @staticmethod + def installDNS_CyberPanelACMEFile(): + filePath = '/root/.acme.sh/dns_cyberpanel.sh' + if os.path.exists(filePath): + os.remove(filePath) + shutil.copy('/usr/local/CyberCP/install/dns_cyberpanel.sh', filePath) + + command = f'chmod +x {filePath}' + Upgrade.executioner(command, command, 0, True) + + def main(): parser = argparse.ArgumentParser(description='CyberPanel Installer')