From 9c1bbae101f1a704017ed552e783562268252a8b Mon Sep 17 00:00:00 2001 From: Sergey Ponomarev Date: Sat, 1 Jun 2024 20:19:41 +0300 Subject: [PATCH] luci-app-acme: Guess the system domain and pre-fill it to domains. Check if the hostname is FQDN (e.g. has least one dot). Check if the domain in the browser is not an IP and FQDN. Signed-off-by: Sergey Ponomarev --- .../htdocs/luci-static/resources/view/acme.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/applications/luci-app-acme/htdocs/luci-static/resources/view/acme.js b/applications/luci-app-acme/htdocs/luci-static/resources/view/acme.js index 4365273affe..1e1f1fdbd9a 100644 --- a/applications/luci-app-acme/htdocs/luci-static/resources/view/acme.js +++ b/applications/luci-app-acme/htdocs/luci-static/resources/view/acme.js @@ -17,12 +17,15 @@ return view.extend({ return certs; }), L.resolveDefault(fs.stat('/usr/lib/acme/client/dnsapi'), null), + fs.lines('/proc/sys/kernel/hostname'), ]); }, render: function (data) { let certs = data[0]; let hasDnsApi = data[1] != null; + let hostname = data[2]; + let systemDomain = _guessDomain(hostname); let wikiUrl = 'https://github.com/acmesh-official/acme.sh/wiki/'; var wikiInstructionUrl = wikiUrl + 'dnsapi'; var m, s, o; @@ -96,6 +99,9 @@ return view.extend({ "The first name will be the subject name, subsequent names will be alt names. " + "Note that all domain names must point at the router in the global DNS.")); o.datatype = "list(string)"; + if (systemDomain) { + o.default = [systemDomain]; + } o.validate = function (section_id, value) { if (!/^[*a-z0-9.-]*$/.test(value)) { return _('Invalid domain'); @@ -579,6 +585,15 @@ return view.extend({ } }) +function _isFqdn(domain) { + // Is not an IP i.e. starts from alphanumeric and has least one dot + return /[a-z0-9-]\..*$/.test(domain); +} + +function _guessDomain(hostname) { + return _isFqdn(hostname) ? hostname : (_isFqdn(window.location.hostname) ? window.location.hostname : ''); +} + function _addDnsProviderField(s, provider, env, title, desc) { let o = s.taboption('challenge_dns', form.Value, '_' + env, _(title),