From a4a5cf6af7035a21abc98959747f157a0b453fb5 Mon Sep 17 00:00:00 2001 From: Paul Donald Date: Sat, 23 Nov 2024 00:24:10 +0100 Subject: [PATCH] luci-app-libreswan: small refactor Signed-off-by: Paul Donald --- .../resources/view/libreswan/overview.js | 19 ++-- .../resources/view/libreswan/tunnels.js | 93 ++++++++----------- 2 files changed, 53 insertions(+), 59 deletions(-) diff --git a/applications/luci-app-libreswan/htdocs/luci-static/resources/view/libreswan/overview.js b/applications/luci-app-libreswan/htdocs/luci-static/resources/view/libreswan/overview.js index 61a2e4cbcc..399e55af97 100644 --- a/applications/luci-app-libreswan/htdocs/luci-static/resources/view/libreswan/overview.js +++ b/applications/luci-app-libreswan/htdocs/luci-static/resources/view/libreswan/overview.js @@ -11,14 +11,19 @@ var callLibreswanStatus = rpc.declare({ }); function secondsToString(seconds) { - var str = ''; - var numdays = Math.floor(seconds / 86400); - var numhours = Math.floor((seconds % 86400) / 3600); - var numminutes = Math.floor(((seconds % 86400) % 3600) / 60); - var numseconds = ((seconds % 86400) % 3600) % 60; + const numdays = Math.floor(seconds / 86400); + seconds %= 86400; + const numhours = Math.floor(seconds / 3600); + seconds %= 3600; + const numminutes = Math.floor(seconds / 60); + const numseconds = seconds % 60; - str = (numdays ? numdays + 'd ' : '') + (numhours ? numhours + 'h ' : '') + (numminutes ? numminutes + 'm ' : '') + numseconds + 's'; - return str; + return [ + numdays ? `${numdays}d` : '', + numhours ? `${numhours}h` : '', + numminutes ? `${numminutes}m` : '', + `${numseconds}s` + ].filter(Boolean).join(' '); } return view.extend({ diff --git a/applications/luci-app-libreswan/htdocs/luci-static/resources/view/libreswan/tunnels.js b/applications/luci-app-libreswan/htdocs/luci-static/resources/view/libreswan/tunnels.js index 762e17cee7..a2d5fd8493 100644 --- a/applications/luci-app-libreswan/htdocs/luci-static/resources/view/libreswan/tunnels.js +++ b/applications/luci-app-libreswan/htdocs/luci-static/resources/view/libreswan/tunnels.js @@ -8,23 +8,17 @@ 'require tools.widgets as widgets'; function calculateNetwork(addr, mask) { - addr = validation.parseIPv4(String(addr)); - - if (!isNaN(mask)) - mask = validation.parseIPv4(network.prefixToMask(+mask)); - else - mask = validation.parseIPv4(String(mask)); - - if (addr == null || mask == null) - return null; - - return [ - addr[0] & (mask[0] >>> 0 & 255), - addr[1] & (mask[1] >>> 0 & 255), - addr[2] & (mask[2] >>> 0 & 255), - addr[3] & (mask[3] >>> 0 & 255) - ].join('.') + '/' + - network.maskToPrefix(mask.join('.')); + const parsedAddr = validation.parseIPv4(String(addr)); + if (parsedAddr == null) return null; + + const parsedMask = !isNaN(mask) + ? validation.parseIPv4(network.prefixToMask(+mask)) + : validation.parseIPv4(String(mask)); + if (parsedMask == null) return null; + + const networkAddr = parsedAddr.map((byte, i) => byte & (parsedMask[i] >>> 0 & 255)); + + return `${networkAddr.join('.')}/${network.maskToPrefix(parsedMask.join('.'))}`; } return view.extend({ @@ -91,15 +85,14 @@ return view.extend({ o = s.taboption('general', form.Value, 'left', _('Left IP/Device')); o.datatype = 'or(string, ipaddr)'; - for (var i = 0; i < netDevs.length; i++) { - var addrs = netDevs[i].getIPAddrs(); - for (var j = 0; j < addrs.length; j++) { - o.value(addrs[j].split('/')[0]); - } - } - for (var i = 0; i < netDevs.length; i++) { - o.value('%' + netDevs[i].device); - } + netDevs.forEach(netDev => { + netDev.getIPAddrs().forEach(addr => { + o.value(addr.split('/')[0]); + }); + }); + netDevs.forEach(netDev => { + o.value(`%${netDev.device}`); + }); o.value('%defaultroute'); o.optional = false; o.depends({ 'left_interface' : '' }); @@ -122,12 +115,11 @@ return view.extend({ o = s.taboption('general', form.Value, 'leftsourceip', _('Local Source IP')); o.datatype = 'ipaddr'; - for (var i = 0; i < netDevs.length; i++) { - var addrs = netDevs[i].getIPAddrs(); - for (var j = 0; j < addrs.length; j++) { - o.value(addrs[j].split('/')[0]); - } - } + netDevs.forEach(netDev => { + netDev.getIPAddrs().forEach(addr => { + o.value(addr.split('/')[0]); + }); + }); o.optional = false; o.modalonly = true; @@ -180,6 +172,13 @@ return view.extend({ } o.modalonly = true; + function timevalidate(section_id, value) { + if (!/^[0-9]{1,3}[smhd]$/.test(value)) { + return _('Acceptable values are an integer followed by m, h, d'); + } + return true; + } + o = s.taboption('advanced', form.Value, 'ikelifetime', _('IKE Life Time'), _('Acceptable values are an integer followed by m, h, d')); o.default = '8h'; o.value('1h', '1h'); @@ -191,12 +190,7 @@ return view.extend({ o.value('24h', '24h'); o.modalonly = false; o.modalonly = true; - o.validate = function(section_id, value) { - if (!/^[0-9]{1,3}[smhd]$/.test(value)) { - return _('Acceptable values are an integer followed by m, h, d'); - } - return true; - } + o.validate = timevalidate; o = s.taboption('advanced', form.Flag, 'rekey', _('Rekey')); o.default = false; @@ -213,12 +207,7 @@ return view.extend({ o.value('60m', '60m'); o.modalonly = false; o.modalonly = true; - o.validate = function(section_id, value) { - if (!/^[0-9]{1,3}[smhd]$/.test(value)) { - return _('Acceptable values are an integer followed by m, h, d'); - } - return true; - } + o.validate = timevalidate; o = s.taboption('advanced', form.ListValue, 'dpdaction', _('DPD Action')); o.default = 'restart'; @@ -265,17 +254,17 @@ return view.extend({ o.rmempty = true; o.modalonly = true; o.value(''); - for (var i = 0; i < interfaces.length; i++) { - if ((interfaces[i]['proto'] == "vti") && interfaces[i]['ikey'] && interfaces[i]['okey']) { - o.value(interfaces[i]['.name'], 'VTI - ' + interfaces[i]['.name']); + interfaces.forEach(iface => { + const { proto, ikey, okey, ifid, ['.name']: name } = iface; + + if (proto === "vti" && ikey && okey) { + o.value(name, `VTI - ${name}`); } - if ((interfaces[i]['proto'] == "xfrm") - && interfaces[i]['ifid'] - && interfaces[i]['.name'].match('ipsec' + interfaces[i]['ifid'])) { - o.value(interfaces[i]['.name'], 'XFRM - ' + interfaces[i]['.name']); + if (proto === "xfrm" && ifid && name.match(`ipsec${ifid}`)) { + o.value(name, `XFRM - ${name}`); } - } + }); o = s.taboption('advanced', form.Flag, 'update_peeraddr', _('Update Peer Address'), _('Auto Update Peer Address of VTI interface')); -- 2.30.2