mirror of
https://github.com/kiddin9/op-packages.git
synced 2026-10-29 19:45:13 +08:00
🍕 Sync 2026-09-14 23:30:37
This commit is contained in:
1 parent
d6d7df32a5
commit
8a76697528
214 files changed
+10806
-2630
No files matched your search
+27
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/ucode
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
'use strict';
|
||||
import { readfile, writefile, rename, chmod } from 'fs';
|
||||
import { connect } from 'ubus';
|
||||
import { arp_leases, update_history } from 'luci.client_history';
|
||||
|
||||
const path = '/tmp/luci-client-history.json';
|
||||
const boot_id = trim(readfile('/proc/sys/kernel/random/boot_id') || '');
|
||||
if (!length(boot_id))
|
||||
exit(1);
|
||||
let previous = {};
|
||||
try { previous = json(readfile(path) || '{}'); } catch (e) {}
|
||||
const bus = connect();
|
||||
let current = bus ? bus.call('luci-rpc', 'getDHCPLeases', {}) : null;
|
||||
if (bus)
|
||||
bus.disconnect();
|
||||
current ||= {};
|
||||
current.dhcp_leases = [ ...(current.dhcp_leases || []), ...arp_leases(readfile('/proc/net/arp')) ];
|
||||
const next = sprintf('%J', update_history(previous, current, boot_id));
|
||||
if (next != readfile(path)) {
|
||||
if (writefile(path + '.new', next) != length(next))
|
||||
exit(1);
|
||||
chmod(path + '.new', 0600);
|
||||
if (!rename(path + '.new', path))
|
||||
exit(1);
|
||||
}
|
||||
@@ -4,6 +4,8 @@
|
||||
"read": {
|
||||
"file": {
|
||||
"/etc/board.json": [ "read" ],
|
||||
"/proc/net/arp": [ "read" ],
|
||||
"/tmp/luci-client-history.json": [ "read" ],
|
||||
"/proc/sys/net/netfilter/nf_conntrack_count": [ "read" ],
|
||||
"/proc/sys/net/netfilter/nf_conntrack_max": [ "read" ],
|
||||
"/www/luci-static/resources/view/status/include": [ "list" ]
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
"ubus": {
|
||||
"luci": [ "getConntrackList", "getRealtimeStats", "getCPUBench", "getCPUUsage", "getOnlineUsers" ],
|
||||
"luci-rpc": [ "getHostHints", "getNetworkDevices", "getDHCPLeases" ],
|
||||
"luci.client-rates": [ "get" ],
|
||||
"luci.client-web": [ "get" ],
|
||||
"network.rrdns": [ "lookup" ]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// Retain discovered clients in RAM for this boot, independently of the browser.
|
||||
export function arp_leases(text) {
|
||||
let leases = [];
|
||||
for (let line in split(text || '', '\n')) {
|
||||
let f = split(trim(line), /\s+/);
|
||||
if (length(f) != 6 || f[5] != 'br-lan' || !(int(f[2], 16) & 2) ||
|
||||
!match(f[0], /^\d+\.\d+\.\d+\.\d+$/) ||
|
||||
!match(f[3], /^([0-9a-f]{2}:){5}[0-9a-f]{2}$/i) ||
|
||||
f[3] == '00:00:00:00:00:00' || (int(substr(f[3], 0, 2), 16) & 1))
|
||||
continue;
|
||||
push(leases, { ipaddr: f[0], macaddr: uc(f[3]), interface: f[5] });
|
||||
}
|
||||
return leases;
|
||||
};
|
||||
|
||||
export function update_history(previous, current, boot_id) {
|
||||
if (type(previous) != 'object' || previous.version != 1 || previous.boot_id != boot_id)
|
||||
previous = {};
|
||||
let result = { version: 1, boot_id, dhcp_leases: [], dhcp6_leases: [] };
|
||||
for (let family in ['dhcp_leases', 'dhcp6_leases']) {
|
||||
let records = {};
|
||||
for (let source in [previous, current]) {
|
||||
for (let lease in type(source[family]) == 'array' ? source[family] : []) {
|
||||
if (type(lease) != 'object')
|
||||
continue;
|
||||
let mac = type(lease.macaddr) == 'string' ? uc(lease.macaddr) : '';
|
||||
let identity = mac || lc(lease.duid || '');
|
||||
let key = sprintf('%J', [identity, lease.interface || '',
|
||||
family == 'dhcp_leases' ? lease.ipaddr : [lease.duid || '', lease.iaid || '', lease.ip6addr || '']]);
|
||||
let old = records[key] || {};
|
||||
let record = { ...old, ...lease };
|
||||
if (mac)
|
||||
record.macaddr = mac;
|
||||
if (family == 'dhcp6_leases') {
|
||||
let ips = [];
|
||||
for (let item in [old, lease])
|
||||
for (let ip in type(item.ip6addrs) == 'array' ? item.ip6addrs : [])
|
||||
if (index(ips, ip) < 0)
|
||||
push(ips, ip);
|
||||
record.ip6addrs = ips;
|
||||
}
|
||||
records[key] = record;
|
||||
}
|
||||
}
|
||||
result[family] = values(records);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
Reference in new issue
Block a user