@usex/mikrotik-mcp 6.8.0 → 6.10.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.
Files changed (31) hide show
  1. package/README.md +2 -0
  2. package/dist/cli.js +630 -25
  3. package/dist/index.js +1 -1
  4. package/dist/shared/{cli-ksq2qr7d.js → cli-0srgjr3v.js} +1 -1
  5. package/dist/shared/{cli-mh3kf1gx.js → cli-81s63cts.js} +124 -33
  6. package/dist/shared/{cli-qszw8tv2.js → cli-b4kkd56k.js} +1 -1
  7. package/dist/shared/{cli-pan4hpjf.js → cli-chcn79z7.js} +1 -1
  8. package/dist/shared/{cli-4a4dxv04.js → cli-twafzgn3.js} +1 -1
  9. package/dist/shared/{library-r8m6ftrj.js → library-2kk6k7zh.js} +1 -1
  10. package/dist/shared/{library-g8fvds7b.js → library-f89nvhan.js} +1 -1
  11. package/dist/shared/{library-5ejy1n1c.js → library-sxxt02xq.js} +1 -1
  12. package/dist/shared/{library-qwkp17zf.js → library-thv5s4bs.js} +112 -21
  13. package/dist/shared/{library-qdh9kmg5.js → library-x66xvb3s.js} +1 -1
  14. package/dist/ui/observability.html +78 -86
  15. package/package.json +8 -8
  16. package/schemas/README.md +1 -1
  17. package/schemas/devtools-manifest.json +23 -1
  18. package/schemas/tool-catalog.json +148 -5
  19. package/schemas/tools/add_bridge_port.json +4 -0
  20. package/schemas/tools/add_dot1x_server.json +4 -0
  21. package/schemas/tools/add_wireguard_peer.json +6 -0
  22. package/schemas/tools/create_bridge.json +21 -0
  23. package/schemas/tools/create_dhcp_server.json +4 -0
  24. package/schemas/tools/create_ipv6_dhcp_server.json +8 -0
  25. package/schemas/tools/create_vlan_interface.json +4 -0
  26. package/schemas/tools/list_bgp_instances.json +7 -0
  27. package/schemas/tools/update_bgp_instance_med_comparison.json +22 -0
  28. package/schemas/tools/update_bridge.json +21 -0
  29. package/schemas/tools/update_dot1x_server.json +4 -0
  30. package/schemas/tools/update_vlan_interface.json +4 -0
  31. package/schemas/tools/update_wireguard_peer.json +17 -3
