Skip to content

Commit

Permalink
luci-app-libreswan: small refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Donald <[email protected]>
  • Loading branch information
systemcrash committed Nov 22, 2024
1 parent 2bf802d commit a4a5cf6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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' : '' });
Expand All @@ -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;

Expand Down Expand Up @@ -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');
Expand All @@ -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;
Expand All @@ -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';
Expand Down Expand Up @@ -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'));
Expand Down

0 comments on commit a4a5cf6

Please sign in to comment.