@usex/mikrotik-mcp 5.14.0 → 5.16.0
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/cli.js +301 -65
- package/dist/index.js +1 -1
- package/dist/shared/{library-hdt3dj74.js → library-0ysgbdj6.js} +2 -2
- package/dist/shared/{library-pj8kwdeg.js → library-pdyv4a32.js} +1 -1
- package/dist/ui/observability.html +67 -67
- package/package.json +1 -1
- package/schemas/tool-catalog.json +1 -1
- /package/dist/shared/{cli-28qevt9d.js → cli-gee7615j.js} +0 -0
package/dist/cli.js
CHANGED
|
@@ -19980,31 +19980,27 @@ function normalizeMac(mac) {
|
|
|
19980
19980
|
var resolvedSource = new Map;
|
|
19981
19981
|
var firstLine = (s) => s.trim().split(`
|
|
19982
19982
|
`)[0].trim();
|
|
19983
|
-
async function
|
|
19984
|
-
const out = await executeMikrotikCommand("/ip accounting
|
|
19985
|
-
|
|
19986
|
-
|
|
19987
|
-
|
|
19988
|
-
|
|
19989
|
-
|
|
19990
|
-
|
|
19991
|
-
|
|
19992
|
-
return null;
|
|
19993
|
-
const days = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"].map((d) => `${d}=0s-1d`).join(" ");
|
|
19994
|
-
const add = await executeMikrotikCommand(`/ip kid-control add name=mcp-monitor ${days}`, ctx);
|
|
19995
|
-
return looksLikeError(add) ? firstLine(add) : null;
|
|
19983
|
+
async function checkAccounting(ctx) {
|
|
19984
|
+
const out = await executeMikrotikCommand("/ip accounting print", ctx);
|
|
19985
|
+
if (looksLikeError(out))
|
|
19986
|
+
return firstLine(out);
|
|
19987
|
+
return /\benabled\s*[:=]\s*yes\b/.test(out) ? null : "Accounting is not enabled";
|
|
19988
|
+
}
|
|
19989
|
+
async function checkKidControl(ctx) {
|
|
19990
|
+
const count = await executeMikrotikCommand("/ip kid-control device print count-only", ctx);
|
|
19991
|
+
return looksLikeError(count) ? firstLine(count) : null;
|
|
19996
19992
|
}
|
|
19997
19993
|
async function resolveSource(ctx) {
|
|
19998
19994
|
const key = ctx.device ?? "";
|
|
19999
19995
|
const cached = resolvedSource.get(key);
|
|
20000
19996
|
if (cached)
|
|
20001
19997
|
return { source: cached };
|
|
20002
|
-
const accErr = await
|
|
19998
|
+
const accErr = await checkAccounting(ctx);
|
|
20003
19999
|
if (!accErr) {
|
|
20004
20000
|
resolvedSource.set(key, "accounting");
|
|
20005
20001
|
return { source: "accounting" };
|
|
20006
20002
|
}
|
|
20007
|
-
const kidErr = await
|
|
20003
|
+
const kidErr = await checkKidControl(ctx);
|
|
20008
20004
|
if (!kidErr) {
|
|
20009
20005
|
resolvedSource.set(key, "kid-control");
|
|
20010
20006
|
return { source: "kid-control" };
|
|
@@ -20162,25 +20158,29 @@ async function sampleAccounting(ctx, ts) {
|
|
|
20162
20158
|
function parseKidDevices(rows) {
|
|
20163
20159
|
const hosts = {};
|
|
20164
20160
|
for (const row of rows) {
|
|
20165
|
-
const
|
|
20166
|
-
|
|
20167
|
-
continue;
|
|
20168
|
-
hosts[ip] = {
|
|
20161
|
+
const addresses = (row["ip-address"] ?? "").split(",").map((ip) => ip.trim()).filter(Boolean);
|
|
20162
|
+
const traffic = {
|
|
20169
20163
|
rxRate: parseMagnitude(row["rate-down"]),
|
|
20170
20164
|
txRate: parseMagnitude(row["rate-up"]),
|
|
20171
|
-
rxBytes: parseMagnitude(row["bytes-down"]),
|
|
20172
|
-
txBytes: parseMagnitude(row["bytes-up"])
|
|
20165
|
+
rxBytes: parseSize(row["bytes-down"]) ?? parseMagnitude(row["bytes-down"]),
|
|
20166
|
+
txBytes: parseSize(row["bytes-up"]) ?? parseMagnitude(row["bytes-up"])
|
|
20173
20167
|
};
|
|
20168
|
+
for (const ip of addresses)
|
|
20169
|
+
hosts[ip] = traffic;
|
|
20174
20170
|
}
|
|
20175
20171
|
return hosts;
|
|
20176
20172
|
}
|
|
20173
|
+
function parseKidControlOutput(output) {
|
|
20174
|
+
const unwrapped = output.replace(/(ip-address=)([\da-fA-F:.]+(?:,\s*[\da-fA-F:.]+)*)/g, (_match, key, value) => `${key}${value.replace(/\s+/g, "")}`);
|
|
20175
|
+
return parseKidDevices(parseRecords(unwrapped).rows);
|
|
20176
|
+
}
|
|
20177
20177
|
async function sampleKidControl(ctx) {
|
|
20178
|
-
const out = await executeMikrotikCommand("/ip kid-control device print detail", ctx);
|
|
20178
|
+
const out = await executeMikrotikCommand("/ip kid-control device print detail without-paging", ctx);
|
|
20179
20179
|
if (looksLikeError(out))
|
|
20180
20180
|
throw new Error(firstLine(out));
|
|
20181
20181
|
if (isEmpty(out))
|
|
20182
20182
|
return {};
|
|
20183
|
-
return
|
|
20183
|
+
return parseKidControlOutput(out);
|
|
20184
20184
|
}
|
|
20185
20185
|
async function sampleAllTraffic(ctx) {
|
|
20186
20186
|
const ts = Date.now();
|
|
@@ -20190,8 +20190,15 @@ async function sampleAllTraffic(ctx) {
|
|
|
20190
20190
|
return { ts, source, note, hosts: {}, limits };
|
|
20191
20191
|
try {
|
|
20192
20192
|
const hosts = source === "accounting" ? await sampleAccounting(ctx, ts) : await sampleKidControl(ctx);
|
|
20193
|
-
return {
|
|
20193
|
+
return {
|
|
20194
|
+
ts,
|
|
20195
|
+
source,
|
|
20196
|
+
hosts,
|
|
20197
|
+
limits,
|
|
20198
|
+
note: Object.keys(hosts).length ? undefined : "No per-device counters were returned. Monitoring may be inactive or no clients have been observed. Router configuration was not changed."
|
|
20199
|
+
};
|
|
20194
20200
|
} catch (e) {
|
|
20201
|
+
resolvedSource.delete(ctx.device ?? "");
|
|
20195
20202
|
return {
|
|
20196
20203
|
ts,
|
|
20197
20204
|
source: "none",
|
|
@@ -30974,7 +30981,7 @@ var cache2 = null;
|
|
|
30974
30981
|
async function gateway() {
|
|
30975
30982
|
if (cache2)
|
|
30976
30983
|
return cache2;
|
|
30977
|
-
const { moduleCatalog } = await import("./shared/cli-
|
|
30984
|
+
const { moduleCatalog } = await import("./shared/cli-gee7615j.js");
|
|
30978
30985
|
const forIndex = [];
|
|
30979
30986
|
const byName = new Map;
|
|
30980
30987
|
for (const mod of moduleCatalog) {
|
|
@@ -53172,7 +53179,7 @@ var riskByTool = null;
|
|
|
53172
53179
|
async function riskIndex() {
|
|
53173
53180
|
if (riskByTool)
|
|
53174
53181
|
return riskByTool;
|
|
53175
|
-
const { moduleCatalog } = await import("./shared/cli-
|
|
53182
|
+
const { moduleCatalog } = await import("./shared/cli-gee7615j.js");
|
|
53176
53183
|
const index = new Map;
|
|
53177
53184
|
for (const mod of moduleCatalog) {
|
|
53178
53185
|
for (const tool of mod.tools)
|
|
@@ -63380,6 +63387,11 @@ function applyModuleToggle(filter, slug, enabled) {
|
|
|
63380
63387
|
// src/config-write.ts
|
|
63381
63388
|
import { mkdirSync as mkdirSync14, renameSync as renameSync2, writeFileSync as writeFileSync3 } from "fs";
|
|
63382
63389
|
import { dirname as dirname15 } from "path";
|
|
63390
|
+
|
|
63391
|
+
// src/config-device-draft.ts
|
|
63392
|
+
var CREDENTIAL_SOURCE = "$credentialsFrom";
|
|
63393
|
+
|
|
63394
|
+
// src/config-write.ts
|
|
63383
63395
|
var SCHEMA_REF = "./schemas/config.schema.json";
|
|
63384
63396
|
function mergeSecrets(incoming, current) {
|
|
63385
63397
|
if (incoming === REDACTED) {
|
|
@@ -63401,6 +63413,31 @@ function mergeSecrets(incoming, current) {
|
|
|
63401
63413
|
}
|
|
63402
63414
|
return incoming;
|
|
63403
63415
|
}
|
|
63416
|
+
function mergeDeviceDraft(incoming, name, devices) {
|
|
63417
|
+
if (!incoming || typeof incoming !== "object" || Array.isArray(incoming))
|
|
63418
|
+
return incoming;
|
|
63419
|
+
const { [CREDENTIAL_SOURCE]: source, ...draft } = incoming;
|
|
63420
|
+
if (source !== undefined && (typeof source !== "string" || !Object.hasOwn(devices, source)))
|
|
63421
|
+
throw new Error("Credential source is unavailable. Reopen the editor or enter new credentials.");
|
|
63422
|
+
const key = typeof source === "string" ? source : name;
|
|
63423
|
+
return mergeSecrets(draft, Object.hasOwn(devices, key) ? devices[key] : undefined);
|
|
63424
|
+
}
|
|
63425
|
+
function mergeConfigDraft(incoming, current) {
|
|
63426
|
+
if (!incoming || typeof incoming !== "object" || Array.isArray(incoming))
|
|
63427
|
+
return incoming;
|
|
63428
|
+
const draft = incoming;
|
|
63429
|
+
if (!draft.devices || typeof draft.devices !== "object" || Array.isArray(draft.devices))
|
|
63430
|
+
return mergeSecrets(incoming, current);
|
|
63431
|
+
const { devices: _devices, ...rest } = draft;
|
|
63432
|
+
const currentDevices = current?.devices ?? {};
|
|
63433
|
+
return {
|
|
63434
|
+
...mergeSecrets(rest, current),
|
|
63435
|
+
devices: Object.fromEntries(Object.entries(draft.devices).map(([name, device]) => [
|
|
63436
|
+
name,
|
|
63437
|
+
mergeDeviceDraft(device, name, currentDevices)
|
|
63438
|
+
]))
|
|
63439
|
+
};
|
|
63440
|
+
}
|
|
63404
63441
|
function serializeConfig(config) {
|
|
63405
63442
|
const body = config && typeof config === "object" && !Array.isArray(config) ? config : {};
|
|
63406
63443
|
const { $schema: _drop, ...rest } = body;
|
|
@@ -64164,6 +64201,184 @@ function subscribeTraffic(device, fn) {
|
|
|
64164
64201
|
};
|
|
64165
64202
|
}
|
|
64166
64203
|
|
|
64204
|
+
// src/observability/http-error.ts
|
|
64205
|
+
import { homedir as homedir4 } from "os";
|
|
64206
|
+
var HOME = homedir4();
|
|
64207
|
+
var MAX_LEN = 300;
|
|
64208
|
+
function sanitizeMessage(raw, home, fallback) {
|
|
64209
|
+
const line = raw.split(`
|
|
64210
|
+
`, 1)[0].trim();
|
|
64211
|
+
const masked = home.length > 1 ? line.split(home).join("~") : line;
|
|
64212
|
+
if (!masked)
|
|
64213
|
+
return fallback;
|
|
64214
|
+
return masked.length > MAX_LEN ? `${masked.slice(0, MAX_LEN)}\u2026` : masked;
|
|
64215
|
+
}
|
|
64216
|
+
function clientError(e, fallback = "unknown error") {
|
|
64217
|
+
return sanitizeMessage(e instanceof Error ? e.message : String(e), HOME, fallback);
|
|
64218
|
+
}
|
|
64219
|
+
function logError(e) {
|
|
64220
|
+
if (e instanceof Error)
|
|
64221
|
+
return e.stack ?? e.message;
|
|
64222
|
+
return String(e);
|
|
64223
|
+
}
|
|
64224
|
+
|
|
64225
|
+
// src/observability/interface-stats.ts
|
|
64226
|
+
var INTERVAL = 2000;
|
|
64227
|
+
var counter2 = (value) => {
|
|
64228
|
+
const n = parseSize(value);
|
|
64229
|
+
return n != null && Number.isFinite(n) && n >= 0 ? n : null;
|
|
64230
|
+
};
|
|
64231
|
+
function parseInterfaceStats(detail, stats) {
|
|
64232
|
+
const normalized = stats.replace(/((?:rx|tx|fp-rx|fp-tx)-(?:byte|packet|error|drop|queue-drop)=)(\d+(?:[ \t]+\d{3})*)(?=\s|$)/g, (_match, key, value) => key + value.replace(/[ \t]/g, ""));
|
|
64233
|
+
const counters = new Map(parseRecords(normalized).rows.map((r) => [r.name, r]));
|
|
64234
|
+
return parseRecords(detail).rows.filter((r) => r.name).map((r) => {
|
|
64235
|
+
const s = counters.get(r.name) ?? {};
|
|
64236
|
+
const flags = s.flags ?? r.flags ?? "";
|
|
64237
|
+
return {
|
|
64238
|
+
name: r.name,
|
|
64239
|
+
type: r.type || "unknown",
|
|
64240
|
+
comment: r.comment || "",
|
|
64241
|
+
running: isYes(r.running ?? "") || flags.includes("R"),
|
|
64242
|
+
disabled: isYes(r.disabled ?? "") || flags.includes("X"),
|
|
64243
|
+
dynamic: isYes(r.dynamic ?? "") || flags.includes("D"),
|
|
64244
|
+
mtu: counter2(r["actual-mtu"] ?? r.mtu),
|
|
64245
|
+
mac: r["mac-address"] || "",
|
|
64246
|
+
rxBytes: counter2(s["rx-byte"]),
|
|
64247
|
+
txBytes: counter2(s["tx-byte"]),
|
|
64248
|
+
rxPackets: counter2(s["rx-packet"]),
|
|
64249
|
+
txPackets: counter2(s["tx-packet"]),
|
|
64250
|
+
rxErrors: counter2(s["rx-error"]),
|
|
64251
|
+
txErrors: counter2(s["tx-error"]),
|
|
64252
|
+
rxDrops: counter2(s["rx-drop"]),
|
|
64253
|
+
txDrops: counter2(s["tx-drop"]),
|
|
64254
|
+
queueDrops: counter2(s["tx-queue-drop"]),
|
|
64255
|
+
linkDowns: counter2(s["link-downs"] ?? r["link-downs"]),
|
|
64256
|
+
rxRate: null,
|
|
64257
|
+
txRate: null,
|
|
64258
|
+
rxPps: null,
|
|
64259
|
+
txPps: null
|
|
64260
|
+
};
|
|
64261
|
+
});
|
|
64262
|
+
}
|
|
64263
|
+
function interfaceRates(current, previous) {
|
|
64264
|
+
if (!previous || previous.error || previous.device !== current.device)
|
|
64265
|
+
return current;
|
|
64266
|
+
const seconds = (current.ts - previous.ts) / 1000;
|
|
64267
|
+
if (seconds <= 0 || seconds > 30)
|
|
64268
|
+
return current;
|
|
64269
|
+
const old = new Map(previous.interfaces.map((row) => [row.name, row]));
|
|
64270
|
+
const rate = (a, b, multiplier = 1) => a != null && b != null && a >= b ? (a - b) * multiplier / seconds : null;
|
|
64271
|
+
return {
|
|
64272
|
+
...current,
|
|
64273
|
+
interfaces: current.interfaces.map((row) => {
|
|
64274
|
+
const before = old.get(row.name);
|
|
64275
|
+
if (!before || before.type !== row.type || before.mac !== row.mac)
|
|
64276
|
+
return row;
|
|
64277
|
+
return {
|
|
64278
|
+
...row,
|
|
64279
|
+
rxRate: rate(row.rxBytes, before.rxBytes, 8),
|
|
64280
|
+
txRate: rate(row.txBytes, before.txBytes, 8),
|
|
64281
|
+
rxPps: rate(row.rxPackets, before.rxPackets),
|
|
64282
|
+
txPps: rate(row.txPackets, before.txPackets)
|
|
64283
|
+
};
|
|
64284
|
+
})
|
|
64285
|
+
};
|
|
64286
|
+
}
|
|
64287
|
+
async function readInterfaces(device) {
|
|
64288
|
+
const ctx = createContext(undefined, device);
|
|
64289
|
+
const read = async (mode) => {
|
|
64290
|
+
const output = await executeMikrotikCommand(new Cmd("/interface print").raw(mode).raw("without-paging").build(), ctx);
|
|
64291
|
+
if (looksLikeError(output))
|
|
64292
|
+
throw new Error(output);
|
|
64293
|
+
return output;
|
|
64294
|
+
};
|
|
64295
|
+
const detail = await read("detail");
|
|
64296
|
+
const stats = await read("stats-detail");
|
|
64297
|
+
const interfaces = parseInterfaceStats(detail, stats);
|
|
64298
|
+
if (!interfaces.length && detail.trim())
|
|
64299
|
+
throw new Error("No readable interface records returned");
|
|
64300
|
+
return { device, ts: Date.now(), interfaces };
|
|
64301
|
+
}
|
|
64302
|
+
var hubs2 = new Map;
|
|
64303
|
+
function subscribeInterfaces(target, listener) {
|
|
64304
|
+
const device = resolveDeviceName(target);
|
|
64305
|
+
let hub = hubs2.get(device);
|
|
64306
|
+
if (!hub) {
|
|
64307
|
+
hub = { listeners: new Set };
|
|
64308
|
+
hubs2.set(device, hub);
|
|
64309
|
+
const active = hub;
|
|
64310
|
+
const poll = async () => {
|
|
64311
|
+
let sample;
|
|
64312
|
+
try {
|
|
64313
|
+
sample = interfaceRates(await readInterfaces(device), active.last);
|
|
64314
|
+
} catch (error) {
|
|
64315
|
+
sample = { device, ts: Date.now(), interfaces: [], error: clientError(error) };
|
|
64316
|
+
}
|
|
64317
|
+
if (hubs2.get(device) !== active)
|
|
64318
|
+
return;
|
|
64319
|
+
active.last = sample;
|
|
64320
|
+
for (const fn of active.listeners)
|
|
64321
|
+
fn(sample);
|
|
64322
|
+
active.timer = setTimeout(() => void poll(), INTERVAL);
|
|
64323
|
+
};
|
|
64324
|
+
poll();
|
|
64325
|
+
}
|
|
64326
|
+
hub.listeners.add(listener);
|
|
64327
|
+
if (hub.last)
|
|
64328
|
+
listener(hub.last);
|
|
64329
|
+
const active = hub;
|
|
64330
|
+
return () => {
|
|
64331
|
+
active.listeners.delete(listener);
|
|
64332
|
+
if (!active.listeners.size) {
|
|
64333
|
+
clearTimeout(active.timer);
|
|
64334
|
+
if (hubs2.get(device) === active)
|
|
64335
|
+
hubs2.delete(device);
|
|
64336
|
+
}
|
|
64337
|
+
};
|
|
64338
|
+
}
|
|
64339
|
+
function interfaceStatsResponse(req, target) {
|
|
64340
|
+
const device = resolveDeviceName(target);
|
|
64341
|
+
let unsubscribe;
|
|
64342
|
+
let heartbeat;
|
|
64343
|
+
const cleanup = () => {
|
|
64344
|
+
unsubscribe?.();
|
|
64345
|
+
clearInterval(heartbeat);
|
|
64346
|
+
req.signal.removeEventListener("abort", cleanup);
|
|
64347
|
+
};
|
|
64348
|
+
const stream = new ReadableStream({
|
|
64349
|
+
start(controller) {
|
|
64350
|
+
const encoder = new TextEncoder;
|
|
64351
|
+
const send = (text) => {
|
|
64352
|
+
try {
|
|
64353
|
+
controller.enqueue(encoder.encode(text));
|
|
64354
|
+
} catch {
|
|
64355
|
+
cleanup();
|
|
64356
|
+
}
|
|
64357
|
+
};
|
|
64358
|
+
if (req.signal.aborted) {
|
|
64359
|
+
controller.close();
|
|
64360
|
+
return;
|
|
64361
|
+
}
|
|
64362
|
+
unsubscribe = subscribeInterfaces(device, (sample) => send(`event: interfaces
|
|
64363
|
+
data: ${JSON.stringify(sample)}
|
|
64364
|
+
|
|
64365
|
+
`));
|
|
64366
|
+
heartbeat = setInterval(() => send(`: ping
|
|
64367
|
+
|
|
64368
|
+
`), 15000);
|
|
64369
|
+
req.signal.addEventListener("abort", cleanup, { once: true });
|
|
64370
|
+
},
|
|
64371
|
+
cancel: cleanup
|
|
64372
|
+
});
|
|
64373
|
+
return new Response(stream, {
|
|
64374
|
+
headers: {
|
|
64375
|
+
"content-type": "text/event-stream; charset=utf-8",
|
|
64376
|
+
"cache-control": "no-cache, no-transform",
|
|
64377
|
+
connection: "keep-alive"
|
|
64378
|
+
}
|
|
64379
|
+
});
|
|
64380
|
+
}
|
|
64381
|
+
|
|
64167
64382
|
// src/observability/store.ts
|
|
64168
64383
|
import { mkdirSync as mkdirSync16 } from "fs";
|
|
64169
64384
|
import { dirname as dirname16 } from "path";
|
|
@@ -64526,6 +64741,8 @@ function dailyUsageFromSamples(samples) {
|
|
|
64526
64741
|
for (let i = 1;i < samples.length; i++) {
|
|
64527
64742
|
const prev = samples[i - 1];
|
|
64528
64743
|
const cur = samples[i];
|
|
64744
|
+
if ((prev.source ?? "queue") !== (cur.source ?? "queue"))
|
|
64745
|
+
continue;
|
|
64529
64746
|
const dRx = cur.rx >= prev.rx ? cur.rx - prev.rx : cur.rx;
|
|
64530
64747
|
const dTx = cur.tx >= prev.tx ? cur.tx - prev.tx : cur.tx;
|
|
64531
64748
|
const day = dayOf(cur.ts);
|
|
@@ -64542,7 +64759,8 @@ var SCHEMA_STATEMENTS8 = [
|
|
|
64542
64759
|
subject TEXT NOT NULL,
|
|
64543
64760
|
ts INTEGER NOT NULL,
|
|
64544
64761
|
rx INTEGER NOT NULL,
|
|
64545
|
-
tx INTEGER NOT NULL
|
|
64762
|
+
tx INTEGER NOT NULL,
|
|
64763
|
+
source TEXT NOT NULL DEFAULT 'queue'
|
|
64546
64764
|
)`,
|
|
64547
64765
|
"CREATE INDEX IF NOT EXISTS idx_usage_sub ON usage_samples(device, subject, ts)",
|
|
64548
64766
|
"CREATE INDEX IF NOT EXISTS idx_usage_ts ON usage_samples(ts)",
|
|
@@ -64571,14 +64789,25 @@ class SqliteUsageStore {
|
|
|
64571
64789
|
db.run("PRAGMA synchronous = NORMAL");
|
|
64572
64790
|
for (const stmt of SCHEMA_STATEMENTS8)
|
|
64573
64791
|
db.run(stmt);
|
|
64792
|
+
const columns = db.query("PRAGMA table_info(usage_samples)").all();
|
|
64793
|
+
if (!columns.some((column) => column.name === "source")) {
|
|
64794
|
+
db.run("ALTER TABLE usage_samples ADD COLUMN source TEXT NOT NULL DEFAULT 'queue'");
|
|
64795
|
+
}
|
|
64574
64796
|
}
|
|
64575
64797
|
recordClientSamples(device, ts, samples) {
|
|
64576
64798
|
if (samples.length === 0)
|
|
64577
64799
|
return;
|
|
64578
|
-
const insert = this.db.query("INSERT INTO usage_samples (device, subject, ts, rx, tx) VALUES ($d,$s,$ts,$rx,$tx)");
|
|
64800
|
+
const insert = this.db.query("INSERT INTO usage_samples (device, subject, ts, rx, tx, source) VALUES ($d,$s,$ts,$rx,$tx,$source)");
|
|
64579
64801
|
const tx = this.db.transaction((rows) => {
|
|
64580
64802
|
for (const r of rows) {
|
|
64581
|
-
insert.run({
|
|
64803
|
+
insert.run({
|
|
64804
|
+
$d: device,
|
|
64805
|
+
$s: r.ip,
|
|
64806
|
+
$ts: ts,
|
|
64807
|
+
$rx: r.rx,
|
|
64808
|
+
$tx: r.tx,
|
|
64809
|
+
$source: r.source ?? "queue"
|
|
64810
|
+
});
|
|
64582
64811
|
}
|
|
64583
64812
|
});
|
|
64584
64813
|
tx(samples);
|
|
@@ -64610,7 +64839,7 @@ class SqliteUsageStore {
|
|
|
64610
64839
|
return n;
|
|
64611
64840
|
}
|
|
64612
64841
|
clientDailyUsage(device, ip, sinceTs) {
|
|
64613
|
-
const rows = this.db.query("SELECT ts, rx, tx FROM usage_samples WHERE device=$d AND subject=$s AND ts>=$since ORDER BY ts ASC").all({ $d: device, $s: ip, $since: sinceTs });
|
|
64842
|
+
const rows = this.db.query("SELECT ts, rx, tx, source FROM usage_samples WHERE device=$d AND subject=$s AND ts>=$since ORDER BY ts ASC").all({ $d: device, $s: ip, $since: sinceTs });
|
|
64614
64843
|
return dailyUsageFromSamples(rows);
|
|
64615
64844
|
}
|
|
64616
64845
|
umUserDailyUsage(device, user, sinceTs) {
|
|
@@ -64654,6 +64883,7 @@ async function openUsageStore(path) {
|
|
|
64654
64883
|
}
|
|
64655
64884
|
|
|
64656
64885
|
// src/observability/usage-sampler.ts
|
|
64886
|
+
import { isIPv4 } from "net";
|
|
64657
64887
|
var SERVER_TAG2 = "mikrotik-mcp";
|
|
64658
64888
|
var USAGE_RETENTION_MS = 93 * 24 * 60 * 60 * 1000;
|
|
64659
64889
|
var DEFAULT_USAGE_INTERVAL_MS = 60000;
|
|
@@ -64672,22 +64902,34 @@ function bytesOf(v) {
|
|
|
64672
64902
|
return parseSize(v) ?? parseLeadingNumber(v) ?? 0;
|
|
64673
64903
|
}
|
|
64674
64904
|
function ipOf(target) {
|
|
64675
|
-
|
|
64905
|
+
const [ip, mask, extra] = target.trim().split("/");
|
|
64906
|
+
return isIPv4(ip ?? "") && (!mask || mask === "32") && !extra ? ip : "";
|
|
64676
64907
|
}
|
|
64677
64908
|
async function sampleClients(store, device, ts) {
|
|
64678
64909
|
const ctx = createContext(undefined, device);
|
|
64910
|
+
const samples = new Map;
|
|
64911
|
+
const kid = await executeMikrotikCommand("/ip kid-control device print detail without-paging", ctx);
|
|
64912
|
+
if (!looksLikeError(kid) && !commandUnsupported(kid)) {
|
|
64913
|
+
for (const [ip, counter] of Object.entries(parseKidControlOutput(kid))) {
|
|
64914
|
+
if (isIPv4(ip))
|
|
64915
|
+
samples.set(ip, {
|
|
64916
|
+
ip,
|
|
64917
|
+
rx: counter.rxBytes,
|
|
64918
|
+
tx: counter.txBytes,
|
|
64919
|
+
source: "kid-control"
|
|
64920
|
+
});
|
|
64921
|
+
}
|
|
64922
|
+
}
|
|
64679
64923
|
const out = await executeMikrotikCommand("/queue simple print stats detail", ctx);
|
|
64680
|
-
|
|
64681
|
-
|
|
64682
|
-
const samples = [];
|
|
64683
|
-
for (const row of parseRecords(out).rows) {
|
|
64924
|
+
const queues = isEmpty(out) || looksLikeError(out) || commandUnsupported(out) ? [] : parseRecords(out).rows;
|
|
64925
|
+
for (const row of queues) {
|
|
64684
64926
|
const ip = ipOf(row.target ?? "");
|
|
64685
|
-
if (!ip)
|
|
64927
|
+
if (!ip || samples.has(ip) || !row.bytes)
|
|
64686
64928
|
continue;
|
|
64687
64929
|
const [tx, rx] = (row.bytes ?? "0/0").split("/");
|
|
64688
|
-
samples.
|
|
64930
|
+
samples.set(ip, { ip, rx: bytesOf(rx), tx: bytesOf(tx), source: "queue" });
|
|
64689
64931
|
}
|
|
64690
|
-
store.recordClientSamples(device, ts, samples);
|
|
64932
|
+
store.recordClientSamples(device, ts, [...samples.values()]);
|
|
64691
64933
|
}
|
|
64692
64934
|
var noUserManager = new Set;
|
|
64693
64935
|
async function ingestSessions(store, device) {
|
|
@@ -64866,27 +65108,6 @@ function computeStats(events, opts) {
|
|
|
64866
65108
|
};
|
|
64867
65109
|
}
|
|
64868
65110
|
|
|
64869
|
-
// src/observability/http-error.ts
|
|
64870
|
-
import { homedir as homedir4 } from "os";
|
|
64871
|
-
var HOME = homedir4();
|
|
64872
|
-
var MAX_LEN = 300;
|
|
64873
|
-
function sanitizeMessage(raw, home, fallback) {
|
|
64874
|
-
const line = raw.split(`
|
|
64875
|
-
`, 1)[0].trim();
|
|
64876
|
-
const masked = home.length > 1 ? line.split(home).join("~") : line;
|
|
64877
|
-
if (!masked)
|
|
64878
|
-
return fallback;
|
|
64879
|
-
return masked.length > MAX_LEN ? `${masked.slice(0, MAX_LEN)}\u2026` : masked;
|
|
64880
|
-
}
|
|
64881
|
-
function clientError(e, fallback = "unknown error") {
|
|
64882
|
-
return sanitizeMessage(e instanceof Error ? e.message : String(e), HOME, fallback);
|
|
64883
|
-
}
|
|
64884
|
-
function logError(e) {
|
|
64885
|
-
if (e instanceof Error)
|
|
64886
|
-
return e.stack ?? e.message;
|
|
64887
|
-
return String(e);
|
|
64888
|
-
}
|
|
64889
|
-
|
|
64890
65111
|
// src/observability/drift-routes.ts
|
|
64891
65112
|
var storePromise14 = null;
|
|
64892
65113
|
function getStore() {
|
|
@@ -66829,14 +67050,16 @@ async function configRoutes(req, url, admin) {
|
|
|
66829
67050
|
return json15(configSchemaJson());
|
|
66830
67051
|
}
|
|
66831
67052
|
if (p === "/api/config/validate" && req.method === "POST") {
|
|
66832
|
-
const merged =
|
|
67053
|
+
const merged = mergeConfigDraft(await readJson(req), getConfig());
|
|
66833
67054
|
return json15(validateConfig(merged));
|
|
66834
67055
|
}
|
|
66835
67056
|
if (p === "/api/config/test-device" && req.method === "POST") {
|
|
66836
67057
|
const body = await readJson(req);
|
|
66837
67058
|
const name = typeof body?.name === "string" ? body.name : "(unsaved)";
|
|
66838
|
-
const
|
|
66839
|
-
const
|
|
67059
|
+
const merged = mergeDeviceDraft(body?.config, name, getConfig().devices);
|
|
67060
|
+
const target = merged;
|
|
67061
|
+
if (![target?.host, target?.mac].some((value) => typeof value === "string" && value.trim().length > 0))
|
|
67062
|
+
return json15({ ok: false, error: "Enter a Host / IP or MAC address before testing." }, 400);
|
|
66840
67063
|
const parsed = DeviceConfigSchema.safeParse(merged);
|
|
66841
67064
|
if (!parsed.success)
|
|
66842
67065
|
return json15({ ok: false, errors: issues(parsed.error) }, 400);
|
|
@@ -66845,13 +67068,13 @@ async function configRoutes(req, url, admin) {
|
|
|
66845
67068
|
}
|
|
66846
67069
|
if (p === "/api/config/preview" && req.method === "POST") {
|
|
66847
67070
|
const before = JSON.stringify(redact(getConfig()), null, 2);
|
|
66848
|
-
const after = JSON.stringify(await readJson(req)
|
|
67071
|
+
const after = JSON.stringify(redact(mergeConfigDraft(await readJson(req), getConfig())), null, 2);
|
|
66849
67072
|
const d = diffLines(before, after, { fromLabel: "current", toLabel: "edited" });
|
|
66850
67073
|
return json15({ summary: d.summary, unified: d.unified });
|
|
66851
67074
|
}
|
|
66852
67075
|
if (p === "/api/config" && req.method === "POST") {
|
|
66853
67076
|
const body = await readJson(req);
|
|
66854
|
-
const merged =
|
|
67077
|
+
const merged = mergeConfigDraft(body?.config, getConfig());
|
|
66855
67078
|
const v = validateConfig(merged);
|
|
66856
67079
|
if (!v.ok || !v.value)
|
|
66857
67080
|
return json15({ ok: false, errors: v.errors }, 400);
|
|
@@ -66863,7 +67086,14 @@ async function configRoutes(req, url, admin) {
|
|
|
66863
67086
|
const nextDevs = Object.keys(v.value.devices);
|
|
66864
67087
|
const devicesChanged = prevDevs.length !== nextDevs.length || prevDevs.some((d) => !nextDevs.includes(d));
|
|
66865
67088
|
const res = admin.applyConfig(v.value, clampRollback(body?.rollbackMs));
|
|
66866
|
-
return json15({
|
|
67089
|
+
return json15({
|
|
67090
|
+
ok: true,
|
|
67091
|
+
...res,
|
|
67092
|
+
config: redact(v.value),
|
|
67093
|
+
devicesChanged,
|
|
67094
|
+
summary: diff.summary,
|
|
67095
|
+
unified: diff.unified
|
|
67096
|
+
});
|
|
66867
67097
|
}
|
|
66868
67098
|
if (p === "/api/config/keep" && req.method === "POST") {
|
|
66869
67099
|
const body = await readJson(req);
|
|
@@ -67612,6 +67842,12 @@ async function runDashboard(cfg, transportLabel) {
|
|
|
67612
67842
|
};
|
|
67613
67843
|
const accessSettingsRoutes = createAccessSettingsRoutes(configAdmin);
|
|
67614
67844
|
async function dashboardRoute(req, url, srv, ca, tl) {
|
|
67845
|
+
if (url.pathname === "/api/interfaces/stream" && req.method === "GET") {
|
|
67846
|
+
const device = url.searchParams.get("device");
|
|
67847
|
+
if (!device)
|
|
67848
|
+
return json15({ error: "device required" }, 400);
|
|
67849
|
+
return interfaceStatsResponse(req, device);
|
|
67850
|
+
}
|
|
67615
67851
|
if (url.pathname === "/api/stream") {
|
|
67616
67852
|
const traffic = url.searchParams.has("traffic") ? url.searchParams.get("traffic") ?? "" : undefined;
|
|
67617
67853
|
if (srv.upgrade(req, { data: { traffic } }))
|
package/dist/index.js
CHANGED
|
@@ -62,7 +62,7 @@ import {
|
|
|
62
62
|
updateSummaryLine,
|
|
63
63
|
allToolModules2,
|
|
64
64
|
selectToolModules2
|
|
65
|
-
} from "./shared/library-
|
|
65
|
+
} from "./shared/library-0ysgbdj6.js";
|
|
66
66
|
|
|
67
67
|
// node_modules/ajv/dist/compile/codegen/code.js
|
|
68
68
|
var require_code = __commonJS(function(exports) {
|
|
@@ -23775,7 +23775,7 @@ var cache2 = null;
|
|
|
23775
23775
|
async function gateway() {
|
|
23776
23776
|
if (cache2)
|
|
23777
23777
|
return cache2;
|
|
23778
|
-
const { moduleCatalog } = await import("./library-
|
|
23778
|
+
const { moduleCatalog } = await import("./library-pdyv4a32.js");
|
|
23779
23779
|
const forIndex = [];
|
|
23780
23780
|
const byName = new Map;
|
|
23781
23781
|
for (const mod of moduleCatalog) {
|
|
@@ -45725,7 +45725,7 @@ var riskByTool = null;
|
|
|
45725
45725
|
async function riskIndex() {
|
|
45726
45726
|
if (riskByTool)
|
|
45727
45727
|
return riskByTool;
|
|
45728
|
-
const { moduleCatalog } = await import("./library-
|
|
45728
|
+
const { moduleCatalog } = await import("./library-pdyv4a32.js");
|
|
45729
45729
|
const index = new Map;
|
|
45730
45730
|
for (const mod of moduleCatalog) {
|
|
45731
45731
|
for (const tool of mod.tools)
|