@usex/mikrotik-mcp 6.9.0 → 6.11.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/README.md +47 -1076
- package/dist/cli.js +1254 -132
- package/dist/index.js +1 -1
- package/dist/shared/{cli-fb8k4p45.js → cli-n6heg9q6.js} +1 -1
- package/dist/shared/{cli-7en5gay6.js → cli-p3rfcsg3.js} +1 -1
- package/dist/shared/{cli-0ckmf678.js → cli-vrfw477g.js} +1 -1
- package/dist/shared/{cli-amd43kc5.js → cli-xgyy9he1.js} +1 -1
- package/dist/shared/{cli-v5tym2a3.js → cli-xkhmxe9c.js} +5222 -5140
- package/dist/shared/{library-znv72s94.js → library-dc9pv2hm.js} +1 -1
- package/dist/shared/{library-j1ptdg4x.js → library-qx9hax3s.js} +1 -1
- package/dist/shared/{library-3hjxj4ph.js → library-w2eyfdqz.js} +189 -161
- package/dist/shared/{library-5mbnzvye.js → library-y783969c.js} +1 -1
- package/dist/shared/{library-8fxp8mc8.js → library-zwdxcca2.js} +1 -1
- package/dist/ui/observability.html +222 -97
- package/package.json +35 -30
- package/schemas/config.schema.json +16 -0
- package/schemas/devtools-manifest.json +1 -1
- package/schemas/tool-catalog.json +36 -2
- package/schemas/tools/update_bandwidth_server.json +34 -0
package/dist/cli.js
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
toJSONSchema,
|
|
18
18
|
ZodError,
|
|
19
19
|
string,
|
|
20
|
+
ipv4,
|
|
20
21
|
number,
|
|
21
22
|
boolean,
|
|
22
23
|
_null,
|
|
@@ -44,8 +45,6 @@ import {
|
|
|
44
45
|
MikrotikConfigSchema2,
|
|
45
46
|
getConfigSource2,
|
|
46
47
|
loadConfig2,
|
|
47
|
-
logger,
|
|
48
|
-
deviceEndpoints,
|
|
49
48
|
onConfigChanged,
|
|
50
49
|
setConfig,
|
|
51
50
|
getConfig,
|
|
@@ -53,7 +52,16 @@ import {
|
|
|
53
52
|
deviceLabels,
|
|
54
53
|
resolveDeviceName,
|
|
55
54
|
deviceDirectory,
|
|
55
|
+
getDevice,
|
|
56
|
+
evaluateAccess,
|
|
57
|
+
previewAccessPolicy,
|
|
58
|
+
getAccessPolicy,
|
|
59
|
+
hasSessionNarrowing,
|
|
60
|
+
recentDenials,
|
|
61
|
+
assertDeviceAccess,
|
|
56
62
|
DeviceConnectionError,
|
|
63
|
+
logger,
|
|
64
|
+
deviceEndpoints,
|
|
57
65
|
isMacTelnetDevice,
|
|
58
66
|
createDeviceClient,
|
|
59
67
|
describeTransport,
|
|
@@ -61,28 +69,6 @@ import {
|
|
|
61
69
|
closeAll,
|
|
62
70
|
closeDevice,
|
|
63
71
|
poolStatus,
|
|
64
|
-
ScopeSchema,
|
|
65
|
-
RememberSchema,
|
|
66
|
-
ReviseSchema,
|
|
67
|
-
RecallSchema,
|
|
68
|
-
BrowseSchema,
|
|
69
|
-
openMemoryStore,
|
|
70
|
-
getMemoryStore,
|
|
71
|
-
closeMemoryStore,
|
|
72
|
-
installMemoryStore,
|
|
73
|
-
Cmd,
|
|
74
|
-
isEmpty,
|
|
75
|
-
looksLikeError,
|
|
76
|
-
commandUnsupported,
|
|
77
|
-
riskOf,
|
|
78
|
-
redact,
|
|
79
|
-
createContext,
|
|
80
|
-
evaluateAccess,
|
|
81
|
-
previewAccessPolicy,
|
|
82
|
-
getAccessPolicy,
|
|
83
|
-
hasSessionNarrowing,
|
|
84
|
-
recentDenials,
|
|
85
|
-
assertDeviceAccess,
|
|
86
72
|
getSafeModeManager,
|
|
87
73
|
parseKeyValues,
|
|
88
74
|
parseRouterosDate,
|
|
@@ -90,23 +76,30 @@ import {
|
|
|
90
76
|
parseRecords,
|
|
91
77
|
parseLeadingNumber,
|
|
92
78
|
executeMikrotikCommand,
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
subscriberCount,
|
|
103
|
-
startFlightRecorder,
|
|
104
|
-
stopFlightRecorder,
|
|
79
|
+
executeMikrotikJson,
|
|
80
|
+
riskOf,
|
|
81
|
+
redact,
|
|
82
|
+
createContext,
|
|
83
|
+
Cmd,
|
|
84
|
+
isEmpty,
|
|
85
|
+
flattenLiveOutput,
|
|
86
|
+
looksLikeError,
|
|
87
|
+
commandUnsupported,
|
|
105
88
|
explainUnmet,
|
|
106
89
|
serializeCapabilities,
|
|
90
|
+
isYes,
|
|
107
91
|
getCapabilities,
|
|
108
92
|
peekCapabilities,
|
|
109
93
|
invalidateCapabilities,
|
|
94
|
+
ScopeSchema,
|
|
95
|
+
RememberSchema,
|
|
96
|
+
ReviseSchema,
|
|
97
|
+
RecallSchema,
|
|
98
|
+
BrowseSchema,
|
|
99
|
+
openMemoryStore,
|
|
100
|
+
getMemoryStore,
|
|
101
|
+
closeMemoryStore,
|
|
102
|
+
installMemoryStore,
|
|
110
103
|
redactChannels,
|
|
111
104
|
setMcpAlertSender,
|
|
112
105
|
deliver,
|
|
@@ -122,7 +115,19 @@ import {
|
|
|
122
115
|
AlertEngine,
|
|
123
116
|
setAlertEngine,
|
|
124
117
|
getAlertEngine,
|
|
118
|
+
configureRecorder,
|
|
119
|
+
getEventStore,
|
|
120
|
+
subscribe,
|
|
121
|
+
subscriberCount,
|
|
125
122
|
registerTools,
|
|
123
|
+
bandwidthServerTools,
|
|
124
|
+
normalizeExport2,
|
|
125
|
+
openSnapshotStore2,
|
|
126
|
+
captureSnapshot,
|
|
127
|
+
startRoutingMonitor,
|
|
128
|
+
stopRoutingMonitor,
|
|
129
|
+
startFlightRecorder,
|
|
130
|
+
stopFlightRecorder,
|
|
126
131
|
clientCheckOrigin,
|
|
127
132
|
noteClientPresence,
|
|
128
133
|
authorizedCheck,
|
|
@@ -145,6 +150,7 @@ import {
|
|
|
145
150
|
setDeviceLabel,
|
|
146
151
|
removeDeviceLease,
|
|
147
152
|
devicesView,
|
|
153
|
+
ipv6,
|
|
148
154
|
getFlowCollector,
|
|
149
155
|
topTalkers,
|
|
150
156
|
conversations,
|
|
@@ -209,6 +215,7 @@ import {
|
|
|
209
215
|
matchAdvisories,
|
|
210
216
|
collectDeviceFacts,
|
|
211
217
|
getDeviceGeo,
|
|
218
|
+
isPublicIpLiteral,
|
|
212
219
|
sourceIpLiteral,
|
|
213
220
|
getIpGeo,
|
|
214
221
|
ipGeoRoute,
|
|
@@ -216,6 +223,7 @@ import {
|
|
|
216
223
|
stopGeoLookups,
|
|
217
224
|
IpIntelligenceInput,
|
|
218
225
|
lookupIpIntelligence,
|
|
226
|
+
decodeTzsp,
|
|
219
227
|
DEFAULT_TZSP_PORT,
|
|
220
228
|
capture,
|
|
221
229
|
OpenVpnOperationError,
|
|
@@ -342,7 +350,7 @@ import {
|
|
|
342
350
|
allToolModules2,
|
|
343
351
|
ALWAYS_ON_MODULES2,
|
|
344
352
|
selectToolModules2
|
|
345
|
-
} from "./shared/cli-
|
|
353
|
+
} from "./shared/cli-xkhmxe9c.js";
|
|
346
354
|
|
|
347
355
|
// node_modules/ajv/dist/compile/codegen/code.js
|
|
348
356
|
var require_code = __commonJS(function(exports) {
|
|
@@ -6903,6 +6911,400 @@ var require_dist = __commonJS(function(exports, module) {
|
|
|
6903
6911
|
exports.default = formatsPlugin;
|
|
6904
6912
|
});
|
|
6905
6913
|
|
|
6914
|
+
// src/bandwidth/service.ts
|
|
6915
|
+
import { join, dirname as dirname2 } from "path";
|
|
6916
|
+
|
|
6917
|
+
// src/bandwidth/model.ts
|
|
6918
|
+
var BandwidthJobSchema = object({
|
|
6919
|
+
id: string().regex(/^[a-zA-Z0-9_-]{1,64}$/),
|
|
6920
|
+
name: string().trim().min(1).max(100),
|
|
6921
|
+
source: string().min(1).max(128),
|
|
6922
|
+
destination: string().min(1).max(128),
|
|
6923
|
+
sourceInterface: string().min(1).max(128),
|
|
6924
|
+
sourceAddress: ipv4(),
|
|
6925
|
+
destinationAddress: ipv4(),
|
|
6926
|
+
protocol: _enum(["tcp", "udp"]).default("tcp"),
|
|
6927
|
+
direction: _enum(["transmit", "receive", "both"]).default("both"),
|
|
6928
|
+
durationSeconds: number().int().min(5).max(60).default(10),
|
|
6929
|
+
localTxBps: number().int().min(1).max(100000000000).default(1e7),
|
|
6930
|
+
remoteTxBps: number().int().min(1).max(100000000000).default(1e7),
|
|
6931
|
+
localUdpTxSize: number().int().min(28).max(64000).default(1500),
|
|
6932
|
+
remoteUdpTxSize: number().int().min(28).max(64000).default(1500),
|
|
6933
|
+
randomData: boolean().default(false),
|
|
6934
|
+
connectionCount: number().int().min(1).max(255).default(1),
|
|
6935
|
+
reportIntervalSeconds: number().min(0.02).max(5).refine((v) => Math.abs(v * 1000 - Math.round(v * 1000)) < 0.000000001, "Use whole milliseconds").default(1),
|
|
6936
|
+
intervalSeconds: number().int().min(300).max(604800).default(3600),
|
|
6937
|
+
enabled: boolean().default(false),
|
|
6938
|
+
nextRunAt: number().int().nonnegative().optional()
|
|
6939
|
+
}).strict();
|
|
6940
|
+
var PATH_WARNING = "Interface and source address are route expectations, not bindings. RouterOS bandwidth-test has no documented local-address parameter. Routing chooses the source and path; policy routing, NAT, firewall and route changes can affect delivery. Transmit is source \u2192 destination; receive is destination \u2192 source. Tests consume link bandwidth and router CPU. Destination server/firewall settings are never changed.";
|
|
6941
|
+
function bandwidthCommand(job, credentials) {
|
|
6942
|
+
const cmd = new Cmd("/tool bandwidth-test").set("address", job.destinationAddress).set("direction", job.direction).set("duration", `${job.durationSeconds}s`).set("interval", `${Math.round(job.reportIntervalSeconds * 1000)}ms`).set("protocol", job.protocol).set("local-tx-speed", job.localTxBps).set("remote-tx-speed", job.remoteTxBps).bool("random-data", job.randomData);
|
|
6943
|
+
if (job.protocol === "udp") {
|
|
6944
|
+
cmd.set("local-udp-tx-size", job.localUdpTxSize).set("remote-udp-tx-size", job.remoteUdpTxSize);
|
|
6945
|
+
} else
|
|
6946
|
+
cmd.set("connection-count", job.connectionCount);
|
|
6947
|
+
if (credentials)
|
|
6948
|
+
cmd.set("user", credentials.username).set("password", credentials.password);
|
|
6949
|
+
return cmd.build();
|
|
6950
|
+
}
|
|
6951
|
+
function bandwidthRate(value) {
|
|
6952
|
+
const match = value?.match(/^(\d+(?:\.\d+)?)\s*([kMGT]?)bps$/i);
|
|
6953
|
+
if (!match)
|
|
6954
|
+
return null;
|
|
6955
|
+
const valueBps = Number(match[1]) * 1000 ** ({ "": 0, k: 1, m: 2, g: 3, t: 4 }[match[2].toLowerCase()] ?? 0);
|
|
6956
|
+
return Number.isFinite(valueBps) ? valueBps : null;
|
|
6957
|
+
}
|
|
6958
|
+
function parseBandwidthResult(raw, direction) {
|
|
6959
|
+
const output = flattenLiveOutput(raw);
|
|
6960
|
+
const fields = Object.assign({}, ...output.split(/\r?\n/).map((line) => parseKeyValues(line)));
|
|
6961
|
+
const txBps = direction === "receive" ? null : bandwidthRate(fields["tx-total-average"]);
|
|
6962
|
+
const rxBps = direction === "transmit" ? null : bandwidthRate(fields["rx-total-average"]);
|
|
6963
|
+
const ok = !looksLikeError(output) && fields.status === "done testing" && (direction === "receive" || txBps !== null) && (direction === "transmit" || rxBps !== null);
|
|
6964
|
+
const lost = fields["lost-packets"];
|
|
6965
|
+
return {
|
|
6966
|
+
status: ok ? "ok" : "failed",
|
|
6967
|
+
txBps: ok ? txBps : null,
|
|
6968
|
+
rxBps: ok ? rxBps : null,
|
|
6969
|
+
lostPackets: ok && lost && /^\d+$/.test(lost) && Number.isSafeInteger(Number(lost)) ? Number(lost) : null,
|
|
6970
|
+
message: ok ? "Completed; averages are measured from the source router." : "No complete measurement. Check destination authentication, firewall, route, device mode and server availability. Partial output is not a successful test."
|
|
6971
|
+
};
|
|
6972
|
+
}
|
|
6973
|
+
function prefixLength(cidr, address) {
|
|
6974
|
+
if (!cidr)
|
|
6975
|
+
return -1;
|
|
6976
|
+
const [network, bitsText] = cidr.split("/");
|
|
6977
|
+
const bits = Number(bitsText ?? 32);
|
|
6978
|
+
if (!ipv4().safeParse(network).success || !Number.isInteger(bits) || bits < 0 || bits > 32)
|
|
6979
|
+
return -1;
|
|
6980
|
+
const number2 = (ip) => ip.split(".").reduce((acc, octet) => acc * 256 + Number(octet), 0);
|
|
6981
|
+
return Math.floor(number2(network) / 2 ** (32 - bits)) === Math.floor(number2(address) / 2 ** (32 - bits)) ? bits : -1;
|
|
6982
|
+
}
|
|
6983
|
+
function expectedRoute(job, rows) {
|
|
6984
|
+
const candidates = rows.filter((row) => (row.active === "true" || row.active === "yes" || /A/.test(row.flags ?? "")) && !/[XI]/.test(row.flags ?? "") && row.disabled !== "true" && row.disabled !== "yes" && (row["routing-table"] ?? "main") === "main" && !row["routing-mark"] && prefixLength(row["dst-address"], job.destinationAddress) >= 0);
|
|
6985
|
+
const best = Math.max(-1, ...candidates.map((row) => prefixLength(row["dst-address"], job.destinationAddress)));
|
|
6986
|
+
const matches = candidates.filter((row) => prefixLength(row["dst-address"], job.destinationAddress) === best);
|
|
6987
|
+
if (matches.length !== 1)
|
|
6988
|
+
return false;
|
|
6989
|
+
const route = matches[0];
|
|
6990
|
+
if (["true", "yes"].includes(route.blackhole) || ["blackhole", "unreachable", "prohibit"].includes(route.type) || /B/.test(route.flags ?? ""))
|
|
6991
|
+
return false;
|
|
6992
|
+
const gateway = route["immediate-gw"] || route.gateway || "";
|
|
6993
|
+
if (gateway.includes(",") || gateway.includes(";"))
|
|
6994
|
+
return false;
|
|
6995
|
+
const iface = gateway.includes("%") ? gateway.split("%").at(-1) : gateway;
|
|
6996
|
+
return iface === job.sourceInterface && (!route["pref-src"] || route["pref-src"] === job.sourceAddress);
|
|
6997
|
+
}
|
|
6998
|
+
|
|
6999
|
+
// src/bandwidth/store.ts
|
|
7000
|
+
import { mkdirSync } from "fs";
|
|
7001
|
+
import { dirname } from "path";
|
|
7002
|
+
var BANDWIDTH_CONCURRENCY = 2;
|
|
7003
|
+
var LEASE_MS = 300000;
|
|
7004
|
+
|
|
7005
|
+
class BandwidthStore {
|
|
7006
|
+
db;
|
|
7007
|
+
storeId;
|
|
7008
|
+
constructor(db) {
|
|
7009
|
+
this.db = db;
|
|
7010
|
+
db.run("PRAGMA journal_mode=WAL");
|
|
7011
|
+
db.run("PRAGMA busy_timeout=5000");
|
|
7012
|
+
db.run("CREATE TABLE IF NOT EXISTS bandwidth_jobs (id TEXT PRIMARY KEY, body TEXT NOT NULL)");
|
|
7013
|
+
db.run("CREATE TABLE IF NOT EXISTS bandwidth_runs (id TEXT PRIMARY KEY, body TEXT NOT NULL, source TEXT NOT NULL, destination TEXT NOT NULL, expires INTEGER NOT NULL)");
|
|
7014
|
+
db.run("CREATE TABLE IF NOT EXISTS bandwidth_notifications (sequence INTEGER PRIMARY KEY AUTOINCREMENT, body TEXT NOT NULL)");
|
|
7015
|
+
db.run("CREATE TABLE IF NOT EXISTS bandwidth_metadata (id TEXT PRIMARY KEY, value TEXT NOT NULL)");
|
|
7016
|
+
db.query("INSERT OR IGNORE INTO bandwidth_metadata VALUES ('storeId', ?)").run(crypto.randomUUID());
|
|
7017
|
+
this.storeId = db.query("SELECT value FROM bandwidth_metadata WHERE id='storeId'").get().value;
|
|
7018
|
+
}
|
|
7019
|
+
jobs() {
|
|
7020
|
+
return this.db.query("SELECT body FROM bandwidth_jobs ORDER BY id").all().map((r) => JSON.parse(r.body));
|
|
7021
|
+
}
|
|
7022
|
+
job(id) {
|
|
7023
|
+
const row = this.db.query("SELECT body FROM bandwidth_jobs WHERE id=?").get(id);
|
|
7024
|
+
return row ? JSON.parse(row.body) : null;
|
|
7025
|
+
}
|
|
7026
|
+
save(job) {
|
|
7027
|
+
this.db.query("INSERT OR REPLACE INTO bandwidth_jobs VALUES (?,?)").run(job.id, JSON.stringify(job));
|
|
7028
|
+
}
|
|
7029
|
+
remove(id) {
|
|
7030
|
+
this.db.query("DELETE FROM bandwidth_jobs WHERE id=?").run(id);
|
|
7031
|
+
}
|
|
7032
|
+
runs() {
|
|
7033
|
+
return this.db.query("SELECT body FROM bandwidth_runs ORDER BY rowid DESC LIMIT 2000").all().map((r) => JSON.parse(r.body));
|
|
7034
|
+
}
|
|
7035
|
+
finish(run) {
|
|
7036
|
+
this.db.transaction(() => {
|
|
7037
|
+
const row = this.db.query("SELECT body FROM bandwidth_runs WHERE id=?").get(run.id);
|
|
7038
|
+
if (!row || JSON.parse(row.body).status !== "running")
|
|
7039
|
+
return;
|
|
7040
|
+
this.db.query("UPDATE bandwidth_runs SET body=?, expires=0 WHERE id=?").run(JSON.stringify(run), run.id);
|
|
7041
|
+
this.db.query("INSERT INTO bandwidth_notifications(body) VALUES (?)").run(JSON.stringify(run));
|
|
7042
|
+
}).immediate();
|
|
7043
|
+
}
|
|
7044
|
+
recover(now) {
|
|
7045
|
+
const expired = this.db.query("SELECT body FROM bandwidth_runs WHERE expires > 0 AND expires <= ?").all(now);
|
|
7046
|
+
for (const row of expired)
|
|
7047
|
+
this.finish({
|
|
7048
|
+
...JSON.parse(row.body),
|
|
7049
|
+
status: "interrupted",
|
|
7050
|
+
finishedAt: now,
|
|
7051
|
+
message: "Host stopped before recording completion; outcome unknown. Not retried."
|
|
7052
|
+
});
|
|
7053
|
+
}
|
|
7054
|
+
claim(run, source, destination, now) {
|
|
7055
|
+
return this.db.transaction(() => {
|
|
7056
|
+
this.recover(now);
|
|
7057
|
+
const active = this.db.query("SELECT source,destination FROM bandwidth_runs WHERE expires>0").all();
|
|
7058
|
+
if (active.length >= BANDWIDTH_CONCURRENCY || active.some((r) => [r.source, r.destination].some((d) => d === source || d === destination)))
|
|
7059
|
+
return false;
|
|
7060
|
+
if (run.trigger === "schedule") {
|
|
7061
|
+
const job = this.job(run.job.id);
|
|
7062
|
+
if (!job?.enabled || job.nextRunAt > now || job.nextRunAt !== run.job.nextRunAt)
|
|
7063
|
+
return false;
|
|
7064
|
+
this.save({ ...job, nextRunAt: now + job.intervalSeconds * 1000 });
|
|
7065
|
+
}
|
|
7066
|
+
this.db.query("INSERT INTO bandwidth_runs VALUES (?,?,?,?,?)").run(run.id, JSON.stringify(run), source, destination, now + LEASE_MS);
|
|
7067
|
+
return true;
|
|
7068
|
+
}).immediate();
|
|
7069
|
+
}
|
|
7070
|
+
renew(id, now) {
|
|
7071
|
+
this.db.query("UPDATE bandwidth_runs SET expires=? WHERE id=? AND expires>0").run(now + LEASE_MS, id);
|
|
7072
|
+
}
|
|
7073
|
+
notifications(after, limit) {
|
|
7074
|
+
const rows = this.db.query("SELECT sequence,body FROM bandwidth_notifications WHERE sequence>? ORDER BY sequence LIMIT ?").all(after, limit + 1);
|
|
7075
|
+
const events = rows.slice(0, limit).map((row) => ({ sequence: row.sequence, run: JSON.parse(row.body) }));
|
|
7076
|
+
return {
|
|
7077
|
+
storeId: this.storeId,
|
|
7078
|
+
events,
|
|
7079
|
+
nextCursor: events.at(-1)?.sequence ?? after,
|
|
7080
|
+
hasMore: rows.length > limit
|
|
7081
|
+
};
|
|
7082
|
+
}
|
|
7083
|
+
close() {
|
|
7084
|
+
this.db.close();
|
|
7085
|
+
}
|
|
7086
|
+
}
|
|
7087
|
+
async function openBandwidthStore(path) {
|
|
7088
|
+
if (path !== ":memory:")
|
|
7089
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
7090
|
+
const { Database } = await import("bun:sqlite");
|
|
7091
|
+
return new BandwidthStore(new Database(path, { create: true }));
|
|
7092
|
+
}
|
|
7093
|
+
|
|
7094
|
+
// src/bandwidth/service.ts
|
|
7095
|
+
class BandwidthError extends Error {
|
|
7096
|
+
}
|
|
7097
|
+
function validateBandwidthJob(input) {
|
|
7098
|
+
const parsed = BandwidthJobSchema.safeParse(input);
|
|
7099
|
+
if (!parsed.success)
|
|
7100
|
+
throw new BandwidthError("Invalid test settings. Check IPv4 addresses, IDs, bounded duration/rates and interval (whole milliseconds).");
|
|
7101
|
+
const job = { ...parsed.data, nextRunAt: parsed.data.nextRunAt ?? 0 };
|
|
7102
|
+
try {
|
|
7103
|
+
job.source = resolveDeviceName(job.source);
|
|
7104
|
+
job.destination = resolveDeviceName(job.destination);
|
|
7105
|
+
if (routerIdentity(job.source) === routerIdentity(job.destination))
|
|
7106
|
+
throw new Error("Source and destination resolve to the same router");
|
|
7107
|
+
} catch {
|
|
7108
|
+
throw new BandwidthError("Select two different enabled configured routers.");
|
|
7109
|
+
}
|
|
7110
|
+
for (const address of [job.sourceAddress, job.destinationAddress]) {
|
|
7111
|
+
const first = Number(address.split(".")[0]);
|
|
7112
|
+
if (first === 0 || first === 127 || first >= 224 || address === "255.255.255.255")
|
|
7113
|
+
throw new BandwidthError("Use unicast tunnel IPv4 addresses.");
|
|
7114
|
+
}
|
|
7115
|
+
if (job.sourceAddress === job.destinationAddress)
|
|
7116
|
+
throw new BandwidthError("Source and destination addresses must differ.");
|
|
7117
|
+
return job;
|
|
7118
|
+
}
|
|
7119
|
+
function routerIdentity(name) {
|
|
7120
|
+
const dc = getDevice(name);
|
|
7121
|
+
return dc.mac ? `mac:${dc.mac.toLowerCase()}` : `host:${dc.host.toLowerCase()}`;
|
|
7122
|
+
}
|
|
7123
|
+
function bandwidthAccess(job, write) {
|
|
7124
|
+
try {
|
|
7125
|
+
assertDeviceAccess([job.source, job.destination], "bandwidth_test", write ? "WRITE" : "READ");
|
|
7126
|
+
} catch {
|
|
7127
|
+
throw new BandwidthError("Bandwidth test access denied by server policy or read-only mode.");
|
|
7128
|
+
}
|
|
7129
|
+
}
|
|
7130
|
+
async function bandwidthAddresses(device) {
|
|
7131
|
+
assertDeviceAccess([device], "bandwidth_test", "READ");
|
|
7132
|
+
const out = await executeMikrotikJson(new Cmd("/ip address print").raw("detail").build(), createContext(undefined, device), { maxMs: 20000 });
|
|
7133
|
+
return out.rows.filter((row) => row.address && row.interface && !/[XI]/.test(row.flags ?? "") && row.disabled !== "true" && row.disabled !== "yes" && row.invalid !== "true").map((row) => ({ interface: row.interface, address: row.address.split("/")[0] }));
|
|
7134
|
+
}
|
|
7135
|
+
async function preflightBandwidth(job) {
|
|
7136
|
+
bandwidthAccess(job, false);
|
|
7137
|
+
const checks = [];
|
|
7138
|
+
const check = (name, ok, message) => checks.push({ name, ok, message });
|
|
7139
|
+
try {
|
|
7140
|
+
const source = getDevice(job.source);
|
|
7141
|
+
const safe = getSafeModeManager(job.source);
|
|
7142
|
+
check("transport", !source.mac && !source.api && !safe.isActive && !safe.requiresRecovery, "Source must use SSH without REST or an active/recovering Safe Mode session, to enforce the test deadline.");
|
|
7143
|
+
const addresses = await bandwidthAddresses(job.source);
|
|
7144
|
+
check("source-address", addresses.some((a) => a.interface === job.sourceInterface && a.address === job.sourceAddress), "Expected source address must be assigned and enabled on the selected interface.");
|
|
7145
|
+
const interfaces = await executeMikrotikJson(new Cmd("/interface print").raw("detail").build(), createContext(undefined, job.source), { maxMs: 20000 });
|
|
7146
|
+
check("source-interface", interfaces.rows.some((r) => r.name === job.sourceInterface && (r.running === "true" || r.running === "yes" || /R/.test(r.flags ?? "")) && !/X/.test(r.flags ?? "") && r.disabled !== "true" && r.disabled !== "yes"), "Expected source interface must be enabled and running.");
|
|
7147
|
+
const destination = await bandwidthAddresses(job.destination);
|
|
7148
|
+
check("destination-address", destination.some((a) => a.address === job.destinationAddress), "Explicit destination IP must belong to the selected destination router.");
|
|
7149
|
+
const routes = await executeMikrotikJson(new Cmd("/ip route print").raw("detail").build(), createContext(undefined, job.source), { maxMs: 20000 });
|
|
7150
|
+
check("route", expectedRoute(job, routes.rows), "Require one active longest-prefix main-table route via the selected interface; if pref-src is present it must match. ECMP/unknown paths are refused. Policy routing and firewall delivery are not proven.");
|
|
7151
|
+
const raw = await bandwidthServerTools[0].handler({}, createContext(undefined, job.destination));
|
|
7152
|
+
const server = typeof raw === "string" ? parseKeyValues(raw) : {};
|
|
7153
|
+
check("server", typeof raw === "string" && !looksLikeError(raw) && ["yes", "true"].includes(server.enabled), "Destination bandwidth server must already be enabled. No server or firewall settings will be changed.");
|
|
7154
|
+
const destinationConfig = getDevice(job.destination);
|
|
7155
|
+
const credentials = destinationConfig.bandwidthAuth ?? destinationConfig;
|
|
7156
|
+
check("authentication", ["no", "false"].includes(server.authenticate) || ["yes", "true"].includes(server.authenticate) && !!credentials.username && !!credentials.password && credentials.password.length <= 32, "Authenticated tests use destination credentials from server configuration at execution time. SSH keys cannot authenticate bandwidth-test; the password must be 1\u201332 characters. No password is collected or persisted here.");
|
|
7157
|
+
} catch {
|
|
7158
|
+
check("read", false, "Read-only preflight could not finish. Check connection, permissions and RouterOS support.");
|
|
7159
|
+
}
|
|
7160
|
+
return { ready: checks.length > 0 && checks.every((c) => c.ok), checks, warning: PATH_WARNING };
|
|
7161
|
+
}
|
|
7162
|
+
var liveExecutor2 = {
|
|
7163
|
+
preflight: preflightBandwidth,
|
|
7164
|
+
async measure(job) {
|
|
7165
|
+
bandwidthAccess(job, true);
|
|
7166
|
+
const dc = getDevice(job.source);
|
|
7167
|
+
const safe = getSafeModeManager(job.source);
|
|
7168
|
+
if (dc.mac || dc.api || safe.isActive || safe.requiresRecovery)
|
|
7169
|
+
throw new BandwidthError("Source transport changed; rerun preflight.");
|
|
7170
|
+
const destination = getDevice(job.destination);
|
|
7171
|
+
const auth = destination.bandwidthAuth ?? destination;
|
|
7172
|
+
const credentials = auth.password ? { username: auth.username, password: auth.password } : undefined;
|
|
7173
|
+
const ctx = { device: job.source, info: () => {}, error: () => {} };
|
|
7174
|
+
const raw = await executeMikrotikCommand(bandwidthCommand(job, credentials), ctx, {
|
|
7175
|
+
maxMs: job.durationSeconds * 1000 + 12000
|
|
7176
|
+
});
|
|
7177
|
+
return parseBandwidthResult(raw, job.direction);
|
|
7178
|
+
}
|
|
7179
|
+
};
|
|
7180
|
+
var outstanding = new Set;
|
|
7181
|
+
var stopping = false;
|
|
7182
|
+
function launchBandwidth(store, job, trigger, executor = liveExecutor2, now = Date.now()) {
|
|
7183
|
+
if (stopping)
|
|
7184
|
+
throw new BandwidthError("Bandwidth service is shutting down.");
|
|
7185
|
+
bandwidthAccess(job, true);
|
|
7186
|
+
const source = routerIdentity(job.source);
|
|
7187
|
+
const destination = routerIdentity(job.destination);
|
|
7188
|
+
const run = {
|
|
7189
|
+
id: crypto.randomUUID(),
|
|
7190
|
+
job,
|
|
7191
|
+
startedAt: now,
|
|
7192
|
+
finishedAt: null,
|
|
7193
|
+
status: "running",
|
|
7194
|
+
trigger,
|
|
7195
|
+
txBps: null,
|
|
7196
|
+
rxBps: null,
|
|
7197
|
+
lostPackets: null,
|
|
7198
|
+
message: "Read-only preflight in progress."
|
|
7199
|
+
};
|
|
7200
|
+
if (!store.claim(run, source, destination, now))
|
|
7201
|
+
throw new BandwidthError("Routers are busy, concurrency limit reached, or this schedule was already claimed. Try later.");
|
|
7202
|
+
const done = (async () => {
|
|
7203
|
+
const heartbeat = setInterval(() => {
|
|
7204
|
+
try {
|
|
7205
|
+
store.renew(run.id, Date.now());
|
|
7206
|
+
} catch {}
|
|
7207
|
+
}, 1e4);
|
|
7208
|
+
heartbeat.unref?.();
|
|
7209
|
+
try {
|
|
7210
|
+
const preflight = await executor.preflight(job);
|
|
7211
|
+
if (!preflight.ready) {
|
|
7212
|
+
run.status = "failed";
|
|
7213
|
+
run.message = preflight.checks.filter((c) => !c.ok).map((c) => c.message).join(" ");
|
|
7214
|
+
} else {
|
|
7215
|
+
bandwidthAccess(job, true);
|
|
7216
|
+
if (source !== routerIdentity(job.source) || destination !== routerIdentity(job.destination))
|
|
7217
|
+
throw new BandwidthError("Router configuration changed during preflight; test refused.");
|
|
7218
|
+
Object.assign(run, await executor.measure(job));
|
|
7219
|
+
}
|
|
7220
|
+
} catch {
|
|
7221
|
+
run.status = "failed";
|
|
7222
|
+
run.message = "Test did not complete. Check access, transport, credentials and path preflight. Raw command/output is not stored.";
|
|
7223
|
+
} finally {
|
|
7224
|
+
clearInterval(heartbeat);
|
|
7225
|
+
run.finishedAt = Date.now();
|
|
7226
|
+
store.finish(run);
|
|
7227
|
+
}
|
|
7228
|
+
})();
|
|
7229
|
+
outstanding.add(done);
|
|
7230
|
+
done.then(() => outstanding.delete(done), () => outstanding.delete(done));
|
|
7231
|
+
return { run: { ...run }, done };
|
|
7232
|
+
}
|
|
7233
|
+
var storePromise;
|
|
7234
|
+
function bandwidthStore() {
|
|
7235
|
+
storePromise ??= openBandwidthStore(join(dirname2(getConfig().dashboard.dbPath), "bandwidth.db")).catch((error) => {
|
|
7236
|
+
storePromise = undefined;
|
|
7237
|
+
throw error;
|
|
7238
|
+
});
|
|
7239
|
+
return storePromise;
|
|
7240
|
+
}
|
|
7241
|
+
var timer;
|
|
7242
|
+
var ticking = false;
|
|
7243
|
+
function tickBandwidth(store, executor = liveExecutor2, now = Date.now()) {
|
|
7244
|
+
store.recover(now);
|
|
7245
|
+
const pending = [];
|
|
7246
|
+
for (const job of store.jobs()) {
|
|
7247
|
+
if (!job.enabled || job.nextRunAt > now)
|
|
7248
|
+
continue;
|
|
7249
|
+
try {
|
|
7250
|
+
pending.push(launchBandwidth(store, validateBandwidthJob(job), "schedule", executor, now).done);
|
|
7251
|
+
} catch {}
|
|
7252
|
+
}
|
|
7253
|
+
return pending;
|
|
7254
|
+
}
|
|
7255
|
+
async function startBandwidthScheduler() {
|
|
7256
|
+
if (timer)
|
|
7257
|
+
return;
|
|
7258
|
+
stopping = false;
|
|
7259
|
+
const store = await bandwidthStore();
|
|
7260
|
+
timer = setInterval(() => {
|
|
7261
|
+
if (ticking)
|
|
7262
|
+
return;
|
|
7263
|
+
ticking = true;
|
|
7264
|
+
try {
|
|
7265
|
+
for (const done of tickBandwidth(store))
|
|
7266
|
+
done.catch(() => logger.error("Bandwidth result could not be persisted; lease retained for recovery."));
|
|
7267
|
+
} catch {
|
|
7268
|
+
logger.error("Bandwidth scheduler tick failed; retrying on next tick.");
|
|
7269
|
+
} finally {
|
|
7270
|
+
ticking = false;
|
|
7271
|
+
}
|
|
7272
|
+
}, 5000);
|
|
7273
|
+
timer.unref?.();
|
|
7274
|
+
}
|
|
7275
|
+
async function stopBandwidthScheduler() {
|
|
7276
|
+
stopping = true;
|
|
7277
|
+
if (timer)
|
|
7278
|
+
clearInterval(timer);
|
|
7279
|
+
timer = undefined;
|
|
7280
|
+
let timeout;
|
|
7281
|
+
await Promise.race([
|
|
7282
|
+
Promise.allSettled(outstanding),
|
|
7283
|
+
new Promise((resolve) => {
|
|
7284
|
+
timeout = setTimeout(resolve, 1e4);
|
|
7285
|
+
})
|
|
7286
|
+
]);
|
|
7287
|
+
if (timeout)
|
|
7288
|
+
clearTimeout(timeout);
|
|
7289
|
+
if (!outstanding.size && storePromise) {
|
|
7290
|
+
try {
|
|
7291
|
+
(await storePromise).close();
|
|
7292
|
+
} catch {}
|
|
7293
|
+
storePromise = undefined;
|
|
7294
|
+
}
|
|
7295
|
+
}
|
|
7296
|
+
function canReadBandwidth(job) {
|
|
7297
|
+
try {
|
|
7298
|
+
bandwidthAccess(job, false);
|
|
7299
|
+
return true;
|
|
7300
|
+
} catch {
|
|
7301
|
+
return false;
|
|
7302
|
+
}
|
|
7303
|
+
}
|
|
7304
|
+
function bandwidthDevices() {
|
|
7305
|
+
return listDevices().names.filter((name) => canReadBandwidth({ source: name, destination: name }));
|
|
7306
|
+
}
|
|
7307
|
+
|
|
6906
7308
|
// src/cli.ts
|
|
6907
7309
|
import { existsSync as existsSync2 } from "fs";
|
|
6908
7310
|
|
|
@@ -6910,7 +7312,7 @@ import { existsSync as existsSync2 } from "fs";
|
|
|
6910
7312
|
import { spawn } from "child_process";
|
|
6911
7313
|
import { readFileSync as readFileSync4 } from "fs";
|
|
6912
7314
|
import { homedir as homedir5, networkInterfaces as networkInterfaces2 } from "os";
|
|
6913
|
-
import { dirname as
|
|
7315
|
+
import { dirname as dirname10, join as join9 } from "path";
|
|
6914
7316
|
var {serve } = globalThis.Bun;
|
|
6915
7317
|
|
|
6916
7318
|
// src/observability/modules.ts
|
|
@@ -6991,16 +7393,16 @@ import { createHash } from "crypto";
|
|
|
6991
7393
|
import { constants } from "fs";
|
|
6992
7394
|
import { access, lstat, open, readdir } from "fs/promises";
|
|
6993
7395
|
import { homedir } from "os";
|
|
6994
|
-
import { basename, join, resolve } from "path";
|
|
7396
|
+
import { basename, join as join2, resolve } from "path";
|
|
6995
7397
|
import { utils } from "ssh2";
|
|
6996
|
-
async function listSshKeys(configuredPaths, directory =
|
|
7398
|
+
async function listSshKeys(configuredPaths, directory = join2(homedir(), ".ssh")) {
|
|
6997
7399
|
const paths = new Set(configuredPaths.filter(Boolean).map((path) => resolve(path)));
|
|
6998
7400
|
let warning;
|
|
6999
7401
|
try {
|
|
7000
7402
|
const entries = await readdir(directory, { withFileTypes: true });
|
|
7001
7403
|
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
7002
7404
|
if (entry.isFile() && entry.name.endsWith(".pub"))
|
|
7003
|
-
paths.add(
|
|
7405
|
+
paths.add(join2(directory, entry.name.slice(0, -4)));
|
|
7004
7406
|
}
|
|
7005
7407
|
} catch (error) {
|
|
7006
7408
|
if (error.code !== "ENOENT")
|
|
@@ -7056,13 +7458,13 @@ async function listSshKeys(configuredPaths, directory = join(homedir(), ".ssh"))
|
|
|
7056
7458
|
import { createHash as createHash2 } from "crypto";
|
|
7057
7459
|
import { lstat as lstat2, mkdir, open as open2, unlink } from "fs/promises";
|
|
7058
7460
|
import { homedir as homedir2 } from "os";
|
|
7059
|
-
import { join as
|
|
7461
|
+
import { join as join3 } from "path";
|
|
7060
7462
|
import { utils as utils2 } from "ssh2";
|
|
7061
7463
|
var inputSchema = object({
|
|
7062
7464
|
name: string().regex(/^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/)
|
|
7063
7465
|
}).strict();
|
|
7064
7466
|
var creating = false;
|
|
7065
|
-
async function createSshKey(input, directory =
|
|
7467
|
+
async function createSshKey(input, directory = join3(homedir2(), ".ssh")) {
|
|
7066
7468
|
const parsed = inputSchema.safeParse(input);
|
|
7067
7469
|
if (!parsed.success)
|
|
7068
7470
|
throw new Error("Use 1\u201364 letters, digits, underscores or hyphens for the key name.");
|
|
@@ -7071,7 +7473,7 @@ async function createSshKey(input, directory = join2(homedir2(), ".ssh")) {
|
|
|
7071
7473
|
if (creating)
|
|
7072
7474
|
throw new Error("A key is already being created. Wait before trying again.");
|
|
7073
7475
|
creating = true;
|
|
7074
|
-
const path =
|
|
7476
|
+
const path = join3(directory, parsed.data.name);
|
|
7075
7477
|
let privateFile;
|
|
7076
7478
|
let publicFile;
|
|
7077
7479
|
let complete = false;
|
|
@@ -7284,14 +7686,14 @@ async function workspaceRoutes(req, url) {
|
|
|
7284
7686
|
|
|
7285
7687
|
// src/client-check/routes.ts
|
|
7286
7688
|
import { readFileSync } from "fs";
|
|
7287
|
-
import { join as
|
|
7689
|
+
import { join as join4 } from "path";
|
|
7288
7690
|
import { randomBytes } from "crypto";
|
|
7289
7691
|
var MiB = 1048576;
|
|
7290
7692
|
var budgets = new Map;
|
|
7291
7693
|
async function clientCheckRoutes(req, url, peer = "unknown") {
|
|
7292
7694
|
if (url.pathname === "/client-check" && req.method === "GET") {
|
|
7293
7695
|
try {
|
|
7294
|
-
return new Response(readFileSync(
|
|
7696
|
+
return new Response(readFileSync(join4(PROJECT_ROOT, "dist/ui/client-check.html"), "utf8"), {
|
|
7295
7697
|
headers: {
|
|
7296
7698
|
"content-type": "text/html;charset=utf-8",
|
|
7297
7699
|
"cache-control": "no-store",
|
|
@@ -7639,8 +8041,8 @@ async function openVpnRoutes(req, url, history) {
|
|
|
7639
8041
|
|
|
7640
8042
|
// src/observability/openvpn-history.ts
|
|
7641
8043
|
import { randomUUID } from "crypto";
|
|
7642
|
-
import { chmodSync, mkdirSync } from "fs";
|
|
7643
|
-
import { dirname } from "path";
|
|
8044
|
+
import { chmodSync, mkdirSync as mkdirSync2 } from "fs";
|
|
8045
|
+
import { dirname as dirname3 } from "path";
|
|
7644
8046
|
var OPENVPN_HISTORY_INTERVAL = 15000;
|
|
7645
8047
|
function sameObservedSession(previous, session, at) {
|
|
7646
8048
|
if (previous.ended !== null)
|
|
@@ -7672,6 +8074,9 @@ class OpenVpnHistoryStore {
|
|
|
7672
8074
|
device TEXT PRIMARY KEY, first INTEGER NOT NULL, last INTEGER NOT NULL,
|
|
7673
8075
|
gaps INTEGER NOT NULL DEFAULT 0, failures INTEGER NOT NULL DEFAULT 0)`);
|
|
7674
8076
|
}
|
|
8077
|
+
activeTunnelOwners(now = Date.now()) {
|
|
8078
|
+
return this.db.query("SELECT tunnel ip,user,device FROM ovpn_history WHERE ended IS NULL AND tunnel<>'' AND lastSeen>=?").all(now - 60000);
|
|
8079
|
+
}
|
|
7675
8080
|
ingest(snapshot) {
|
|
7676
8081
|
const { device, observedAt: at, sessions } = snapshot;
|
|
7677
8082
|
this.db.transaction(() => {
|
|
@@ -7772,7 +8177,7 @@ class OpenVpnHistoryStore {
|
|
|
7772
8177
|
}
|
|
7773
8178
|
}
|
|
7774
8179
|
async function openOpenVpnHistory(path) {
|
|
7775
|
-
|
|
8180
|
+
mkdirSync2(dirname3(path), { recursive: true, mode: 448 });
|
|
7776
8181
|
const { Database } = await import("bun:sqlite");
|
|
7777
8182
|
const db = new Database(path, { create: true });
|
|
7778
8183
|
chmodSync(path, 384);
|
|
@@ -7835,6 +8240,577 @@ function startOpenVpnHistorySampler(store) {
|
|
|
7835
8240
|
};
|
|
7836
8241
|
}
|
|
7837
8242
|
|
|
8243
|
+
// src/observability/site-activity.ts
|
|
8244
|
+
import { chmodSync as chmodSync2, mkdirSync as mkdirSync3 } from "fs";
|
|
8245
|
+
import { dirname as dirname4 } from "path";
|
|
8246
|
+
import { isIP as isIP2 } from "net";
|
|
8247
|
+
function validSiteHost(host) {
|
|
8248
|
+
const normalized = host.toLowerCase().replace(/\.$/, "");
|
|
8249
|
+
if (normalized.length > 253 || !/^[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?$/.test(normalized))
|
|
8250
|
+
return null;
|
|
8251
|
+
if (isIP2(normalized))
|
|
8252
|
+
return null;
|
|
8253
|
+
const labels = normalized.split(".");
|
|
8254
|
+
if (labels.length < 2 || labels.some((label) => !label || label.length > 63 || label.startsWith("-") || label.endsWith("-")))
|
|
8255
|
+
return null;
|
|
8256
|
+
return normalized;
|
|
8257
|
+
}
|
|
8258
|
+
function dnsAnswers(frame, seen) {
|
|
8259
|
+
const u16 = (at) => frame[at] << 8 | frame[at + 1];
|
|
8260
|
+
const u32 = (at) => frame[at] * 2 ** 24 + frame[at + 1] * 2 ** 16 + frame[at + 2] * 256 + frame[at + 3] >>> 0;
|
|
8261
|
+
if (frame.length < 42)
|
|
8262
|
+
return [];
|
|
8263
|
+
let l3 = 14;
|
|
8264
|
+
let eth = u16(12);
|
|
8265
|
+
if (eth === 33024) {
|
|
8266
|
+
l3 += 4;
|
|
8267
|
+
eth = u16(16);
|
|
8268
|
+
}
|
|
8269
|
+
let udp;
|
|
8270
|
+
let client;
|
|
8271
|
+
if (eth === 2048) {
|
|
8272
|
+
if (frame.length < l3 + 20 || frame[l3 + 9] !== 17 || u16(l3 + 6) & 16383)
|
|
8273
|
+
return [];
|
|
8274
|
+
udp = l3 + (frame[l3] & 15) * 4;
|
|
8275
|
+
client = `${frame[l3 + 16]}.${frame[l3 + 17]}.${frame[l3 + 18]}.${frame[l3 + 19]}`;
|
|
8276
|
+
} else if (eth === 34525) {
|
|
8277
|
+
if (frame.length < l3 + 40 || frame[l3 + 6] !== 17)
|
|
8278
|
+
return [];
|
|
8279
|
+
udp = l3 + 40;
|
|
8280
|
+
client = ipv6(frame, l3 + 24);
|
|
8281
|
+
} else
|
|
8282
|
+
return [];
|
|
8283
|
+
if (frame.length < udp + 20 || u16(udp) !== 53)
|
|
8284
|
+
return [];
|
|
8285
|
+
const end = Math.min(frame.length, udp + u16(udp + 4));
|
|
8286
|
+
const dns = udp + 8;
|
|
8287
|
+
if (end < dns + 12 || !(u16(dns + 2) & 32768) || u16(dns + 2) & 15 || u16(dns + 4) !== 1 || u16(dns + 6) > 32)
|
|
8288
|
+
return [];
|
|
8289
|
+
function name(at) {
|
|
8290
|
+
const labels = [];
|
|
8291
|
+
let next = at;
|
|
8292
|
+
let jumped = false;
|
|
8293
|
+
const visited = new Set;
|
|
8294
|
+
for (let i = 0;i < 64; i++) {
|
|
8295
|
+
if (at >= end || visited.has(at))
|
|
8296
|
+
return null;
|
|
8297
|
+
visited.add(at);
|
|
8298
|
+
const len = frame[at++];
|
|
8299
|
+
if ((len & 192) === 192) {
|
|
8300
|
+
if (at >= end)
|
|
8301
|
+
return null;
|
|
8302
|
+
const pointer = dns + ((len & 63) << 8 | frame[at++]);
|
|
8303
|
+
if (pointer >= end || pointer < dns)
|
|
8304
|
+
return null;
|
|
8305
|
+
if (!jumped)
|
|
8306
|
+
next = at;
|
|
8307
|
+
jumped = true;
|
|
8308
|
+
at = pointer;
|
|
8309
|
+
continue;
|
|
8310
|
+
}
|
|
8311
|
+
if (len & 192)
|
|
8312
|
+
return null;
|
|
8313
|
+
if (!len)
|
|
8314
|
+
return { value: labels.join("."), next: jumped ? next : at };
|
|
8315
|
+
if (len > 63 || at + len > end)
|
|
8316
|
+
return null;
|
|
8317
|
+
labels.push(String.fromCharCode(...frame.subarray(at, at + len)));
|
|
8318
|
+
at += len;
|
|
8319
|
+
}
|
|
8320
|
+
return null;
|
|
8321
|
+
}
|
|
8322
|
+
const question = name(dns + 12);
|
|
8323
|
+
if (!question || question.next + 4 > end)
|
|
8324
|
+
return [];
|
|
8325
|
+
const domain = validSiteHost(question.value);
|
|
8326
|
+
if (!domain)
|
|
8327
|
+
return [];
|
|
8328
|
+
let at = question.next + 4;
|
|
8329
|
+
const answers = [];
|
|
8330
|
+
for (let i = 0;i < u16(dns + 6); i++) {
|
|
8331
|
+
const rr = name(at);
|
|
8332
|
+
if (!rr || rr.next + 10 > end)
|
|
8333
|
+
return [];
|
|
8334
|
+
at = rr.next;
|
|
8335
|
+
const kind = u16(at), klass = u16(at + 2), ttl = u32(at + 4), size = u16(at + 8);
|
|
8336
|
+
at += 10;
|
|
8337
|
+
if (at + size > end)
|
|
8338
|
+
return [];
|
|
8339
|
+
if (rr.value.toLowerCase().replace(/\.$/, "") === domain && klass === 1 && ttl > 0) {
|
|
8340
|
+
const destination = kind === 1 && size === 4 ? `${frame[at]}.${frame[at + 1]}.${frame[at + 2]}.${frame[at + 3]}` : kind === 28 && size === 16 ? ipv6(frame, at) : null;
|
|
8341
|
+
if (destination && isPublicIpLiteral(destination))
|
|
8342
|
+
answers.push({
|
|
8343
|
+
client,
|
|
8344
|
+
destination,
|
|
8345
|
+
domain,
|
|
8346
|
+
seen,
|
|
8347
|
+
expires: seen + Math.min(ttl, 86400) * 1000
|
|
8348
|
+
});
|
|
8349
|
+
}
|
|
8350
|
+
at += size;
|
|
8351
|
+
}
|
|
8352
|
+
return answers;
|
|
8353
|
+
}
|
|
8354
|
+
function proxyVisit(message) {
|
|
8355
|
+
const client = /(?:\b(?:from|src(?:-address)?)\s*[=:]?\s*)(\d{1,3}(?:\.\d{1,3}){3})\b/i.exec(message)?.[1];
|
|
8356
|
+
const target = /\b(?:CONNECT\s+|https?:\/\/)([^\s/"?]+)/i.exec(message)?.[1];
|
|
8357
|
+
if (!client || isIP2(client) !== 4 || !target)
|
|
8358
|
+
return null;
|
|
8359
|
+
const host = validSiteHost(target.replace(/:\d+$/, ""));
|
|
8360
|
+
return host ? { client, host } : null;
|
|
8361
|
+
}
|
|
8362
|
+
|
|
8363
|
+
class SqliteSiteActivityStore {
|
|
8364
|
+
db;
|
|
8365
|
+
currentPolicy = { enabled: true, owners: [], until: null };
|
|
8366
|
+
constructor(db) {
|
|
8367
|
+
this.db = db;
|
|
8368
|
+
db.run("PRAGMA journal_mode=WAL");
|
|
8369
|
+
db.run(`CREATE TABLE IF NOT EXISTS site_dns (client TEXT, destination TEXT, domain TEXT, seen INTEGER, expires INTEGER, PRIMARY KEY(client,destination,domain,seen))`);
|
|
8370
|
+
db.run("CREATE INDEX IF NOT EXISTS site_dns_lookup ON site_dns(client,destination,seen,expires)");
|
|
8371
|
+
db.run(`CREATE TABLE IF NOT EXISTS site_activity (client TEXT, destination TEXT, domain TEXT NOT NULL, source TEXT, bucket INTEGER, first_seen INTEGER, last_seen INTEGER, bytes INTEGER, connections INTEGER, PRIMARY KEY(client,destination,domain,source,bucket))`);
|
|
8372
|
+
db.run("CREATE INDEX IF NOT EXISTS site_activity_time ON site_activity(last_seen)");
|
|
8373
|
+
db.run("CREATE INDEX IF NOT EXISTS site_activity_client ON site_activity(client,last_seen)");
|
|
8374
|
+
db.run("CREATE TABLE IF NOT EXISTS site_proxy_ids (id TEXT PRIMARY KEY, seen INTEGER NOT NULL)");
|
|
8375
|
+
db.run("CREATE TABLE IF NOT EXISTS site_identity (client TEXT NOT NULL, kind TEXT NOT NULL, owner TEXT NOT NULL, device TEXT NOT NULL, mac TEXT NOT NULL, observed INTEGER NOT NULL, PRIMARY KEY(client,device,observed))");
|
|
8376
|
+
db.run("CREATE INDEX IF NOT EXISTS site_identity_lookup ON site_identity(client,observed)");
|
|
8377
|
+
db.run("CREATE TABLE IF NOT EXISTS site_policy (id INTEGER PRIMARY KEY CHECK(id=1), enabled INTEGER NOT NULL, owners TEXT NOT NULL, until INTEGER)");
|
|
8378
|
+
const saved = db.query("SELECT enabled,owners,until FROM site_policy WHERE id=1").get();
|
|
8379
|
+
if (saved)
|
|
8380
|
+
this.currentPolicy = {
|
|
8381
|
+
enabled: !!saved.enabled,
|
|
8382
|
+
owners: JSON.parse(saved.owners),
|
|
8383
|
+
until: saved.until
|
|
8384
|
+
};
|
|
8385
|
+
}
|
|
8386
|
+
answer(client, destination, domain, expires, seen) {
|
|
8387
|
+
if (!isIP2(client) || !this.allows(client, seen) || !isPublicIpLiteral(destination) || !validSiteHost(domain) || expires <= seen)
|
|
8388
|
+
return;
|
|
8389
|
+
this.db.query("INSERT OR IGNORE INTO site_dns VALUES (?,?,?,?,?)").run(client, destination, domain, seen, expires);
|
|
8390
|
+
}
|
|
8391
|
+
flows(records) {
|
|
8392
|
+
const match = this.db.query("SELECT DISTINCT domain FROM site_dns WHERE client=? AND destination=? AND seen<=? AND expires>=? LIMIT 2");
|
|
8393
|
+
const upsert = this.db.query(`INSERT INTO site_activity VALUES (?,?,?,?,?,?,?,?,1)
|
|
8394
|
+
ON CONFLICT(client,destination,domain,source,bucket) DO UPDATE SET
|
|
8395
|
+
first_seen=MIN(first_seen,excluded.first_seen), last_seen=MAX(last_seen,excluded.last_seen),
|
|
8396
|
+
bytes=bytes+excluded.bytes, connections=connections+1`);
|
|
8397
|
+
this.db.transaction(() => {
|
|
8398
|
+
for (const r of records) {
|
|
8399
|
+
if (!isPublicIpLiteral(r.dst) || !isIP2(r.src) || !this.allows(r.src, r.start) || ![80, 443].includes(r.dstPort) || ![6, 17].includes(r.protocol))
|
|
8400
|
+
continue;
|
|
8401
|
+
const domains = match.all(r.src, r.dst, r.start, r.start);
|
|
8402
|
+
const domain = domains.length === 1 ? domains[0].domain : "";
|
|
8403
|
+
upsert.run(r.src, r.dst, domain, domain ? "dns+flow" : "flow", Math.floor(r.start / 300000) * 300000, r.start, r.end, Math.max(0, r.bytes));
|
|
8404
|
+
}
|
|
8405
|
+
})();
|
|
8406
|
+
}
|
|
8407
|
+
proxy(id, client, host, at) {
|
|
8408
|
+
if (!id || !isIP2(client) || !this.allows(client, at) || !validSiteHost(host))
|
|
8409
|
+
return;
|
|
8410
|
+
this.db.transaction(() => {
|
|
8411
|
+
const inserted = this.db.query("INSERT OR IGNORE INTO site_proxy_ids VALUES (?,?)").run(id, at);
|
|
8412
|
+
if (!inserted.changes)
|
|
8413
|
+
return;
|
|
8414
|
+
this.db.query(`INSERT INTO site_activity VALUES (?,?,?,?,?,?,?,?,1)
|
|
8415
|
+
ON CONFLICT(client,destination,domain,source,bucket) DO UPDATE SET
|
|
8416
|
+
last_seen=MAX(last_seen,excluded.last_seen),connections=connections+1`).run(client, "", host, "proxy", Math.floor(at / 300000) * 300000, at, at, 0);
|
|
8417
|
+
})();
|
|
8418
|
+
}
|
|
8419
|
+
proxyDestination(client, destination, at, bytes) {
|
|
8420
|
+
if (!isIP2(client) || !this.allows(client, at) || !isPublicIpLiteral(destination))
|
|
8421
|
+
return;
|
|
8422
|
+
this.db.query(`INSERT INTO site_activity VALUES (?,?,?,?,?,?,?,?,1)
|
|
8423
|
+
ON CONFLICT(client,destination,domain,source,bucket) DO UPDATE SET
|
|
8424
|
+
last_seen=MAX(last_seen,excluded.last_seen),bytes=MAX(bytes,excluded.bytes)`).run(client, destination, "", "proxy", Math.floor(at / 300000) * 300000, at, at, Math.max(0, bytes));
|
|
8425
|
+
}
|
|
8426
|
+
identify(rows) {
|
|
8427
|
+
const insert = this.db.query("INSERT OR REPLACE INTO site_identity VALUES (?,?,?,?,?,?)");
|
|
8428
|
+
this.db.transaction(() => {
|
|
8429
|
+
for (const row of rows)
|
|
8430
|
+
if (isIP2(row.client) && row.owner && row.observed > 0)
|
|
8431
|
+
insert.run(row.client, row.kind, row.owner, row.device, row.mac, row.observed);
|
|
8432
|
+
})();
|
|
8433
|
+
}
|
|
8434
|
+
policy() {
|
|
8435
|
+
return { ...this.currentPolicy, owners: [...this.currentPolicy.owners] };
|
|
8436
|
+
}
|
|
8437
|
+
setPolicy(policy) {
|
|
8438
|
+
this.db.query("INSERT OR REPLACE INTO site_policy VALUES (1,?,?,?)").run(policy.enabled ? 1 : 0, JSON.stringify(policy.owners), policy.until);
|
|
8439
|
+
this.currentPolicy = { ...policy, owners: [...policy.owners] };
|
|
8440
|
+
}
|
|
8441
|
+
allows(client, at) {
|
|
8442
|
+
const policy = this.currentPolicy;
|
|
8443
|
+
if (!policy.enabled || policy.until !== null && at >= policy.until)
|
|
8444
|
+
return false;
|
|
8445
|
+
if (!policy.owners.length)
|
|
8446
|
+
return true;
|
|
8447
|
+
const identity = this.db.query("SELECT owner FROM site_identity WHERE client=? AND observed<=? AND observed>=? ORDER BY observed DESC LIMIT 1").get(client, at, at - 600000);
|
|
8448
|
+
return !!identity && policy.owners.includes(identity.owner);
|
|
8449
|
+
}
|
|
8450
|
+
identities(from, to) {
|
|
8451
|
+
return this.db.query("SELECT client,kind,owner,device,mac,MAX(observed) observed FROM site_identity WHERE observed>=? AND observed<=? GROUP BY client,kind,owner,device,mac ORDER BY owner LIMIT 1000").all(from - 600000, to);
|
|
8452
|
+
}
|
|
8453
|
+
static ENRICHED = `WITH enriched AS (
|
|
8454
|
+
SELECT a.*, CASE WHEN a.source='proxy' THEN 'proxy' ELSE COALESCE(i.kind,'unknown') END kind,
|
|
8455
|
+
CASE WHEN a.source='proxy' THEN a.client ELSE COALESCE(i.owner,'') END owner,
|
|
8456
|
+
COALESCE(i.device,'') device
|
|
8457
|
+
FROM site_activity a LEFT JOIN site_identity i ON i.rowid=(
|
|
8458
|
+
SELECT rowid FROM site_identity WHERE client=a.client AND observed<=a.last_seen
|
|
8459
|
+
AND observed>=a.first_seen-600000 ORDER BY observed DESC LIMIT 1)
|
|
8460
|
+
WHERE a.last_seen>=? AND a.first_seen<? AND (?='' OR a.client=?)
|
|
8461
|
+
AND (?='' OR a.domain LIKE ? OR a.destination LIKE ?))`;
|
|
8462
|
+
filterArgs(f) {
|
|
8463
|
+
return [
|
|
8464
|
+
f.from,
|
|
8465
|
+
f.to,
|
|
8466
|
+
f.client,
|
|
8467
|
+
f.client,
|
|
8468
|
+
f.search,
|
|
8469
|
+
`%${f.search}%`,
|
|
8470
|
+
`%${f.search}%`,
|
|
8471
|
+
f.kind,
|
|
8472
|
+
f.kind,
|
|
8473
|
+
f.owner,
|
|
8474
|
+
f.owner
|
|
8475
|
+
];
|
|
8476
|
+
}
|
|
8477
|
+
visits(filters, offset, limit = 100) {
|
|
8478
|
+
const rows = this.db.query(`${SqliteSiteActivityStore.ENRICHED}
|
|
8479
|
+
SELECT client,destination,domain,source,kind,owner,device,MIN(first_seen) firstSeen,MAX(last_seen) lastSeen,SUM(bytes) bytes,SUM(connections) connections
|
|
8480
|
+
FROM enriched WHERE (?='' OR kind=?) AND (?='' OR owner=?)
|
|
8481
|
+
GROUP BY client,destination,domain,source,kind,owner,device ORDER BY lastSeen DESC LIMIT ? OFFSET ?`).all(...this.filterArgs(filters), limit, offset);
|
|
8482
|
+
return rows.map((r) => ({ ...r, domain: r.domain || null }));
|
|
8483
|
+
}
|
|
8484
|
+
summary(filters) {
|
|
8485
|
+
const where = "FROM enriched WHERE (?='' OR kind=?) AND (?='' OR owner=?)";
|
|
8486
|
+
const args = this.filterArgs(filters);
|
|
8487
|
+
const totals = this.db.query(`${SqliteSiteActivityStore.ENRICHED}
|
|
8488
|
+
SELECT COALESCE(SUM(connections),0) observations,COALESCE(SUM(bytes),0) bytes,
|
|
8489
|
+
COUNT(DISTINCT COALESCE(NULLIF(domain,''),destination)) destinations,COUNT(DISTINCT client) clients ${where}`).get(...args);
|
|
8490
|
+
const groups = this.db.query(`${SqliteSiteActivityStore.ENRICHED}
|
|
8491
|
+
SELECT COALESCE(NULLIF(domain,''),destination) label,SUM(connections) observations,SUM(bytes) bytes ${where}
|
|
8492
|
+
GROUP BY label ORDER BY observations DESC LIMIT 30`).all(...args);
|
|
8493
|
+
return { ...totals, groups };
|
|
8494
|
+
}
|
|
8495
|
+
clients(from, to) {
|
|
8496
|
+
return this.db.query("SELECT DISTINCT client FROM site_activity WHERE last_seen>=? AND first_seen<? ORDER BY client LIMIT 500").all(from, to).map((r) => r.client);
|
|
8497
|
+
}
|
|
8498
|
+
prune(now) {
|
|
8499
|
+
this.db.query("DELETE FROM site_dns WHERE expires<?").run(now);
|
|
8500
|
+
const cutoff = now - 30 * 86400000;
|
|
8501
|
+
this.db.query("DELETE FROM site_activity WHERE last_seen<?").run(cutoff);
|
|
8502
|
+
this.db.query("DELETE FROM site_activity WHERE first_seen>?").run(now + 300000);
|
|
8503
|
+
this.db.query("DELETE FROM site_proxy_ids WHERE seen<?").run(cutoff);
|
|
8504
|
+
this.db.query("DELETE FROM site_identity WHERE observed<?").run(cutoff - 600000);
|
|
8505
|
+
}
|
|
8506
|
+
close() {
|
|
8507
|
+
this.db.close();
|
|
8508
|
+
}
|
|
8509
|
+
}
|
|
8510
|
+
async function openSiteActivityStore(path) {
|
|
8511
|
+
if (path !== ":memory:")
|
|
8512
|
+
mkdirSync3(dirname4(path), { recursive: true, mode: 448 });
|
|
8513
|
+
const { Database } = await import("bun:sqlite");
|
|
8514
|
+
const db = new Database(path, { create: true });
|
|
8515
|
+
if (path !== ":memory:")
|
|
8516
|
+
chmodSync2(path, 384);
|
|
8517
|
+
try {
|
|
8518
|
+
return new SqliteSiteActivityStore(db);
|
|
8519
|
+
} catch (error) {
|
|
8520
|
+
db.close();
|
|
8521
|
+
throw error;
|
|
8522
|
+
}
|
|
8523
|
+
}
|
|
8524
|
+
|
|
8525
|
+
// src/observability/site-activity-collector.ts
|
|
8526
|
+
import { createSocket } from "dgram";
|
|
8527
|
+
import { createHash as createHash3 } from "crypto";
|
|
8528
|
+
var SITE_DNS_PORT = 37009;
|
|
8529
|
+
var PROXY_INTERVAL = 30000;
|
|
8530
|
+
var MAX_LOG_BYTES = 250000;
|
|
8531
|
+
function parseProxyLogRows(output, device) {
|
|
8532
|
+
if (output.length > MAX_LOG_BYTES)
|
|
8533
|
+
throw new Error("Proxy log response exceeds limit");
|
|
8534
|
+
const rows = JSON.parse(output);
|
|
8535
|
+
if (!Array.isArray(rows))
|
|
8536
|
+
throw new Error("Invalid proxy log response");
|
|
8537
|
+
return rows.flatMap((row) => {
|
|
8538
|
+
if (!row || typeof row !== "object")
|
|
8539
|
+
return [];
|
|
8540
|
+
const r = row;
|
|
8541
|
+
if (typeof r.message !== "string" || typeof r[".id"] !== "string")
|
|
8542
|
+
return [];
|
|
8543
|
+
const visit = proxyVisit(r.message);
|
|
8544
|
+
return visit ? [
|
|
8545
|
+
{
|
|
8546
|
+
id: createHash3("sha256").update(`${device}:${r[".id"]}:${typeof r.time === "string" ? r.time : ""}:${r.message}`).digest("hex"),
|
|
8547
|
+
...visit
|
|
8548
|
+
}
|
|
8549
|
+
] : [];
|
|
8550
|
+
});
|
|
8551
|
+
}
|
|
8552
|
+
function parseProxyConnections(output) {
|
|
8553
|
+
if (output.length > MAX_LOG_BYTES)
|
|
8554
|
+
throw new Error("Proxy connection response exceeds limit");
|
|
8555
|
+
const rows = JSON.parse(output);
|
|
8556
|
+
if (!Array.isArray(rows))
|
|
8557
|
+
throw new Error("Invalid proxy connections response");
|
|
8558
|
+
return rows.flatMap((row) => {
|
|
8559
|
+
if (!row || typeof row !== "object")
|
|
8560
|
+
return [];
|
|
8561
|
+
const r = row;
|
|
8562
|
+
if (typeof r["src-address"] !== "string" || typeof r["dst-address"] !== "string")
|
|
8563
|
+
return [];
|
|
8564
|
+
const bytes = Number(r["rx-bytes"] ?? 0);
|
|
8565
|
+
return [
|
|
8566
|
+
{
|
|
8567
|
+
client: r["src-address"],
|
|
8568
|
+
destination: r["dst-address"],
|
|
8569
|
+
bytes: Number.isSafeInteger(bytes) && bytes >= 0 ? bytes : 0
|
|
8570
|
+
}
|
|
8571
|
+
];
|
|
8572
|
+
});
|
|
8573
|
+
}
|
|
8574
|
+
async function startSiteActivityCollector(store, history) {
|
|
8575
|
+
const unsubscribe = getFlowCollector().subscribe((records) => store.flows(records));
|
|
8576
|
+
const socket = createSocket("udp4");
|
|
8577
|
+
let dnsPackets = 0;
|
|
8578
|
+
let stopped = false;
|
|
8579
|
+
let polling = false;
|
|
8580
|
+
const proxyEnabled = new Map;
|
|
8581
|
+
let lastLanRead = 0;
|
|
8582
|
+
async function identifyClients() {
|
|
8583
|
+
const at = Date.now();
|
|
8584
|
+
if (history) {
|
|
8585
|
+
const owners = history.activeTunnelOwners(at);
|
|
8586
|
+
const unique = owners.filter((owner) => owners.filter((other) => other.ip === owner.ip).length === 1);
|
|
8587
|
+
store.identify(unique.map((owner) => ({
|
|
8588
|
+
client: owner.ip,
|
|
8589
|
+
kind: "openvpn",
|
|
8590
|
+
owner: owner.user,
|
|
8591
|
+
device: owner.device,
|
|
8592
|
+
mac: "",
|
|
8593
|
+
observed: at
|
|
8594
|
+
})));
|
|
8595
|
+
}
|
|
8596
|
+
if (at - lastLanRead < 300000)
|
|
8597
|
+
return;
|
|
8598
|
+
lastLanRead = at;
|
|
8599
|
+
const device = getConfig().defaultDevice;
|
|
8600
|
+
const ctx = createContext(undefined, device);
|
|
8601
|
+
try {
|
|
8602
|
+
const clients = await fetchDevices(ctx);
|
|
8603
|
+
let wifi = "";
|
|
8604
|
+
try {
|
|
8605
|
+
wifi = await executeMikrotikCommand("/interface wifi registration-table print detail", ctx, { maxMs: 1e4 });
|
|
8606
|
+
} catch {}
|
|
8607
|
+
const associated = new Set(parseRecords(wifi).rows.map((row) => row["mac-address"]?.toUpperCase()).filter(Boolean));
|
|
8608
|
+
store.identify(clients.filter((c) => c.ip && c.mac).map((c) => ({
|
|
8609
|
+
client: c.ip,
|
|
8610
|
+
kind: associated.has(c.mac.toUpperCase()) ? "wifi" : /^ether/i.test(c.iface) ? "lan" : "lan-wifi",
|
|
8611
|
+
owner: c.host || c.comment || c.mac,
|
|
8612
|
+
device,
|
|
8613
|
+
mac: c.mac,
|
|
8614
|
+
observed: at
|
|
8615
|
+
})));
|
|
8616
|
+
} catch (error) {
|
|
8617
|
+
logger.warn(`Site client identity read failed: ${String(error)}`);
|
|
8618
|
+
}
|
|
8619
|
+
}
|
|
8620
|
+
socket.on("message", (packet) => {
|
|
8621
|
+
const decoded = decodeTzsp(new Uint8Array(packet));
|
|
8622
|
+
if (decoded?.encap !== 1 || !decoded.frame)
|
|
8623
|
+
return;
|
|
8624
|
+
dnsPackets++;
|
|
8625
|
+
const at = Date.now();
|
|
8626
|
+
try {
|
|
8627
|
+
for (const answer of dnsAnswers(decoded.frame, at))
|
|
8628
|
+
store.answer(answer.client, answer.destination, answer.domain, answer.expires, answer.seen);
|
|
8629
|
+
} catch (e) {
|
|
8630
|
+
logger.warn(`Site DNS evidence could not be saved: ${String(e)}`);
|
|
8631
|
+
}
|
|
8632
|
+
});
|
|
8633
|
+
socket.on("error", (e) => logger.warn(`Site DNS collector: ${e.message}`));
|
|
8634
|
+
try {
|
|
8635
|
+
await new Promise((resolve, reject) => {
|
|
8636
|
+
socket.once("error", reject);
|
|
8637
|
+
socket.bind(SITE_DNS_PORT, () => {
|
|
8638
|
+
socket.off("error", reject);
|
|
8639
|
+
resolve();
|
|
8640
|
+
});
|
|
8641
|
+
});
|
|
8642
|
+
} catch (e) {
|
|
8643
|
+
unsubscribe();
|
|
8644
|
+
socket.close();
|
|
8645
|
+
throw e;
|
|
8646
|
+
}
|
|
8647
|
+
async function poll() {
|
|
8648
|
+
if (polling || stopped)
|
|
8649
|
+
return;
|
|
8650
|
+
polling = true;
|
|
8651
|
+
try {
|
|
8652
|
+
const policy = store.policy();
|
|
8653
|
+
if (!policy.enabled || policy.until !== null && Date.now() >= policy.until)
|
|
8654
|
+
return;
|
|
8655
|
+
try {
|
|
8656
|
+
await identifyClients();
|
|
8657
|
+
} catch (e) {
|
|
8658
|
+
logger.warn(`Site identity sample failed: ${String(e)}`);
|
|
8659
|
+
}
|
|
8660
|
+
for (const [device, config] of Object.entries(getConfig().devices)) {
|
|
8661
|
+
if (stopped)
|
|
8662
|
+
break;
|
|
8663
|
+
if (config.disabled || config.mac)
|
|
8664
|
+
continue;
|
|
8665
|
+
let enabled = proxyEnabled.get(device);
|
|
8666
|
+
if (!enabled || enabled.expires < Date.now()) {
|
|
8667
|
+
try {
|
|
8668
|
+
const settings = await executeMikrotikCommand("/ip proxy print", createContext(undefined, device), { maxMs: 1e4 });
|
|
8669
|
+
enabled = {
|
|
8670
|
+
enabled: /^\s*enabled:\s*yes\b/im.test(settings),
|
|
8671
|
+
expires: Date.now() + 600000
|
|
8672
|
+
};
|
|
8673
|
+
proxyEnabled.set(device, enabled);
|
|
8674
|
+
} catch {
|
|
8675
|
+
continue;
|
|
8676
|
+
}
|
|
8677
|
+
}
|
|
8678
|
+
if (!enabled.enabled)
|
|
8679
|
+
continue;
|
|
8680
|
+
try {
|
|
8681
|
+
const connections = await executeMikrotikCommand(":put [:serialize to=json value=[/ip proxy connections print as-value] options=json.no-string-conversion]", createContext(undefined, device), { maxMs: 1e4 });
|
|
8682
|
+
for (const connection of parseProxyConnections(connections))
|
|
8683
|
+
store.proxyDestination(connection.client, connection.destination, Date.now(), connection.bytes);
|
|
8684
|
+
} catch {}
|
|
8685
|
+
try {
|
|
8686
|
+
const output = await executeMikrotikCommand(':put [:serialize to=json value=[/log print as-value where topics~"web-proxy"] options=json.no-string-conversion]', createContext(undefined, device), { maxMs: 1e4 });
|
|
8687
|
+
for (const visit of parseProxyLogRows(output, device))
|
|
8688
|
+
store.proxy(visit.id, visit.client, visit.host, Date.now());
|
|
8689
|
+
} catch {}
|
|
8690
|
+
}
|
|
8691
|
+
if (!stopped) {
|
|
8692
|
+
try {
|
|
8693
|
+
store.prune(Date.now());
|
|
8694
|
+
} catch (e) {
|
|
8695
|
+
logger.warn(`Site activity pruning failed: ${String(e)}`);
|
|
8696
|
+
}
|
|
8697
|
+
}
|
|
8698
|
+
} finally {
|
|
8699
|
+
polling = false;
|
|
8700
|
+
}
|
|
8701
|
+
}
|
|
8702
|
+
poll();
|
|
8703
|
+
const timer = setInterval(() => void poll(), PROXY_INTERVAL);
|
|
8704
|
+
logger.info(`Site activity DNS receiver listening on UDP ${SITE_DNS_PORT}`);
|
|
8705
|
+
return () => {
|
|
8706
|
+
stopped = true;
|
|
8707
|
+
clearInterval(timer);
|
|
8708
|
+
unsubscribe();
|
|
8709
|
+
socket.close();
|
|
8710
|
+
logger.info(`Site activity stopped after ${dnsPackets} DNS mirror packet(s)`);
|
|
8711
|
+
};
|
|
8712
|
+
}
|
|
8713
|
+
|
|
8714
|
+
// src/observability/site-activity-routes.ts
|
|
8715
|
+
import { isIP as isIP3 } from "net";
|
|
8716
|
+
async function siteActivityRoutes(req, url, store) {
|
|
8717
|
+
if (!["/api/site-activity", "/api/site-activity/export", "/api/site-activity/policy"].includes(url.pathname))
|
|
8718
|
+
return null;
|
|
8719
|
+
if (!store)
|
|
8720
|
+
return Response.json({ error: "Site activity storage is unavailable" }, { status: 503 });
|
|
8721
|
+
if (url.pathname.endsWith("/policy")) {
|
|
8722
|
+
if (req.method === "GET")
|
|
8723
|
+
return Response.json(store.policy());
|
|
8724
|
+
if (req.method !== "PUT")
|
|
8725
|
+
return Response.json({ error: "Method not allowed" }, { status: 405 });
|
|
8726
|
+
try {
|
|
8727
|
+
const body = await req.json();
|
|
8728
|
+
if (!body || typeof body !== "object")
|
|
8729
|
+
throw new Error("Invalid policy");
|
|
8730
|
+
const policy = body;
|
|
8731
|
+
if (typeof policy.enabled !== "boolean" || !Array.isArray(policy.owners) || policy.owners.length > 100 || policy.owners.some((owner) => typeof owner !== "string" || !owner.trim() || owner.length > 256) || policy.until !== null && (!Number.isSafeInteger(policy.until) || Number(policy.until) <= Date.now()))
|
|
8732
|
+
throw new Error("Invalid policy");
|
|
8733
|
+
store.setPolicy({
|
|
8734
|
+
enabled: policy.enabled,
|
|
8735
|
+
owners: [...new Set(policy.owners)],
|
|
8736
|
+
until: policy.until
|
|
8737
|
+
});
|
|
8738
|
+
return Response.json(store.policy());
|
|
8739
|
+
} catch {
|
|
8740
|
+
return Response.json({ error: "Invalid activity policy" }, { status: 400 });
|
|
8741
|
+
}
|
|
8742
|
+
}
|
|
8743
|
+
if (req.method !== "GET")
|
|
8744
|
+
return Response.json({ error: "Method not allowed" }, { status: 405 });
|
|
8745
|
+
const now = Date.now();
|
|
8746
|
+
const from = Number(url.searchParams.get("from") ?? now - 86400000);
|
|
8747
|
+
const to = Number(url.searchParams.get("to") ?? now);
|
|
8748
|
+
const client = url.searchParams.get("client") ?? "";
|
|
8749
|
+
const search = (url.searchParams.get("search") ?? "").trim().toLowerCase();
|
|
8750
|
+
const offset = Number(url.searchParams.get("offset") ?? 0);
|
|
8751
|
+
const kind = url.searchParams.get("kind") ?? "";
|
|
8752
|
+
const owner = url.searchParams.get("owner") ?? "";
|
|
8753
|
+
if (!Number.isSafeInteger(from) || !Number.isSafeInteger(to) || from < 0 || to < from || to - from > 30 * 86400000 || client && !isIP3(client) || search.length > 128 || owner.length > 256 || !["", "wifi", "lan", "lan-wifi", "openvpn", "proxy", "unknown"].includes(kind) || !Number.isSafeInteger(offset) || offset < 0 || offset > 1e5)
|
|
8754
|
+
return Response.json({ error: "Invalid activity filters" }, { status: 400 });
|
|
8755
|
+
try {
|
|
8756
|
+
const filters = { from, to, client, search, kind, owner };
|
|
8757
|
+
if (url.pathname.endsWith("/export")) {
|
|
8758
|
+
const csv = (value) => `"${String(value ?? "").replace(/"/g, '""').replace(/^([=+@-])/, "'$1")}"`;
|
|
8759
|
+
const columns = [
|
|
8760
|
+
"Client IP",
|
|
8761
|
+
"Connection",
|
|
8762
|
+
"Owner",
|
|
8763
|
+
"Router",
|
|
8764
|
+
"Domain",
|
|
8765
|
+
"Destination IP",
|
|
8766
|
+
"Evidence",
|
|
8767
|
+
"First seen",
|
|
8768
|
+
"Last seen",
|
|
8769
|
+
"Observations",
|
|
8770
|
+
"Bytes"
|
|
8771
|
+
];
|
|
8772
|
+
const rows = store.visits(filters, 0, 1e4).map((r) => [
|
|
8773
|
+
r.client,
|
|
8774
|
+
r.kind,
|
|
8775
|
+
r.owner,
|
|
8776
|
+
r.device,
|
|
8777
|
+
r.domain,
|
|
8778
|
+
r.destination,
|
|
8779
|
+
r.source,
|
|
8780
|
+
new Date(r.firstSeen).toISOString(),
|
|
8781
|
+
new Date(r.lastSeen).toISOString(),
|
|
8782
|
+
r.connections,
|
|
8783
|
+
r.bytes
|
|
8784
|
+
]);
|
|
8785
|
+
return new Response([columns, ...rows].map((row) => row.map(csv).join(",")).join(`
|
|
8786
|
+
`), {
|
|
8787
|
+
headers: {
|
|
8788
|
+
"content-type": "text/csv; charset=utf-8",
|
|
8789
|
+
"content-disposition": "attachment; filename=site-activity.csv",
|
|
8790
|
+
"cache-control": "no-store"
|
|
8791
|
+
}
|
|
8792
|
+
});
|
|
8793
|
+
}
|
|
8794
|
+
return Response.json({
|
|
8795
|
+
from,
|
|
8796
|
+
to,
|
|
8797
|
+
clients: store.clients(from, to),
|
|
8798
|
+
identities: store.identities(from, to),
|
|
8799
|
+
policy: store.policy(),
|
|
8800
|
+
visits: store.visits(filters, offset),
|
|
8801
|
+
summary: store.summary(filters),
|
|
8802
|
+
collection: {
|
|
8803
|
+
flows: getFlowCollector().stats(),
|
|
8804
|
+
dnsPort: SITE_DNS_PORT,
|
|
8805
|
+
proxyLogs: "polled"
|
|
8806
|
+
},
|
|
8807
|
+
note: "Only observed traffic is shown. DNS evidence is client-specific and time-limited; IP-only rows have no reliable hostname. HTTPS paths and page content are not collected."
|
|
8808
|
+
});
|
|
8809
|
+
} catch {
|
|
8810
|
+
return Response.json({ error: "Could not read site activity" }, { status: 503 });
|
|
8811
|
+
}
|
|
8812
|
+
}
|
|
8813
|
+
|
|
7838
8814
|
// src/observability/ip-intelligence-routes.ts
|
|
7839
8815
|
async function ipIntelligenceRoutes(req, url) {
|
|
7840
8816
|
if (url.pathname !== "/api/ip-intelligence")
|
|
@@ -7861,6 +8837,119 @@ async function ipIntelligenceRoutes(req, url) {
|
|
|
7861
8837
|
}
|
|
7862
8838
|
}
|
|
7863
8839
|
|
|
8840
|
+
// src/bandwidth/inventory.ts
|
|
8841
|
+
var cache = new Map;
|
|
8842
|
+
onConfigChanged(() => cache.clear());
|
|
8843
|
+
async function bandwidthInventory(device) {
|
|
8844
|
+
const name = resolveDeviceName(device);
|
|
8845
|
+
assertDeviceAccess([name], "bandwidth_test", "READ");
|
|
8846
|
+
let entry = cache.get(name);
|
|
8847
|
+
if (!entry || entry.expires <= Date.now()) {
|
|
8848
|
+
const promise = Promise.all([
|
|
8849
|
+
executeMikrotikJson(new Cmd("/interface print").raw("detail").build(), createContext(undefined, name), { maxMs: 20000 }),
|
|
8850
|
+
bandwidthAddresses(name)
|
|
8851
|
+
]).then(([output, addresses]) => ({
|
|
8852
|
+
interfaces: output.rows.filter((row) => row.name).map((row) => ({
|
|
8853
|
+
name: row.name,
|
|
8854
|
+
disabled: ["true", "yes"].includes(row.disabled) || /X/.test(row.flags ?? ""),
|
|
8855
|
+
running: ["true", "yes"].includes(row.running) || /R/.test(row.flags ?? "")
|
|
8856
|
+
})),
|
|
8857
|
+
addresses
|
|
8858
|
+
}));
|
|
8859
|
+
entry = { expires: Infinity, promise };
|
|
8860
|
+
cache.set(name, entry);
|
|
8861
|
+
promise.then(() => {
|
|
8862
|
+
const current = cache.get(name);
|
|
8863
|
+
if (current?.promise === promise)
|
|
8864
|
+
current.expires = Date.now() + 30000;
|
|
8865
|
+
}, () => {
|
|
8866
|
+
if (cache.get(name)?.promise === promise)
|
|
8867
|
+
cache.delete(name);
|
|
8868
|
+
});
|
|
8869
|
+
}
|
|
8870
|
+
const result = await entry.promise;
|
|
8871
|
+
assertDeviceAccess([name], "bandwidth_test", "READ");
|
|
8872
|
+
return result;
|
|
8873
|
+
}
|
|
8874
|
+
|
|
8875
|
+
// src/observability/bandwidth-routes.ts
|
|
8876
|
+
async function bandwidthRoutes(req, url, getStore = bandwidthStore) {
|
|
8877
|
+
if (url.pathname !== "/api/bandwidth" && !url.pathname.startsWith("/api/bandwidth/"))
|
|
8878
|
+
return null;
|
|
8879
|
+
const json = (body, status = 200) => Response.json(body, { status, headers: { "cache-control": "no-store" } });
|
|
8880
|
+
try {
|
|
8881
|
+
const store = await getStore();
|
|
8882
|
+
store.recover(Date.now());
|
|
8883
|
+
if (req.method === "GET" && url.pathname === "/api/bandwidth") {
|
|
8884
|
+
return json({
|
|
8885
|
+
jobs: store.jobs().filter(canReadBandwidth),
|
|
8886
|
+
runs: store.runs().filter((r) => canReadBandwidth(r.job)),
|
|
8887
|
+
devices: bandwidthDevices(),
|
|
8888
|
+
concurrency: BANDWIDTH_CONCURRENCY
|
|
8889
|
+
});
|
|
8890
|
+
}
|
|
8891
|
+
if (req.method === "GET" && url.pathname === "/api/bandwidth/notifications") {
|
|
8892
|
+
const after = Number(url.searchParams.get("after") ?? 0);
|
|
8893
|
+
const limit = Number(url.searchParams.get("limit") ?? 50);
|
|
8894
|
+
if (!Number.isSafeInteger(after) || after < 0 || !Number.isInteger(limit) || limit < 1 || limit > 100)
|
|
8895
|
+
return json({ error: "Invalid notification cursor or limit" }, 400);
|
|
8896
|
+
const feed = store.notifications(after, limit);
|
|
8897
|
+
return json({ ...feed, events: feed.events.filter((e) => canReadBandwidth(e.run.job)) });
|
|
8898
|
+
}
|
|
8899
|
+
if (req.method === "GET" && url.pathname === "/api/bandwidth/options") {
|
|
8900
|
+
const device = url.searchParams.get("device");
|
|
8901
|
+
if (!device || !bandwidthDevices().includes(device))
|
|
8902
|
+
return json({ error: "Select an allowed router" }, 400);
|
|
8903
|
+
return json(await bandwidthInventory(device));
|
|
8904
|
+
}
|
|
8905
|
+
if (req.method !== "POST")
|
|
8906
|
+
return json({ error: "Method not allowed" }, 405);
|
|
8907
|
+
const body = JSON.parse(await readOperationBody(req));
|
|
8908
|
+
if (url.pathname === "/api/bandwidth/preflight") {
|
|
8909
|
+
const input = object({ job: unknown() }).strict().parse(body);
|
|
8910
|
+
return json(await preflightBandwidth(validateBandwidthJob(input.job)));
|
|
8911
|
+
}
|
|
8912
|
+
if (url.pathname === "/api/bandwidth/save") {
|
|
8913
|
+
const input = object({ job: unknown(), acknowledgeImpact: boolean().optional() }).strict().parse(body);
|
|
8914
|
+
const job = validateBandwidthJob(input.job);
|
|
8915
|
+
bandwidthAccess(job, true);
|
|
8916
|
+
const previous = store.job(job.id);
|
|
8917
|
+
if (previous)
|
|
8918
|
+
bandwidthAccess(previous, true);
|
|
8919
|
+
if (job.enabled && input.acknowledgeImpact !== true)
|
|
8920
|
+
return json({ error: "Acknowledge bandwidth/CPU impact before enabling a schedule" }, 400);
|
|
8921
|
+
if (!previous && store.jobs().length >= 100)
|
|
8922
|
+
return json({ error: "Maximum 100 saved tests" }, 400);
|
|
8923
|
+
job.nextRunAt = Date.now() + job.intervalSeconds * 1000;
|
|
8924
|
+
store.save(job);
|
|
8925
|
+
return json({ job });
|
|
8926
|
+
}
|
|
8927
|
+
if (url.pathname === "/api/bandwidth/run" || url.pathname === "/api/bandwidth/delete") {
|
|
8928
|
+
const input = object({ id: string().min(1).max(64), acknowledgeImpact: boolean().optional() }).strict().parse(body);
|
|
8929
|
+
const saved = store.job(input.id);
|
|
8930
|
+
if (!saved || !canReadBandwidth(saved))
|
|
8931
|
+
return json({ error: "Saved test not found" }, 404);
|
|
8932
|
+
bandwidthAccess(saved, true);
|
|
8933
|
+
if (url.pathname.endsWith("/delete")) {
|
|
8934
|
+
store.remove(saved.id);
|
|
8935
|
+
return json({ removed: saved.id });
|
|
8936
|
+
}
|
|
8937
|
+
if (input.acknowledgeImpact !== true)
|
|
8938
|
+
return json({ error: "Acknowledge bandwidth/CPU impact before running" }, 400);
|
|
8939
|
+
const { run, done } = launchBandwidth(store, validateBandwidthJob(saved), "manual");
|
|
8940
|
+
done.catch(() => logger.error("Bandwidth completion could not be persisted; lease retained for recovery."));
|
|
8941
|
+
return json({ run }, 202);
|
|
8942
|
+
}
|
|
8943
|
+
return json({ error: "Not found" }, 404);
|
|
8944
|
+
} catch (error) {
|
|
8945
|
+
if (error instanceof DashboardInputError)
|
|
8946
|
+
return json({ error: error.message }, error.status);
|
|
8947
|
+
if (error instanceof BandwidthError)
|
|
8948
|
+
return json({ error: error.message }, 400);
|
|
8949
|
+
return json({ error: "Bandwidth operation unavailable. Check settings, access and local storage." }, 400);
|
|
8950
|
+
}
|
|
8951
|
+
}
|
|
8952
|
+
|
|
7864
8953
|
// src/observability/round-trip-routes.ts
|
|
7865
8954
|
async function roundTripRoutes(req, url) {
|
|
7866
8955
|
if (url.pathname === "/api/round-trip/options") {
|
|
@@ -7927,9 +9016,9 @@ async function roundTripRoutes(req, url) {
|
|
|
7927
9016
|
}
|
|
7928
9017
|
|
|
7929
9018
|
// src/observability/um-report-cache.ts
|
|
7930
|
-
import { createHash as
|
|
9019
|
+
import { createHash as createHash4, randomUUID as randomUUID2 } from "crypto";
|
|
7931
9020
|
import { mkdir as mkdir2, readFile, rename, stat, unlink as unlink2, writeFile } from "fs/promises";
|
|
7932
|
-
import { dirname as
|
|
9021
|
+
import { dirname as dirname5, join as join5 } from "path";
|
|
7933
9022
|
var MAX_BYTES = 32 * 1024 * 1024;
|
|
7934
9023
|
var MAX_AGE = 24 * 60 * 60000;
|
|
7935
9024
|
var sourceSchema = object({
|
|
@@ -7957,8 +9046,8 @@ function umReportCachePath(device) {
|
|
|
7957
9046
|
chain.push([name, target]);
|
|
7958
9047
|
name = target?.jumpVia;
|
|
7959
9048
|
}
|
|
7960
|
-
const key =
|
|
7961
|
-
return
|
|
9049
|
+
const key = createHash4("sha256").update(JSON.stringify(chain)).digest("hex");
|
|
9050
|
+
return join5(`${config.dashboard.dbPath}.um-reports`, `${key}.json`);
|
|
7962
9051
|
}
|
|
7963
9052
|
async function loadUmReportCache(path, device, fields) {
|
|
7964
9053
|
if (!path)
|
|
@@ -7994,7 +9083,7 @@ async function saveUmReportCache(path, snapshot) {
|
|
|
7994
9083
|
const text = JSON.stringify({ version: 1, snapshot });
|
|
7995
9084
|
if (Buffer.byteLength(text) > MAX_BYTES)
|
|
7996
9085
|
return false;
|
|
7997
|
-
await mkdir2(
|
|
9086
|
+
await mkdir2(dirname5(path), { recursive: true, mode: 448 });
|
|
7998
9087
|
await writeFile(temp, text, { encoding: "utf8", mode: 384, flag: "wx" });
|
|
7999
9088
|
await rename(temp, path);
|
|
8000
9089
|
return true;
|
|
@@ -8053,8 +9142,8 @@ function umConnectionDate(value) {
|
|
|
8053
9142
|
}
|
|
8054
9143
|
return normalized;
|
|
8055
9144
|
}
|
|
8056
|
-
var
|
|
8057
|
-
onConfigChanged(() =>
|
|
9145
|
+
var cache2 = new Map;
|
|
9146
|
+
onConfigChanged(() => cache2.clear());
|
|
8058
9147
|
function getUmLastConnections(device) {
|
|
8059
9148
|
try {
|
|
8060
9149
|
assertDeviceAccess([device], "list_user_manager_sessions", "READ");
|
|
@@ -8062,7 +9151,7 @@ function getUmLastConnections(device) {
|
|
|
8062
9151
|
return { status: "unavailable", values: new Map };
|
|
8063
9152
|
}
|
|
8064
9153
|
const config = getConfig();
|
|
8065
|
-
let entry =
|
|
9154
|
+
let entry = cache2.get(device);
|
|
8066
9155
|
if (!entry || entry.config !== config) {
|
|
8067
9156
|
entry = {
|
|
8068
9157
|
config,
|
|
@@ -8073,7 +9162,7 @@ function getUmLastConnections(device) {
|
|
|
8073
9162
|
retryAt: 0,
|
|
8074
9163
|
failed: false
|
|
8075
9164
|
};
|
|
8076
|
-
|
|
9165
|
+
cache2.set(device, entry);
|
|
8077
9166
|
}
|
|
8078
9167
|
const target = entry;
|
|
8079
9168
|
if (!target.pending && Date.now() >= target.retryAt) {
|
|
@@ -8362,8 +9451,8 @@ async function collect(device, progress) {
|
|
|
8362
9451
|
throw new Error("Configuration changed during report collection.");
|
|
8363
9452
|
return { device, collectedAt: Date.now(), collectionMs: Date.now() - start, clock, sources };
|
|
8364
9453
|
}
|
|
8365
|
-
var
|
|
8366
|
-
onConfigChanged(() =>
|
|
9454
|
+
var cache3 = new Map;
|
|
9455
|
+
onConfigChanged(() => cache3.clear());
|
|
8367
9456
|
var freshnessMs = (value) => Math.max(CACHE_MS2, Math.min(900000, (value?.collectionMs ?? 0) * 5));
|
|
8368
9457
|
async function getUmSnapshot(device, refresh = false, wait = true) {
|
|
8369
9458
|
const name = resolveDeviceName(device);
|
|
@@ -8377,12 +9466,12 @@ async function getUmSnapshot(device, refresh = false, wait = true) {
|
|
|
8377
9466
|
"get_user_manager_settings"
|
|
8378
9467
|
])
|
|
8379
9468
|
assertDeviceAccess([name], tool, "READ");
|
|
8380
|
-
let entry =
|
|
9469
|
+
let entry = cache3.get(name);
|
|
8381
9470
|
const config = getConfig();
|
|
8382
9471
|
const path = umReportCachePath(name);
|
|
8383
9472
|
if (!entry) {
|
|
8384
9473
|
entry = { retryAt: 0, progress: { source: "users", completed: 0, total: null } };
|
|
8385
|
-
|
|
9474
|
+
cache3.set(name, entry);
|
|
8386
9475
|
const target = entry;
|
|
8387
9476
|
target.restored = loadUmReportCache(path, name, UM_REPORT_FIELDS).then((value) => {
|
|
8388
9477
|
target.value = value;
|
|
@@ -8717,14 +9806,14 @@ function buildUmReport(snapshot, query) {
|
|
|
8717
9806
|
// src/observability/config-history.ts
|
|
8718
9807
|
import {
|
|
8719
9808
|
existsSync,
|
|
8720
|
-
mkdirSync as
|
|
9809
|
+
mkdirSync as mkdirSync4,
|
|
8721
9810
|
readFileSync as readFileSync2,
|
|
8722
9811
|
readdirSync,
|
|
8723
9812
|
rmSync,
|
|
8724
9813
|
statSync,
|
|
8725
9814
|
writeFileSync
|
|
8726
9815
|
} from "fs";
|
|
8727
|
-
import { join as
|
|
9816
|
+
import { join as join6 } from "path";
|
|
8728
9817
|
var AUTO_RETENTION = 50;
|
|
8729
9818
|
function historyDir() {
|
|
8730
9819
|
return process.env.MIKROTIK_CONFIG_HISTORY_DIR || DEFAULT_CONFIG_HISTORY_DIR2;
|
|
@@ -8743,7 +9832,7 @@ function listVersions() {
|
|
|
8743
9832
|
if (!/^v\d+\.json$/.test(f))
|
|
8744
9833
|
continue;
|
|
8745
9834
|
try {
|
|
8746
|
-
const raw = readFileSync2(
|
|
9835
|
+
const raw = readFileSync2(join6(dir, f), "utf8");
|
|
8747
9836
|
const parsed = JSON.parse(raw);
|
|
8748
9837
|
out.push({
|
|
8749
9838
|
id: f.replace(/\.json$/, ""),
|
|
@@ -8757,11 +9846,11 @@ function listVersions() {
|
|
|
8757
9846
|
return out.sort((a, b) => b.ts - a.ts);
|
|
8758
9847
|
}
|
|
8759
9848
|
function readVersion(id) {
|
|
8760
|
-
const raw = readFileSync2(
|
|
9849
|
+
const raw = readFileSync2(join6(historyDir(), idToFile(id)), "utf8");
|
|
8761
9850
|
return JSON.parse(raw);
|
|
8762
9851
|
}
|
|
8763
9852
|
function deleteVersion(id) {
|
|
8764
|
-
const p =
|
|
9853
|
+
const p = join6(historyDir(), idToFile(id));
|
|
8765
9854
|
if (!existsSync(p))
|
|
8766
9855
|
return false;
|
|
8767
9856
|
rmSync(p);
|
|
@@ -8769,12 +9858,12 @@ function deleteVersion(id) {
|
|
|
8769
9858
|
}
|
|
8770
9859
|
function recordVersion(config, kind, now, label) {
|
|
8771
9860
|
const dir = historyDir();
|
|
8772
|
-
|
|
9861
|
+
mkdirSync4(dir, { recursive: true });
|
|
8773
9862
|
let ts = now;
|
|
8774
|
-
while (existsSync(
|
|
9863
|
+
while (existsSync(join6(dir, `v${ts}.json`)))
|
|
8775
9864
|
ts++;
|
|
8776
9865
|
const body = { ts, kind, label, config };
|
|
8777
|
-
writeFileSync(
|
|
9866
|
+
writeFileSync(join6(dir, `v${ts}.json`), `${JSON.stringify(body, null, 2)}
|
|
8778
9867
|
`, "utf8");
|
|
8779
9868
|
pruneAuto(dir);
|
|
8780
9869
|
return {
|
|
@@ -8791,7 +9880,7 @@ function pruneAuto(dir) {
|
|
|
8791
9880
|
return;
|
|
8792
9881
|
for (const v of autos.slice(AUTO_RETENTION)) {
|
|
8793
9882
|
try {
|
|
8794
|
-
rmSync(
|
|
9883
|
+
rmSync(join6(dir, `${v.id}.json`));
|
|
8795
9884
|
} catch {}
|
|
8796
9885
|
}
|
|
8797
9886
|
}
|
|
@@ -8810,7 +9899,7 @@ function historyBytes() {
|
|
|
8810
9899
|
if (!/^v\d+\.json$/.test(f))
|
|
8811
9900
|
continue;
|
|
8812
9901
|
try {
|
|
8813
|
-
total += statSync(
|
|
9902
|
+
total += statSync(join6(dir, f)).size;
|
|
8814
9903
|
} catch {}
|
|
8815
9904
|
}
|
|
8816
9905
|
return total;
|
|
@@ -8818,7 +9907,7 @@ function historyBytes() {
|
|
|
8818
9907
|
|
|
8819
9908
|
// src/prompts/index.ts
|
|
8820
9909
|
import { readFileSync as readFileSync3, readdirSync as readdirSync2 } from "fs";
|
|
8821
|
-
import { join as
|
|
9910
|
+
import { join as join7 } from "path";
|
|
8822
9911
|
|
|
8823
9912
|
// src/txn/guidance.ts
|
|
8824
9913
|
var TRANSACTION_TOOL_NAMES = [
|
|
@@ -8968,7 +10057,7 @@ function listPrompts() {
|
|
|
8968
10057
|
const out = [];
|
|
8969
10058
|
for (const file of files) {
|
|
8970
10059
|
try {
|
|
8971
|
-
const parsed = parseFrontmatter(readFileSync3(
|
|
10060
|
+
const parsed = parseFrontmatter(readFileSync3(join7(PROMPTS_DIR, file), "utf8"));
|
|
8972
10061
|
if (parsed) {
|
|
8973
10062
|
out.push({
|
|
8974
10063
|
name: parsed.name,
|
|
@@ -8995,7 +10084,7 @@ function registerPrompts(server, opts = {}) {
|
|
|
8995
10084
|
for (const file of files) {
|
|
8996
10085
|
let parsed;
|
|
8997
10086
|
try {
|
|
8998
|
-
parsed = parseFrontmatter(readFileSync3(
|
|
10087
|
+
parsed = parseFrontmatter(readFileSync3(join7(PROMPTS_DIR, file), "utf8"));
|
|
8999
10088
|
} catch (e) {
|
|
9000
10089
|
logger.warn(`Skipping prompt ${file}: ${String(e)}`);
|
|
9001
10090
|
continue;
|
|
@@ -9097,14 +10186,14 @@ async function setCapsmanManager(ctx, input) {
|
|
|
9097
10186
|
}
|
|
9098
10187
|
|
|
9099
10188
|
// src/observability/flags.ts
|
|
9100
|
-
import { join as
|
|
10189
|
+
import { join as join8 } from "path";
|
|
9101
10190
|
import { mkdir as mkdir3, readFile as readFile2, writeFile as writeFile2 } from "fs/promises";
|
|
9102
10191
|
import { homedir as homedir4 } from "os";
|
|
9103
10192
|
var LOG_TAG = "mikrotik-mcp";
|
|
9104
10193
|
var CODE_RE = /^[a-z]{2}$/;
|
|
9105
10194
|
var sourceUrl = (code) => `https://hatscripts.github.io/circle-flags/flags/${code}.svg`;
|
|
9106
|
-
var VENDOR_DIR =
|
|
9107
|
-
var CACHE_DIR =
|
|
10195
|
+
var VENDOR_DIR = join8(PROJECT_ROOT, "assets", "flags");
|
|
10196
|
+
var CACHE_DIR = join8(homedir4(), ".mikrotik-mcp", "flags");
|
|
9108
10197
|
var mem = new Map;
|
|
9109
10198
|
async function flagSvg(code) {
|
|
9110
10199
|
const c = code.toLowerCase();
|
|
@@ -9113,7 +10202,7 @@ async function flagSvg(code) {
|
|
|
9113
10202
|
const cached = mem.get(c);
|
|
9114
10203
|
if (cached !== undefined)
|
|
9115
10204
|
return cached;
|
|
9116
|
-
for (const file of [
|
|
10205
|
+
for (const file of [join8(VENDOR_DIR, `${c}.svg`), join8(CACHE_DIR, `${c}.svg`)]) {
|
|
9117
10206
|
try {
|
|
9118
10207
|
const svg = await readFile2(file, "utf8");
|
|
9119
10208
|
mem.set(c, svg);
|
|
@@ -9129,7 +10218,7 @@ async function flagSvg(code) {
|
|
|
9129
10218
|
}
|
|
9130
10219
|
const svg = await res.text();
|
|
9131
10220
|
mem.set(c, svg);
|
|
9132
|
-
mkdir3(CACHE_DIR, { recursive: true }).then(() => writeFile2(
|
|
10221
|
+
mkdir3(CACHE_DIR, { recursive: true }).then(() => writeFile2(join8(CACHE_DIR, `${c}.svg`), svg)).catch(() => {});
|
|
9133
10222
|
return svg;
|
|
9134
10223
|
} catch (e) {
|
|
9135
10224
|
logger.warn(`[${LOG_TAG}] flag fetch failed for '${c}': ${e instanceof Error ? e.message : String(e)}`);
|
|
@@ -9356,8 +10445,8 @@ data: ${JSON.stringify(sample)}
|
|
|
9356
10445
|
}
|
|
9357
10446
|
|
|
9358
10447
|
// src/observability/store.ts
|
|
9359
|
-
import { mkdirSync as
|
|
9360
|
-
import { dirname as
|
|
10448
|
+
import { mkdirSync as mkdirSync5 } from "fs";
|
|
10449
|
+
import { dirname as dirname6 } from "path";
|
|
9361
10450
|
function rowToEvent(r) {
|
|
9362
10451
|
return {
|
|
9363
10452
|
id: r.id,
|
|
@@ -9543,7 +10632,7 @@ class SqliteEventStore {
|
|
|
9543
10632
|
async function openSqliteStore(path) {
|
|
9544
10633
|
if (path !== ":memory:") {
|
|
9545
10634
|
try {
|
|
9546
|
-
|
|
10635
|
+
mkdirSync5(dirname6(path), { recursive: true });
|
|
9547
10636
|
} catch {}
|
|
9548
10637
|
}
|
|
9549
10638
|
const { Database } = await import("bun:sqlite");
|
|
@@ -9552,8 +10641,8 @@ async function openSqliteStore(path) {
|
|
|
9552
10641
|
}
|
|
9553
10642
|
|
|
9554
10643
|
// src/observability/capsman-store.ts
|
|
9555
|
-
import { mkdirSync as
|
|
9556
|
-
import { dirname as
|
|
10644
|
+
import { mkdirSync as mkdirSync6 } from "fs";
|
|
10645
|
+
import { dirname as dirname7 } from "path";
|
|
9557
10646
|
function summariseRadioSamples(rows) {
|
|
9558
10647
|
const byRadio = new Map;
|
|
9559
10648
|
for (const r of rows) {
|
|
@@ -9643,7 +10732,7 @@ class SqliteCapsmanStore {
|
|
|
9643
10732
|
async function openCapsmanStore(path) {
|
|
9644
10733
|
if (path !== ":memory:") {
|
|
9645
10734
|
try {
|
|
9646
|
-
|
|
10735
|
+
mkdirSync6(dirname7(path), { recursive: true });
|
|
9647
10736
|
} catch {}
|
|
9648
10737
|
}
|
|
9649
10738
|
const { Database } = await import("bun:sqlite");
|
|
@@ -9657,7 +10746,7 @@ var CAPSMAN_RETENTION_MS = 30 * 24 * 60 * 60 * 1000;
|
|
|
9657
10746
|
var DEFAULT_CAPSMAN_INTERVAL_MS = 5 * 60000;
|
|
9658
10747
|
var MIN_CAPSMAN_INTERVAL_MS = 60000;
|
|
9659
10748
|
var MAX_CAPSMAN_INTERVAL_MS = 6 * 60 * 60000;
|
|
9660
|
-
var
|
|
10749
|
+
var timer2 = null;
|
|
9661
10750
|
var inFlight = false;
|
|
9662
10751
|
var noCapsman = new Set;
|
|
9663
10752
|
function clampInterval(ms) {
|
|
@@ -9712,18 +10801,18 @@ async function sampleCapsmanOnce(store) {
|
|
|
9712
10801
|
function startCapsmanSampler(store, intervalMs = DEFAULT_CAPSMAN_INTERVAL_MS) {
|
|
9713
10802
|
const ms = clampInterval(intervalMs);
|
|
9714
10803
|
sampleCapsmanOnce(store);
|
|
9715
|
-
|
|
10804
|
+
timer2 = setInterval(() => void sampleCapsmanOnce(store), ms);
|
|
9716
10805
|
}
|
|
9717
10806
|
function stopCapsmanSampler() {
|
|
9718
|
-
if (
|
|
9719
|
-
clearInterval(
|
|
9720
|
-
|
|
10807
|
+
if (timer2) {
|
|
10808
|
+
clearInterval(timer2);
|
|
10809
|
+
timer2 = null;
|
|
9721
10810
|
}
|
|
9722
10811
|
}
|
|
9723
10812
|
|
|
9724
10813
|
// src/observability/usage-store.ts
|
|
9725
|
-
import { mkdirSync as
|
|
9726
|
-
import { dirname as
|
|
10814
|
+
import { mkdirSync as mkdirSync7 } from "fs";
|
|
10815
|
+
import { dirname as dirname8 } from "path";
|
|
9727
10816
|
function dayOf(ts) {
|
|
9728
10817
|
return new Date(ts).toISOString().slice(0, 10);
|
|
9729
10818
|
}
|
|
@@ -9860,7 +10949,7 @@ class SqliteUsageStore {
|
|
|
9860
10949
|
async function openUsageStore(path) {
|
|
9861
10950
|
if (path !== ":memory:") {
|
|
9862
10951
|
try {
|
|
9863
|
-
|
|
10952
|
+
mkdirSync7(dirname8(path), { recursive: true });
|
|
9864
10953
|
} catch {}
|
|
9865
10954
|
}
|
|
9866
10955
|
const { Database } = await import("bun:sqlite");
|
|
@@ -9880,7 +10969,7 @@ var USAGE_RETENTION_MS = 93 * 24 * 60 * 60 * 1000;
|
|
|
9880
10969
|
var DEFAULT_USAGE_INTERVAL_MS = 60000;
|
|
9881
10970
|
var MIN_USAGE_INTERVAL_MS = 30000;
|
|
9882
10971
|
var MAX_USAGE_INTERVAL_MS = 6 * 60 * 60000;
|
|
9883
|
-
var
|
|
10972
|
+
var timer3 = null;
|
|
9884
10973
|
var inFlight2 = false;
|
|
9885
10974
|
var currentStore = null;
|
|
9886
10975
|
var currentIntervalMs = DEFAULT_USAGE_INTERVAL_MS;
|
|
@@ -9983,25 +11072,25 @@ function startUsageSampler(store, intervalMs = DEFAULT_USAGE_INTERVAL_MS) {
|
|
|
9983
11072
|
currentStore = store;
|
|
9984
11073
|
currentIntervalMs = clampInterval2(intervalMs);
|
|
9985
11074
|
sampleUsageOnce(store);
|
|
9986
|
-
|
|
11075
|
+
timer3 = setInterval(() => void sampleUsageOnce(store), currentIntervalMs);
|
|
9987
11076
|
}
|
|
9988
11077
|
function getUsageSamplerInterval() {
|
|
9989
11078
|
return currentIntervalMs;
|
|
9990
11079
|
}
|
|
9991
11080
|
function setUsageSamplerInterval(intervalMs) {
|
|
9992
11081
|
currentIntervalMs = clampInterval2(intervalMs);
|
|
9993
|
-
if (
|
|
9994
|
-
clearInterval(
|
|
11082
|
+
if (timer3)
|
|
11083
|
+
clearInterval(timer3);
|
|
9995
11084
|
if (currentStore) {
|
|
9996
11085
|
const store = currentStore;
|
|
9997
|
-
|
|
11086
|
+
timer3 = setInterval(() => void sampleUsageOnce(store), currentIntervalMs);
|
|
9998
11087
|
}
|
|
9999
11088
|
return currentIntervalMs;
|
|
10000
11089
|
}
|
|
10001
11090
|
function stopUsageSampler() {
|
|
10002
|
-
if (
|
|
10003
|
-
clearInterval(
|
|
10004
|
-
|
|
11091
|
+
if (timer3) {
|
|
11092
|
+
clearInterval(timer3);
|
|
11093
|
+
timer3 = null;
|
|
10005
11094
|
}
|
|
10006
11095
|
currentStore = null;
|
|
10007
11096
|
}
|
|
@@ -10100,11 +11189,11 @@ function computeStats(events, opts) {
|
|
|
10100
11189
|
}
|
|
10101
11190
|
|
|
10102
11191
|
// src/observability/drift-routes.ts
|
|
10103
|
-
var
|
|
11192
|
+
var storePromise2 = null;
|
|
10104
11193
|
function getStore() {
|
|
10105
|
-
if (!
|
|
10106
|
-
|
|
10107
|
-
return
|
|
11194
|
+
if (!storePromise2)
|
|
11195
|
+
storePromise2 = openSnapshotStore2(DEFAULT_SNAPSHOT_DB2);
|
|
11196
|
+
return storePromise2;
|
|
10108
11197
|
}
|
|
10109
11198
|
var JSON_HEADERS = { "content-type": "application/json; charset=utf-8" };
|
|
10110
11199
|
function json(body, status = 200) {
|
|
@@ -10294,7 +11383,7 @@ async function postureRoutes(req, url) {
|
|
|
10294
11383
|
}
|
|
10295
11384
|
|
|
10296
11385
|
// src/observability/access-settings.ts
|
|
10297
|
-
import { createHash as
|
|
11386
|
+
import { createHash as createHash5 } from "crypto";
|
|
10298
11387
|
var list = array(string().trim().min(1).max(200)).max(200);
|
|
10299
11388
|
var accessSchema = AccessConfigSchema2.extend({
|
|
10300
11389
|
devices: list,
|
|
@@ -10324,7 +11413,7 @@ function createAccessSettingsRoutes(admin, deps = {
|
|
|
10324
11413
|
}) {
|
|
10325
11414
|
let pending = null;
|
|
10326
11415
|
const tools = moduleCatalog2.flatMap((m) => m.tools.map((t) => ({ name: t.name, risk: riskOf(t.annotations), noDevice: !!t.noDevice })));
|
|
10327
|
-
const revision = () =>
|
|
11416
|
+
const revision = () => createHash5("sha256").update(JSON.stringify([deps.getConfig(), deps.getAccessPolicy()])).digest("hex");
|
|
10328
11417
|
const payload = () => {
|
|
10329
11418
|
const cfg = deps.getConfig();
|
|
10330
11419
|
const id = admin.pendingId();
|
|
@@ -10707,8 +11796,8 @@ async function rolloutRoutes(req, url) {
|
|
|
10707
11796
|
}
|
|
10708
11797
|
|
|
10709
11798
|
// src/policy/results.ts
|
|
10710
|
-
import { mkdirSync as
|
|
10711
|
-
import { dirname as
|
|
11799
|
+
import { mkdirSync as mkdirSync8 } from "fs";
|
|
11800
|
+
import { dirname as dirname9 } from "path";
|
|
10712
11801
|
var SCHEMA_STATEMENTS4 = [
|
|
10713
11802
|
`CREATE TABLE IF NOT EXISTS policy_results (
|
|
10714
11803
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
@@ -10768,17 +11857,17 @@ class SqlitePolicyResultStore {
|
|
|
10768
11857
|
async function openPolicyResultStore(path) {
|
|
10769
11858
|
if (path !== ":memory:") {
|
|
10770
11859
|
try {
|
|
10771
|
-
|
|
11860
|
+
mkdirSync8(dirname9(path), { recursive: true });
|
|
10772
11861
|
} catch {}
|
|
10773
11862
|
}
|
|
10774
11863
|
const { Database } = await import("bun:sqlite");
|
|
10775
11864
|
const db = new Database(path, { create: true });
|
|
10776
11865
|
return new SqlitePolicyResultStore(db);
|
|
10777
11866
|
}
|
|
10778
|
-
var
|
|
11867
|
+
var storePromise3 = null;
|
|
10779
11868
|
function store() {
|
|
10780
|
-
|
|
10781
|
-
return
|
|
11869
|
+
storePromise3 ??= openPolicyResultStore(DEFAULT_SNAPSHOT_DB2);
|
|
11870
|
+
return storePromise3;
|
|
10782
11871
|
}
|
|
10783
11872
|
async function recordPolicyResult(report) {
|
|
10784
11873
|
try {
|
|
@@ -10882,10 +11971,10 @@ function json8(body, status = 200) {
|
|
|
10882
11971
|
async function bodyJson3(req) {
|
|
10883
11972
|
return await req.json().catch(() => ({}));
|
|
10884
11973
|
}
|
|
10885
|
-
var
|
|
11974
|
+
var storePromise4 = null;
|
|
10886
11975
|
function snapshots() {
|
|
10887
|
-
|
|
10888
|
-
return
|
|
11976
|
+
storePromise4 ??= openSnapshotStore2(DEFAULT_SNAPSHOT_DB2);
|
|
11977
|
+
return storePromise4;
|
|
10889
11978
|
}
|
|
10890
11979
|
async function configFor(body) {
|
|
10891
11980
|
if (body.configText)
|
|
@@ -11210,10 +12299,10 @@ var JSON_HEADERS9 = { "content-type": "application/json; charset=utf-8" };
|
|
|
11210
12299
|
function json10(body, status = 200) {
|
|
11211
12300
|
return new Response(JSON.stringify(body), { status, headers: JSON_HEADERS9 });
|
|
11212
12301
|
}
|
|
11213
|
-
var
|
|
12302
|
+
var storePromise5 = null;
|
|
11214
12303
|
function snapshots2() {
|
|
11215
|
-
|
|
11216
|
-
return
|
|
12304
|
+
storePromise5 ??= openSnapshotStore2(DEFAULT_SNAPSHOT_DB2);
|
|
12305
|
+
return storePromise5;
|
|
11217
12306
|
}
|
|
11218
12307
|
async function configFor2(device, snapshotId) {
|
|
11219
12308
|
if (snapshotId) {
|
|
@@ -11865,7 +12954,7 @@ spawn error: ${String(e)}` });
|
|
|
11865
12954
|
}
|
|
11866
12955
|
function dashboardHtml() {
|
|
11867
12956
|
try {
|
|
11868
|
-
return readFileSync4(
|
|
12957
|
+
return readFileSync4(join9(UI_DIST_DIR, "observability.html"), "utf8");
|
|
11869
12958
|
} catch {
|
|
11870
12959
|
return `<!doctype html><meta charset=utf-8><body style="font:14px system-ui;padding:24px;background:#0b0d10;color:#e8eaed">
|
|
11871
12960
|
<h2>MikroTik MCP \u2014 Observability Dashboard</h2>
|
|
@@ -12801,7 +13890,7 @@ async function featureRoutes(req, url) {
|
|
|
12801
13890
|
const raw = b?.dir?.trim();
|
|
12802
13891
|
if (!raw)
|
|
12803
13892
|
return json14({ error: "dir required" }, 400);
|
|
12804
|
-
const dir = raw === "~" || raw.startsWith("~/") ?
|
|
13893
|
+
const dir = raw === "~" || raw.startsWith("~/") ? join9(homedir5(), raw.slice(1)) : raw;
|
|
12805
13894
|
const next = { ...getConfig(), backupDir: dir };
|
|
12806
13895
|
setConfig(next);
|
|
12807
13896
|
try {
|
|
@@ -12912,19 +14001,40 @@ async function runDashboard(cfg, transportLabel) {
|
|
|
12912
14001
|
let openVpnHistory;
|
|
12913
14002
|
let stopOpenVpnHistory;
|
|
12914
14003
|
try {
|
|
12915
|
-
openVpnHistory = await openOpenVpnHistory(
|
|
14004
|
+
openVpnHistory = await openOpenVpnHistory(join9(dirname10(cfg.dbPath), "openvpn-history.db"));
|
|
12916
14005
|
stopOpenVpnHistory = startOpenVpnHistorySampler(openVpnHistory);
|
|
12917
14006
|
} catch (e) {
|
|
12918
14007
|
logger.warn(`[${SERVER_TAG3}] OpenVPN history disabled: ${String(e)}`);
|
|
12919
14008
|
}
|
|
14009
|
+
let siteActivity;
|
|
14010
|
+
let stopSiteActivity;
|
|
14011
|
+
let dashboardStartedFlows = false;
|
|
14012
|
+
if (getConfig().flows.enabled && !getFlowCollector().running) {
|
|
14013
|
+
try {
|
|
14014
|
+
const started = await getFlowCollector().start(await flowStore(), getConfig().flows.port);
|
|
14015
|
+
dashboardStartedFlows = started.ok;
|
|
14016
|
+
if (!started.ok)
|
|
14017
|
+
logger.warn(`[${SERVER_TAG3}] Flow collector: ${started.error}`);
|
|
14018
|
+
} catch (e) {
|
|
14019
|
+
logger.warn(`[${SERVER_TAG3}] Flow collector unavailable: ${String(e)}`);
|
|
14020
|
+
}
|
|
14021
|
+
}
|
|
12920
14022
|
try {
|
|
12921
|
-
|
|
14023
|
+
siteActivity = await openSiteActivityStore(join9(dirname10(cfg.dbPath), "site-activity.db"));
|
|
14024
|
+
stopSiteActivity = await startSiteActivityCollector(siteActivity, openVpnHistory);
|
|
14025
|
+
} catch (e) {
|
|
14026
|
+
logger.warn(`[${SERVER_TAG3}] Site activity unavailable: ${String(e)}`);
|
|
14027
|
+
siteActivity?.close();
|
|
14028
|
+
siteActivity = undefined;
|
|
14029
|
+
}
|
|
14030
|
+
try {
|
|
14031
|
+
usageStore = await openUsageStore(join9(dirname10(cfg.dbPath), "usage.db"));
|
|
12922
14032
|
startUsageSampler(usageStore);
|
|
12923
14033
|
} catch (e) {
|
|
12924
14034
|
logger.warn(`[${SERVER_TAG3}] usage history disabled: ${String(e)}`);
|
|
12925
14035
|
}
|
|
12926
14036
|
try {
|
|
12927
|
-
capsmanStore = await openCapsmanStore(
|
|
14037
|
+
capsmanStore = await openCapsmanStore(join9(dirname10(cfg.dbPath), "capsman.db"));
|
|
12928
14038
|
startCapsmanSampler(capsmanStore);
|
|
12929
14039
|
} catch (e) {
|
|
12930
14040
|
logger.warn(`[${SERVER_TAG3}] capsman trends disabled: ${String(e)}`);
|
|
@@ -13004,6 +14114,9 @@ async function runDashboard(cfg, transportLabel) {
|
|
|
13004
14114
|
const ipIntelligenceResp = await ipIntelligenceRoutes(req, url);
|
|
13005
14115
|
if (ipIntelligenceResp)
|
|
13006
14116
|
return ipIntelligenceResp;
|
|
14117
|
+
const bandwidthResp = await bandwidthRoutes(req, url);
|
|
14118
|
+
if (bandwidthResp)
|
|
14119
|
+
return bandwidthResp;
|
|
13007
14120
|
const roundTripResp = await roundTripRoutes(req, url);
|
|
13008
14121
|
if (roundTripResp)
|
|
13009
14122
|
return roundTripResp;
|
|
@@ -13028,6 +14141,9 @@ async function runDashboard(cfg, transportLabel) {
|
|
|
13028
14141
|
const flowResp = await flowRoutes(req, url);
|
|
13029
14142
|
if (flowResp)
|
|
13030
14143
|
return flowResp;
|
|
14144
|
+
const siteActivityResp = await siteActivityRoutes(req, url, siteActivity);
|
|
14145
|
+
if (siteActivityResp)
|
|
14146
|
+
return siteActivityResp;
|
|
13031
14147
|
const rolloutResp = await rolloutRoutes(req, url);
|
|
13032
14148
|
if (rolloutResp)
|
|
13033
14149
|
return rolloutResp;
|
|
@@ -13343,6 +14459,10 @@ async function runDashboard(cfg, transportLabel) {
|
|
|
13343
14459
|
stop() {
|
|
13344
14460
|
stopOpenVpnHistory?.();
|
|
13345
14461
|
openVpnHistory?.close();
|
|
14462
|
+
stopSiteActivity?.();
|
|
14463
|
+
siteActivity?.close();
|
|
14464
|
+
if (dashboardStartedFlows)
|
|
14465
|
+
getFlowCollector().stop();
|
|
13346
14466
|
stopHealthChecks();
|
|
13347
14467
|
stopGeoLookups();
|
|
13348
14468
|
stopUsageSampler();
|
|
@@ -23149,7 +24269,7 @@ async function printBanner() {
|
|
|
23149
24269
|
|
|
23150
24270
|
// src/alerts/sampler.ts
|
|
23151
24271
|
var DEFAULT_ALERT_SAMPLE_MS = 30000;
|
|
23152
|
-
var
|
|
24272
|
+
var timer4 = null;
|
|
23153
24273
|
var snapshotStorePromise = null;
|
|
23154
24274
|
function getSnapshotStore() {
|
|
23155
24275
|
snapshotStorePromise ??= openSnapshotStore2(DEFAULT_SNAPSHOT_DB2);
|
|
@@ -23227,18 +24347,18 @@ async function sampleAlertsOnce(now = Date.now()) {
|
|
|
23227
24347
|
}
|
|
23228
24348
|
function startAlertSampler(intervalMs = DEFAULT_ALERT_SAMPLE_MS) {
|
|
23229
24349
|
stopAlertSampler();
|
|
23230
|
-
|
|
24350
|
+
timer4 = setInterval(() => {
|
|
23231
24351
|
sampleAlertsOnce().catch((e) => {
|
|
23232
24352
|
logger.debug(`[alerts] sampler tick failed: ${e instanceof Error ? e.message : String(e)}`);
|
|
23233
24353
|
});
|
|
23234
24354
|
}, intervalMs);
|
|
23235
|
-
|
|
24355
|
+
timer4.unref?.();
|
|
23236
24356
|
logger.debug(`[alerts] metric sampler started (${intervalMs}ms)`);
|
|
23237
24357
|
}
|
|
23238
24358
|
function stopAlertSampler() {
|
|
23239
|
-
if (
|
|
23240
|
-
clearInterval(
|
|
23241
|
-
|
|
24359
|
+
if (timer4)
|
|
24360
|
+
clearInterval(timer4);
|
|
24361
|
+
timer4 = null;
|
|
23242
24362
|
}
|
|
23243
24363
|
|
|
23244
24364
|
// src/cli.ts
|
|
@@ -23410,6 +24530,7 @@ async function main() {
|
|
|
23410
24530
|
logger.error(`Alerting disabled \u2014 invalid rules: ${e instanceof Error ? e.message : String(e)}`);
|
|
23411
24531
|
}
|
|
23412
24532
|
}
|
|
24533
|
+
startBandwidthScheduler().catch(() => logger.error("Bandwidth scheduler unavailable; check local storage."));
|
|
23413
24534
|
startScheduler().catch((e) => {
|
|
23414
24535
|
logger.error(`Scheduled audits disabled: ${e instanceof Error ? e.message : String(e)}`);
|
|
23415
24536
|
});
|
|
@@ -23419,11 +24540,12 @@ async function main() {
|
|
|
23419
24540
|
logger.error(`Attack detection disabled: ${e instanceof Error ? e.message : String(e)}`);
|
|
23420
24541
|
}
|
|
23421
24542
|
let shuttingDown = false;
|
|
23422
|
-
const shutdown = (signal) => {
|
|
24543
|
+
const shutdown = async (signal) => {
|
|
23423
24544
|
if (shuttingDown)
|
|
23424
24545
|
return;
|
|
23425
24546
|
shuttingDown = true;
|
|
23426
24547
|
logger.info(`Received ${signal}, shutting down\u2026`);
|
|
24548
|
+
await stopBandwidthScheduler();
|
|
23427
24549
|
closeAll();
|
|
23428
24550
|
closeMemoryStore();
|
|
23429
24551
|
stopAlertSampler();
|