package/dist/cli.js CHANGED
@@ -145,6 +145,7 @@ import {
145
145
  setDeviceLabel,
146
146
  removeDeviceLease,
147
147
  devicesView,
148
+ ipv6,
148
149
  getFlowCollector,
149
150
  topTalkers,
150
151
  conversations,
@@ -209,6 +210,7 @@ import {
209
210
  matchAdvisories,
210
211
  collectDeviceFacts,
211
212
  getDeviceGeo,
213
+ isPublicIpLiteral,
212
214
  sourceIpLiteral,
213
215
  getIpGeo,
214
216
  ipGeoRoute,
@@ -216,6 +218,7 @@ import {
216
218
  stopGeoLookups,
217
219
  IpIntelligenceInput,
218
220
  lookupIpIntelligence,
221
+ decodeTzsp,
219
222
  DEFAULT_TZSP_PORT,
220
223
  capture,
221
224
  OpenVpnOperationError,
@@ -342,7 +345,7 @@ import {
342
345
  allToolModules2,
343
346
  ALWAYS_ON_MODULES2,
344
347
  selectToolModules2
345
- } from "./shared/cli-mh3kf1gx.js";
348
+ } from "./shared/cli-81s63cts.js";
346
349
 
347
350
  // node_modules/ajv/dist/compile/codegen/code.js
348
351
  var require_code = __commonJS(function(exports) {
@@ -6910,7 +6913,7 @@ import { existsSync as existsSync2 } from "fs";
6910
6913
  import { spawn } from "child_process";
6911
6914
  import { readFileSync as readFileSync4 } from "fs";
6912
6915
  import { homedir as homedir5, networkInterfaces as networkInterfaces2 } from "os";
6913
- import { dirname as dirname7, join as join8 } from "path";
6916
+ import { dirname as dirname8, join as join8 } from "path";
6914
6917
  var {serve } = globalThis.Bun;
6915
6918
 
6916
6919
  // src/observability/modules.ts
@@ -7672,6 +7675,9 @@ class OpenVpnHistoryStore {
7672
7675
  device TEXT PRIMARY KEY, first INTEGER NOT NULL, last INTEGER NOT NULL,
7673
7676
  gaps INTEGER NOT NULL DEFAULT 0, failures INTEGER NOT NULL DEFAULT 0)`);
7674
7677
  }
7678
+ activeTunnelOwners(now = Date.now()) {
7679
+ return this.db.query("SELECT tunnel ip,user,device FROM ovpn_history WHERE ended IS NULL AND tunnel<>'' AND lastSeen>=?").all(now - 60000);
7680
+ }
7675
7681
  ingest(snapshot) {
7676
7682
  const { device, observedAt: at, sessions } = snapshot;
7677
7683
  this.db.transaction(() => {
@@ -7835,6 +7841,577 @@ function startOpenVpnHistorySampler(store) {
7835
7841
  };
7836
7842
  }
7837
7843
 
7844
+ // src/observability/site-activity.ts
7845
+ import { chmodSync as chmodSync2, mkdirSync as mkdirSync2 } from "fs";
7846
+ import { dirname as dirname2 } from "path";
7847
+ import { isIP as isIP2 } from "net";
7848
+ function validSiteHost(host) {
7849
+ const normalized = host.toLowerCase().replace(/\.$/, "");
7850
+ if (normalized.length > 253 || !/^[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?$/.test(normalized))
7851
+ return null;
7852
+ if (isIP2(normalized))
7853
+ return null;
7854
+ const labels = normalized.split(".");
7855
+ if (labels.length < 2 || labels.some((label) => !label || label.length > 63 || label.startsWith("-") || label.endsWith("-")))
7856
+ return null;
7857
+ return normalized;
7858
+ }
7859
+ function dnsAnswers(frame, seen) {
7860
+ const u16 = (at) => frame[at] << 8 | frame[at + 1];
7861
+ const u32 = (at) => frame[at] * 2 ** 24 + frame[at + 1] * 2 ** 16 + frame[at + 2] * 256 + frame[at + 3] >>> 0;
7862
+ if (frame.length < 42)
7863
+ return [];
7864
+ let l3 = 14;
7865
+ let eth = u16(12);
7866
+ if (eth === 33024) {
7867
+ l3 += 4;
7868
+ eth = u16(16);
7869
+ }
7870
+ let udp;
7871
+ let client;
7872
+ if (eth === 2048) {
7873
+ if (frame.length < l3 + 20 || frame[l3 + 9] !== 17 || u16(l3 + 6) & 16383)
7874
+ return [];
7875
+ udp = l3 + (frame[l3] & 15) * 4;
7876
+ client = `${frame[l3 + 16]}.${frame[l3 + 17]}.${frame[l3 + 18]}.${frame[l3 + 19]}`;
7877
+ } else if (eth === 34525) {
7878
+ if (frame.length < l3 + 40 || frame[l3 + 6] !== 17)
7879
+ return [];
7880
+ udp = l3 + 40;
7881
+ client = ipv6(frame, l3 + 24);
7882
+ } else
7883
+ return [];
7884
+ if (frame.length < udp + 20 || u16(udp) !== 53)
7885
+ return [];
7886
+ const end = Math.min(frame.length, udp + u16(udp + 4));
7887
+ const dns = udp + 8;
7888
+ if (end < dns + 12 || !(u16(dns + 2) & 32768) || u16(dns + 2) & 15 || u16(dns + 4) !== 1 || u16(dns + 6) > 32)
7889
+ return [];
7890
+ function name(at) {
7891
+ const labels = [];
7892
+ let next = at;
7893
+ let jumped = false;
7894
+ const visited = new Set;
7895
+ for (let i = 0;i < 64; i++) {
7896
+ if (at >= end || visited.has(at))
7897
+ return null;
7898
+ visited.add(at);
7899
+ const len = frame[at++];
7900
+ if ((len & 192) === 192) {
7901
+ if (at >= end)
7902
+ return null;
7903
+ const pointer = dns + ((len & 63) << 8 | frame[at++]);
7904
+ if (pointer >= end || pointer < dns)
7905
+ return null;
7906
+ if (!jumped)
7907
+ next = at;
7908
+ jumped = true;
7909
+ at = pointer;
7910
+ continue;
7911
+ }
7912
+ if (len & 192)
7913
+ return null;
7914
+ if (!len)
7915
+ return { value: labels.join("."), next: jumped ? next : at };
7916
+ if (len > 63 || at + len > end)
7917
+ return null;
7918
+ labels.push(String.fromCharCode(...frame.subarray(at, at + len)));
7919
+ at += len;
7920
+ }
7921
+ return null;
7922
+ }
7923
+ const question = name(dns + 12);
7924
+ if (!question || question.next + 4 > end)
7925
+ return [];
7926
+ const domain = validSiteHost(question.value);
7927
+ if (!domain)
7928
+ return [];
7929
+ let at = question.next + 4;
7930
+ const answers = [];
7931
+ for (let i = 0;i < u16(dns + 6); i++) {
7932
+ const rr = name(at);
7933
+ if (!rr || rr.next + 10 > end)
7934
+ return [];
7935
+ at = rr.next;
7936
+ const kind = u16(at), klass = u16(at + 2), ttl = u32(at + 4), size = u16(at + 8);
7937
+ at += 10;
7938
+ if (at + size > end)
7939
+ return [];
7940
+ if (rr.value.toLowerCase().replace(/\.$/, "") === domain && klass === 1 && ttl > 0) {
7941
+ 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;
7942
+ if (destination && isPublicIpLiteral(destination))
7943
+ answers.push({
7944
+ client,
7945
+ destination,
7946
+ domain,
7947
+ seen,
7948
+ expires: seen + Math.min(ttl, 86400) * 1000
7949
+ });
7950
+ }
7951
+ at += size;
7952
+ }
7953
+ return answers;
7954
+ }
7955
+ function proxyVisit(message) {
7956
+ const client = /(?:\b(?:from|src(?:-address)?)\s*[=:]?\s*)(\d{1,3}(?:\.\d{1,3}){3})\b/i.exec(message)?.[1];
7957
+ const target = /\b(?:CONNECT\s+|https?:\/\/)([^\s/"?]+)/i.exec(message)?.[1];
7958
+ if (!client || isIP2(client) !== 4 || !target)
7959
+ return null;
7960
+ const host = validSiteHost(target.replace(/:\d+$/, ""));
7961
+ return host ? { client, host } : null;
7962
+ }
7963
+
7964
+ class SqliteSiteActivityStore {
7965
+ db;
7966
+ currentPolicy = { enabled: true, owners: [], until: null };
7967
+ constructor(db) {
7968
+ this.db = db;
7969
+ db.run("PRAGMA journal_mode=WAL");
7970
+ 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))`);
7971
+ db.run("CREATE INDEX IF NOT EXISTS site_dns_lookup ON site_dns(client,destination,seen,expires)");
7972
+ 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))`);
7973
+ db.run("CREATE INDEX IF NOT EXISTS site_activity_time ON site_activity(last_seen)");
7974
+ db.run("CREATE INDEX IF NOT EXISTS site_activity_client ON site_activity(client,last_seen)");
7975
+ db.run("CREATE TABLE IF NOT EXISTS site_proxy_ids (id TEXT PRIMARY KEY, seen INTEGER NOT NULL)");
7976
+ 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))");
7977
+ db.run("CREATE INDEX IF NOT EXISTS site_identity_lookup ON site_identity(client,observed)");
7978
+ 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)");
7979
+ const saved = db.query("SELECT enabled,owners,until FROM site_policy WHERE id=1").get();
7980
+ if (saved)
7981
+ this.currentPolicy = {
7982
+ enabled: !!saved.enabled,
7983
+ owners: JSON.parse(saved.owners),
7984
+ until: saved.until
7985
+ };
7986
+ }
7987
+ answer(client, destination, domain, expires, seen) {
7988
+ if (!isIP2(client) || !this.allows(client, seen) || !isPublicIpLiteral(destination) || !validSiteHost(domain) || expires <= seen)
7989
+ return;
7990
+ this.db.query("INSERT OR IGNORE INTO site_dns VALUES (?,?,?,?,?)").run(client, destination, domain, seen, expires);
7991
+ }
7992
+ flows(records) {
7993
+ const match = this.db.query("SELECT DISTINCT domain FROM site_dns WHERE client=? AND destination=? AND seen<=? AND expires>=? LIMIT 2");
7994
+ const upsert = this.db.query(`INSERT INTO site_activity VALUES (?,?,?,?,?,?,?,?,1)
7995
+ ON CONFLICT(client,destination,domain,source,bucket) DO UPDATE SET
7996
+ first_seen=MIN(first_seen,excluded.first_seen), last_seen=MAX(last_seen,excluded.last_seen),
7997
+ bytes=bytes+excluded.bytes, connections=connections+1`);
7998
+ this.db.transaction(() => {
7999
+ for (const r of records) {
8000
+ if (!isPublicIpLiteral(r.dst) || !isIP2(r.src) || !this.allows(r.src, r.start) || ![80, 443].includes(r.dstPort) || ![6, 17].includes(r.protocol))
8001
+ continue;
8002
+ const domains = match.all(r.src, r.dst, r.start, r.start);
8003
+ const domain = domains.length === 1 ? domains[0].domain : "";
8004
+ 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));
8005
+ }
8006
+ })();
8007
+ }
8008
+ proxy(id, client, host, at) {
8009
+ if (!id || !isIP2(client) || !this.allows(client, at) || !validSiteHost(host))
8010
+ return;
8011
+ this.db.transaction(() => {
8012
+ const inserted = this.db.query("INSERT OR IGNORE INTO site_proxy_ids VALUES (?,?)").run(id, at);
8013
+ if (!inserted.changes)
8014
+ return;
8015
+ this.db.query(`INSERT INTO site_activity VALUES (?,?,?,?,?,?,?,?,1)
8016
+ ON CONFLICT(client,destination,domain,source,bucket) DO UPDATE SET
8017
+ last_seen=MAX(last_seen,excluded.last_seen),connections=connections+1`).run(client, "", host, "proxy", Math.floor(at / 300000) * 300000, at, at, 0);
8018
+ })();
8019
+ }
8020
+ proxyDestination(client, destination, at, bytes) {
8021
+ if (!isIP2(client) || !this.allows(client, at) || !isPublicIpLiteral(destination))
8022
+ return;
8023
+ this.db.query(`INSERT INTO site_activity VALUES (?,?,?,?,?,?,?,?,1)
8024
+ ON CONFLICT(client,destination,domain,source,bucket) DO UPDATE SET
8025
+ 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));
8026
+ }
8027
+ identify(rows) {
8028
+ const insert = this.db.query("INSERT OR REPLACE INTO site_identity VALUES (?,?,?,?,?,?)");
8029
+ this.db.transaction(() => {
8030
+ for (const row of rows)
8031
+ if (isIP2(row.client) && row.owner && row.observed > 0)
8032
+ insert.run(row.client, row.kind, row.owner, row.device, row.mac, row.observed);
8033
+ })();
8034
+ }
8035
+ policy() {
8036
+ return { ...this.currentPolicy, owners: [...this.currentPolicy.owners] };
8037
+ }
8038
+ setPolicy(policy) {
8039
+ this.db.query("INSERT OR REPLACE INTO site_policy VALUES (1,?,?,?)").run(policy.enabled ? 1 : 0, JSON.stringify(policy.owners), policy.until);
8040
+ this.currentPolicy = { ...policy, owners: [...policy.owners] };
8041
+ }
8042
+ allows(client, at) {
8043
+ const policy = this.currentPolicy;
8044
+ if (!policy.enabled || policy.until !== null && at >= policy.until)
8045
+ return false;
8046
+ if (!policy.owners.length)
8047
+ return true;
8048
+ 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);
8049
+ return !!identity && policy.owners.includes(identity.owner);
8050
+ }
8051
+ identities(from, to) {
8052
+ 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);
8053
+ }
8054
+ static ENRICHED = `WITH enriched AS (
8055
+ SELECT a.*, CASE WHEN a.source='proxy' THEN 'proxy' ELSE COALESCE(i.kind,'unknown') END kind,
8056
+ CASE WHEN a.source='proxy' THEN a.client ELSE COALESCE(i.owner,'') END owner,
8057
+ COALESCE(i.device,'') device
8058
+ FROM site_activity a LEFT JOIN site_identity i ON i.rowid=(
8059
+ SELECT rowid FROM site_identity WHERE client=a.client AND observed<=a.last_seen
8060
+ AND observed>=a.first_seen-600000 ORDER BY observed DESC LIMIT 1)
8061
+ WHERE a.last_seen>=? AND a.first_seen<? AND (?='' OR a.client=?)
8062
+ AND (?='' OR a.domain LIKE ? OR a.destination LIKE ?))`;
8063
+ filterArgs(f) {
8064
+ return [
8065
+ f.from,
8066
+ f.to,
8067
+ f.client,
8068
+ f.client,
8069
+ f.search,
8070
+ `%${f.search}%`,
8071
+ `%${f.search}%`,
8072
+ f.kind,
8073
+ f.kind,
8074
+ f.owner,
8075
+ f.owner
8076
+ ];
8077
+ }
8078
+ visits(filters, offset, limit = 100) {
8079
+ const rows = this.db.query(`${SqliteSiteActivityStore.ENRICHED}
8080
+ SELECT client,destination,domain,source,kind,owner,device,MIN(first_seen) firstSeen,MAX(last_seen) lastSeen,SUM(bytes) bytes,SUM(connections) connections
8081
+ FROM enriched WHERE (?='' OR kind=?) AND (?='' OR owner=?)
8082
+ GROUP BY client,destination,domain,source,kind,owner,device ORDER BY lastSeen DESC LIMIT ? OFFSET ?`).all(...this.filterArgs(filters), limit, offset);
8083
+ return rows.map((r) => ({ ...r, domain: r.domain || null }));
8084
+ }
8085
+ summary(filters) {
8086
+ const where = "FROM enriched WHERE (?='' OR kind=?) AND (?='' OR owner=?)";
8087
+ const args = this.filterArgs(filters);
8088
+ const totals = this.db.query(`${SqliteSiteActivityStore.ENRICHED}
8089
+ SELECT COALESCE(SUM(connections),0) observations,COALESCE(SUM(bytes),0) bytes,
8090
+ COUNT(DISTINCT COALESCE(NULLIF(domain,''),destination)) destinations,COUNT(DISTINCT client) clients ${where}`).get(...args);
8091
+ const groups = this.db.query(`${SqliteSiteActivityStore.ENRICHED}
8092
+ SELECT COALESCE(NULLIF(domain,''),destination) label,SUM(connections) observations,SUM(bytes) bytes ${where}
8093
+ GROUP BY label ORDER BY observations DESC LIMIT 30`).all(...args);
8094
+ return { ...totals, groups };
8095
+ }
8096
+ clients(from, to) {
8097
+ 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);
8098
+ }
8099
+ prune(now) {
8100
+ this.db.query("DELETE FROM site_dns WHERE expires<?").run(now);
8101
+ const cutoff = now - 30 * 86400000;
8102
+ this.db.query("DELETE FROM site_activity WHERE last_seen<?").run(cutoff);
8103
+ this.db.query("DELETE FROM site_activity WHERE first_seen>?").run(now + 300000);
8104
+ this.db.query("DELETE FROM site_proxy_ids WHERE seen<?").run(cutoff);
8105
+ this.db.query("DELETE FROM site_identity WHERE observed<?").run(cutoff - 600000);
8106
+ }
8107
+ close() {
8108
+ this.db.close();
8109
+ }
8110
+ }
8111
+ async function openSiteActivityStore(path) {
8112
+ if (path !== ":memory:")
8113
+ mkdirSync2(dirname2(path), { recursive: true, mode: 448 });
8114
+ const { Database } = await import("bun:sqlite");
8115
+ const db = new Database(path, { create: true });
8116
+ if (path !== ":memory:")
8117
+ chmodSync2(path, 384);
8118
+ try {
8119
+ return new SqliteSiteActivityStore(db);
8120
+ } catch (error) {
8121
+ db.close();
8122
+ throw error;
8123
+ }
8124
+ }
8125
+
8126
+ // src/observability/site-activity-collector.ts
8127
+ import { createSocket } from "dgram";
8128
+ import { createHash as createHash3 } from "crypto";
8129
+ var SITE_DNS_PORT = 37009;
8130
+ var PROXY_INTERVAL = 30000;
8131
+ var MAX_LOG_BYTES = 250000;
8132
+ function parseProxyLogRows(output, device) {
8133
+ if (output.length > MAX_LOG_BYTES)
8134
+ throw new Error("Proxy log response exceeds limit");
8135
+ const rows = JSON.parse(output);
8136
+ if (!Array.isArray(rows))
8137
+ throw new Error("Invalid proxy log response");
8138
+ return rows.flatMap((row) => {
8139
+ if (!row || typeof row !== "object")
8140
+ return [];
8141
+ const r = row;
8142
+ if (typeof r.message !== "string" || typeof r[".id"] !== "string")
8143
+ return [];
8144
+ const visit = proxyVisit(r.message);
8145
+ return visit ? [
8146
+ {
8147
+ id: createHash3("sha256").update(`${device}:${r[".id"]}:${typeof r.time === "string" ? r.time : ""}:${r.message}`).digest("hex"),
8148
+ ...visit
8149
+ }
8150
+ ] : [];
8151
+ });
8152
+ }
8153
+ function parseProxyConnections(output) {
8154
+ if (output.length > MAX_LOG_BYTES)
8155
+ throw new Error("Proxy connection response exceeds limit");
8156
+ const rows = JSON.parse(output);
8157
+ if (!Array.isArray(rows))
8158
+ throw new Error("Invalid proxy connections response");
8159
+ return rows.flatMap((row) => {
8160
+ if (!row || typeof row !== "object")
8161
+ return [];
8162
+ const r = row;
8163
+ if (typeof r["src-address"] !== "string" || typeof r["dst-address"] !== "string")
8164
+ return [];
8165
+ const bytes = Number(r["rx-bytes"] ?? 0);
8166
+ return [
8167
+ {
8168
+ client: r["src-address"],
8169
+ destination: r["dst-address"],
8170
+ bytes: Number.isSafeInteger(bytes) && bytes >= 0 ? bytes : 0
8171
+ }
8172
+ ];
8173
+ });
8174
+ }
8175
+ async function startSiteActivityCollector(store, history) {
8176
+ const unsubscribe = getFlowCollector().subscribe((records) => store.flows(records));
8177
+ const socket = createSocket("udp4");
8178
+ let dnsPackets = 0;
8179
+ let stopped = false;
8180
+ let polling = false;
8181
+ const proxyEnabled = new Map;
8182
+ let lastLanRead = 0;
8183
+ async function identifyClients() {
8184
+ const at = Date.now();
8185
+ if (history) {
8186
+ const owners = history.activeTunnelOwners(at);
8187
+ const unique = owners.filter((owner) => owners.filter((other) => other.ip === owner.ip).length === 1);
8188
+ store.identify(unique.map((owner) => ({
8189
+ client: owner.ip,
8190
+ kind: "openvpn",
8191
+ owner: owner.user,
8192
+ device: owner.device,
8193
+ mac: "",
8194
+ observed: at
8195
+ })));
8196
+ }
8197
+ if (at - lastLanRead < 300000)
8198
+ return;
8199
+ lastLanRead = at;
8200
+ const device = getConfig().defaultDevice;
8201
+ const ctx = createContext(undefined, device);
8202
+ try {
8203
+ const clients = await fetchDevices(ctx);
8204
+ let wifi = "";
8205
+ try {
8206
+ wifi = await executeMikrotikCommand("/interface wifi registration-table print detail", ctx, { maxMs: 1e4 });
8207
+ } catch {}
8208
+ const associated = new Set(parseRecords(wifi).rows.map((row) => row["mac-address"]?.toUpperCase()).filter(Boolean));
8209
+ store.identify(clients.filter((c) => c.ip && c.mac).map((c) => ({
8210
+ client: c.ip,
8211
+ kind: associated.has(c.mac.toUpperCase()) ? "wifi" : /^ether/i.test(c.iface) ? "lan" : "lan-wifi",
8212
+ owner: c.host || c.comment || c.mac,
8213
+ device,
8214
+ mac: c.mac,
8215
+ observed: at
8216
+ })));
8217
+ } catch (error) {
8218
+ logger.warn(`Site client identity read failed: ${String(error)}`);
8219
+ }
8220
+ }
8221
+ socket.on("message", (packet) => {
8222
+ const decoded = decodeTzsp(new Uint8Array(packet));
8223
+ if (decoded?.encap !== 1 || !decoded.frame)
8224
+ return;
8225
+ dnsPackets++;
8226
+ const at = Date.now();
8227
+ try {
8228
+ for (const answer of dnsAnswers(decoded.frame, at))
8229
+ store.answer(answer.client, answer.destination, answer.domain, answer.expires, answer.seen);
8230
+ } catch (e) {
8231
+ logger.warn(`Site DNS evidence could not be saved: ${String(e)}`);
8232
+ }
8233
+ });
8234
+ socket.on("error", (e) => logger.warn(`Site DNS collector: ${e.message}`));
8235
+ try {
8236
+ await new Promise((resolve, reject) => {
8237
+ socket.once("error", reject);
8238
+ socket.bind(SITE_DNS_PORT, () => {
8239
+ socket.off("error", reject);
8240
+ resolve();
8241
+ });
8242
+ });
8243
+ } catch (e) {
8244
+ unsubscribe();
8245
+ socket.close();
8246
+ throw e;
8247
+ }
8248
+ async function poll() {
8249
+ if (polling || stopped)
8250
+ return;
8251
+ polling = true;
8252
+ try {
8253
+ const policy = store.policy();
8254
+ if (!policy.enabled || policy.until !== null && Date.now() >= policy.until)
8255
+ return;
8256
+ try {
8257
+ await identifyClients();
8258
+ } catch (e) {
8259
+ logger.warn(`Site identity sample failed: ${String(e)}`);
8260
+ }
8261
+ for (const [device, config] of Object.entries(getConfig().devices)) {
8262
+ if (stopped)
8263
+ break;
8264
+ if (config.disabled || config.mac)
8265
+ continue;
8266
+ let enabled = proxyEnabled.get(device);
8267
+ if (!enabled || enabled.expires < Date.now()) {
8268
+ try {
8269
+ const settings = await executeMikrotikCommand("/ip proxy print", createContext(undefined, device), { maxMs: 1e4 });
8270
+ enabled = {
8271
+ enabled: /^\s*enabled:\s*yes\b/im.test(settings),
8272
+ expires: Date.now() + 600000
8273
+ };
8274
+ proxyEnabled.set(device, enabled);
8275
+ } catch {
8276
+ continue;
8277
+ }
8278
+ }
8279
+ if (!enabled.enabled)
8280
+ continue;
8281
+ try {
8282
+ 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 });
8283
+ for (const connection of parseProxyConnections(connections))
8284
+ store.proxyDestination(connection.client, connection.destination, Date.now(), connection.bytes);
8285
+ } catch {}
8286
+ try {
8287
+ 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 });
8288
+ for (const visit of parseProxyLogRows(output, device))
8289
+ store.proxy(visit.id, visit.client, visit.host, Date.now());
8290
+ } catch {}
8291
+ }
8292
+ if (!stopped) {
8293
+ try {
8294
+ store.prune(Date.now());
8295
+ } catch (e) {
8296
+ logger.warn(`Site activity pruning failed: ${String(e)}`);
8297
+ }
8298
+ }
8299
+ } finally {
8300
+ polling = false;
8301
+ }
8302
+ }
8303
+ poll();
8304
+ const timer = setInterval(() => void poll(), PROXY_INTERVAL);
8305
+ logger.info(`Site activity DNS receiver listening on UDP ${SITE_DNS_PORT}`);
8306
+ return () => {
8307
+ stopped = true;
8308
+ clearInterval(timer);
8309
+ unsubscribe();
8310
+ socket.close();
8311
+ logger.info(`Site activity stopped after ${dnsPackets} DNS mirror packet(s)`);
8312
+ };
8313
+ }
8314
+
8315
+ // src/observability/site-activity-routes.ts
8316
+ import { isIP as isIP3 } from "net";
8317
+ async function siteActivityRoutes(req, url, store) {
8318
+ if (!["/api/site-activity", "/api/site-activity/export", "/api/site-activity/policy"].includes(url.pathname))
8319
+ return null;
8320
+ if (!store)
8321
+ return Response.json({ error: "Site activity storage is unavailable" }, { status: 503 });
8322
+ if (url.pathname.endsWith("/policy")) {
8323
+ if (req.method === "GET")
8324
+ return Response.json(store.policy());
8325
+ if (req.method !== "PUT")
8326
+ return Response.json({ error: "Method not allowed" }, { status: 405 });
8327
+ try {
8328
+ const body = await req.json();
8329
+ if (!body || typeof body !== "object")
8330
+ throw new Error("Invalid policy");
8331
+ const policy = body;
8332
+ 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()))
8333
+ throw new Error("Invalid policy");
8334
+ store.setPolicy({
8335
+ enabled: policy.enabled,
8336
+ owners: [...new Set(policy.owners)],
8337
+ until: policy.until
8338
+ });
8339
+ return Response.json(store.policy());
8340
+ } catch {
8341
+ return Response.json({ error: "Invalid activity policy" }, { status: 400 });
8342
+ }
8343
+ }
8344
+ if (req.method !== "GET")
8345
+ return Response.json({ error: "Method not allowed" }, { status: 405 });
8346
+ const now = Date.now();
8347
+ const from = Number(url.searchParams.get("from") ?? now - 86400000);
8348
+ const to = Number(url.searchParams.get("to") ?? now);
8349
+ const client = url.searchParams.get("client") ?? "";
8350
+ const search = (url.searchParams.get("search") ?? "").trim().toLowerCase();
8351
+ const offset = Number(url.searchParams.get("offset") ?? 0);
8352
+ const kind = url.searchParams.get("kind") ?? "";
8353
+ const owner = url.searchParams.get("owner") ?? "";
8354
+ 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)
8355
+ return Response.json({ error: "Invalid activity filters" }, { status: 400 });
8356
+ try {
8357
+ const filters = { from, to, client, search, kind, owner };
8358
+ if (url.pathname.endsWith("/export")) {
8359
+ const csv = (value) => `"${String(value ?? "").replace(/"/g, '""').replace(/^([=+@-])/, "'$1")}"`;
8360
+ const columns = [
8361
+ "Client IP",
8362
+ "Connection",
8363
+ "Owner",
8364
+ "Router",
8365
+ "Domain",
8366
+ "Destination IP",
8367
+ "Evidence",
8368
+ "First seen",
8369
+ "Last seen",
8370
+ "Observations",
8371
+ "Bytes"
8372
+ ];
8373
+ const rows = store.visits(filters, 0, 1e4).map((r) => [
8374
+ r.client,
8375
+ r.kind,
8376
+ r.owner,
8377
+ r.device,
8378
+ r.domain,
8379
+ r.destination,
8380
+ r.source,
8381
+ new Date(r.firstSeen).toISOString(),
8382
+ new Date(r.lastSeen).toISOString(),
8383
+ r.connections,
8384
+ r.bytes
8385
+ ]);
8386
+ return new Response([columns, ...rows].map((row) => row.map(csv).join(",")).join(`
8387
+ `), {
8388
+ headers: {
8389
+ "content-type": "text/csv; charset=utf-8",
8390
+ "content-disposition": "attachment; filename=site-activity.csv",
8391
+ "cache-control": "no-store"
8392
+ }
8393
+ });
8394
+ }
8395
+ return Response.json({
8396
+ from,
8397
+ to,
8398
+ clients: store.clients(from, to),
8399
+ identities: store.identities(from, to),
8400
+ policy: store.policy(),
8401
+ visits: store.visits(filters, offset),
8402
+ summary: store.summary(filters),
8403
+ collection: {
8404
+ flows: getFlowCollector().stats(),
8405
+ dnsPort: SITE_DNS_PORT,
8406
+ proxyLogs: "polled"
8407
+ },
8408
+ 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."
8409
+ });
8410
+ } catch {
8411
+ return Response.json({ error: "Could not read site activity" }, { status: 503 });
8412
+ }
8413
+ }
8414
+
7838
8415
  // src/observability/ip-intelligence-routes.ts
