@daemonitor/plugins 1.1.17 → 1.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/plugins/index.js
CHANGED
|
@@ -9,6 +9,7 @@ import { createLogWatchPlugin } from "./monitoring/LogWatchPlugin";
|
|
|
9
9
|
import { createNetScanPlugin } from "./monitoring/NetScanPlugin";
|
|
10
10
|
import { createSnmpPlugin } from "./monitoring/SnmpPlugin";
|
|
11
11
|
import { createThreeCXPlugin } from "./monitoring/ThreeCXPlugin";
|
|
12
|
+
import { createTplinkPlugin } from "./monitoring/TplinkPlugin";
|
|
12
13
|
// In future, implement other plugin factory functions using the same pattern
|
|
13
14
|
// import { createOSPlugin } from "./monitoring/OSPlugin"
|
|
14
15
|
// import { createMongoDBPlugin } from "./monitoring/MongoDBPlugin"
|
|
@@ -29,6 +30,7 @@ export default {
|
|
|
29
30
|
Netscan: createNetScanPlugin,
|
|
30
31
|
Snmp: createSnmpPlugin,
|
|
31
32
|
ThreeCX: createThreeCXPlugin,
|
|
33
|
+
Tplink: createTplinkPlugin,
|
|
32
34
|
// CloudflarePlugin: createCloudflarePlugin,
|
|
33
35
|
// EwelinkPlugin: createEwelinkPlugin,
|
|
34
36
|
// MongoDBPlugin: createMongoDBPlugin,
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { createMonitoringPlugin } from "../../lib/MonitoringPlugin";
|
|
3
|
+
// Live sessions per device IP — re-used across polls, rebuilt when they lapse.
|
|
4
|
+
const sessions = {};
|
|
5
|
+
function slug(s) {
|
|
6
|
+
return s.replace(/[^a-zA-Z0-9_.-]+/g, "-").replace(/^-+|-+$/g, "").toLowerCase();
|
|
7
|
+
}
|
|
8
|
+
const b64u = (hex) => Buffer.from(hex, "hex").toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
9
|
+
function headers(ip, page, cookie) {
|
|
10
|
+
return {
|
|
11
|
+
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
|
12
|
+
Referer: `http://${ip}/webpages/${page}`,
|
|
13
|
+
"X-Requested-With": "XMLHttpRequest",
|
|
14
|
+
...(cookie ? { Cookie: `sysauth=${cookie}` } : {}),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
async function post(url, hdrs, body, timeout) {
|
|
18
|
+
const res = await fetch(url, { method: "POST", headers: hdrs, body, signal: AbortSignal.timeout(timeout) });
|
|
19
|
+
return await res.json();
|
|
20
|
+
}
|
|
21
|
+
/** TP-Link RSA login → { stok, sysauth }. Throws on bad credentials. */
|
|
22
|
+
async function login(d, timeout) {
|
|
23
|
+
const base = `http://${d.ip}/cgi-bin/luci/;stok=`;
|
|
24
|
+
const keyRes = await post(`${base}/login?form=login`, headers(d.ip, "login.html"), "operation=read", timeout);
|
|
25
|
+
const pair = keyRes?.data?.password;
|
|
26
|
+
if (!Array.isArray(pair) || pair.length < 2)
|
|
27
|
+
throw new Error("no RSA key from login form");
|
|
28
|
+
const pub = crypto.createPublicKey({
|
|
29
|
+
key: { kty: "RSA", n: b64u(pair[0]), e: b64u(pair[1]) },
|
|
30
|
+
format: "jwk",
|
|
31
|
+
});
|
|
32
|
+
const enc = crypto
|
|
33
|
+
.publicEncrypt({ key: pub, padding: crypto.constants.RSA_PKCS1_PADDING }, Buffer.from(d.password))
|
|
34
|
+
.toString("hex");
|
|
35
|
+
const res = await fetch(`${base}/login?form=login`, {
|
|
36
|
+
method: "POST",
|
|
37
|
+
headers: headers(d.ip, "login.html"),
|
|
38
|
+
body: `operation=login&password=${enc}`,
|
|
39
|
+
signal: AbortSignal.timeout(timeout),
|
|
40
|
+
});
|
|
41
|
+
const setCookie = res.headers.get("set-cookie") || "";
|
|
42
|
+
const body = await res.json();
|
|
43
|
+
const stok = body?.data?.stok;
|
|
44
|
+
const sysauth = (setCookie.match(/sysauth=([^;]+)/) || [])[1];
|
|
45
|
+
if (!stok || !sysauth) {
|
|
46
|
+
throw new Error(body?.errorcode === "login failed" ? "login failed (bad password?)" : "login rejected");
|
|
47
|
+
}
|
|
48
|
+
return { stok, sysauth };
|
|
49
|
+
}
|
|
50
|
+
/** Authenticated read, re-logging in once if the session has lapsed. */
|
|
51
|
+
async function read(d, endpoint, timeout) {
|
|
52
|
+
const call = async (s) => post(`http://${d.ip}/cgi-bin/luci/;stok=${s.stok}/${endpoint}`, headers(d.ip, "index.html", s.sysauth), "operation=read", timeout);
|
|
53
|
+
let s = sessions[d.ip];
|
|
54
|
+
if (s) {
|
|
55
|
+
try {
|
|
56
|
+
const out = await call(s);
|
|
57
|
+
if (out?.success)
|
|
58
|
+
return out.data;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
/* fall through to re-login */
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
s = await login(d, timeout);
|
|
65
|
+
sessions[d.ip] = s;
|
|
66
|
+
const out = await call(s);
|
|
67
|
+
if (!out?.success)
|
|
68
|
+
throw new Error(out?.errorcode || "read failed");
|
|
69
|
+
return out.data;
|
|
70
|
+
}
|
|
71
|
+
async function collect(d, timeout) {
|
|
72
|
+
try {
|
|
73
|
+
const status = await read(d, "admin/status?form=ap_status", timeout);
|
|
74
|
+
// Firmware is static — nice for drift, but never fail the poll over it.
|
|
75
|
+
let fw = {};
|
|
76
|
+
try {
|
|
77
|
+
fw = (await read(d, "admin/firmware?form=upgrade", timeout)) || {};
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
/* non-fatal */
|
|
81
|
+
}
|
|
82
|
+
const grid = status?.wirelessGrid;
|
|
83
|
+
const clients = Array.isArray(grid)
|
|
84
|
+
? grid.map((c) => ({
|
|
85
|
+
name: c.name || c.hostname || "device",
|
|
86
|
+
mac: c.mac || "",
|
|
87
|
+
band: c.type || "",
|
|
88
|
+
}))
|
|
89
|
+
: [];
|
|
90
|
+
const radio2g = String(status?.wireless_2g_enable ?? "") === "on";
|
|
91
|
+
const radio5g = String(status?.wireless_5g_enable ?? "") === "on";
|
|
92
|
+
return {
|
|
93
|
+
name: d.name,
|
|
94
|
+
ip: d.ip,
|
|
95
|
+
kind: "extender",
|
|
96
|
+
reachable: true,
|
|
97
|
+
// The uplink — an extender with this down still pings but serves nobody.
|
|
98
|
+
internetStatus: status?.internet_status ?? undefined,
|
|
99
|
+
uplinkUp: status?.internet_status === "connected",
|
|
100
|
+
phyconn: status?.phyconn ?? undefined,
|
|
101
|
+
radio2g,
|
|
102
|
+
radio5g,
|
|
103
|
+
clientCount: typeof status?.wirelessCount === "number" ? status.wirelessCount : clients.length,
|
|
104
|
+
clients,
|
|
105
|
+
model: fw.model || undefined,
|
|
106
|
+
hardwareVersion: fw.hardware_version || undefined,
|
|
107
|
+
firmware: fw.firmware_version || undefined,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
catch (e) {
|
|
111
|
+
delete sessions[d.ip]; // force a fresh login next pass
|
|
112
|
+
return {
|
|
113
|
+
name: d.name,
|
|
114
|
+
ip: d.ip,
|
|
115
|
+
kind: "extender",
|
|
116
|
+
reachable: false,
|
|
117
|
+
error: String(e?.message || e).slice(0, 160),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
export function createTplinkPlugin() {
|
|
122
|
+
let refreshTimer = null;
|
|
123
|
+
const devicesOf = (plugin) => {
|
|
124
|
+
const cfg = (plugin.config || {});
|
|
125
|
+
return (Array.isArray(cfg.devices) ? cfg.devices : []).filter((d) => d?.ip && d?.password);
|
|
126
|
+
};
|
|
127
|
+
const refreshFn = async (plugin) => {
|
|
128
|
+
const cfg = (plugin.config || {});
|
|
129
|
+
const timeout = cfg.timeout || 8000;
|
|
130
|
+
await Promise.all(devicesOf(plugin).map(async (d) => {
|
|
131
|
+
const uid = d.uniqueId || `tplink-${slug(d.name || d.ip)}`;
|
|
132
|
+
try {
|
|
133
|
+
await plugin.send(await collect(d, timeout), uid);
|
|
134
|
+
}
|
|
135
|
+
catch (e) {
|
|
136
|
+
await plugin.send({ name: d.name, ip: d.ip, kind: "extender", reachable: false, error: String(e?.message || e).slice(0, 160) }, uid);
|
|
137
|
+
}
|
|
138
|
+
}));
|
|
139
|
+
};
|
|
140
|
+
const monitorFn = async (plugin) => {
|
|
141
|
+
await refreshFn(plugin);
|
|
142
|
+
refreshTimer = setInterval(() => refreshFn(plugin), plugin.config?.refreshInterval || 120000);
|
|
143
|
+
};
|
|
144
|
+
const teardownFn = async () => {
|
|
145
|
+
if (refreshTimer)
|
|
146
|
+
clearInterval(refreshTimer);
|
|
147
|
+
refreshTimer = null;
|
|
148
|
+
};
|
|
149
|
+
return createMonitoringPlugin("tplink", "tplink", "TP-Link extender/AP monitor (uplink, radios, clients, firmware)", async () => { }, monitorFn, refreshFn, teardownFn);
|
|
150
|
+
}
|
|
151
|
+
export default createTplinkPlugin;
|