optimize & bump version

This commit is contained in:
asvow
2025-04-24 22:50:00 +08:00
parent 4d6b5e104d
commit ed92314719
8 changed files with 354 additions and 373 deletions

View File

@@ -11,62 +11,51 @@
'require ui';
'require view';
function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
return view.extend({
load: function() {
return fs.exec('/sbin/ifconfig').then(function(res) {
return fs.exec('/sbin/ip', ['-s', '-j', 'ad']).then(function(res) {
if (res.code !== 0 || !res.stdout || res.stdout.trim() === '') {
ui.addNotification(null, E('p', {}, _('Unable to get interface info: %s.').format(res.message)));
return '';
return [];
}
var interfaces = res.stdout.match(/tailscale[0-9]+/g);
if (!interfaces || interfaces.length === 0)
return 'No interface online.';
try {
const interfaces = JSON.parse(res.stdout);
const tailscaleInterfaces = interfaces.filter(iface => iface.ifname.match(/tailscale[0-9]+/));
var promises = interfaces.map(function(name) {
return fs.exec('/sbin/ifconfig', [name]);
});
return Promise.all(promises).then(function(results) {
var data = results.map(function(res, index) {
if (res.code !== 0 || !res.stdout || res.stdout.trim() === '') {
ui.addNotification(null, E('p', {}, _('Unable to get interface %s info: %s.').format(interfaces[index], res.message)));
return null;
}
return {
name: interfaces[index],
stdout: res.stdout.trim()
};
}).filter(Boolean);
return data.map(function(info) {
var lines = info.stdout.split('\n');
var parsedInfo = {
name: info.name
return tailscaleInterfaces.map(iface => {
const parsedInfo = {
name: iface.ifname
};
lines.forEach(function(line) {
if (line.includes('inet addr:')) {
parsedInfo.ipv4 = line.split('inet addr:')[1].trim().split(' ')[0];
} else if (line.includes('inet6 addr:')) {
parsedInfo.ipv6 = line.split('inet6 addr:')[1].trim().split('/')[0];
} else if (line.includes('MTU:')) {
parsedInfo.mtu = line.split('MTU:')[1].trim().split(' ')[0];
} else if (line.includes('RX bytes:')) {
var rxMatch = line.match(/RX bytes:\d+ \(([\d.]+\s*[a-zA-Z]+)\)/);
if (rxMatch && rxMatch[1]) {
parsedInfo.rxBytes = rxMatch[1];
}
var txMatch = line.match(/TX bytes:\d+ \(([\d.]+\s*[a-zA-Z]+)\)/);
if (txMatch && txMatch[1]) {
parsedInfo.txBytes = txMatch[1];
}
const addr_info = iface.addr_info || [];
addr_info.forEach(addr => {
if (addr.family === 'inet') {
parsedInfo.ipv4 = addr.local;
} else if (addr.family === 'inet6') {
parsedInfo.ipv6 = addr.local;
}
});
parsedInfo.mtu = iface.mtu;
parsedInfo.rxBytes = formatBytes(iface.stats64.rx.bytes);
parsedInfo.txBytes = formatBytes(iface.stats64.tx.bytes);
return parsedInfo;
});
});
} catch (e) {
ui.addNotification(null, E('p', {}, _('Error parsing interface info: %s.').format(e.message)));
return [];
}
});
},
@@ -82,46 +71,48 @@ return view.extend({
if (!Array.isArray(data)) {
return E('div', {}, _('No interface online.'));
}
var rows = data.flatMap(function(interfaceData) {
return [
E('th', {class: 'th', colspan: '2'}, _('Network Interface Information')),
E('tr', {class: 'tr'}, [
E('td', {class: 'td left', width: '25%'}, _('Interface Name')),
E('td', {class: 'td left', width: '25%'}, interfaceData.name)
const rows = [
E('th', { class: 'th', colspan: '2' }, _('Network Interface Information'))
];
data.forEach(interfaceData => {
rows.push(
E('tr', { class: 'tr' }, [
E('td', { class: 'td left', width: '25%' }, _('Interface Name')),
E('td', { class: 'td left', width: '25%' }, interfaceData.name)
]),
E('tr', {class: 'tr'}, [
E('td', {class: 'td left', width: '25%'}, _('IPv4 Address')),
E('td', {class: 'td left', width: '25%'}, interfaceData.ipv4)
E('tr', { class: 'tr' }, [
E('td', { class: 'td left', width: '25%' }, _('IPv4 Address')),
E('td', { class: 'td left', width: '25%' }, interfaceData.ipv4)
]),
E('tr', {class: 'tr'}, [
E('td', {class: 'td left', width: '25%'}, _('IPv6 Address')),
E('td', {class: 'td left', width: '25%'}, interfaceData.ipv6)
E('tr', { class: 'tr' }, [
E('td', { class: 'td left', width: '25%' }, _('IPv6 Address')),
E('td', { class: 'td left', width: '25%' }, interfaceData.ipv6)
]),
E('tr', {class: 'tr'}, [
E('td', {class: 'td left', width: '25%'}, _('MTU')),
E('td', {class: 'td left', width: '25%'}, interfaceData.mtu)
E('tr', { class: 'tr' }, [
E('td', { class: 'td left', width: '25%' }, _('MTU')),
E('td', { class: 'td left', width: '25%' }, interfaceData.mtu)
]),
E('tr', {class: 'tr'}, [
E('td', {class: 'td left', width: '25%'}, _('Total Download')),
E('td', {class: 'td left', width: '25%'}, interfaceData.rxBytes)
E('tr', { class: 'tr' }, [
E('td', { class: 'td left', width: '25%' }, _('Total Download')),
E('td', { class: 'td left', width: '25%' }, interfaceData.rxBytes)
]),
E('tr', {class: 'tr'}, [
E('td', {class: 'td left', width: '25%'}, _('Total Upload')),
E('td', {class: 'td left', width: '25%'}, interfaceData.txBytes)
E('tr', { class: 'tr' }, [
E('td', { class: 'td left', width: '25%' }, _('Total Upload')),
E('td', { class: 'td left', width: '25%' }, interfaceData.txBytes)
])
];
);
});
return E('table', { 'class': 'table' }, rows);
},
render: function(data) {
var content = E([], [
E('h2', {class: 'content'}, _('Tailscale')),
E('div', {class: 'cbi-map-descr'}, _('Tailscale is a cross-platform and easy to use virtual LAN.')),
const content = E([], [
E('h2', { class: 'content' }, _('Tailscale')),
E('div', { class: 'cbi-map-descr' }, _('Tailscale is a cross-platform and easy to use virtual LAN.')),
E('div')
]);
var container = content.lastElementChild;
const container = content.lastElementChild;
dom.content(container, this.renderContent(data));
this.pollData(container);

View File

@@ -6,6 +6,7 @@
'use strict';
'require form';
'require fs';
'require network';
'require poll';
'require rpc';
'require uci';
@@ -18,49 +19,22 @@ var callServiceList = rpc.declare({
expect: { '': {} }
});
function callInterfaceStatus(interfaceName) {
return rpc.declare({
object: `network.interface.${interfaceName}`,
method: 'status',
params: ['name'],
expect: { '': {} }
});
}
function getInterfaceSubnets(interfaces = ['lan', 'wan']) {
const calculateSubnetAndCIDR = (ip, cidr) => {
const cidrInt = parseInt(cidr, 10);
const maskBinary = '1'.repeat(cidrInt).padEnd(32, '0');
const ipBinary = (ip) =>
ip.split('.').map(octet => parseInt(octet, 10).toString(2).padStart(8, '0'))
.join('');
const subnetBinary = ipBinary(ip).split('').map((bit, index) =>
(bit === '1' && maskBinary[index] === '1') ? '1' : '0'
).join('');
const subnet = [
parseInt(subnetBinary.slice(0, 8), 2),
parseInt(subnetBinary.slice(8, 16), 2),
parseInt(subnetBinary.slice(16, 24), 2),
parseInt(subnetBinary.slice(24, 32), 2)
].join('.');
return `${subnet}/${cidrInt}`;
};
const rpcCalls = interfaces.map(interfaceName => {
const callStatus = callInterfaceStatus(interfaceName);
return callStatus('ipv4-address').catch(() => ({ 'ipv4-address': [] }));
});
return Promise.all(rpcCalls)
.then(res => {
const interfaceSubnets = res.flatMap(status =>
(status['ipv4-address'] || []).map(addr => {
return calculateSubnetAndCIDR(addr.address, addr.mask)
})
);
return [...new Set(interfaceSubnets)];
})
.catch(() => []);
return network.getNetworks().then(networks => {
return [...new Set(
networks
.filter(ifc => interfaces.includes(ifc.getName()))
.flatMap(ifc => ifc.getIPAddrs())
.filter(addr => addr.includes('/'))
.map(addr => {
const [ip, cidr] = addr.split('/');
const ipParts = ip.split('.').map(Number);
const mask = ~((1 << (32 - parseInt(cidr))) - 1);
const subnetParts = ipParts.map((part, i) => (part & (mask >> (24 - i * 8))) & 255);
return `${subnetParts.join('.')}/${cidr}`;
})
)];
});
}
function getStatus() {
@@ -111,10 +85,10 @@ function renderLogin(loginStatus, authURL, displayName) {
var spanTemp = '<span style="color:%s">%s</span>';
var renderHTML;
if (loginStatus == "NeedsLogin") {
renderHTML = String.format('<a href="%s" target="_blank">%s</a>', authURL, _('Needs Login'));
renderHTML = String.format('<a href="%s" target="_blank">%s</a>', authURL, _('Need to log in'));
} else if (loginStatus == "Running") {
renderHTML = String.format('<a href="%s" target="_blank">%s</a>', 'https://login.tailscale.com/admin/machines', displayName);
renderHTML += String.format('<br><a style="color:green" id="logout_button">%s</a>', _('Logout and Unbind'));
renderHTML += String.format('<br><a style="color:green" id="logout_button">%s</a>', _('Log out and Unbind'));
} else {
renderHTML = String.format(spanTemp, 'orange', _('NOT RUNNING'));
}
@@ -152,7 +126,7 @@ return view.extend({
var logoutButton = document.getElementById('logout_button');
if (logoutButton) {
logoutButton.onclick = function() {
if (confirm(_('Are you sure you want to logout and unbind the current device?'))) {
if (confirm(_('Are you sure you want to log out and unbind the current device?'))) {
fs.exec("/usr/sbin/tailscale", ["logout"]);
}
}
@@ -221,7 +195,7 @@ return view.extend({
o = s.taboption('advance', form.ListValue, 'exitNode', _('Online Exit Nodes'), _('Select an online machine name to use as an exit node.'));
if (onlineExitNodes.length > 0) {
o.value('', _('-- Please choose --'));
o.optional = false;
onlineExitNodes.forEach(function(node) {
o.value(node, node);
});