7839
8416
  async function ipIntelligenceRoutes(req, url) {
7840
8417
  if (url.pathname !== "/api/ip-intelligence")
@@ -7927,9 +8504,9 @@ async function roundTripRoutes(req, url) {
7927
8504
  }
7928
8505
 
7929
8506
  // src/observability/um-report-cache.ts
7930
- import { createHash as createHash3, randomUUID as randomUUID2 } from "crypto";
8507
+ import { createHash as createHash4, randomUUID as randomUUID2 } from "crypto";
7931
8508
  import { mkdir as mkdir2, readFile, rename, stat, unlink as unlink2, writeFile } from "fs/promises";
7932
- import { dirname as dirname2, join as join4 } from "path";
8509
+ import { dirname as dirname3, join as join4 } from "path";
7933
8510
  var MAX_BYTES = 32 * 1024 * 1024;
7934
8511
  var MAX_AGE = 24 * 60 * 60000;
7935
8512
  var sourceSchema = object({
@@ -7957,7 +8534,7 @@ function umReportCachePath(device) {
7957
8534
  chain.push([name, target]);
7958
8535
  name = target?.jumpVia;
7959
8536
  }
7960
- const key = createHash3("sha256").update(JSON.stringify(chain)).digest("hex");
8537
+ const key = createHash4("sha256").update(JSON.stringify(chain)).digest("hex");
7961
8538
  return join4(`${config.dashboard.dbPath}.um-reports`, `${key}.json`);
7962
8539
  }
7963
8540
  async function loadUmReportCache(path, device, fields) {
@@ -7994,7 +8571,7 @@ async function saveUmReportCache(path, snapshot) {
7994
8571
  const text = JSON.stringify({ version: 1, snapshot });
7995
8572
  if (Buffer.byteLength(text) > MAX_BYTES)
7996
8573
  return false;
7997
- await mkdir2(dirname2(path), { recursive: true, mode: 448 });
8574
+ await mkdir2(dirname3(path), { recursive: true, mode: 448 });
7998
8575
  await writeFile(temp, text, { encoding: "utf8", mode: 384, flag: "wx" });
7999
8576
  await rename(temp, path);
8000
8577
  return true;
@@ -8717,7 +9294,7 @@ function buildUmReport(snapshot, query) {
8717
9294
  // src/observability/config-history.ts
8718
9295
  import {
8719
9296
  existsSync,
8720
- mkdirSync as mkdirSync2,
9297
+ mkdirSync as mkdirSync3,
8721
9298
  readFileSync as readFileSync2,
8722
9299
  readdirSync,
8723
9300
  rmSync,
@@ -8769,7 +9346,7 @@ function deleteVersion(id) {
8769
9346
  }
8770
9347
  function recordVersion(config, kind, now, label) {
8771
9348
  const dir = historyDir();
8772
- mkdirSync2(dir, { recursive: true });
9349
+ mkdirSync3(dir, { recursive: true });
8773
9350
  let ts = now;
8774
9351
  while (existsSync(join5(dir, `v${ts}.json`)))
8775
9352
  ts++;
@@ -9356,8 +9933,8 @@ data: ${JSON.stringify(sample)}
9356
9933
  }
9357
9934
 
9358
9935
  // src/observability/store.ts
9359
- import { mkdirSync as mkdirSync3 } from "fs";
9360
- import { dirname as dirname3 } from "path";
9936
+ import { mkdirSync as mkdirSync4 } from "fs";
9937
+ import { dirname as dirname4 } from "path";
9361
9938
  function rowToEvent(r) {
9362
9939
  return {
9363
9940
  id: r.id,
@@ -9543,7 +10120,7 @@ class SqliteEventStore {
9543
10120
  async function openSqliteStore(path) {
9544
10121
  if (path !== ":memory:") {
9545
10122
  try {
9546
- mkdirSync3(dirname3(path), { recursive: true });
10123
+ mkdirSync4(dirname4(path), { recursive: true });
9547
10124
  } catch {}
9548
10125
  }
9549
10126
  const { Database } = await import("bun:sqlite");
@@ -9552,8 +10129,8 @@ async function openSqliteStore(path) {
9552
10129
  }
9553
10130
 
9554
10131
  // src/observability/capsman-store.ts
9555
- import { mkdirSync as mkdirSync4 } from "fs";
9556
- import { dirname as dirname4 } from "path";
10132
+ import { mkdirSync as mkdirSync5 } from "fs";
10133
+ import { dirname as dirname5 } from "path";
9557
10134
  function summariseRadioSamples(rows) {
9558
10135
  const byRadio = new Map;
9559
10136
  for (const r of rows) {
@@ -9643,7 +10220,7 @@ class SqliteCapsmanStore {
9643
10220
  async function openCapsmanStore(path) {
9644
10221
  if (path !== ":memory:") {
9645
10222
  try {
9646
- mkdirSync4(dirname4(path), { recursive: true });
10223
+ mkdirSync5(dirname5(path), { recursive: true });
9647
10224
  } catch {}
9648
10225
  }
9649
10226
  const { Database } = await import("bun:sqlite");
@@ -9722,8 +10299,8 @@ function stopCapsmanSampler() {
9722
10299
  }
9723
10300
 
9724
10301
  // src/observability/usage-store.ts
9725
- import { mkdirSync as mkdirSync5 } from "fs";
9726
- import { dirname as dirname5 } from "path";
10302
+ import { mkdirSync as mkdirSync6 } from "fs";
10303
+ import { dirname as dirname6 } from "path";
9727
10304
  function dayOf(ts) {
9728
10305
  return new Date(ts).toISOString().slice(0, 10);
9729
10306
  }
@@ -9860,7 +10437,7 @@ class SqliteUsageStore {
9860
10437
  async function openUsageStore(path) {
9861
10438
  if (path !== ":memory:") {
9862
10439
  try {
9863
- mkdirSync5(dirname5(path), { recursive: true });
10440
+ mkdirSync6(dirname6(path), { recursive: true });
9864
10441
  } catch {}
9865
10442
  }
9866
10443
  const { Database } = await import("bun:sqlite");
@@ -10294,7 +10871,7 @@ async function postureRoutes(req, url) {
10294
10871
  }
10295
10872
 
10296
10873
  // src/observability/access-settings.ts
10297
- import { createHash as createHash4 } from "crypto";
10874
+ import { createHash as createHash5 } from "crypto";
10298
10875
  var list = array(string().trim().min(1).max(200)).max(200);
10299
10876
  var accessSchema = AccessConfigSchema2.extend({
10300
10877
  devices: list,
@@ -10324,7 +10901,7 @@ function createAccessSettingsRoutes(admin, deps = {
10324
10901
  }) {
10325
10902
  let pending = null;
10326
10903
  const tools = moduleCatalog2.flatMap((m) => m.tools.map((t) => ({ name: t.name, risk: riskOf(t.annotations), noDevice: !!t.noDevice })));
10327
- const revision = () => createHash4("sha256").update(JSON.stringify([deps.getConfig(), deps.getAccessPolicy()])).digest("hex");
10904
+ const revision = () => createHash5("sha256").update(JSON.stringify([deps.getConfig(), deps.getAccessPolicy()])).digest("hex");
10328
10905
  const payload = () => {
10329
10906
  const cfg = deps.getConfig();
10330
10907
  const id = admin.pendingId();
@@ -10707,8 +11284,8 @@ async function rolloutRoutes(req, url) {
10707
11284
  }
10708
11285
 
10709
11286
  // src/policy/results.ts
10710
- import { mkdirSync as mkdirSync6 } from "fs";
10711
- import { dirname as dirname6 } from "path";
11287
+ import { mkdirSync as mkdirSync7 } from "fs";
11288
+ import { dirname as dirname7 } from "path";
10712
11289
  var SCHEMA_STATEMENTS4 = [
10713
11290
  `CREATE TABLE IF NOT EXISTS policy_results (
10714
11291
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -10768,7 +11345,7 @@ class SqlitePolicyResultStore {
10768
11345
  async function openPolicyResultStore(path) {
10769
11346
  if (path !== ":memory:") {
10770
11347
  try {
10771
- mkdirSync6(dirname6(path), { recursive: true });
11348
+ mkdirSync7(dirname7(path), { recursive: true });
10772
11349
  } catch {}
10773
11350
  }
10774
11351
  const { Database } = await import("bun:sqlite");
@@ -12912,19 +13489,40 @@ async function runDashboard(cfg, transportLabel) {
12912
13489
  let openVpnHistory;
12913
13490
  let stopOpenVpnHistory;
12914
13491
  try {
12915
- openVpnHistory = await openOpenVpnHistory(join8(dirname7(cfg.dbPath), "openvpn-history.db"));
13492
+ openVpnHistory = await openOpenVpnHistory(join8(dirname8(cfg.dbPath), "openvpn-history.db"));
12916
13493
  stopOpenVpnHistory = startOpenVpnHistorySampler(openVpnHistory);
12917
13494
  } catch (e) {
12918
13495
  logger.warn(`[${SERVER_TAG3}] OpenVPN history disabled: ${String(e)}`);
12919
13496
  }
13497
+ let siteActivity;
13498
+ let stopSiteActivity;
13499
+ let dashboardStartedFlows = false;
13500
+ if (getConfig().flows.enabled && !getFlowCollector().running) {
13501
+ try {
13502
+ const started = await getFlowCollector().start(await flowStore(), getConfig().flows.port);
13503
+ dashboardStartedFlows = started.ok;
13504
+ if (!started.ok)
13505
+ logger.warn(`[${SERVER_TAG3}] Flow collector: ${started.error}`);
13506
+ } catch (e) {
13507
+ logger.warn(`[${SERVER_TAG3}] Flow collector unavailable: ${String(e)}`);
13508
+ }
13509
+ }
13510
+ try {
13511
+ siteActivity = await openSiteActivityStore(join8(dirname8(cfg.dbPath), "site-activity.db"));
13512
+ stopSiteActivity = await startSiteActivityCollector(siteActivity, openVpnHistory);
13513
+ } catch (e) {
13514
+ logger.warn(`[${SERVER_TAG3}] Site activity unavailable: ${String(e)}`);
13515
+ siteActivity?.close();
13516
+ siteActivity = undefined;
13517
+ }
12920
13518
  try {
12921
- usageStore = await openUsageStore(join8(dirname7(cfg.dbPath), "usage.db"));
13519
+ usageStore = await openUsageStore(join8(dirname8(cfg.dbPath), "usage.db"));
12922
13520
  startUsageSampler(usageStore);
12923
13521
  } catch (e) {
12924
13522
  logger.warn(`[${SERVER_TAG3}] usage history disabled: ${String(e)}`);
12925
13523
  }
12926
13524
  try {
12927
- capsmanStore = await openCapsmanStore(join8(dirname7(cfg.dbPath), "capsman.db"));
13525
+ capsmanStore = await openCapsmanStore(join8(dirname8(cfg.dbPath), "capsman.db"));
12928
13526
  startCapsmanSampler(capsmanStore);
12929
13527
  } catch (e) {
12930
13528
  logger.warn(`[${SERVER_TAG3}] capsman trends disabled: ${String(e)}`);
@@ -13028,6 +13626,9 @@ async function runDashboard(cfg, transportLabel) {
13028
13626
  const flowResp = await flowRoutes(req, url);
13029
13627
  if (flowResp)
13030
13628
  return flowResp;
13629
+ const siteActivityResp = await siteActivityRoutes(req, url, siteActivity);
13630
+ if (siteActivityResp)
13631
+ return siteActivityResp;
13031
13632
  const rolloutResp = await rolloutRoutes(req, url);
13032
13633
  if (rolloutResp)
13033
13634
  return rolloutResp;
@@ -13343,6 +13944,10 @@ async function runDashboard(cfg, transportLabel) {
13343
13944
  stop() {
13344
13945
  stopOpenVpnHistory?.();
13345
13946
  openVpnHistory?.close();
13947
+ stopSiteActivity?.();
13948
+ siteActivity?.close();
13949
+ if (dashboardStartedFlows)
13950
+ getFlowCollector().stop();
13346
13951
  stopHealthChecks();
13347
13952
  stopGeoLookups();
13348
13953
  stopUsageSampler();