nerdgram1337 1.7.5 → 1.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-53YCVNDJ.js → chunk-2WFLIMQG.js} +9 -2
- package/dist/chunk-3WBI2WV6.js +18 -0
- package/dist/{chunk-R3RZTEM3.js → chunk-46CLFN4M.js} +22 -4
- package/dist/{chunk-P666JXDB.js → chunk-7NPZMMPE.js} +6 -4
- package/dist/{chunk-GDPSIPXV.js → chunk-7O4HSURR.js} +12 -5
- package/dist/{chunk-KA5X3YY6.js → chunk-7XBDBMFZ.js} +9 -60
- package/dist/{chunk-6ELR2KYA.js → chunk-ARL7TA3D.js} +1744 -1743
- package/dist/chunk-DHVVDJDI.js +470 -0
- package/dist/chunk-DWP7W42J.js +12 -0
- package/dist/{chunk-7GEA3JEU.js → chunk-GD2NZSZO.js} +40 -18
- package/dist/chunk-GI4QATKE.js +87 -0
- package/dist/chunk-GK3V52XM.js +70 -0
- package/dist/chunk-KIK3WXJL.js +959 -0
- package/dist/{chunk-RB3SBRHP.js → chunk-M4HT24R6.js} +21 -9
- package/dist/chunk-M63FV47T.js +14 -0
- package/dist/chunk-TDN24VZZ.js +196 -0
- package/dist/chunk-TFNQIO7Z.js +21 -0
- package/dist/{chunk-5IHMGYXV.js → chunk-TVIG7AQK.js} +1 -1
- package/dist/chunk-WYQ4QDCF.js +85 -0
- package/dist/chunk-XC6UP46Q.js +15 -0
- package/dist/cli.js +135 -6
- package/package.json +1 -1
|
@@ -1,1479 +1,1456 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
REVISION_RETENTION,
|
|
5
|
-
atomicWriteJson,
|
|
6
|
-
atomicWriteText,
|
|
7
|
-
createId,
|
|
8
|
-
dropThumbnail,
|
|
9
|
-
ensureDataRoot,
|
|
10
|
-
evaluateWrite,
|
|
11
|
-
getInstanceIdentity,
|
|
12
|
-
getLocksRoot,
|
|
13
|
-
getMetaPath,
|
|
14
|
-
getVersionsRoot,
|
|
15
|
-
hashContent,
|
|
16
|
-
httpFail,
|
|
17
|
-
isProcessAlive,
|
|
18
|
-
normalizeRelative,
|
|
19
|
-
readJsonFile,
|
|
20
|
-
resolveSafePath,
|
|
21
|
-
rethrowKnownReadError,
|
|
22
|
-
safeStorageKey,
|
|
23
|
-
textFiles,
|
|
24
|
-
toRelativePath
|
|
25
|
-
} from "./chunk-KA5X3YY6.js";
|
|
2
|
+
isDataFileName
|
|
3
|
+
} from "./chunk-M63FV47T.js";
|
|
26
4
|
import {
|
|
27
5
|
clickTargetKind,
|
|
28
6
|
resolveDiagramTarget
|
|
29
7
|
} from "./chunk-BW6ANOTX.js";
|
|
8
|
+
import {
|
|
9
|
+
matchFrontMatter
|
|
10
|
+
} from "./chunk-6LCHDAAN.js";
|
|
30
11
|
|
|
31
|
-
// server/src/
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (!trimmed || trimmed.length > 200) return null;
|
|
45
|
-
const comware = /^<([A-Za-z][\w.-]{0,63})>\s*(.*)$/.exec(trimmed);
|
|
46
|
-
if (comware?.[1]) return comware[1];
|
|
47
|
-
const mikrotik = /^\[(?:[\w.+-]+@)?([A-Za-z0-9][\w.-]*)\]\s*>?\s*(.*)$/.exec(trimmed);
|
|
48
|
-
if (mikrotik?.[1]) return mikrotik[1];
|
|
49
|
-
const userHost = /^[\w.+-]+@([A-Za-z][\w.-]{0,63})\s*[>#]\s*(.*)$/.exec(trimmed);
|
|
50
|
-
if (userHost?.[1]) return userHost[1];
|
|
51
|
-
const host = /^([A-Za-z][\w.-]{0,63})\s*[>#]\s*(.*)$/.exec(trimmed);
|
|
52
|
-
if (!host?.[1]) return null;
|
|
53
|
-
const rest = host[2] ?? "";
|
|
54
|
-
if (rest === "") return host[1];
|
|
55
|
-
if (/^(?:show|display|sh|lldpctl|\/)/i.test(rest)) return host[1];
|
|
56
|
-
if (rest.length < 80 && !/[:=]/.test(rest)) return host[1];
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
function clipIssue(value) {
|
|
60
|
-
const flat = value.replace(/[\u0000-\u001f]/g, " ").replace(/\s+/g, " ").trim();
|
|
61
|
-
return flat.length > 80 ? `${flat.slice(0, 80)}\u2026` : flat;
|
|
62
|
-
}
|
|
63
|
-
function cleanSentinel(value) {
|
|
64
|
-
const trimmed = value.trim().replace(/^["']|["']$/g, "").trim();
|
|
65
|
-
if (!trimmed) return "";
|
|
66
|
-
if (/^(?:null|n\/a|na|none|unknown|not advertised|-|--)$/i.test(trimmed)) return "";
|
|
67
|
-
return trimmed;
|
|
68
|
-
}
|
|
69
|
-
function stripSubtype(value) {
|
|
70
|
-
const match = /^(?:mac|ifname|local|network|interface|ip|ipv4|ipv6)\s+(.+)$/i.exec(value.trim());
|
|
71
|
-
return match?.[1]?.trim() || value.trim();
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// server/src/neighbors/normalize.ts
|
|
75
|
-
var PREFIXES = [
|
|
76
|
-
{ re: /^(?:twentyfivegigabitethernet|twentyfivegige|twe)/, display: "Twe", key: "twe" },
|
|
77
|
-
{ re: /^(?:hundredgigabitethernet|hundredgige|hu)/, display: "Hu", key: "hu" },
|
|
78
|
-
{ re: /^(?:fortygigabitethernet|fortygige|fo)/, display: "Fo", key: "fo" },
|
|
79
|
-
{ re: /^(?:tengigabitethernet|tengige|te)/, display: "Te", key: "te" },
|
|
80
|
-
{ re: /^(?:fivegigabitethernet|fivegige|fi)/, display: "Fi", key: "fi" },
|
|
81
|
-
{ re: /^(?:twogigabitethernet|twogige|tw)/, display: "Tw", key: "tw" },
|
|
82
|
-
{ re: /^(?:appgigabitethernet|appgige|ap)/, display: "Ap", key: "ap" },
|
|
83
|
-
{ re: /^(?:gigabitethernet|gig|gi)/, display: "Gi", key: "gi" },
|
|
84
|
-
{ re: /^(?:fastethernet|fas|fa)/, display: "Fa", key: "fa" },
|
|
85
|
-
{ re: /^(?:port-channel|portchannel|po)/, display: "Po", key: "po" },
|
|
86
|
-
{ re: /^(?:management|mgmt|ma)/, display: "Ma", key: "ma" },
|
|
87
|
-
{ re: /^(?:ethernet|ether|eth|et)/, display: "Eth", key: "eth" },
|
|
88
|
-
{ re: /^(?:loopback|lo)/, display: "Lo", key: "lo" },
|
|
89
|
-
{ re: /^(?:vlan|vl)/, display: "Vl", key: "vl" }
|
|
90
|
-
];
|
|
91
|
-
var CAPABILITY_ORDER = ["Router", "Switch", "Phone", "WLAN", "Station", "Cable", "Repeater", "Other"];
|
|
92
|
-
var CAPABILITY_LETTER = {
|
|
93
|
-
r: "Router",
|
|
94
|
-
b: "Switch",
|
|
95
|
-
t: "Phone",
|
|
96
|
-
w: "WLAN",
|
|
97
|
-
s: "Station",
|
|
98
|
-
c: "Cable",
|
|
99
|
-
p: "Repeater",
|
|
100
|
-
o: "Other"
|
|
101
|
-
};
|
|
102
|
-
var NOT_CAPABILITY_CODES = /* @__PURE__ */ new Set(["stp", "igmp", "cdp", "lldp", "mndp", "vlan"]);
|
|
103
|
-
var CAPABILITY_WORD = {
|
|
104
|
-
router: "Router",
|
|
105
|
-
switch: "Switch",
|
|
106
|
-
bridge: "Switch",
|
|
107
|
-
telephone: "Phone",
|
|
108
|
-
phone: "Phone",
|
|
109
|
-
wlan: "WLAN",
|
|
110
|
-
wireless: "WLAN",
|
|
111
|
-
station: "Station",
|
|
112
|
-
host: "Station",
|
|
113
|
-
docsis: "Cable",
|
|
114
|
-
cable: "Cable",
|
|
115
|
-
repeater: "Repeater",
|
|
116
|
-
other: "Other"
|
|
117
|
-
};
|
|
118
|
-
function isMacAddress(value) {
|
|
119
|
-
return macOf(value) !== "";
|
|
120
|
-
}
|
|
121
|
-
function macOf(value) {
|
|
122
|
-
const trimmed = value.trim();
|
|
123
|
-
if (!trimmed || trimmed.length > 40) return "";
|
|
124
|
-
const hex = trimmed.toLowerCase().replace(/[^0-9a-f]/g, "");
|
|
125
|
-
if (hex.length !== 12) return "";
|
|
126
|
-
const hasSep = /[:.\-\s]/.test(trimmed);
|
|
127
|
-
const hasLetter = /[a-f]/i.test(hex);
|
|
128
|
-
if (!hasSep && !hasLetter) return "";
|
|
129
|
-
if (!/^[0-9a-f:.\-\s]+$/i.test(trimmed)) return "";
|
|
130
|
-
const parts = hex.match(/.{2}/g);
|
|
131
|
-
return parts ? parts.join(":") : "";
|
|
12
|
+
// server/src/network/ip.ts
|
|
13
|
+
function parseIpv4(address) {
|
|
14
|
+
const match = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(address.trim());
|
|
15
|
+
if (!match) return null;
|
|
16
|
+
let value = 0;
|
|
17
|
+
for (let index = 1; index <= 4; index += 1) {
|
|
18
|
+
const octet = match[index] ?? "";
|
|
19
|
+
if (octet.length > 1 && octet.startsWith("0")) return null;
|
|
20
|
+
const number = Number(octet);
|
|
21
|
+
if (!Number.isInteger(number) || number < 0 || number > 255) return null;
|
|
22
|
+
value = value << 8 | number;
|
|
23
|
+
}
|
|
24
|
+
return value >>> 0;
|
|
132
25
|
}
|
|
133
|
-
function
|
|
134
|
-
return
|
|
26
|
+
function formatIpv4(value) {
|
|
27
|
+
return `${value >>> 24 & 255}.${value >>> 16 & 255}.${value >>> 8 & 255}.${value & 255}`;
|
|
135
28
|
}
|
|
136
|
-
function
|
|
137
|
-
|
|
138
|
-
if (!
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
29
|
+
function parseIpv6(address) {
|
|
30
|
+
let text = address.trim().toLowerCase();
|
|
31
|
+
if (!text || text.length > 64 || text.includes("%") || /\s/.test(text)) return null;
|
|
32
|
+
if (text.includes(".")) {
|
|
33
|
+
const lastColon = text.lastIndexOf(":");
|
|
34
|
+
if (lastColon < 0) return null;
|
|
35
|
+
const v4 = parseIpv4(text.slice(lastColon + 1));
|
|
36
|
+
if (v4 == null) return null;
|
|
37
|
+
const hi = (v4 >>> 16 & 65535).toString(16);
|
|
38
|
+
const lo = (v4 & 65535).toString(16);
|
|
39
|
+
text = `${text.slice(0, lastColon)}:${hi}:${lo}`;
|
|
144
40
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
if (!
|
|
150
|
-
|
|
41
|
+
if (text.includes(":::")) return null;
|
|
42
|
+
const pieces = text.split("::");
|
|
43
|
+
if (pieces.length > 2) return null;
|
|
44
|
+
const parseSide = (part) => {
|
|
45
|
+
if (!part) return [];
|
|
46
|
+
const groups = part.split(":");
|
|
47
|
+
const out = [];
|
|
48
|
+
for (const group of groups) {
|
|
49
|
+
if (!/^[0-9a-f]{1,4}$/.test(group)) return null;
|
|
50
|
+
out.push(Number.parseInt(group, 16));
|
|
51
|
+
}
|
|
52
|
+
return out;
|
|
53
|
+
};
|
|
54
|
+
const left = parseSide(pieces[0] ?? "");
|
|
55
|
+
if (!left) return null;
|
|
56
|
+
if (pieces.length === 1) {
|
|
57
|
+
if (left.length !== 8) return null;
|
|
58
|
+
return Uint16Array.from(left);
|
|
151
59
|
}
|
|
152
|
-
const
|
|
153
|
-
|
|
60
|
+
const right = parseSide(pieces[1] ?? "");
|
|
61
|
+
if (!right) return null;
|
|
62
|
+
const missing = 8 - left.length - right.length;
|
|
63
|
+
if (missing < 1) return null;
|
|
64
|
+
return Uint16Array.from([...left, ...new Array(missing).fill(0), ...right]);
|
|
154
65
|
}
|
|
155
|
-
function
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
if (
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const label = CAPABILITY_LETTER[letter];
|
|
168
|
-
if (label) found.add(label);
|
|
66
|
+
function formatIpv6(groups) {
|
|
67
|
+
let bestStart = -1;
|
|
68
|
+
let bestLength = 0;
|
|
69
|
+
let runStart = -1;
|
|
70
|
+
for (let index = 0; index <= groups.length; index += 1) {
|
|
71
|
+
const zero = index < groups.length && groups[index] === 0;
|
|
72
|
+
if (zero && runStart < 0) runStart = index;
|
|
73
|
+
if (!zero && runStart >= 0) {
|
|
74
|
+
const length = index - runStart;
|
|
75
|
+
if (length > bestLength) {
|
|
76
|
+
bestStart = runStart;
|
|
77
|
+
bestLength = length;
|
|
169
78
|
}
|
|
79
|
+
runStart = -1;
|
|
170
80
|
}
|
|
171
81
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
if (/nx-os|nexus|\bn[3579]k\b/.test(text)) return "Cisco NX-OS";
|
|
178
|
-
if (/ios-xe|ios xe|cat9k|iosxe|\bc9[0-9]{3}\b|\bisr\d|\basr\d/.test(text)) return "Cisco IOS-XE";
|
|
179
|
-
if (/cisco ios|\bcatalyst\b|ws-c\d|\bc3750\b|\bc2960\b/.test(text)) return "Cisco IOS";
|
|
180
|
-
if (/arista|\beos\b|\bdcs-/.test(text)) return "Arista EOS";
|
|
181
|
-
if (/junos|juniper/.test(text)) return "Junos";
|
|
182
|
-
if (/aruba|provision|aos-cx|arubaos/.test(text)) return "Aruba";
|
|
183
|
-
if (/comware|hewlett|\bhpe\b|\bhp\b/.test(text)) return "HPE";
|
|
184
|
-
if (/extreme\s*xos|\bexos\b|extremexos/.test(text)) return "Extreme";
|
|
185
|
-
if (/os10|\bdell\b|force10|\bdnos\b/.test(text)) return "Dell";
|
|
186
|
-
if (/routeros|mikrotik/.test(text)) return "MikroTik";
|
|
187
|
-
if (/ubiquiti|unifi|edgeswitch|edgeos|\buisp\b/.test(text)) return "Ubiquiti";
|
|
188
|
-
if (/lldpd|\blinux\b|debian|ubuntu/.test(text)) return "Linux";
|
|
189
|
-
const clip = (platformField || description).split("\n")[0]?.trim() ?? "";
|
|
190
|
-
if (!clip) return "";
|
|
191
|
-
return clip.length > 40 ? `${clip.slice(0, 40)}\u2026` : clip;
|
|
192
|
-
}
|
|
193
|
-
function deviceKey(name, shortNames) {
|
|
194
|
-
const lower = name.trim().toLowerCase();
|
|
195
|
-
if (isIp(lower)) return `ip:${lower}`;
|
|
196
|
-
const short = lower.split(".")[0] ?? lower;
|
|
197
|
-
if (short && shortNames.has(short)) return `host:${short}`;
|
|
198
|
-
return `host:${lower}`;
|
|
199
|
-
}
|
|
200
|
-
function chooseDisplay(variants, preferred) {
|
|
201
|
-
const prefs = variants.filter((name) => preferred.has(name.trim().toLowerCase()));
|
|
202
|
-
const pool = prefs.length > 0 ? prefs : variants;
|
|
203
|
-
return [...pool].sort((a, b) => a.length - b.length || a.localeCompare(b))[0] ?? "";
|
|
82
|
+
const hex = (group) => group.toString(16);
|
|
83
|
+
if (bestLength < 2) return [...groups].map(hex).join(":");
|
|
84
|
+
const left = [...groups.slice(0, bestStart)].map(hex).join(":");
|
|
85
|
+
const right = [...groups.slice(bestStart + bestLength)].map(hex).join(":");
|
|
86
|
+
return `${left}::${right}`;
|
|
204
87
|
}
|
|
205
|
-
function
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
names.push(link.localDevice, link.remoteDevice);
|
|
210
|
-
}
|
|
211
|
-
const shortNames = /* @__PURE__ */ new Set();
|
|
212
|
-
for (const name of names) {
|
|
213
|
-
const lower = name.trim().toLowerCase();
|
|
214
|
-
if (lower && !lower.includes(".") && !isIp(lower)) shortNames.add(lower);
|
|
215
|
-
}
|
|
216
|
-
for (const name of preferred) {
|
|
217
|
-
if (!name.includes(".") && !isIp(name)) shortNames.add(name);
|
|
88
|
+
function canonicalIp(address, family) {
|
|
89
|
+
if (family === "ipv4") {
|
|
90
|
+
const value = parseIpv4(address);
|
|
91
|
+
return value == null ? null : formatIpv4(value);
|
|
218
92
|
}
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
93
|
+
const groups = parseIpv6(address);
|
|
94
|
+
return groups ? formatIpv6(groups) : null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// server/src/network/validate.ts
|
|
98
|
+
var DEVICE_FIELDS = /* @__PURE__ */ new Set(["name", "platform", "model", "role", "site", "managementIp", "diagram"]);
|
|
99
|
+
var INTERFACE_FIELDS = /* @__PURE__ */ new Set([
|
|
100
|
+
"device",
|
|
101
|
+
"name",
|
|
102
|
+
"description",
|
|
103
|
+
"speed",
|
|
104
|
+
"adminStatus",
|
|
105
|
+
"operStatus",
|
|
106
|
+
"ipv4",
|
|
107
|
+
"ipv6",
|
|
108
|
+
"vrf",
|
|
109
|
+
"switchportMode",
|
|
110
|
+
"accessVlan",
|
|
111
|
+
"trunkVlans",
|
|
112
|
+
"nativeVlan",
|
|
113
|
+
"lag"
|
|
114
|
+
]);
|
|
115
|
+
var ADDRESS_FIELDS = /* @__PURE__ */ new Set(["address", "prefix"]);
|
|
116
|
+
var LINK_FIELDS = /* @__PURE__ */ new Set(["a", "b", "label", "inferred"]);
|
|
117
|
+
var ENDPOINT_FIELDS = /* @__PURE__ */ new Set(["device", "interface"]);
|
|
118
|
+
var VLAN_FIELDS = /* @__PURE__ */ new Set(["id", "name"]);
|
|
119
|
+
var ROOT_FIELDS = /* @__PURE__ */ new Set(["schemaVersion", "format", "devices", "interfaces", "links", "vlans", "neighbors"]);
|
|
120
|
+
var NEIGHBOR_FIELDS = /* @__PURE__ */ new Set(["cdp", "lldp", "localDevice"]);
|
|
121
|
+
function validateNetwork(value) {
|
|
122
|
+
const issues = [];
|
|
123
|
+
if (!isRecord(value)) {
|
|
124
|
+
issues.push({ path: "", message: "Network data must be a JSON object." });
|
|
125
|
+
return { document: null, issues };
|
|
243
126
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
const links = [];
|
|
248
|
-
let dropped = 0;
|
|
249
|
-
for (const link of prepared) {
|
|
250
|
-
const left = `${link.localKey}\0${link.localPort.key}`;
|
|
251
|
-
const right = `${link.remoteKey}\0${link.remotePort.key}`;
|
|
252
|
-
const undirected = left < right ? `${left}\0${right}` : `${right}\0${left}`;
|
|
253
|
-
if (seen.has(undirected)) {
|
|
254
|
-
dropped += 1;
|
|
255
|
-
continue;
|
|
256
|
-
}
|
|
257
|
-
seen.add(undirected);
|
|
258
|
-
const localAttr = attrs.get(link.localKey) ?? { platform: "", capability: "" };
|
|
259
|
-
const remoteAttr = attrs.get(link.remoteKey) ?? { platform: "", capability: "" };
|
|
260
|
-
links.push({
|
|
261
|
-
line: link.line,
|
|
262
|
-
localDevice: display.get(link.localKey) ?? link.localKey,
|
|
263
|
-
localPort: link.localPort.display,
|
|
264
|
-
remoteDevice: display.get(link.remoteKey) ?? link.remoteKey,
|
|
265
|
-
remotePort: link.remotePort.display,
|
|
266
|
-
localPlatform: localAttr.platform,
|
|
267
|
-
remotePlatform: remoteAttr.platform,
|
|
268
|
-
localCapability: localAttr.capability,
|
|
269
|
-
remoteCapability: remoteAttr.capability
|
|
270
|
-
});
|
|
127
|
+
rejectUnknown(value, ROOT_FIELDS, "", issues);
|
|
128
|
+
if (value.schemaVersion !== 1) {
|
|
129
|
+
issues.push({ path: "/schemaVersion", message: "schemaVersion must be 1." });
|
|
271
130
|
}
|
|
272
|
-
if (
|
|
273
|
-
|
|
274
|
-
row: 0,
|
|
275
|
-
message: `Combined ${dropped} duplicate link${dropped === 1 ? "" : "s"} seen from both ends.`
|
|
276
|
-
});
|
|
131
|
+
if (value.format != null && value.format !== "network") {
|
|
132
|
+
issues.push({ path: "/format", message: 'format must be "network" when it is set.' });
|
|
277
133
|
}
|
|
278
|
-
|
|
279
|
-
|
|
134
|
+
const devices = readDevices(value.devices, issues);
|
|
135
|
+
const interfaces = readInterfaces(value.interfaces, issues);
|
|
136
|
+
const links = value.links == null ? [] : readLinks(value.links, issues);
|
|
137
|
+
const vlans = value.vlans == null ? [] : readVlans(value.vlans, issues);
|
|
138
|
+
const neighbors = readNeighbors(value.neighbors, issues);
|
|
139
|
+
if (issues.length > 0 || !devices || !interfaces || !links || !vlans || !neighbors) {
|
|
140
|
+
return { document: null, issues };
|
|
280
141
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
142
|
+
const names = /* @__PURE__ */ new Map();
|
|
143
|
+
devices.forEach((device, index) => {
|
|
144
|
+
const key = device.name.toLowerCase();
|
|
145
|
+
const previous = names.get(key);
|
|
146
|
+
if (previous != null) {
|
|
147
|
+
issues.push({ path: `/devices/${index}/name`, message: `duplicate device "${previous}".` });
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
names.set(key, device.name);
|
|
151
|
+
});
|
|
152
|
+
interfaces.forEach((item, index) => {
|
|
153
|
+
if (!names.has(item.device.toLowerCase())) {
|
|
154
|
+
issues.push({ path: `/interfaces/${index}/device`, message: `no device named "${item.device}".` });
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
links.forEach((link, index) => {
|
|
158
|
+
for (const side of ["a", "b"]) {
|
|
159
|
+
const end = link[side];
|
|
160
|
+
if (!names.has(end.device.toLowerCase())) {
|
|
161
|
+
issues.push({ path: `/links/${index}/${side}/device`, message: `no device named "${end.device}".` });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
if (issues.length > 0) return { document: null, issues };
|
|
289
166
|
return {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
platform: "",
|
|
293
|
-
capability: "",
|
|
294
|
-
localPort: "",
|
|
295
|
-
remotePort: "",
|
|
296
|
-
version: "",
|
|
297
|
-
readingVersion: false
|
|
167
|
+
document: { schemaVersion: 1, devices, interfaces, links, vlans, neighbors },
|
|
168
|
+
issues: []
|
|
298
169
|
};
|
|
299
170
|
}
|
|
300
|
-
function
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
return
|
|
304
|
-
parserId: "cisco-cdp",
|
|
305
|
-
links: [],
|
|
306
|
-
warnings: [],
|
|
307
|
-
errors: [
|
|
308
|
-
{
|
|
309
|
-
row: 1,
|
|
310
|
-
message: "This is the short CDP table. Paste show cdp neighbors detail."
|
|
311
|
-
}
|
|
312
|
-
]
|
|
313
|
-
};
|
|
171
|
+
function readDevices(value, issues) {
|
|
172
|
+
if (!Array.isArray(value)) {
|
|
173
|
+
issues.push({ path: "/devices", message: "devices must be an array." });
|
|
174
|
+
return null;
|
|
314
175
|
}
|
|
315
|
-
|
|
316
|
-
const
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
let count = 0;
|
|
323
|
-
const flush = () => {
|
|
324
|
-
if (!bag) return;
|
|
325
|
-
const current = bag;
|
|
326
|
-
bag = null;
|
|
327
|
-
if (!current.deviceId && !current.localPort && !current.remotePort) return;
|
|
328
|
-
count += 1;
|
|
329
|
-
if (count > MAX_NEIGHBOR_BLOCKS) return;
|
|
330
|
-
const remote = cleanSentinel(current.deviceId).replace(/\(([A-Za-z0-9_-]{4,})\)\s*$/, "").trim();
|
|
331
|
-
const localPort = cleanSentinel(current.localPort);
|
|
332
|
-
const remotePort = cleanSentinel(current.remotePort);
|
|
333
|
-
const missing = [];
|
|
334
|
-
if (!localDevice) missing.push("local device");
|
|
335
|
-
if (!localPort) missing.push("local interface");
|
|
336
|
-
if (!remote) missing.push("remote device");
|
|
337
|
-
if (!remotePort) missing.push("remote port");
|
|
338
|
-
if (missing.length > 0) {
|
|
339
|
-
const who = remote ? ` "${clipIssue(remote)}"` : "";
|
|
340
|
-
errors.push({
|
|
341
|
-
row: current.line,
|
|
342
|
-
message: `Line ${current.line}: CDP neighbor${who} is missing ${missing.join(" and ")}.`
|
|
343
|
-
});
|
|
176
|
+
let ok = true;
|
|
177
|
+
const devices = [];
|
|
178
|
+
value.forEach((item, index) => {
|
|
179
|
+
const path = `/devices/${index}`;
|
|
180
|
+
if (!isRecord(item)) {
|
|
181
|
+
issues.push({ path, message: "must be an object." });
|
|
182
|
+
ok = false;
|
|
344
183
|
return;
|
|
345
184
|
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
remotePort,
|
|
352
|
-
remotePlatform: platformOf(current.version, current.platform),
|
|
353
|
-
remoteCapability: normalizeCapability(current.capability)
|
|
354
|
-
});
|
|
355
|
-
};
|
|
356
|
-
const lines = cleaned.split("\n");
|
|
357
|
-
for (let index = 0; index < lines.length; index += 1) {
|
|
358
|
-
if (count > MAX_NEIGHBOR_BLOCKS) break;
|
|
359
|
-
const lineNo = index + 1;
|
|
360
|
-
const trimmed = (lines[index] ?? "").trim();
|
|
361
|
-
if (!trimmed) {
|
|
362
|
-
if (bag?.readingVersion) bag.readingVersion = false;
|
|
363
|
-
continue;
|
|
185
|
+
rejectUnknown(item, DEVICE_FIELDS, path, issues);
|
|
186
|
+
const name = requiredString(item.name, `${path}/name`, "name", issues);
|
|
187
|
+
if (!name) {
|
|
188
|
+
ok = false;
|
|
189
|
+
return;
|
|
364
190
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
191
|
+
const diagram = childDiagram(item.diagram, `${path}/diagram`, issues);
|
|
192
|
+
if (diagram == null) {
|
|
193
|
+
ok = false;
|
|
194
|
+
return;
|
|
368
195
|
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
196
|
+
devices.push({
|
|
197
|
+
name,
|
|
198
|
+
platform: optionalString(item.platform, `${path}/platform`, "platform", issues),
|
|
199
|
+
model: optionalString(item.model, `${path}/model`, "model", issues),
|
|
200
|
+
role: optionalString(item.role, `${path}/role`, "role", issues),
|
|
201
|
+
site: optionalString(item.site, `${path}/site`, "site", issues),
|
|
202
|
+
managementIp: optionalString(item.managementIp, `${path}/managementIp`, "managementIp", issues),
|
|
203
|
+
diagram
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
return ok && !issues.some((issue) => issue.path.startsWith("/devices/")) ? devices : null;
|
|
207
|
+
}
|
|
208
|
+
function readInterfaces(value, issues) {
|
|
209
|
+
if (!Array.isArray(value)) {
|
|
210
|
+
issues.push({ path: "/interfaces", message: "interfaces must be an array." });
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
const interfaces = [];
|
|
214
|
+
let ok = true;
|
|
215
|
+
value.forEach((item, index) => {
|
|
216
|
+
const path = `/interfaces/${index}`;
|
|
217
|
+
if (!isRecord(item)) {
|
|
218
|
+
issues.push({ path, message: "must be an object." });
|
|
219
|
+
ok = false;
|
|
220
|
+
return;
|
|
375
221
|
}
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
222
|
+
rejectUnknown(item, INTERFACE_FIELDS, path, issues);
|
|
223
|
+
const device = requiredString(item.device, `${path}/device`, "device", issues);
|
|
224
|
+
const name = requiredString(item.name, `${path}/name`, "name", issues);
|
|
225
|
+
const adminStatus = statusOf(item.adminStatus, `${path}/adminStatus`, "adminStatus", issues);
|
|
226
|
+
const operStatus = statusOf(item.operStatus, `${path}/operStatus`, "operStatus", issues);
|
|
227
|
+
const ipv4 = item.ipv4 == null ? [] : readAddresses(item.ipv4, `${path}/ipv4`, "ipv4", issues);
|
|
228
|
+
const ipv6 = item.ipv6 == null ? [] : readAddresses(item.ipv6, `${path}/ipv6`, "ipv6", issues);
|
|
229
|
+
const mode = switchportOf(item.switchportMode, `${path}/switchportMode`, issues);
|
|
230
|
+
const accessVlan = item.accessVlan == null ? null : vlanId(item.accessVlan, `${path}/accessVlan`, issues);
|
|
231
|
+
const nativeVlan = item.nativeVlan == null ? null : vlanId(item.nativeVlan, `${path}/nativeVlan`, issues);
|
|
232
|
+
const trunkVlans = item.trunkVlans == null ? null : readVlanList(item.trunkVlans, `${path}/trunkVlans`, issues);
|
|
233
|
+
if (!device || !name || adminStatus == null || operStatus == null || !ipv4 || !ipv6 || mode == null) {
|
|
234
|
+
ok = false;
|
|
235
|
+
return;
|
|
382
236
|
}
|
|
383
|
-
if (
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
}
|
|
395
|
-
|
|
237
|
+
if (item.accessVlan != null && accessVlan == null || item.nativeVlan != null && nativeVlan == null) ok = false;
|
|
238
|
+
if (item.trunkVlans != null && !trunkVlans) ok = false;
|
|
239
|
+
interfaces.push({
|
|
240
|
+
device,
|
|
241
|
+
name,
|
|
242
|
+
description: optionalString(item.description, `${path}/description`, "description", issues),
|
|
243
|
+
speed: optionalString(item.speed, `${path}/speed`, "speed", issues),
|
|
244
|
+
adminStatus,
|
|
245
|
+
operStatus,
|
|
246
|
+
ipv4,
|
|
247
|
+
ipv6,
|
|
248
|
+
vrf: optionalString(item.vrf, `${path}/vrf`, "vrf", issues),
|
|
249
|
+
switchportMode: mode,
|
|
250
|
+
accessVlan,
|
|
251
|
+
trunkVlans,
|
|
252
|
+
nativeVlan,
|
|
253
|
+
lag: optionalString(item.lag, `${path}/lag`, "lag", issues)
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
return ok ? interfaces : null;
|
|
257
|
+
}
|
|
258
|
+
function readAddresses(value, path, family, issues) {
|
|
259
|
+
if (!Array.isArray(value)) {
|
|
260
|
+
issues.push({ path, message: `${family} must be an array.` });
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
const max = family === "ipv4" ? 32 : 128;
|
|
264
|
+
const addresses = [];
|
|
265
|
+
let ok = true;
|
|
266
|
+
value.forEach((item, index) => {
|
|
267
|
+
const itemPath = `${path}/${index}`;
|
|
268
|
+
if (!isRecord(item)) {
|
|
269
|
+
issues.push({ path: itemPath, message: "must be an object with address and prefix." });
|
|
270
|
+
ok = false;
|
|
271
|
+
return;
|
|
396
272
|
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
273
|
+
rejectUnknown(item, ADDRESS_FIELDS, itemPath, issues);
|
|
274
|
+
const address = requiredString(item.address, `${itemPath}/address`, "address", issues);
|
|
275
|
+
const prefixOk = Number.isInteger(item.prefix) && item.prefix >= 0 && item.prefix <= max;
|
|
276
|
+
if (!prefixOk) {
|
|
277
|
+
issues.push({ path: `${itemPath}/prefix`, message: `prefix must be an integer from 0 to ${max}.` });
|
|
278
|
+
ok = false;
|
|
402
279
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
const value = iface[1] ?? "";
|
|
407
|
-
const combined = /^(.*?)\s*,\s*Port ID \(outgoing port\)\s*:\s*(.+)$/i.exec(value);
|
|
408
|
-
if (combined) {
|
|
409
|
-
bag.localPort = combined[1]?.trim() ?? "";
|
|
410
|
-
bag.remotePort = combined[2]?.trim() ?? "";
|
|
411
|
-
} else {
|
|
412
|
-
bag.localPort = value.replace(/,\s*$/, "").trim();
|
|
413
|
-
}
|
|
414
|
-
continue;
|
|
280
|
+
if (!address) {
|
|
281
|
+
ok = false;
|
|
282
|
+
return;
|
|
415
283
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
284
|
+
if (!canonicalIp(address, family)) {
|
|
285
|
+
issues.push({
|
|
286
|
+
path: `${itemPath}/address`,
|
|
287
|
+
message: family === "ipv4" ? "address must be an IPv4 address." : "address must be an IPv6 address."
|
|
288
|
+
});
|
|
289
|
+
ok = false;
|
|
290
|
+
return;
|
|
421
291
|
}
|
|
422
|
-
if (
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
292
|
+
if (prefixOk) addresses.push({ address, prefix: item.prefix });
|
|
293
|
+
});
|
|
294
|
+
return ok ? addresses : null;
|
|
295
|
+
}
|
|
296
|
+
function readLinks(value, issues) {
|
|
297
|
+
if (!Array.isArray(value)) {
|
|
298
|
+
issues.push({ path: "/links", message: "links must be an array." });
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
const links = [];
|
|
302
|
+
let ok = true;
|
|
303
|
+
value.forEach((item, index) => {
|
|
304
|
+
const path = `/links/${index}`;
|
|
305
|
+
if (!isRecord(item)) {
|
|
306
|
+
issues.push({ path, message: "must be an object with a and b." });
|
|
307
|
+
ok = false;
|
|
308
|
+
return;
|
|
427
309
|
}
|
|
428
|
-
|
|
429
|
-
|
|
310
|
+
rejectUnknown(item, LINK_FIELDS, path, issues);
|
|
311
|
+
const a = readEndpoint(item.a, `${path}/a`, issues);
|
|
312
|
+
const b = readEndpoint(item.b, `${path}/b`, issues);
|
|
313
|
+
if (!a || !b) {
|
|
314
|
+
ok = false;
|
|
315
|
+
return;
|
|
430
316
|
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
errors,
|
|
445
|
-
warnings,
|
|
446
|
-
preferred,
|
|
447
|
-
"No CDP neighbors were found. Paste show cdp neighbors detail."
|
|
448
|
-
);
|
|
449
|
-
}
|
|
450
|
-
var ciscoCdpParser = {
|
|
451
|
-
id: "cisco-cdp",
|
|
452
|
-
kind: "cdp",
|
|
453
|
-
label: "Cisco CDP (IOS, IOS-XE, NX-OS)",
|
|
454
|
-
parse: parseCdpNeighbors
|
|
455
|
-
};
|
|
456
|
-
|
|
457
|
-
// server/src/neighbors/lldp.ts
|
|
458
|
-
var FIELDS = {
|
|
459
|
-
"local intf": "localPort",
|
|
460
|
-
localintf: "localPort",
|
|
461
|
-
"local interface": "localPort",
|
|
462
|
-
localinterface: "localPort",
|
|
463
|
-
"local port": "localPort",
|
|
464
|
-
localport: "localPort",
|
|
465
|
-
interface: "localPort",
|
|
466
|
-
intf: "localPort",
|
|
467
|
-
"chassis id": "chassisId",
|
|
468
|
-
chassisid: "chassisId",
|
|
469
|
-
"chassis identifier": "chassisId",
|
|
470
|
-
"port id": "portId",
|
|
471
|
-
portid: "portId",
|
|
472
|
-
"port info": "portId",
|
|
473
|
-
portinfo: "portId",
|
|
474
|
-
"port description": "portDescription",
|
|
475
|
-
portdescription: "portDescription",
|
|
476
|
-
"port descr": "portDescription",
|
|
477
|
-
portdescr: "portDescription",
|
|
478
|
-
"port desc": "portDescription",
|
|
479
|
-
portdesc: "portDescription",
|
|
480
|
-
"system name": "systemName",
|
|
481
|
-
systemname: "systemName",
|
|
482
|
-
sysname: "systemName",
|
|
483
|
-
"sys name": "systemName",
|
|
484
|
-
"chassis name": "systemName",
|
|
485
|
-
chassisname: "systemName",
|
|
486
|
-
"device id": "systemName",
|
|
487
|
-
deviceid: "systemName",
|
|
488
|
-
identity: "systemName",
|
|
489
|
-
"system description": "systemDescription",
|
|
490
|
-
systemdescription: "systemDescription",
|
|
491
|
-
"system descr": "systemDescription",
|
|
492
|
-
systemdescr: "systemDescription",
|
|
493
|
-
"system desc": "systemDescription",
|
|
494
|
-
sysdescr: "systemDescription",
|
|
495
|
-
"sys descr": "systemDescription",
|
|
496
|
-
"existing system capabilities": "enabledCaps",
|
|
497
|
-
existingsystemcapabilities: "enabledCaps",
|
|
498
|
-
"enabled capabilities": "enabledCaps",
|
|
499
|
-
enabledcapabilities: "enabledCaps",
|
|
500
|
-
"capabilities enabled": "enabledCaps",
|
|
501
|
-
"enabled caps": "enabledCaps",
|
|
502
|
-
enabledcaps: "enabledCaps",
|
|
503
|
-
"system capabilities enabled": "enabledCaps",
|
|
504
|
-
"system capabilities": "supportedCaps",
|
|
505
|
-
systemcapabilities: "supportedCaps",
|
|
506
|
-
"system caps": "supportedCaps",
|
|
507
|
-
systemcaps: "supportedCaps",
|
|
508
|
-
"capabilities supported": "supportedCaps",
|
|
509
|
-
"capabilities available": "supportedCaps",
|
|
510
|
-
"system capabilities supported": "supportedCaps",
|
|
511
|
-
capabilities: "supportedCaps",
|
|
512
|
-
platform: "platform"
|
|
513
|
-
};
|
|
514
|
-
var REPEAT = /* @__PURE__ */ new Set(["localPort", "chassisId", "portId", "systemName"]);
|
|
515
|
-
var PAIR_FIELDS = {
|
|
516
|
-
interface: "localPort",
|
|
517
|
-
"local-interface": "localPort",
|
|
518
|
-
"interface-name": "portId",
|
|
519
|
-
"port-id": "portId",
|
|
520
|
-
portid: "portId",
|
|
521
|
-
identity: "systemName",
|
|
522
|
-
"system-name": "systemName",
|
|
523
|
-
sysname: "systemName",
|
|
524
|
-
"system-description": "systemDescription",
|
|
525
|
-
sysdescr: "systemDescription",
|
|
526
|
-
"mac-address": "chassisId",
|
|
527
|
-
"chassis-id": "chassisId",
|
|
528
|
-
chassisid: "chassisId",
|
|
529
|
-
"port-description": "portDescription",
|
|
530
|
-
portdescr: "portDescription",
|
|
531
|
-
platform: "platform"
|
|
532
|
-
};
|
|
533
|
-
function emptyBag2(line) {
|
|
534
|
-
return {
|
|
535
|
-
line,
|
|
536
|
-
localPort: "",
|
|
537
|
-
chassisId: "",
|
|
538
|
-
portId: "",
|
|
539
|
-
portDescription: "",
|
|
540
|
-
systemName: "",
|
|
541
|
-
systemDescription: "",
|
|
542
|
-
enabledCaps: "",
|
|
543
|
-
supportedCaps: "",
|
|
544
|
-
platform: "",
|
|
545
|
-
openField: "",
|
|
546
|
-
skippedBlank: false
|
|
547
|
-
};
|
|
548
|
-
}
|
|
549
|
-
function normLabel(label) {
|
|
550
|
-
return label.toLowerCase().replace(/[^a-z0-9]+/g, " ").trim().replace(/\s+/g, " ").replace(/^(?:neighbor|neighbour|remote)\s+/, "");
|
|
551
|
-
}
|
|
552
|
-
function lookupField(label) {
|
|
553
|
-
const norm = normLabel(label);
|
|
554
|
-
if (FIELDS[norm]) return FIELDS[norm];
|
|
555
|
-
const compact = norm.replace(/ /g, "");
|
|
556
|
-
return FIELDS[compact] ?? null;
|
|
317
|
+
const inferred = optionalFlag(item.inferred, `${path}/inferred`, "inferred", issues);
|
|
318
|
+
if (inferred == null) {
|
|
319
|
+
ok = false;
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
links.push({
|
|
323
|
+
a,
|
|
324
|
+
b,
|
|
325
|
+
label: optionalString(item.label, `${path}/label`, "label", issues),
|
|
326
|
+
inferred
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
return ok ? links : null;
|
|
557
330
|
}
|
|
558
|
-
function
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
331
|
+
function readEndpoint(value, path, issues) {
|
|
332
|
+
if (!isRecord(value)) {
|
|
333
|
+
issues.push({ path, message: "must be an object with device and interface." });
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
rejectUnknown(value, ENDPOINT_FIELDS, path, issues);
|
|
337
|
+
const device = requiredString(value.device, `${path}/device`, "device", issues);
|
|
338
|
+
const iface = requiredString(value.interface, `${path}/interface`, "interface", issues);
|
|
339
|
+
if (!device || !iface) return null;
|
|
340
|
+
return { device, interface: iface };
|
|
565
341
|
}
|
|
566
|
-
function
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
while (match) {
|
|
571
|
-
pairs.push({ key: (match[1] ?? "").toLowerCase(), value: match[2] ?? match[3] ?? "" });
|
|
572
|
-
match = pattern.exec(line);
|
|
342
|
+
function readVlanList(value, path, issues) {
|
|
343
|
+
if (!Array.isArray(value)) {
|
|
344
|
+
issues.push({ path, message: "trunkVlans must be an array of VLAN ids." });
|
|
345
|
+
return null;
|
|
573
346
|
}
|
|
574
|
-
|
|
347
|
+
const ids = [];
|
|
348
|
+
let ok = true;
|
|
349
|
+
value.forEach((item, index) => {
|
|
350
|
+
const id = vlanId(item, `${path}/${index}`, issues);
|
|
351
|
+
if (id == null) ok = false;
|
|
352
|
+
else ids.push(id);
|
|
353
|
+
});
|
|
354
|
+
return ok ? ids : null;
|
|
575
355
|
}
|
|
576
|
-
function
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
356
|
+
function readVlans(value, issues) {
|
|
357
|
+
if (!Array.isArray(value)) {
|
|
358
|
+
issues.push({ path: "/vlans", message: "vlans must be an array." });
|
|
359
|
+
return null;
|
|
360
|
+
}
|
|
361
|
+
const vlans = [];
|
|
362
|
+
const seen = /* @__PURE__ */ new Set();
|
|
363
|
+
let ok = true;
|
|
364
|
+
value.forEach((item, index) => {
|
|
365
|
+
const path = `/vlans/${index}`;
|
|
366
|
+
if (!isRecord(item)) {
|
|
367
|
+
issues.push({ path, message: "must be an object." });
|
|
368
|
+
ok = false;
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
rejectUnknown(item, VLAN_FIELDS, path, issues);
|
|
372
|
+
const id = vlanId(item.id, `${path}/id`, issues);
|
|
373
|
+
if (id == null) {
|
|
374
|
+
ok = false;
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
if (seen.has(id)) issues.push({ path: `${path}/id`, message: `duplicate VLAN ${id}.` });
|
|
378
|
+
seen.add(id);
|
|
379
|
+
vlans.push({ id, name: optionalString(item.name, `${path}/name`, "name", issues) });
|
|
380
|
+
});
|
|
381
|
+
return ok ? vlans : null;
|
|
580
382
|
}
|
|
581
|
-
function
|
|
582
|
-
|
|
383
|
+
function readNeighbors(value, issues) {
|
|
384
|
+
if (value == null) return { cdp: "", lldp: "", localDevice: "" };
|
|
385
|
+
if (!isRecord(value)) {
|
|
386
|
+
issues.push({ path: "/neighbors", message: "neighbors must be an object." });
|
|
387
|
+
return null;
|
|
388
|
+
}
|
|
389
|
+
rejectUnknown(value, NEIGHBOR_FIELDS, "/neighbors", issues);
|
|
390
|
+
return {
|
|
391
|
+
cdp: optionalString(value.cdp, "/neighbors/cdp", "cdp", issues),
|
|
392
|
+
lldp: optionalString(value.lldp, "/neighbors/lldp", "lldp", issues),
|
|
393
|
+
localDevice: optionalString(value.localDevice, "/neighbors/localDevice", "localDevice", issues)
|
|
394
|
+
};
|
|
583
395
|
}
|
|
584
|
-
function
|
|
585
|
-
|
|
396
|
+
function statusOf(value, path, label, issues) {
|
|
397
|
+
if (value == null || value === "") return "";
|
|
398
|
+
if (value === "up" || value === "down") return value;
|
|
399
|
+
issues.push({ path, message: `${label} must be up or down.` });
|
|
400
|
+
return null;
|
|
586
401
|
}
|
|
587
|
-
|
|
588
|
-
(
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
402
|
+
function switchportOf(value, path, issues) {
|
|
403
|
+
if (value == null || value === "") return "";
|
|
404
|
+
if (value === "access" || value === "trunk") return value;
|
|
405
|
+
issues.push({ path, message: "switchportMode must be access or trunk." });
|
|
406
|
+
return null;
|
|
407
|
+
}
|
|
408
|
+
function vlanId(value, path, issues) {
|
|
409
|
+
if (typeof value === "number" && Number.isInteger(value) && value >= 1 && value <= 4094) return value;
|
|
410
|
+
issues.push({ path, message: "must be an integer from 1 to 4094." });
|
|
411
|
+
return null;
|
|
412
|
+
}
|
|
413
|
+
function requiredString(value, path, label, issues) {
|
|
414
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
415
|
+
issues.push({ path, message: `${label} is required.` });
|
|
416
|
+
return "";
|
|
596
417
|
}
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
const portIsMac = Boolean(remotePort) && isMacAddress(remotePort);
|
|
605
|
-
if (portIsMac && bag.portDescription) {
|
|
606
|
-
collected.warnings.push({
|
|
607
|
-
row: bag.line,
|
|
608
|
-
message: `Line ${bag.line}: port ID is a MAC address. Using the port description.`
|
|
609
|
-
});
|
|
610
|
-
remotePort = bag.portDescription;
|
|
611
|
-
} else if (!remotePort && bag.portDescription) {
|
|
612
|
-
remotePort = bag.portDescription;
|
|
418
|
+
return value.trim();
|
|
419
|
+
}
|
|
420
|
+
function childDiagram(value, path, issues) {
|
|
421
|
+
if (value == null || value === "") return "";
|
|
422
|
+
if (typeof value !== "string") {
|
|
423
|
+
issues.push({ path, message: "diagram must be a string." });
|
|
424
|
+
return null;
|
|
613
425
|
}
|
|
614
|
-
const
|
|
615
|
-
|
|
616
|
-
if (
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
return;
|
|
426
|
+
const trimmed = value.trim();
|
|
427
|
+
if (!trimmed) return "";
|
|
428
|
+
if (/[\u0000-\u001f"\\?#]/.test(trimmed) || clickTargetKind(trimmed) === "unsafe" || clickTargetKind(trimmed) === "external") {
|
|
429
|
+
issues.push({ path, message: "diagram must be a workspace .mmd path." });
|
|
430
|
+
return null;
|
|
620
431
|
}
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
});
|
|
432
|
+
const resolved = resolveDiagramTarget(trimmed, "");
|
|
433
|
+
if (!resolved.ok) {
|
|
434
|
+
issues.push({ path, message: "diagram must be a workspace .mmd path." });
|
|
435
|
+
return null;
|
|
626
436
|
}
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
if (
|
|
631
|
-
if (
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
437
|
+
return resolved.path;
|
|
438
|
+
}
|
|
439
|
+
function optionalFlag(value, path, label, issues) {
|
|
440
|
+
if (value == null) return false;
|
|
441
|
+
if (typeof value === "boolean") return value;
|
|
442
|
+
issues.push({ path, message: `${label} must be a boolean.` });
|
|
443
|
+
return null;
|
|
444
|
+
}
|
|
445
|
+
function optionalString(value, path, label, issues) {
|
|
446
|
+
if (value == null || value === "") return "";
|
|
447
|
+
if (typeof value !== "string") {
|
|
448
|
+
issues.push({ path, message: `${label} must be a string.` });
|
|
449
|
+
return "";
|
|
639
450
|
}
|
|
640
|
-
|
|
641
|
-
collected.raw.push({
|
|
642
|
-
line: bag.line,
|
|
643
|
-
localDevice,
|
|
644
|
-
localPort,
|
|
645
|
-
remoteDevice,
|
|
646
|
-
remotePort,
|
|
647
|
-
remotePlatform: platformOf(bag.systemDescription, bag.platform),
|
|
648
|
-
remoteCapability: capability
|
|
649
|
-
});
|
|
451
|
+
return value.trim();
|
|
650
452
|
}
|
|
651
|
-
function
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
links: [],
|
|
656
|
-
warnings: [],
|
|
657
|
-
errors: [{ row: 1, message: `A file can have at most ${MAX_NEIGHBOR_BLOCKS} neighbor blocks.` }]
|
|
658
|
-
};
|
|
453
|
+
function rejectUnknown(value, allowed, path, issues) {
|
|
454
|
+
for (const key of Object.keys(value)) {
|
|
455
|
+
if (allowed.has(key)) continue;
|
|
456
|
+
issues.push({ path: path ? `${path}/${key}` : `/${key}`, message: `unexpected property "${key}".` });
|
|
659
457
|
}
|
|
660
|
-
return finalizeNeighbors("lldp", collected.raw, collected.errors, collected.warnings, collected.preferred, emptyMessage2);
|
|
661
458
|
}
|
|
662
|
-
function
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
const
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
const
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
if (norm === "local port id" || norm === "localportid") {
|
|
776
|
-
if (!/^\d+$/.test(labeled.value)) assign("localPort", labeled.value, lineNo);
|
|
777
|
-
continue;
|
|
778
|
-
}
|
|
779
|
-
if (norm === "interface") {
|
|
780
|
-
const local = labeled.value.split(",")[0]?.trim() ?? "";
|
|
781
|
-
if (local) assign("localPort", local, lineNo);
|
|
782
|
-
continue;
|
|
783
|
-
}
|
|
784
|
-
if (norm === "port") {
|
|
785
|
-
assign("localPort", labeled.value, lineNo);
|
|
786
|
-
continue;
|
|
787
|
-
}
|
|
788
|
-
if (norm === "capability") {
|
|
789
|
-
const token = capToken(labeled.value);
|
|
790
|
-
if (token) assign("enabledCaps", token, lineNo);
|
|
791
|
-
continue;
|
|
792
|
-
}
|
|
793
|
-
if ((norm === "enabled" || norm === "supported") && looksLikeCapability(labeled.value)) {
|
|
794
|
-
assign(norm === "enabled" ? "enabledCaps" : "supportedCaps", labeled.value, lineNo);
|
|
795
|
-
continue;
|
|
796
|
-
}
|
|
797
|
-
const field = lookupField(labeled.label);
|
|
798
|
-
if (!field) {
|
|
799
|
-
if (held.bag) held.bag.openField = "";
|
|
800
|
-
continue;
|
|
801
|
-
}
|
|
802
|
-
assign(field, labeled.value, lineNo);
|
|
459
|
+
function isRecord(value) {
|
|
460
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// server/src/diagramData.ts
|
|
464
|
+
import { Document, isMap, parseDocument } from "yaml";
|
|
465
|
+
|
|
466
|
+
// server/src/neighbors/clean.ts
|
|
467
|
+
function cleanCli(text) {
|
|
468
|
+
return text.replace(/^\uFEFF/, "").replace(/\u0000/g, "").replace(/\x1b\[[0-9;]*[A-Za-z]/g, "").replace(/.\x08/g, "").replace(/\x08/g, "").replace(/--More--|\(END\)/gi, "").replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
469
|
+
}
|
|
470
|
+
function promptHost(line) {
|
|
471
|
+
const trimmed = line.trim();
|
|
472
|
+
if (!trimmed || trimmed.length > 200) return null;
|
|
473
|
+
const comware = /^<([A-Za-z][\w.-]{0,63})>\s*(.*)$/.exec(trimmed);
|
|
474
|
+
if (comware?.[1]) return comware[1];
|
|
475
|
+
const mikrotik = /^\[(?:[\w.+-]+@)?([A-Za-z0-9][\w.-]*)\]\s*>?\s*(.*)$/.exec(trimmed);
|
|
476
|
+
if (mikrotik?.[1]) return mikrotik[1];
|
|
477
|
+
const userHost = /^[\w.+-]+@([A-Za-z][\w.-]{0,63})\s*[>#]\s*(.*)$/.exec(trimmed);
|
|
478
|
+
if (userHost?.[1]) return userHost[1];
|
|
479
|
+
const host = /^([A-Za-z][\w.-]{0,63})\s*[>#]\s*(.*)$/.exec(trimmed);
|
|
480
|
+
if (!host?.[1]) return null;
|
|
481
|
+
const rest = host[2] ?? "";
|
|
482
|
+
if (rest === "") return host[1];
|
|
483
|
+
if (/^(?:show|display|sh|lldpctl|\/)/i.test(rest)) return host[1];
|
|
484
|
+
if (rest.length < 80 && !/[:=]/.test(rest)) return host[1];
|
|
485
|
+
return null;
|
|
486
|
+
}
|
|
487
|
+
function clipIssue(value) {
|
|
488
|
+
const flat = value.replace(/[\u0000-\u001f]/g, " ").replace(/\s+/g, " ").trim();
|
|
489
|
+
return flat.length > 80 ? `${flat.slice(0, 80)}\u2026` : flat;
|
|
490
|
+
}
|
|
491
|
+
function cleanSentinel(value) {
|
|
492
|
+
const trimmed = value.trim().replace(/^["']|["']$/g, "").trim();
|
|
493
|
+
if (!trimmed) return "";
|
|
494
|
+
if (/^(?:null|n\/a|na|none|unknown|not advertised|-|--)$/i.test(trimmed)) return "";
|
|
495
|
+
return trimmed;
|
|
496
|
+
}
|
|
497
|
+
function stripSubtype(value) {
|
|
498
|
+
const match = /^(?:mac|ifname|local|network|interface|ip|ipv4|ipv6)\s+(.+)$/i.exec(value.trim());
|
|
499
|
+
return match?.[1]?.trim() || value.trim();
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// server/src/neighbors/normalize.ts
|
|
503
|
+
var PREFIXES = [
|
|
504
|
+
{ re: /^(?:twentyfivegigabitethernet|twentyfivegige|twe)/, display: "Twe", key: "twe" },
|
|
505
|
+
{ re: /^(?:hundredgigabitethernet|hundredgige|hu)/, display: "Hu", key: "hu" },
|
|
506
|
+
{ re: /^(?:fortygigabitethernet|fortygige|fo)/, display: "Fo", key: "fo" },
|
|
507
|
+
{ re: /^(?:tengigabitethernet|tengige|te)/, display: "Te", key: "te" },
|
|
508
|
+
{ re: /^(?:fivegigabitethernet|fivegige|fi)/, display: "Fi", key: "fi" },
|
|
509
|
+
{ re: /^(?:twogigabitethernet|twogige|tw)/, display: "Tw", key: "tw" },
|
|
510
|
+
{ re: /^(?:appgigabitethernet|appgige|ap)/, display: "Ap", key: "ap" },
|
|
511
|
+
{ re: /^(?:gigabitethernet|gig|gi)/, display: "Gi", key: "gi" },
|
|
512
|
+
{ re: /^(?:fastethernet|fas|fa)/, display: "Fa", key: "fa" },
|
|
513
|
+
{ re: /^(?:port-channel|portchannel|po)/, display: "Po", key: "po" },
|
|
514
|
+
{ re: /^(?:management|mgmt|ma)/, display: "Ma", key: "ma" },
|
|
515
|
+
{ re: /^(?:ethernet|ether|eth|et)/, display: "Eth", key: "eth" },
|
|
516
|
+
{ re: /^(?:loopback|lo)/, display: "Lo", key: "lo" },
|
|
517
|
+
{ re: /^(?:vlan|vl)/, display: "Vl", key: "vl" }
|
|
518
|
+
];
|
|
519
|
+
var CAPABILITY_ORDER = ["Router", "Switch", "Phone", "WLAN", "Station", "Cable", "Repeater", "Other"];
|
|
520
|
+
var CAPABILITY_LETTER = {
|
|
521
|
+
r: "Router",
|
|
522
|
+
b: "Switch",
|
|
523
|
+
t: "Phone",
|
|
524
|
+
w: "WLAN",
|
|
525
|
+
s: "Station",
|
|
526
|
+
c: "Cable",
|
|
527
|
+
p: "Repeater",
|
|
528
|
+
o: "Other"
|
|
529
|
+
};
|
|
530
|
+
var NOT_CAPABILITY_CODES = /* @__PURE__ */ new Set(["stp", "igmp", "cdp", "lldp", "mndp", "vlan"]);
|
|
531
|
+
var CAPABILITY_WORD = {
|
|
532
|
+
router: "Router",
|
|
533
|
+
switch: "Switch",
|
|
534
|
+
bridge: "Switch",
|
|
535
|
+
telephone: "Phone",
|
|
536
|
+
phone: "Phone",
|
|
537
|
+
wlan: "WLAN",
|
|
538
|
+
wireless: "WLAN",
|
|
539
|
+
station: "Station",
|
|
540
|
+
host: "Station",
|
|
541
|
+
docsis: "Cable",
|
|
542
|
+
cable: "Cable",
|
|
543
|
+
repeater: "Repeater",
|
|
544
|
+
other: "Other"
|
|
545
|
+
};
|
|
546
|
+
function isMacAddress(value) {
|
|
547
|
+
return macOf(value) !== "";
|
|
548
|
+
}
|
|
549
|
+
function macOf(value) {
|
|
550
|
+
const trimmed = value.trim();
|
|
551
|
+
if (!trimmed || trimmed.length > 40) return "";
|
|
552
|
+
const hex = trimmed.toLowerCase().replace(/[^0-9a-f]/g, "");
|
|
553
|
+
if (hex.length !== 12) return "";
|
|
554
|
+
const hasSep = /[:.\-\s]/.test(trimmed);
|
|
555
|
+
const hasLetter = /[a-f]/i.test(hex);
|
|
556
|
+
if (!hasSep && !hasLetter) return "";
|
|
557
|
+
if (!/^[0-9a-f:.\-\s]+$/i.test(trimmed)) return "";
|
|
558
|
+
const parts = hex.match(/.{2}/g);
|
|
559
|
+
return parts ? parts.join(":") : "";
|
|
560
|
+
}
|
|
561
|
+
function isIp(value) {
|
|
562
|
+
return /^\d{1,3}(?:\.\d{1,3}){3}$/.test(value);
|
|
563
|
+
}
|
|
564
|
+
function normalizeInterface(raw) {
|
|
565
|
+
const original = cleanSentinel(raw).replace(/\s+/g, " ").trim();
|
|
566
|
+
if (!original) return { display: "", key: "", isMac: false };
|
|
567
|
+
const mac = macOf(original);
|
|
568
|
+
if (mac) return { display: mac, key: `mac:${mac}`, isMac: true };
|
|
569
|
+
const compact = original.toLowerCase().replace(/[\s_]+/g, "");
|
|
570
|
+
if (/^(?:ge|xe|et|ae|em|fxp|irb|lo|me|vme|gr|lt)-[\d/.]+$/.test(compact) || /^(?:ae|em|lo|fxp|irb|vme|me)\d/.test(compact)) {
|
|
571
|
+
return { display: compact, key: compact, isMac: false };
|
|
803
572
|
}
|
|
804
|
-
|
|
805
|
-
|
|
573
|
+
for (const prefix of PREFIXES) {
|
|
574
|
+
const match = prefix.re.exec(compact);
|
|
575
|
+
if (!match) continue;
|
|
576
|
+
const rest = compact.slice(match[0].length).replace(/^[-_/]+/, "");
|
|
577
|
+
if (!rest || !/^[\d:.]/.test(rest)) continue;
|
|
578
|
+
return { display: `${prefix.display}${rest}`, key: `${prefix.key}${rest}`, isMac: false };
|
|
579
|
+
}
|
|
580
|
+
const plain = original.replace(/\s+/g, " ").trim();
|
|
581
|
+
return { display: plain, key: plain.toLowerCase().replace(/\s+/g, ""), isMac: false };
|
|
806
582
|
}
|
|
807
|
-
function
|
|
808
|
-
const
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
if (
|
|
814
|
-
|
|
815
|
-
bag = null;
|
|
816
|
-
if (!current.localPort) current.localPort = current.iface;
|
|
817
|
-
bagToNeighbor(current, localDevice, "", collected);
|
|
818
|
-
};
|
|
819
|
-
const lines = text.split("\n");
|
|
820
|
-
for (let index = 0; index < lines.length; index += 1) {
|
|
821
|
-
if (collected.overflow) break;
|
|
822
|
-
const lineNo = index + 1;
|
|
823
|
-
const trimmed = (lines[index] ?? "").trim();
|
|
824
|
-
if (!trimmed) continue;
|
|
825
|
-
const host = promptHost(trimmed);
|
|
826
|
-
if (host) {
|
|
827
|
-
flush();
|
|
828
|
-
localDevice = host;
|
|
829
|
-
collected.preferred.push(host);
|
|
583
|
+
function normalizeCapability(raw) {
|
|
584
|
+
const found = /* @__PURE__ */ new Set();
|
|
585
|
+
const tokens = raw.split(/[^A-Za-z0-9]+/).map((token) => token.trim()).filter(Boolean);
|
|
586
|
+
for (const token of tokens) {
|
|
587
|
+
const lower = token.toLowerCase();
|
|
588
|
+
if (/^(?:igmp|filtering|enabled|available|supported|on|off|yes|no)$/i.test(lower)) continue;
|
|
589
|
+
if (CAPABILITY_WORD[lower]) {
|
|
590
|
+
found.add(CAPABILITY_WORD[lower]);
|
|
830
591
|
continue;
|
|
831
592
|
}
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
const value = match[3] ?? "";
|
|
837
|
-
if (!bag || bag.iface !== iface) {
|
|
838
|
-
flush();
|
|
839
|
-
bag = { ...emptyBag2(lineNo), iface };
|
|
840
|
-
}
|
|
841
|
-
const current = bag;
|
|
842
|
-
const setOnce = (field, next) => {
|
|
843
|
-
if (!next) return;
|
|
844
|
-
if (REPEAT.has(field) && current[field] && current[field] !== next) {
|
|
845
|
-
const savedIface = current.iface;
|
|
846
|
-
flush();
|
|
847
|
-
bag = { ...emptyBag2(lineNo), iface: savedIface };
|
|
848
|
-
bag[field] = next;
|
|
849
|
-
return;
|
|
850
|
-
}
|
|
851
|
-
if (!current[field]) current[field] = next;
|
|
852
|
-
};
|
|
853
|
-
if (path5 === "chassis.name") setOnce("systemName", value);
|
|
854
|
-
else if (path5 === "chassis.descr" || path5 === "chassis.description") setOnce("systemDescription", value);
|
|
855
|
-
else if (path5 === "chassis.mac") setOnce("chassisId", value);
|
|
856
|
-
else if (path5 === "port.ifname") {
|
|
857
|
-
if (current.portId && isMacAddress(current.portId)) current.portId = "";
|
|
858
|
-
setOnce("portId", value);
|
|
859
|
-
} else if ((path5 === "port.id" || path5 === "port.mac") && !current.portId) setOnce("portId", value);
|
|
860
|
-
else if (path5 === "port.descr" || path5 === "port.description") setOnce("portDescription", value);
|
|
861
|
-
else if (/\.enabled$/.test(path5)) {
|
|
862
|
-
const name = path5.split(".").slice(-2, -1)[0] ?? "";
|
|
863
|
-
if (/^(?:on|true|yes|1)$/i.test(value) && name) {
|
|
864
|
-
current.enabledCaps = current.enabledCaps ? `${current.enabledCaps}, ${name}` : name;
|
|
593
|
+
if (/^[brtwscpo]{1,4}$/i.test(token) && !NOT_CAPABILITY_CODES.has(lower)) {
|
|
594
|
+
for (const letter of token.toLowerCase()) {
|
|
595
|
+
const label = CAPABILITY_LETTER[letter];
|
|
596
|
+
if (label) found.add(label);
|
|
865
597
|
}
|
|
866
598
|
}
|
|
867
599
|
}
|
|
868
|
-
|
|
869
|
-
return collected;
|
|
600
|
+
return CAPABILITY_ORDER.filter((label) => found.has(label)).join(", ");
|
|
870
601
|
}
|
|
871
|
-
function
|
|
872
|
-
|
|
602
|
+
function platformOf(description, platformField = "") {
|
|
603
|
+
const text = `${platformField} ${description}`.toLowerCase().replace(/\s+/g, " ").trim();
|
|
604
|
+
if (!text) return "";
|
|
605
|
+
if (/nx-os|nexus|\bn[3579]k\b/.test(text)) return "Cisco NX-OS";
|
|
606
|
+
if (/ios-xe|ios xe|cat9k|iosxe|\bc9[0-9]{3}\b|\bisr\d|\basr\d/.test(text)) return "Cisco IOS-XE";
|
|
607
|
+
if (/cisco ios|\bcatalyst\b|ws-c\d|\bc3750\b|\bc2960\b/.test(text)) return "Cisco IOS";
|
|
608
|
+
if (/arista|\beos\b|\bdcs-/.test(text)) return "Arista EOS";
|
|
609
|
+
if (/junos|juniper/.test(text)) return "Junos";
|
|
610
|
+
if (/aruba|provision|aos-cx|arubaos/.test(text)) return "Aruba";
|
|
611
|
+
if (/comware|hewlett|\bhpe\b|\bhp\b/.test(text)) return "HPE";
|
|
612
|
+
if (/extreme\s*xos|\bexos\b|extremexos/.test(text)) return "Extreme";
|
|
613
|
+
if (/os10|\bdell\b|force10|\bdnos\b/.test(text)) return "Dell";
|
|
614
|
+
if (/routeros|mikrotik/.test(text)) return "MikroTik";
|
|
615
|
+
if (/ubiquiti|unifi|edgeswitch|edgeos|\buisp\b/.test(text)) return "Ubiquiti";
|
|
616
|
+
if (/lldpd|\blinux\b|debian|ubuntu/.test(text)) return "Linux";
|
|
617
|
+
const clip = (platformField || description).split("\n")[0]?.trim() ?? "";
|
|
618
|
+
if (!clip) return "";
|
|
619
|
+
return clip.length > 40 ? `${clip.slice(0, 40)}\u2026` : clip;
|
|
873
620
|
}
|
|
874
|
-
function
|
|
875
|
-
|
|
876
|
-
if (
|
|
877
|
-
|
|
878
|
-
if (
|
|
879
|
-
|
|
880
|
-
}
|
|
881
|
-
return "";
|
|
621
|
+
function deviceKey(name, shortNames) {
|
|
622
|
+
const lower = name.trim().toLowerCase();
|
|
623
|
+
if (isIp(lower)) return `ip:${lower}`;
|
|
624
|
+
const short = lower.split(".")[0] ?? lower;
|
|
625
|
+
if (short && shortNames.has(short)) return `host:${short}`;
|
|
626
|
+
return `host:${lower}`;
|
|
882
627
|
}
|
|
883
|
-
function
|
|
884
|
-
|
|
885
|
-
const
|
|
886
|
-
|
|
887
|
-
for (const item of items) {
|
|
888
|
-
if (typeof item === "string") {
|
|
889
|
-
enabled.push(item);
|
|
890
|
-
continue;
|
|
891
|
-
}
|
|
892
|
-
if (!isRecord(item)) continue;
|
|
893
|
-
const type = textOf(item.type ?? item.name);
|
|
894
|
-
const flag = item.enabled;
|
|
895
|
-
if (flag === false || flag === "off" || flag === "false") continue;
|
|
896
|
-
if (type) enabled.push(type);
|
|
897
|
-
}
|
|
898
|
-
return enabled.join(", ");
|
|
899
|
-
}
|
|
900
|
-
function chassisFromJson(chassis) {
|
|
901
|
-
if (!isRecord(chassis)) return [];
|
|
902
|
-
const known = /* @__PURE__ */ new Set(["id", "name", "descr", "description", "mgmt-ip", "mgmt_ip", "capability", "capabilities"]);
|
|
903
|
-
const direct = textOf(chassis.name) || chassis.id || chassis.descr || chassis.description;
|
|
904
|
-
if (direct) {
|
|
905
|
-
return [
|
|
906
|
-
{
|
|
907
|
-
name: textOf(chassis.name),
|
|
908
|
-
id: textOf(chassis.id),
|
|
909
|
-
descr: textOf(chassis.descr ?? chassis.description),
|
|
910
|
-
caps: capabilityFromJson(chassis.capability ?? chassis.capabilities)
|
|
911
|
-
}
|
|
912
|
-
];
|
|
913
|
-
}
|
|
914
|
-
const hosts = Object.keys(chassis).filter((key) => !known.has(key) && isRecord(chassis[key]));
|
|
915
|
-
if (hosts.length === 0) return [];
|
|
916
|
-
return hosts.map((host) => {
|
|
917
|
-
const inner = chassis[host];
|
|
918
|
-
return {
|
|
919
|
-
name: host,
|
|
920
|
-
id: textOf(inner.id),
|
|
921
|
-
descr: textOf(inner.descr ?? inner.description),
|
|
922
|
-
caps: capabilityFromJson(inner.capability ?? inner.capabilities)
|
|
923
|
-
};
|
|
924
|
-
});
|
|
925
|
-
}
|
|
926
|
-
function portFromJson(port) {
|
|
927
|
-
if (!isRecord(port)) return { id: "", descr: "" };
|
|
928
|
-
return {
|
|
929
|
-
id: textOf(port.id ?? port.ifname ?? port.name),
|
|
930
|
-
descr: textOf(port.descr ?? port.description)
|
|
931
|
-
};
|
|
628
|
+
function chooseDisplay(variants, preferred) {
|
|
629
|
+
const prefs = variants.filter((name) => preferred.has(name.trim().toLowerCase()));
|
|
630
|
+
const pool = prefs.length > 0 ? prefs : variants;
|
|
631
|
+
return [...pool].sort((a, b) => a.length - b.length || a.localeCompare(b))[0] ?? "";
|
|
932
632
|
}
|
|
933
|
-
function
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
return [{ name, value: item }];
|
|
939
|
-
});
|
|
633
|
+
function finalizeNeighbors(parserId, raw, errors, warnings, preferredNames, emptyMessage2) {
|
|
634
|
+
const preferred = new Set(preferredNames.map((name) => name.trim().toLowerCase()).filter(Boolean));
|
|
635
|
+
const names = [];
|
|
636
|
+
for (const link of raw) {
|
|
637
|
+
names.push(link.localDevice, link.remoteDevice);
|
|
940
638
|
}
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
639
|
+
const shortNames = /* @__PURE__ */ new Set();
|
|
640
|
+
for (const name of names) {
|
|
641
|
+
const lower = name.trim().toLowerCase();
|
|
642
|
+
if (lower && !lower.includes(".") && !isIp(lower)) shortNames.add(lower);
|
|
944
643
|
}
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
function parseLldpJson(text, options) {
|
|
948
|
-
const collected = { raw: [], errors: [], warnings: [], preferred: [], overflow: false };
|
|
949
|
-
const localDevice = cleanSentinel(options.localDevice ?? "");
|
|
950
|
-
if (localDevice) collected.preferred.push(localDevice);
|
|
951
|
-
let parsed;
|
|
952
|
-
try {
|
|
953
|
-
parsed = JSON.parse(text);
|
|
954
|
-
} catch {
|
|
955
|
-
collected.errors.push({ row: 1, message: "This LLDP JSON could not be parsed." });
|
|
956
|
-
return collected;
|
|
644
|
+
for (const name of preferred) {
|
|
645
|
+
if (!name.includes(".") && !isIp(name)) shortNames.add(name);
|
|
957
646
|
}
|
|
958
|
-
const
|
|
959
|
-
const
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
647
|
+
const variants = /* @__PURE__ */ new Map();
|
|
648
|
+
const remember = (name) => {
|
|
649
|
+
const key = deviceKey(name, shortNames);
|
|
650
|
+
const list = variants.get(key) ?? [];
|
|
651
|
+
if (!list.includes(name)) list.push(name);
|
|
652
|
+
variants.set(key, list);
|
|
653
|
+
return key;
|
|
654
|
+
};
|
|
655
|
+
const attrs = /* @__PURE__ */ new Map();
|
|
656
|
+
const touchAttr = (key, platform, capability) => {
|
|
657
|
+
const current = attrs.get(key) ?? { platform: "", capability: "" };
|
|
658
|
+
if (!current.platform && platform) current.platform = platform;
|
|
659
|
+
if (!current.capability && capability) current.capability = capability;
|
|
660
|
+
attrs.set(key, current);
|
|
661
|
+
};
|
|
662
|
+
const prepared = [];
|
|
663
|
+
for (const link of raw) {
|
|
664
|
+
const localPort = normalizeInterface(link.localPort);
|
|
665
|
+
const remotePort = normalizeInterface(link.remotePort);
|
|
666
|
+
if (!localPort.key || !remotePort.key) continue;
|
|
667
|
+
const localKey = remember(link.localDevice);
|
|
668
|
+
const remoteKey = remember(link.remoteDevice);
|
|
669
|
+
touchAttr(remoteKey, link.remotePlatform, link.remoteCapability);
|
|
670
|
+
prepared.push({ line: link.line, localKey, remoteKey, localPort, remotePort });
|
|
963
671
|
}
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
bag.portDescription = port.descr;
|
|
977
|
-
bagToNeighbor(bag, localDevice, "", collected);
|
|
978
|
-
if (collected.overflow) break;
|
|
672
|
+
const display = /* @__PURE__ */ new Map();
|
|
673
|
+
for (const [key, list] of variants) display.set(key, chooseDisplay(list, preferred));
|
|
674
|
+
const seen = /* @__PURE__ */ new Set();
|
|
675
|
+
const links = [];
|
|
676
|
+
let dropped = 0;
|
|
677
|
+
for (const link of prepared) {
|
|
678
|
+
const left = `${link.localKey}\0${link.localPort.key}`;
|
|
679
|
+
const right = `${link.remoteKey}\0${link.remotePort.key}`;
|
|
680
|
+
const undirected = left < right ? `${left}\0${right}` : `${right}\0${left}`;
|
|
681
|
+
if (seen.has(undirected)) {
|
|
682
|
+
dropped += 1;
|
|
683
|
+
continue;
|
|
979
684
|
}
|
|
685
|
+
seen.add(undirected);
|
|
686
|
+
const localAttr = attrs.get(link.localKey) ?? { platform: "", capability: "" };
|
|
687
|
+
const remoteAttr = attrs.get(link.remoteKey) ?? { platform: "", capability: "" };
|
|
688
|
+
links.push({
|
|
689
|
+
line: link.line,
|
|
690
|
+
localDevice: display.get(link.localKey) ?? link.localKey,
|
|
691
|
+
localPort: link.localPort.display,
|
|
692
|
+
remoteDevice: display.get(link.remoteKey) ?? link.remoteKey,
|
|
693
|
+
remotePort: link.remotePort.display,
|
|
694
|
+
localPlatform: localAttr.platform,
|
|
695
|
+
remotePlatform: remoteAttr.platform,
|
|
696
|
+
localCapability: localAttr.capability,
|
|
697
|
+
remoteCapability: remoteAttr.capability
|
|
698
|
+
});
|
|
980
699
|
}
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
return /"lldp"/i.test(trimmed) || /"chassis"/i.test(trimmed) && /"port"/i.test(trimmed);
|
|
987
|
-
}
|
|
988
|
-
function looksLikeKeyvalue(text) {
|
|
989
|
-
return /^lldp\./m.test(text);
|
|
990
|
-
}
|
|
991
|
-
function emptyMessage(text) {
|
|
992
|
-
if (/local interface\s+.*system name/i.test(text) && !/chassis id\s*:/i.test(text) && !/system name\s*:/i.test(text)) {
|
|
993
|
-
return "This is the short LLDP neighbor table. Paste the detail output, or lldpctl on Linux.";
|
|
994
|
-
}
|
|
995
|
-
return "No LLDP neighbors were found. Paste LLDP neighbor detail, or lldpctl output.";
|
|
996
|
-
}
|
|
997
|
-
function parseLldpNeighbors(text, options = {}) {
|
|
998
|
-
const cleaned = cleanCli(text);
|
|
999
|
-
if (looksLikeLldpJson(cleaned)) {
|
|
1000
|
-
return finish(parseLldpJson(cleaned, options), emptyMessage(cleaned));
|
|
700
|
+
if (dropped > 0) {
|
|
701
|
+
warnings.push({
|
|
702
|
+
row: 0,
|
|
703
|
+
message: `Combined ${dropped} duplicate link${dropped === 1 ? "" : "s"} seen from both ends.`
|
|
704
|
+
});
|
|
1001
705
|
}
|
|
1002
|
-
if (
|
|
1003
|
-
|
|
706
|
+
if (links.length === 0 && errors.length === 0) {
|
|
707
|
+
errors.push({ row: 1, message: emptyMessage2 });
|
|
1004
708
|
}
|
|
1005
|
-
return
|
|
1006
|
-
}
|
|
1007
|
-
var lldpParser = {
|
|
1008
|
-
id: "lldp",
|
|
1009
|
-
kind: "lldp",
|
|
1010
|
-
label: "LLDP neighbors (any vendor)",
|
|
1011
|
-
parse: parseLldpNeighbors
|
|
1012
|
-
};
|
|
1013
|
-
|
|
1014
|
-
// server/src/neighbors/registry.ts
|
|
1015
|
-
var parsers = [];
|
|
1016
|
-
function registerNeighborParser(parser) {
|
|
1017
|
-
const index = parsers.findIndex((item) => item.id === parser.id);
|
|
1018
|
-
if (index >= 0) parsers.splice(index, 1);
|
|
1019
|
-
parsers.push(parser);
|
|
709
|
+
return { parserId, links, errors, warnings };
|
|
1020
710
|
}
|
|
1021
711
|
|
|
1022
|
-
// server/src/neighbors/
|
|
1023
|
-
|
|
1024
|
-
registerNeighborParser(lldpParser);
|
|
712
|
+
// server/src/neighbors/types.ts
|
|
713
|
+
var MAX_NEIGHBOR_BLOCKS = 5e3;
|
|
1025
714
|
|
|
1026
|
-
// server/src/
|
|
1027
|
-
function
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
}
|
|
1038
|
-
return value >>> 0;
|
|
1039
|
-
}
|
|
1040
|
-
function formatIpv4(value) {
|
|
1041
|
-
return `${value >>> 24 & 255}.${value >>> 16 & 255}.${value >>> 8 & 255}.${value & 255}`;
|
|
715
|
+
// server/src/neighbors/cdp.ts
|
|
716
|
+
function emptyBag(line) {
|
|
717
|
+
return {
|
|
718
|
+
line,
|
|
719
|
+
deviceId: "",
|
|
720
|
+
platform: "",
|
|
721
|
+
capability: "",
|
|
722
|
+
localPort: "",
|
|
723
|
+
remotePort: "",
|
|
724
|
+
version: "",
|
|
725
|
+
readingVersion: false
|
|
726
|
+
};
|
|
1042
727
|
}
|
|
1043
|
-
function
|
|
1044
|
-
|
|
1045
|
-
if (
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
728
|
+
function parseCdpNeighbors(text, options = {}) {
|
|
729
|
+
const cleaned = cleanCli(text);
|
|
730
|
+
if (/local intrfce/i.test(cleaned) && !/port id \(outgoing port\)/i.test(cleaned)) {
|
|
731
|
+
return {
|
|
732
|
+
parserId: "cisco-cdp",
|
|
733
|
+
links: [],
|
|
734
|
+
warnings: [],
|
|
735
|
+
errors: [
|
|
736
|
+
{
|
|
737
|
+
row: 1,
|
|
738
|
+
message: "This is the short CDP table. Paste show cdp neighbors detail."
|
|
739
|
+
}
|
|
740
|
+
]
|
|
741
|
+
};
|
|
1054
742
|
}
|
|
1055
|
-
|
|
1056
|
-
const
|
|
1057
|
-
|
|
1058
|
-
const
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
743
|
+
const raw = [];
|
|
744
|
+
const errors = [];
|
|
745
|
+
const warnings = [];
|
|
746
|
+
const preferred = [];
|
|
747
|
+
let localDevice = cleanSentinel(options.localDevice ?? "");
|
|
748
|
+
if (localDevice) preferred.push(localDevice);
|
|
749
|
+
let bag = null;
|
|
750
|
+
let count = 0;
|
|
751
|
+
const flush = () => {
|
|
752
|
+
if (!bag) return;
|
|
753
|
+
const current = bag;
|
|
754
|
+
bag = null;
|
|
755
|
+
if (!current.deviceId && !current.localPort && !current.remotePort) return;
|
|
756
|
+
count += 1;
|
|
757
|
+
if (count > MAX_NEIGHBOR_BLOCKS) return;
|
|
758
|
+
const remote = cleanSentinel(current.deviceId).replace(/\(([A-Za-z0-9_-]{4,})\)\s*$/, "").trim();
|
|
759
|
+
const localPort = cleanSentinel(current.localPort);
|
|
760
|
+
const remotePort = cleanSentinel(current.remotePort);
|
|
761
|
+
const missing = [];
|
|
762
|
+
if (!localDevice) missing.push("local device");
|
|
763
|
+
if (!localPort) missing.push("local interface");
|
|
764
|
+
if (!remote) missing.push("remote device");
|
|
765
|
+
if (!remotePort) missing.push("remote port");
|
|
766
|
+
if (missing.length > 0) {
|
|
767
|
+
const who = remote ? ` "${clipIssue(remote)}"` : "";
|
|
768
|
+
errors.push({
|
|
769
|
+
row: current.line,
|
|
770
|
+
message: `Line ${current.line}: CDP neighbor${who} is missing ${missing.join(" and ")}.`
|
|
771
|
+
});
|
|
772
|
+
return;
|
|
1065
773
|
}
|
|
1066
|
-
|
|
774
|
+
raw.push({
|
|
775
|
+
line: current.line,
|
|
776
|
+
localDevice,
|
|
777
|
+
localPort,
|
|
778
|
+
remoteDevice: remote,
|
|
779
|
+
remotePort,
|
|
780
|
+
remotePlatform: platformOf(current.version, current.platform),
|
|
781
|
+
remoteCapability: normalizeCapability(current.capability)
|
|
782
|
+
});
|
|
783
|
+
};
|
|
784
|
+
const lines = cleaned.split("\n");
|
|
785
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
786
|
+
if (count > MAX_NEIGHBOR_BLOCKS) break;
|
|
787
|
+
const lineNo = index + 1;
|
|
788
|
+
const trimmed = (lines[index] ?? "").trim();
|
|
789
|
+
if (!trimmed) {
|
|
790
|
+
if (bag?.readingVersion) bag.readingVersion = false;
|
|
791
|
+
continue;
|
|
792
|
+
}
|
|
793
|
+
if (/^[-=*_]{5,}$/.test(trimmed)) {
|
|
794
|
+
flush();
|
|
795
|
+
continue;
|
|
796
|
+
}
|
|
797
|
+
const host = promptHost(trimmed);
|
|
798
|
+
if (host) {
|
|
799
|
+
flush();
|
|
800
|
+
localDevice = host;
|
|
801
|
+
preferred.push(host);
|
|
802
|
+
continue;
|
|
803
|
+
}
|
|
804
|
+
const device = /^Device ID\s*:\s*(.*)$/i.exec(trimmed);
|
|
805
|
+
if (device) {
|
|
806
|
+
if (bag?.deviceId) flush();
|
|
807
|
+
bag = emptyBag(lineNo);
|
|
808
|
+
bag.deviceId = device[1]?.trim() ?? "";
|
|
809
|
+
continue;
|
|
810
|
+
}
|
|
811
|
+
if (!bag) continue;
|
|
812
|
+
const platform = /^Platform\s*:\s*(.*)$/i.exec(trimmed);
|
|
813
|
+
if (platform) {
|
|
814
|
+
bag.readingVersion = false;
|
|
815
|
+
const value = platform[1] ?? "";
|
|
816
|
+
const caps = /^(.*?)\s*,\s*Capabilities\s*:\s*(.*)$/i.exec(value);
|
|
817
|
+
if (caps) {
|
|
818
|
+
bag.platform = caps[1]?.trim() ?? "";
|
|
819
|
+
bag.capability = caps[2]?.trim() ?? "";
|
|
820
|
+
} else {
|
|
821
|
+
bag.platform = value.trim();
|
|
822
|
+
}
|
|
823
|
+
continue;
|
|
824
|
+
}
|
|
825
|
+
const capsOnly = /^Capabilities\s*:\s*(.*)$/i.exec(trimmed);
|
|
826
|
+
if (capsOnly) {
|
|
827
|
+
bag.readingVersion = false;
|
|
828
|
+
bag.capability = capsOnly[1]?.trim() ?? "";
|
|
829
|
+
continue;
|
|
830
|
+
}
|
|
831
|
+
const iface = /^Interface\s*:\s*(.*)$/i.exec(trimmed);
|
|
832
|
+
if (iface) {
|
|
833
|
+
bag.readingVersion = false;
|
|
834
|
+
const value = iface[1] ?? "";
|
|
835
|
+
const combined = /^(.*?)\s*,\s*Port ID \(outgoing port\)\s*:\s*(.+)$/i.exec(value);
|
|
836
|
+
if (combined) {
|
|
837
|
+
bag.localPort = combined[1]?.trim() ?? "";
|
|
838
|
+
bag.remotePort = combined[2]?.trim() ?? "";
|
|
839
|
+
} else {
|
|
840
|
+
bag.localPort = value.replace(/,\s*$/, "").trim();
|
|
841
|
+
}
|
|
842
|
+
continue;
|
|
843
|
+
}
|
|
844
|
+
const port = /^Port ID \(outgoing port\)\s*:\s*(.*)$/i.exec(trimmed);
|
|
845
|
+
if (port) {
|
|
846
|
+
bag.readingVersion = false;
|
|
847
|
+
bag.remotePort = port[1]?.trim() ?? "";
|
|
848
|
+
continue;
|
|
849
|
+
}
|
|
850
|
+
if (/^Version\s*:?\s*$/i.test(trimmed) || /^Version\s*:\s*\S/i.test(trimmed)) {
|
|
851
|
+
bag.readingVersion = true;
|
|
852
|
+
const inline = /^Version\s*:\s*(.+)$/i.exec(trimmed);
|
|
853
|
+
if (inline?.[1]) bag.version = inline[1].trim();
|
|
854
|
+
continue;
|
|
855
|
+
}
|
|
856
|
+
if (bag.readingVersion && !/^[A-Za-z][\w ./-]{0,40}:\s/.test(trimmed)) {
|
|
857
|
+
bag.version = bag.version ? `${bag.version} ${trimmed}` : trimmed;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
flush();
|
|
861
|
+
if (count > MAX_NEIGHBOR_BLOCKS) {
|
|
862
|
+
return {
|
|
863
|
+
parserId: "cisco-cdp",
|
|
864
|
+
links: [],
|
|
865
|
+
warnings: [],
|
|
866
|
+
errors: [{ row: 1, message: `A file can have at most ${MAX_NEIGHBOR_BLOCKS} neighbor blocks.` }]
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
return finalizeNeighbors(
|
|
870
|
+
"cisco-cdp",
|
|
871
|
+
raw,
|
|
872
|
+
errors,
|
|
873
|
+
warnings,
|
|
874
|
+
preferred,
|
|
875
|
+
"No CDP neighbors were found. Paste show cdp neighbors detail."
|
|
876
|
+
);
|
|
877
|
+
}
|
|
878
|
+
var ciscoCdpParser = {
|
|
879
|
+
id: "cisco-cdp",
|
|
880
|
+
kind: "cdp",
|
|
881
|
+
label: "Cisco CDP (IOS, IOS-XE, NX-OS)",
|
|
882
|
+
parse: parseCdpNeighbors
|
|
883
|
+
};
|
|
884
|
+
|
|
885
|
+
// server/src/neighbors/lldp.ts
|
|
886
|
+
var FIELDS = {
|
|
887
|
+
"local intf": "localPort",
|
|
888
|
+
localintf: "localPort",
|
|
889
|
+
"local interface": "localPort",
|
|
890
|
+
localinterface: "localPort",
|
|
891
|
+
"local port": "localPort",
|
|
892
|
+
localport: "localPort",
|
|
893
|
+
interface: "localPort",
|
|
894
|
+
intf: "localPort",
|
|
895
|
+
"chassis id": "chassisId",
|
|
896
|
+
chassisid: "chassisId",
|
|
897
|
+
"chassis identifier": "chassisId",
|
|
898
|
+
"port id": "portId",
|
|
899
|
+
portid: "portId",
|
|
900
|
+
"port info": "portId",
|
|
901
|
+
portinfo: "portId",
|
|
902
|
+
"port description": "portDescription",
|
|
903
|
+
portdescription: "portDescription",
|
|
904
|
+
"port descr": "portDescription",
|
|
905
|
+
portdescr: "portDescription",
|
|
906
|
+
"port desc": "portDescription",
|
|
907
|
+
portdesc: "portDescription",
|
|
908
|
+
"system name": "systemName",
|
|
909
|
+
systemname: "systemName",
|
|
910
|
+
sysname: "systemName",
|
|
911
|
+
"sys name": "systemName",
|
|
912
|
+
"chassis name": "systemName",
|
|
913
|
+
chassisname: "systemName",
|
|
914
|
+
"device id": "systemName",
|
|
915
|
+
deviceid: "systemName",
|
|
916
|
+
identity: "systemName",
|
|
917
|
+
"system description": "systemDescription",
|
|
918
|
+
systemdescription: "systemDescription",
|
|
919
|
+
"system descr": "systemDescription",
|
|
920
|
+
systemdescr: "systemDescription",
|
|
921
|
+
"system desc": "systemDescription",
|
|
922
|
+
sysdescr: "systemDescription",
|
|
923
|
+
"sys descr": "systemDescription",
|
|
924
|
+
"existing system capabilities": "enabledCaps",
|
|
925
|
+
existingsystemcapabilities: "enabledCaps",
|
|
926
|
+
"enabled capabilities": "enabledCaps",
|
|
927
|
+
enabledcapabilities: "enabledCaps",
|
|
928
|
+
"capabilities enabled": "enabledCaps",
|
|
929
|
+
"enabled caps": "enabledCaps",
|
|
930
|
+
enabledcaps: "enabledCaps",
|
|
931
|
+
"system capabilities enabled": "enabledCaps",
|
|
932
|
+
"system capabilities": "supportedCaps",
|
|
933
|
+
systemcapabilities: "supportedCaps",
|
|
934
|
+
"system caps": "supportedCaps",
|
|
935
|
+
systemcaps: "supportedCaps",
|
|
936
|
+
"capabilities supported": "supportedCaps",
|
|
937
|
+
"capabilities available": "supportedCaps",
|
|
938
|
+
"system capabilities supported": "supportedCaps",
|
|
939
|
+
capabilities: "supportedCaps",
|
|
940
|
+
platform: "platform"
|
|
941
|
+
};
|
|
942
|
+
var REPEAT = /* @__PURE__ */ new Set(["localPort", "chassisId", "portId", "systemName"]);
|
|
943
|
+
var PAIR_FIELDS = {
|
|
944
|
+
interface: "localPort",
|
|
945
|
+
"local-interface": "localPort",
|
|
946
|
+
"interface-name": "portId",
|
|
947
|
+
"port-id": "portId",
|
|
948
|
+
portid: "portId",
|
|
949
|
+
identity: "systemName",
|
|
950
|
+
"system-name": "systemName",
|
|
951
|
+
sysname: "systemName",
|
|
952
|
+
"system-description": "systemDescription",
|
|
953
|
+
sysdescr: "systemDescription",
|
|
954
|
+
"mac-address": "chassisId",
|
|
955
|
+
"chassis-id": "chassisId",
|
|
956
|
+
chassisid: "chassisId",
|
|
957
|
+
"port-description": "portDescription",
|
|
958
|
+
portdescr: "portDescription",
|
|
959
|
+
platform: "platform"
|
|
960
|
+
};
|
|
961
|
+
function emptyBag2(line) {
|
|
962
|
+
return {
|
|
963
|
+
line,
|
|
964
|
+
localPort: "",
|
|
965
|
+
chassisId: "",
|
|
966
|
+
portId: "",
|
|
967
|
+
portDescription: "",
|
|
968
|
+
systemName: "",
|
|
969
|
+
systemDescription: "",
|
|
970
|
+
enabledCaps: "",
|
|
971
|
+
supportedCaps: "",
|
|
972
|
+
platform: "",
|
|
973
|
+
openField: "",
|
|
974
|
+
skippedBlank: false
|
|
1067
975
|
};
|
|
1068
|
-
const left = parseSide(pieces[0] ?? "");
|
|
1069
|
-
if (!left) return null;
|
|
1070
|
-
if (pieces.length === 1) {
|
|
1071
|
-
if (left.length !== 8) return null;
|
|
1072
|
-
return Uint16Array.from(left);
|
|
1073
|
-
}
|
|
1074
|
-
const right = parseSide(pieces[1] ?? "");
|
|
1075
|
-
if (!right) return null;
|
|
1076
|
-
const missing = 8 - left.length - right.length;
|
|
1077
|
-
if (missing < 1) return null;
|
|
1078
|
-
return Uint16Array.from([...left, ...new Array(missing).fill(0), ...right]);
|
|
1079
976
|
}
|
|
1080
|
-
function
|
|
1081
|
-
|
|
1082
|
-
let bestLength = 0;
|
|
1083
|
-
let runStart = -1;
|
|
1084
|
-
for (let index = 0; index <= groups.length; index += 1) {
|
|
1085
|
-
const zero = index < groups.length && groups[index] === 0;
|
|
1086
|
-
if (zero && runStart < 0) runStart = index;
|
|
1087
|
-
if (!zero && runStart >= 0) {
|
|
1088
|
-
const length = index - runStart;
|
|
1089
|
-
if (length > bestLength) {
|
|
1090
|
-
bestStart = runStart;
|
|
1091
|
-
bestLength = length;
|
|
1092
|
-
}
|
|
1093
|
-
runStart = -1;
|
|
1094
|
-
}
|
|
1095
|
-
}
|
|
1096
|
-
const hex = (group) => group.toString(16);
|
|
1097
|
-
if (bestLength < 2) return [...groups].map(hex).join(":");
|
|
1098
|
-
const left = [...groups.slice(0, bestStart)].map(hex).join(":");
|
|
1099
|
-
const right = [...groups.slice(bestStart + bestLength)].map(hex).join(":");
|
|
1100
|
-
return `${left}::${right}`;
|
|
977
|
+
function normLabel(label) {
|
|
978
|
+
return label.toLowerCase().replace(/[^a-z0-9]+/g, " ").trim().replace(/\s+/g, " ").replace(/^(?:neighbor|neighbour|remote)\s+/, "");
|
|
1101
979
|
}
|
|
1102
|
-
function
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
980
|
+
function lookupField(label) {
|
|
981
|
+
const norm = normLabel(label);
|
|
982
|
+
if (FIELDS[norm]) return FIELDS[norm];
|
|
983
|
+
const compact = norm.replace(/ /g, "");
|
|
984
|
+
return FIELDS[compact] ?? null;
|
|
985
|
+
}
|
|
986
|
+
function splitLabel(line) {
|
|
987
|
+
const trimmed = line.trim().replace(/^[>*•]+\s*/, "").replace(/^-\s+/, "");
|
|
988
|
+
const match = /^([A-Za-z][A-Za-z0-9 ./_+-]{0,48}?)\s*[:=]\s*(.*)$/.exec(trimmed);
|
|
989
|
+
if (!match?.[1]) return null;
|
|
990
|
+
const label = match[1].trim();
|
|
991
|
+
if (label.split(/\s+/).length > 6) return null;
|
|
992
|
+
return { label, value: (match[2] ?? "").trim() };
|
|
993
|
+
}
|
|
994
|
+
function parsePairs(line) {
|
|
995
|
+
const pairs = [];
|
|
996
|
+
const pattern = /([A-Za-z][\w-]*)=(?:"([^"]*)"|(\S+))/g;
|
|
997
|
+
let match = pattern.exec(line);
|
|
998
|
+
while (match) {
|
|
999
|
+
pairs.push({ key: (match[1] ?? "").toLowerCase(), value: match[2] ?? match[3] ?? "" });
|
|
1000
|
+
match = pattern.exec(line);
|
|
1106
1001
|
}
|
|
1107
|
-
|
|
1108
|
-
return groups ? formatIpv6(groups) : null;
|
|
1002
|
+
return pairs;
|
|
1109
1003
|
}
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
"
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
var LINK_FIELDS = /* @__PURE__ */ new Set(["a", "b", "label", "inferred"]);
|
|
1131
|
-
var ENDPOINT_FIELDS = /* @__PURE__ */ new Set(["device", "interface"]);
|
|
1132
|
-
var VLAN_FIELDS = /* @__PURE__ */ new Set(["id", "name"]);
|
|
1133
|
-
var ROOT_FIELDS = /* @__PURE__ */ new Set(["schemaVersion", "format", "devices", "interfaces", "links", "vlans", "neighbors"]);
|
|
1134
|
-
var NEIGHBOR_FIELDS = /* @__PURE__ */ new Set(["cdp", "lldp", "localDevice"]);
|
|
1135
|
-
function validateNetwork(value) {
|
|
1136
|
-
const issues = [];
|
|
1137
|
-
if (!isRecord2(value)) {
|
|
1138
|
-
issues.push({ path: "", message: "Network data must be a JSON object." });
|
|
1139
|
-
return { document: null, issues };
|
|
1004
|
+
function capToken(value) {
|
|
1005
|
+
const flagged = /^(.*?)\s*,\s*(on|off|enabled|disabled|yes|no)\s*$/i.exec(value.trim());
|
|
1006
|
+
if (flagged?.[2] && /^(?:off|disabled|no)$/i.test(flagged[2])) return "";
|
|
1007
|
+
return (flagged?.[1] ?? value).trim();
|
|
1008
|
+
}
|
|
1009
|
+
function looksLikeCapability(value) {
|
|
1010
|
+
return normalizeCapability(value) !== "" || /bridge|router|switch|phone|station|wlan/i.test(value);
|
|
1011
|
+
}
|
|
1012
|
+
function clipField(value) {
|
|
1013
|
+
return value.length > 500 ? value.slice(0, 500) : value;
|
|
1014
|
+
}
|
|
1015
|
+
var TWEAKS = [
|
|
1016
|
+
(bag) => {
|
|
1017
|
+
bag.chassisId = cleanSentinel(stripSubtype(bag.chassisId));
|
|
1018
|
+
bag.portId = cleanSentinel(stripSubtype(bag.portId));
|
|
1019
|
+
bag.portDescription = cleanSentinel(bag.portDescription);
|
|
1020
|
+
bag.systemName = cleanSentinel(bag.systemName);
|
|
1021
|
+
bag.localPort = cleanSentinel(bag.localPort);
|
|
1022
|
+
bag.platform = cleanSentinel(bag.platform);
|
|
1023
|
+
bag.systemDescription = cleanSentinel(bag.systemDescription);
|
|
1140
1024
|
}
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1025
|
+
];
|
|
1026
|
+
function bagToNeighbor(bag, localDevice, pendingLocal, collected) {
|
|
1027
|
+
for (const tweak of TWEAKS) tweak(bag);
|
|
1028
|
+
const localPort = bag.localPort || pendingLocal;
|
|
1029
|
+
const chassisId = bag.chassisId;
|
|
1030
|
+
const systemName = bag.systemName;
|
|
1031
|
+
let remotePort = bag.portId;
|
|
1032
|
+
const portIsMac = Boolean(remotePort) && isMacAddress(remotePort);
|
|
1033
|
+
if (portIsMac && bag.portDescription) {
|
|
1034
|
+
collected.warnings.push({
|
|
1035
|
+
row: bag.line,
|
|
1036
|
+
message: `Line ${bag.line}: port ID is a MAC address. Using the port description.`
|
|
1037
|
+
});
|
|
1038
|
+
remotePort = bag.portDescription;
|
|
1039
|
+
} else if (!remotePort && bag.portDescription) {
|
|
1040
|
+
remotePort = bag.portDescription;
|
|
1144
1041
|
}
|
|
1145
|
-
|
|
1146
|
-
|
|
1042
|
+
const remoteDevice = systemName || macOf(chassisId) || chassisId;
|
|
1043
|
+
const hasAny = Boolean(systemName || chassisId || bag.portId || localPort || bag.systemDescription || bag.platform);
|
|
1044
|
+
if (!hasAny) return;
|
|
1045
|
+
if (collected.raw.length + collected.errors.length >= MAX_NEIGHBOR_BLOCKS) {
|
|
1046
|
+
collected.overflow = true;
|
|
1047
|
+
return;
|
|
1147
1048
|
}
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
if (issues.length > 0 || !devices || !interfaces || !links || !vlans || !neighbors) {
|
|
1154
|
-
return { document: null, issues };
|
|
1049
|
+
if (!systemName && chassisId) {
|
|
1050
|
+
collected.warnings.push({
|
|
1051
|
+
row: bag.line,
|
|
1052
|
+
message: `Line ${bag.line}: neighbor has no system name. Using chassis ID ${clipIssue(chassisId)}.`
|
|
1053
|
+
});
|
|
1155
1054
|
}
|
|
1156
|
-
const
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1055
|
+
const missing = [];
|
|
1056
|
+
if (!localDevice) missing.push("local device");
|
|
1057
|
+
if (!localPort) missing.push("local interface");
|
|
1058
|
+
if (!remoteDevice) missing.push("remote device");
|
|
1059
|
+
if (!remotePort) missing.push("remote port");
|
|
1060
|
+
if (missing.length > 0) {
|
|
1061
|
+
const who = remoteDevice ? ` "${clipIssue(remoteDevice)}"` : "";
|
|
1062
|
+
collected.errors.push({
|
|
1063
|
+
row: bag.line,
|
|
1064
|
+
message: `Line ${bag.line}: LLDP neighbor${who} is missing ${missing.join(" and ")}.`
|
|
1065
|
+
});
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
const capability = normalizeCapability(bag.enabledCaps || bag.supportedCaps);
|
|
1069
|
+
collected.raw.push({
|
|
1070
|
+
line: bag.line,
|
|
1071
|
+
localDevice,
|
|
1072
|
+
localPort,
|
|
1073
|
+
remoteDevice,
|
|
1074
|
+
remotePort,
|
|
1075
|
+
remotePlatform: platformOf(bag.systemDescription, bag.platform),
|
|
1076
|
+
remoteCapability: capability
|
|
1077
|
+
});
|
|
1078
|
+
}
|
|
1079
|
+
function finish(collected, emptyMessage2) {
|
|
1080
|
+
if (collected.overflow) {
|
|
1081
|
+
return {
|
|
1082
|
+
parserId: "lldp",
|
|
1083
|
+
links: [],
|
|
1084
|
+
warnings: [],
|
|
1085
|
+
errors: [{ row: 1, message: `A file can have at most ${MAX_NEIGHBOR_BLOCKS} neighbor blocks.` }]
|
|
1086
|
+
};
|
|
1087
|
+
}
|
|
1088
|
+
return finalizeNeighbors("lldp", collected.raw, collected.errors, collected.warnings, collected.preferred, emptyMessage2);
|
|
1089
|
+
}
|
|
1090
|
+
function parseLldpText(text, options) {
|
|
1091
|
+
const collected = { raw: [], errors: [], warnings: [], preferred: [], overflow: false };
|
|
1092
|
+
let localDevice = cleanSentinel(options.localDevice ?? "");
|
|
1093
|
+
if (localDevice) collected.preferred.push(localDevice);
|
|
1094
|
+
let pendingLocal = "";
|
|
1095
|
+
const held = { bag: null };
|
|
1096
|
+
const flush = () => {
|
|
1097
|
+
const current = held.bag;
|
|
1098
|
+
if (!current) return;
|
|
1099
|
+
held.bag = null;
|
|
1100
|
+
bagToNeighbor(current, localDevice, pendingLocal, collected);
|
|
1101
|
+
};
|
|
1102
|
+
const ensure = (line) => {
|
|
1103
|
+
if (!held.bag) held.bag = emptyBag2(line);
|
|
1104
|
+
return held.bag;
|
|
1105
|
+
};
|
|
1106
|
+
const assign = (field, value, line) => {
|
|
1107
|
+
const cleaned = clipField(value.trim().replace(/^["']|["']$/g, "").trim());
|
|
1108
|
+
if (!cleaned) {
|
|
1109
|
+
if (field === "systemDescription" || field === "portDescription") {
|
|
1110
|
+
const current2 = ensure(line);
|
|
1111
|
+
current2.openField = field;
|
|
1112
|
+
}
|
|
1113
|
+
return;
|
|
1114
|
+
}
|
|
1115
|
+
const current = ensure(line);
|
|
1116
|
+
if (REPEAT.has(field) && current[field] && current[field] !== cleaned) {
|
|
1117
|
+
flush();
|
|
1118
|
+
const next = ensure(line);
|
|
1119
|
+
next[field] = cleaned;
|
|
1120
|
+
next.openField = "";
|
|
1121
|
+
return;
|
|
1122
|
+
}
|
|
1123
|
+
if (field === "enabledCaps" || field === "supportedCaps") {
|
|
1124
|
+
const token = capToken(cleaned);
|
|
1125
|
+
if (!token) return;
|
|
1126
|
+
current[field] = current[field] ? `${current[field]}, ${token}` : token;
|
|
1127
|
+
current.openField = "";
|
|
1162
1128
|
return;
|
|
1163
1129
|
}
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1130
|
+
if (!current[field]) current[field] = cleaned;
|
|
1131
|
+
current.openField = field === "systemDescription" || field === "portDescription" ? field : "";
|
|
1132
|
+
};
|
|
1133
|
+
const lines = text.split("\n");
|
|
1134
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
1135
|
+
if (collected.overflow) break;
|
|
1136
|
+
const lineNo = index + 1;
|
|
1137
|
+
const original = lines[index] ?? "";
|
|
1138
|
+
const trimmed = original.trim();
|
|
1139
|
+
if (!trimmed) {
|
|
1140
|
+
const current = held.bag;
|
|
1141
|
+
if (current?.openField === "systemDescription" && !current.systemDescription && !current.skippedBlank) {
|
|
1142
|
+
current.skippedBlank = true;
|
|
1143
|
+
continue;
|
|
1144
|
+
}
|
|
1145
|
+
if (current) current.openField = "";
|
|
1146
|
+
continue;
|
|
1147
|
+
}
|
|
1148
|
+
if (/^[-=*_~]{5,}$/.test(trimmed)) {
|
|
1149
|
+
flush();
|
|
1150
|
+
continue;
|
|
1151
|
+
}
|
|
1152
|
+
const host = promptHost(trimmed);
|
|
1153
|
+
if (host) {
|
|
1154
|
+
flush();
|
|
1155
|
+
localDevice = host;
|
|
1156
|
+
collected.preferred.push(host);
|
|
1157
|
+
continue;
|
|
1158
|
+
}
|
|
1159
|
+
const detected = /^(?:LLDP\s+)?(?:Port|Interface)\s+(\S+)\s+detected\s+(\d+)\s+(?:LLDP\s+)?neighbors?:?\s*$/i.exec(trimmed);
|
|
1160
|
+
if (detected?.[1]) {
|
|
1161
|
+
flush();
|
|
1162
|
+
pendingLocal = detected[2] === "0" ? "" : detected[1];
|
|
1163
|
+
continue;
|
|
1169
1164
|
}
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
}
|
|
1165
|
+
const bracketed = /LLDP neighbor-information of port\s+\d+\s*\[([^\]]+)\]/i.exec(trimmed);
|
|
1166
|
+
const comware = bracketed ?? /LLDP neighbor-information of port\s+(\S+)/i.exec(trimmed);
|
|
1167
|
+
if (comware?.[1] && /neighbor-information of port/i.test(trimmed)) {
|
|
1168
|
+
flush();
|
|
1169
|
+
pendingLocal = (bracketed?.[1] ?? comware[1] ?? "").replace(/:\s*$/, "").trim();
|
|
1170
|
+
continue;
|
|
1177
1171
|
}
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
}
|
|
1190
|
-
let ok = true;
|
|
1191
|
-
const devices = [];
|
|
1192
|
-
value.forEach((item, index) => {
|
|
1193
|
-
const path5 = `/devices/${index}`;
|
|
1194
|
-
if (!isRecord2(item)) {
|
|
1195
|
-
issues.push({ path: path5, message: "must be an object." });
|
|
1196
|
-
ok = false;
|
|
1197
|
-
return;
|
|
1172
|
+
const neighborStart = /^Neighbor\s+(\S+)/i.exec(trimmed);
|
|
1173
|
+
if (neighborStart?.[1] && !/^Neighbor\s+(?:System|Port|Chassis|Device|Entries|Capabilities|Management)/i.test(trimmed)) {
|
|
1174
|
+
const open = held.bag;
|
|
1175
|
+
if (open && (open.chassisId || open.systemName || open.portId)) flush();
|
|
1176
|
+
const slash = /^([^/]+)\/([^,\s]+)/.exec(neighborStart[1]);
|
|
1177
|
+
if (slash?.[1] && slash[2]) {
|
|
1178
|
+
const hint = ensure(lineNo);
|
|
1179
|
+
if (!hint.chassisId) hint.chassisId = slash[1];
|
|
1180
|
+
if (!hint.portId) hint.portId = slash[2];
|
|
1181
|
+
}
|
|
1182
|
+
continue;
|
|
1198
1183
|
}
|
|
1199
|
-
|
|
1200
|
-
const
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1184
|
+
const labeled = splitLabel(trimmed);
|
|
1185
|
+
const pairs = parsePairs(trimmed);
|
|
1186
|
+
const knownPair = pairs.some((pair) => PAIR_FIELDS[pair.key]);
|
|
1187
|
+
if (knownPair) {
|
|
1188
|
+
for (const pair of pairs) {
|
|
1189
|
+
const field2 = PAIR_FIELDS[pair.key];
|
|
1190
|
+
if (field2) assign(field2, pair.value, lineNo);
|
|
1191
|
+
}
|
|
1192
|
+
continue;
|
|
1204
1193
|
}
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1194
|
+
if (!labeled) {
|
|
1195
|
+
const current = held.bag;
|
|
1196
|
+
if (current?.openField) {
|
|
1197
|
+
const field2 = current.openField;
|
|
1198
|
+
current[field2] = clipField(current[field2] ? `${current[field2]} ${trimmed}` : trimmed);
|
|
1199
|
+
}
|
|
1200
|
+
continue;
|
|
1209
1201
|
}
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
role: optionalString(item.role, `${path5}/role`, "role", issues),
|
|
1215
|
-
site: optionalString(item.site, `${path5}/site`, "site", issues),
|
|
1216
|
-
managementIp: optionalString(item.managementIp, `${path5}/managementIp`, "managementIp", issues),
|
|
1217
|
-
diagram
|
|
1218
|
-
});
|
|
1219
|
-
});
|
|
1220
|
-
return ok && !issues.some((issue) => issue.path.startsWith("/devices/")) ? devices : null;
|
|
1221
|
-
}
|
|
1222
|
-
function readInterfaces(value, issues) {
|
|
1223
|
-
if (!Array.isArray(value)) {
|
|
1224
|
-
issues.push({ path: "/interfaces", message: "interfaces must be an array." });
|
|
1225
|
-
return null;
|
|
1226
|
-
}
|
|
1227
|
-
const interfaces = [];
|
|
1228
|
-
let ok = true;
|
|
1229
|
-
value.forEach((item, index) => {
|
|
1230
|
-
const path5 = `/interfaces/${index}`;
|
|
1231
|
-
if (!isRecord2(item)) {
|
|
1232
|
-
issues.push({ path: path5, message: "must be an object." });
|
|
1233
|
-
ok = false;
|
|
1234
|
-
return;
|
|
1202
|
+
const norm = normLabel(labeled.label);
|
|
1203
|
+
if (norm === "local port id" || norm === "localportid") {
|
|
1204
|
+
if (!/^\d+$/.test(labeled.value)) assign("localPort", labeled.value, lineNo);
|
|
1205
|
+
continue;
|
|
1235
1206
|
}
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
const operStatus = statusOf(item.operStatus, `${path5}/operStatus`, "operStatus", issues);
|
|
1241
|
-
const ipv4 = item.ipv4 == null ? [] : readAddresses(item.ipv4, `${path5}/ipv4`, "ipv4", issues);
|
|
1242
|
-
const ipv6 = item.ipv6 == null ? [] : readAddresses(item.ipv6, `${path5}/ipv6`, "ipv6", issues);
|
|
1243
|
-
const mode = switchportOf(item.switchportMode, `${path5}/switchportMode`, issues);
|
|
1244
|
-
const accessVlan = item.accessVlan == null ? null : vlanId(item.accessVlan, `${path5}/accessVlan`, issues);
|
|
1245
|
-
const nativeVlan = item.nativeVlan == null ? null : vlanId(item.nativeVlan, `${path5}/nativeVlan`, issues);
|
|
1246
|
-
const trunkVlans = item.trunkVlans == null ? null : readVlanList(item.trunkVlans, `${path5}/trunkVlans`, issues);
|
|
1247
|
-
if (!device || !name || adminStatus == null || operStatus == null || !ipv4 || !ipv6 || mode == null) {
|
|
1248
|
-
ok = false;
|
|
1249
|
-
return;
|
|
1207
|
+
if (norm === "interface") {
|
|
1208
|
+
const local = labeled.value.split(",")[0]?.trim() ?? "";
|
|
1209
|
+
if (local) assign("localPort", local, lineNo);
|
|
1210
|
+
continue;
|
|
1250
1211
|
}
|
|
1251
|
-
if (
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
device,
|
|
1255
|
-
name,
|
|
1256
|
-
description: optionalString(item.description, `${path5}/description`, "description", issues),
|
|
1257
|
-
speed: optionalString(item.speed, `${path5}/speed`, "speed", issues),
|
|
1258
|
-
adminStatus,
|
|
1259
|
-
operStatus,
|
|
1260
|
-
ipv4,
|
|
1261
|
-
ipv6,
|
|
1262
|
-
vrf: optionalString(item.vrf, `${path5}/vrf`, "vrf", issues),
|
|
1263
|
-
switchportMode: mode,
|
|
1264
|
-
accessVlan,
|
|
1265
|
-
trunkVlans,
|
|
1266
|
-
nativeVlan,
|
|
1267
|
-
lag: optionalString(item.lag, `${path5}/lag`, "lag", issues)
|
|
1268
|
-
});
|
|
1269
|
-
});
|
|
1270
|
-
return ok ? interfaces : null;
|
|
1271
|
-
}
|
|
1272
|
-
function readAddresses(value, path5, family, issues) {
|
|
1273
|
-
if (!Array.isArray(value)) {
|
|
1274
|
-
issues.push({ path: path5, message: `${family} must be an array.` });
|
|
1275
|
-
return null;
|
|
1276
|
-
}
|
|
1277
|
-
const max = family === "ipv4" ? 32 : 128;
|
|
1278
|
-
const addresses = [];
|
|
1279
|
-
let ok = true;
|
|
1280
|
-
value.forEach((item, index) => {
|
|
1281
|
-
const itemPath = `${path5}/${index}`;
|
|
1282
|
-
if (!isRecord2(item)) {
|
|
1283
|
-
issues.push({ path: itemPath, message: "must be an object with address and prefix." });
|
|
1284
|
-
ok = false;
|
|
1285
|
-
return;
|
|
1212
|
+
if (norm === "port") {
|
|
1213
|
+
assign("localPort", labeled.value, lineNo);
|
|
1214
|
+
continue;
|
|
1286
1215
|
}
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
issues.push({ path: `${itemPath}/prefix`, message: `prefix must be an integer from 0 to ${max}.` });
|
|
1292
|
-
ok = false;
|
|
1216
|
+
if (norm === "capability") {
|
|
1217
|
+
const token = capToken(labeled.value);
|
|
1218
|
+
if (token) assign("enabledCaps", token, lineNo);
|
|
1219
|
+
continue;
|
|
1293
1220
|
}
|
|
1294
|
-
if (
|
|
1295
|
-
|
|
1296
|
-
|
|
1221
|
+
if ((norm === "enabled" || norm === "supported") && looksLikeCapability(labeled.value)) {
|
|
1222
|
+
assign(norm === "enabled" ? "enabledCaps" : "supportedCaps", labeled.value, lineNo);
|
|
1223
|
+
continue;
|
|
1297
1224
|
}
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
});
|
|
1303
|
-
ok = false;
|
|
1304
|
-
return;
|
|
1225
|
+
const field = lookupField(labeled.label);
|
|
1226
|
+
if (!field) {
|
|
1227
|
+
if (held.bag) held.bag.openField = "";
|
|
1228
|
+
continue;
|
|
1305
1229
|
}
|
|
1306
|
-
|
|
1307
|
-
});
|
|
1308
|
-
return ok ? addresses : null;
|
|
1309
|
-
}
|
|
1310
|
-
function readLinks(value, issues) {
|
|
1311
|
-
if (!Array.isArray(value)) {
|
|
1312
|
-
issues.push({ path: "/links", message: "links must be an array." });
|
|
1313
|
-
return null;
|
|
1230
|
+
assign(field, labeled.value, lineNo);
|
|
1314
1231
|
}
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1232
|
+
flush();
|
|
1233
|
+
return collected;
|
|
1234
|
+
}
|
|
1235
|
+
function parseLldpKeyvalue(text, options) {
|
|
1236
|
+
const collected = { raw: [], errors: [], warnings: [], preferred: [], overflow: false };
|
|
1237
|
+
let localDevice = cleanSentinel(options.localDevice ?? "");
|
|
1238
|
+
if (localDevice) collected.preferred.push(localDevice);
|
|
1239
|
+
let bag = null;
|
|
1240
|
+
const flush = () => {
|
|
1241
|
+
if (!bag) return;
|
|
1242
|
+
const current = bag;
|
|
1243
|
+
bag = null;
|
|
1244
|
+
if (!current.localPort) current.localPort = current.iface;
|
|
1245
|
+
bagToNeighbor(current, localDevice, "", collected);
|
|
1246
|
+
};
|
|
1247
|
+
const lines = text.split("\n");
|
|
1248
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
1249
|
+
if (collected.overflow) break;
|
|
1250
|
+
const lineNo = index + 1;
|
|
1251
|
+
const trimmed = (lines[index] ?? "").trim();
|
|
1252
|
+
if (!trimmed) continue;
|
|
1253
|
+
const host = promptHost(trimmed);
|
|
1254
|
+
if (host) {
|
|
1255
|
+
flush();
|
|
1256
|
+
localDevice = host;
|
|
1257
|
+
collected.preferred.push(host);
|
|
1258
|
+
continue;
|
|
1323
1259
|
}
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
const
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1260
|
+
const match = /^lldp\.([^.]+)\.(.+?)=(.*)$/.exec(trimmed);
|
|
1261
|
+
if (!match?.[1] || !match[2]) continue;
|
|
1262
|
+
const iface = match[1];
|
|
1263
|
+
const path = match[2];
|
|
1264
|
+
const value = match[3] ?? "";
|
|
1265
|
+
if (!bag || bag.iface !== iface) {
|
|
1266
|
+
flush();
|
|
1267
|
+
bag = { ...emptyBag2(lineNo), iface };
|
|
1330
1268
|
}
|
|
1331
|
-
const
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1269
|
+
const current = bag;
|
|
1270
|
+
const setOnce = (field, next) => {
|
|
1271
|
+
if (!next) return;
|
|
1272
|
+
if (REPEAT.has(field) && current[field] && current[field] !== next) {
|
|
1273
|
+
const savedIface = current.iface;
|
|
1274
|
+
flush();
|
|
1275
|
+
bag = { ...emptyBag2(lineNo), iface: savedIface };
|
|
1276
|
+
bag[field] = next;
|
|
1277
|
+
return;
|
|
1278
|
+
}
|
|
1279
|
+
if (!current[field]) current[field] = next;
|
|
1280
|
+
};
|
|
1281
|
+
if (path === "chassis.name") setOnce("systemName", value);
|
|
1282
|
+
else if (path === "chassis.descr" || path === "chassis.description") setOnce("systemDescription", value);
|
|
1283
|
+
else if (path === "chassis.mac") setOnce("chassisId", value);
|
|
1284
|
+
else if (path === "port.ifname") {
|
|
1285
|
+
if (current.portId && isMacAddress(current.portId)) current.portId = "";
|
|
1286
|
+
setOnce("portId", value);
|
|
1287
|
+
} else if ((path === "port.id" || path === "port.mac") && !current.portId) setOnce("portId", value);
|
|
1288
|
+
else if (path === "port.descr" || path === "port.description") setOnce("portDescription", value);
|
|
1289
|
+
else if (/\.enabled$/.test(path)) {
|
|
1290
|
+
const name = path.split(".").slice(-2, -1)[0] ?? "";
|
|
1291
|
+
if (/^(?:on|true|yes|1)$/i.test(value) && name) {
|
|
1292
|
+
current.enabledCaps = current.enabledCaps ? `${current.enabledCaps}, ${name}` : name;
|
|
1293
|
+
}
|
|
1335
1294
|
}
|
|
1336
|
-
links.push({
|
|
1337
|
-
a,
|
|
1338
|
-
b,
|
|
1339
|
-
label: optionalString(item.label, `${path5}/label`, "label", issues),
|
|
1340
|
-
inferred
|
|
1341
|
-
});
|
|
1342
|
-
});
|
|
1343
|
-
return ok ? links : null;
|
|
1344
|
-
}
|
|
1345
|
-
function readEndpoint(value, path5, issues) {
|
|
1346
|
-
if (!isRecord2(value)) {
|
|
1347
|
-
issues.push({ path: path5, message: "must be an object with device and interface." });
|
|
1348
|
-
return null;
|
|
1349
1295
|
}
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
const iface = requiredString(value.interface, `${path5}/interface`, "interface", issues);
|
|
1353
|
-
if (!device || !iface) return null;
|
|
1354
|
-
return { device, interface: iface };
|
|
1296
|
+
flush();
|
|
1297
|
+
return collected;
|
|
1355
1298
|
}
|
|
1356
|
-
function
|
|
1357
|
-
|
|
1358
|
-
issues.push({ path: path5, message: "trunkVlans must be an array of VLAN ids." });
|
|
1359
|
-
return null;
|
|
1360
|
-
}
|
|
1361
|
-
const ids = [];
|
|
1362
|
-
let ok = true;
|
|
1363
|
-
value.forEach((item, index) => {
|
|
1364
|
-
const id = vlanId(item, `${path5}/${index}`, issues);
|
|
1365
|
-
if (id == null) ok = false;
|
|
1366
|
-
else ids.push(id);
|
|
1367
|
-
});
|
|
1368
|
-
return ok ? ids : null;
|
|
1299
|
+
function isRecord2(value) {
|
|
1300
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
1369
1301
|
}
|
|
1370
|
-
function
|
|
1371
|
-
if (
|
|
1372
|
-
|
|
1373
|
-
|
|
1302
|
+
function textOf(value) {
|
|
1303
|
+
if (typeof value === "string") return value.trim();
|
|
1304
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
1305
|
+
if (Array.isArray(value)) return value.map((item) => textOf(item)).filter(Boolean).join(", ");
|
|
1306
|
+
if (isRecord2(value)) {
|
|
1307
|
+
if (typeof value.value === "string" || typeof value.value === "number") return String(value.value).trim();
|
|
1374
1308
|
}
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
value
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
rejectUnknown(item, VLAN_FIELDS, path5, issues);
|
|
1386
|
-
const id = vlanId(item.id, `${path5}/id`, issues);
|
|
1387
|
-
if (id == null) {
|
|
1388
|
-
ok = false;
|
|
1389
|
-
return;
|
|
1309
|
+
return "";
|
|
1310
|
+
}
|
|
1311
|
+
function capabilityFromJson(value) {
|
|
1312
|
+
if (!value) return "";
|
|
1313
|
+
const items = Array.isArray(value) ? value : [value];
|
|
1314
|
+
const enabled = [];
|
|
1315
|
+
for (const item of items) {
|
|
1316
|
+
if (typeof item === "string") {
|
|
1317
|
+
enabled.push(item);
|
|
1318
|
+
continue;
|
|
1390
1319
|
}
|
|
1391
|
-
if (
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1320
|
+
if (!isRecord2(item)) continue;
|
|
1321
|
+
const type = textOf(item.type ?? item.name);
|
|
1322
|
+
const flag = item.enabled;
|
|
1323
|
+
if (flag === false || flag === "off" || flag === "false") continue;
|
|
1324
|
+
if (type) enabled.push(type);
|
|
1325
|
+
}
|
|
1326
|
+
return enabled.join(", ");
|
|
1396
1327
|
}
|
|
1397
|
-
function
|
|
1398
|
-
if (
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1328
|
+
function chassisFromJson(chassis) {
|
|
1329
|
+
if (!isRecord2(chassis)) return [];
|
|
1330
|
+
const known = /* @__PURE__ */ new Set(["id", "name", "descr", "description", "mgmt-ip", "mgmt_ip", "capability", "capabilities"]);
|
|
1331
|
+
const direct = textOf(chassis.name) || chassis.id || chassis.descr || chassis.description;
|
|
1332
|
+
if (direct) {
|
|
1333
|
+
return [
|
|
1334
|
+
{
|
|
1335
|
+
name: textOf(chassis.name),
|
|
1336
|
+
id: textOf(chassis.id),
|
|
1337
|
+
descr: textOf(chassis.descr ?? chassis.description),
|
|
1338
|
+
caps: capabilityFromJson(chassis.capability ?? chassis.capabilities)
|
|
1339
|
+
}
|
|
1340
|
+
];
|
|
1402
1341
|
}
|
|
1403
|
-
|
|
1342
|
+
const hosts = Object.keys(chassis).filter((key) => !known.has(key) && isRecord2(chassis[key]));
|
|
1343
|
+
if (hosts.length === 0) return [];
|
|
1344
|
+
return hosts.map((host) => {
|
|
1345
|
+
const inner = chassis[host];
|
|
1346
|
+
return {
|
|
1347
|
+
name: host,
|
|
1348
|
+
id: textOf(inner.id),
|
|
1349
|
+
descr: textOf(inner.descr ?? inner.description),
|
|
1350
|
+
caps: capabilityFromJson(inner.capability ?? inner.capabilities)
|
|
1351
|
+
};
|
|
1352
|
+
});
|
|
1353
|
+
}
|
|
1354
|
+
function portFromJson(port) {
|
|
1355
|
+
if (!isRecord2(port)) return { id: "", descr: "" };
|
|
1404
1356
|
return {
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
localDevice: optionalString(value.localDevice, "/neighbors/localDevice", "localDevice", issues)
|
|
1357
|
+
id: textOf(port.id ?? port.ifname ?? port.name),
|
|
1358
|
+
descr: textOf(port.descr ?? port.description)
|
|
1408
1359
|
};
|
|
1409
1360
|
}
|
|
1410
|
-
function
|
|
1411
|
-
if (
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
}
|
|
1416
|
-
|
|
1417
|
-
if (value == null || value === "") return "";
|
|
1418
|
-
if (value === "access" || value === "trunk") return value;
|
|
1419
|
-
issues.push({ path: path5, message: "switchportMode must be access or trunk." });
|
|
1420
|
-
return null;
|
|
1421
|
-
}
|
|
1422
|
-
function vlanId(value, path5, issues) {
|
|
1423
|
-
if (typeof value === "number" && Number.isInteger(value) && value >= 1 && value <= 4094) return value;
|
|
1424
|
-
issues.push({ path: path5, message: "must be an integer from 1 to 4094." });
|
|
1425
|
-
return null;
|
|
1426
|
-
}
|
|
1427
|
-
function requiredString(value, path5, label, issues) {
|
|
1428
|
-
if (typeof value !== "string" || !value.trim()) {
|
|
1429
|
-
issues.push({ path: path5, message: `${label} is required.` });
|
|
1430
|
-
return "";
|
|
1361
|
+
function interfaceRecords(node) {
|
|
1362
|
+
if (Array.isArray(node)) {
|
|
1363
|
+
return node.flatMap((item, index) => {
|
|
1364
|
+
if (!isRecord2(item)) return [];
|
|
1365
|
+
const name = textOf(item.name) || `if${index + 1}`;
|
|
1366
|
+
return [{ name, value: item }];
|
|
1367
|
+
});
|
|
1431
1368
|
}
|
|
1432
|
-
|
|
1369
|
+
if (!isRecord2(node)) return [];
|
|
1370
|
+
if (node.chassis || node.port || node.name) {
|
|
1371
|
+
return [{ name: textOf(node.name) || "interface", value: node }];
|
|
1372
|
+
}
|
|
1373
|
+
return Object.entries(node).flatMap(([name, value]) => isRecord2(value) ? [{ name, value }] : []);
|
|
1433
1374
|
}
|
|
1434
|
-
function
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1375
|
+
function parseLldpJson(text, options) {
|
|
1376
|
+
const collected = { raw: [], errors: [], warnings: [], preferred: [], overflow: false };
|
|
1377
|
+
const localDevice = cleanSentinel(options.localDevice ?? "");
|
|
1378
|
+
if (localDevice) collected.preferred.push(localDevice);
|
|
1379
|
+
let parsed;
|
|
1380
|
+
try {
|
|
1381
|
+
parsed = JSON.parse(text);
|
|
1382
|
+
} catch {
|
|
1383
|
+
collected.errors.push({ row: 1, message: "This LLDP JSON could not be parsed." });
|
|
1384
|
+
return collected;
|
|
1439
1385
|
}
|
|
1440
|
-
const
|
|
1441
|
-
|
|
1442
|
-
if (
|
|
1443
|
-
|
|
1444
|
-
return
|
|
1386
|
+
const root = isRecord2(parsed) && parsed.lldp ? parsed.lldp : parsed;
|
|
1387
|
+
const interfaces = isRecord2(root) ? interfaceRecords(root.interface ?? root.interfaces ?? root) : [];
|
|
1388
|
+
if (interfaces.length === 0) {
|
|
1389
|
+
collected.errors.push({ row: 1, message: "This LLDP JSON has no interface neighbors." });
|
|
1390
|
+
return collected;
|
|
1445
1391
|
}
|
|
1446
|
-
const
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1392
|
+
for (const item of interfaces) {
|
|
1393
|
+
const port = portFromJson(item.value.port);
|
|
1394
|
+
const chassisList = chassisFromJson(item.value.chassis);
|
|
1395
|
+
const bodies = chassisList.length > 0 ? chassisList : [{ name: "", id: "", descr: "", caps: "" }];
|
|
1396
|
+
for (const chassis of bodies) {
|
|
1397
|
+
const bag = emptyBag2(1);
|
|
1398
|
+
bag.localPort = item.name;
|
|
1399
|
+
bag.systemName = chassis.name;
|
|
1400
|
+
bag.chassisId = chassis.id;
|
|
1401
|
+
bag.systemDescription = chassis.descr;
|
|
1402
|
+
bag.enabledCaps = chassis.caps;
|
|
1403
|
+
bag.portId = port.id;
|
|
1404
|
+
bag.portDescription = port.descr;
|
|
1405
|
+
bagToNeighbor(bag, localDevice, "", collected);
|
|
1406
|
+
if (collected.overflow) break;
|
|
1407
|
+
}
|
|
1450
1408
|
}
|
|
1451
|
-
return
|
|
1409
|
+
return collected;
|
|
1452
1410
|
}
|
|
1453
|
-
function
|
|
1454
|
-
|
|
1455
|
-
if (
|
|
1456
|
-
|
|
1457
|
-
return null;
|
|
1411
|
+
function looksLikeLldpJson(text) {
|
|
1412
|
+
const trimmed = text.trim();
|
|
1413
|
+
if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) return false;
|
|
1414
|
+
return /"lldp"/i.test(trimmed) || /"chassis"/i.test(trimmed) && /"port"/i.test(trimmed);
|
|
1458
1415
|
}
|
|
1459
|
-
function
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1416
|
+
function looksLikeKeyvalue(text) {
|
|
1417
|
+
return /^lldp\./m.test(text);
|
|
1418
|
+
}
|
|
1419
|
+
function emptyMessage(text) {
|
|
1420
|
+
if (/local interface\s+.*system name/i.test(text) && !/chassis id\s*:/i.test(text) && !/system name\s*:/i.test(text)) {
|
|
1421
|
+
return "This is the short LLDP neighbor table. Paste the detail output, or lldpctl on Linux.";
|
|
1464
1422
|
}
|
|
1465
|
-
return
|
|
1423
|
+
return "No LLDP neighbors were found. Paste LLDP neighbor detail, or lldpctl output.";
|
|
1466
1424
|
}
|
|
1467
|
-
function
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1425
|
+
function parseLldpNeighbors(text, options = {}) {
|
|
1426
|
+
const cleaned = cleanCli(text);
|
|
1427
|
+
if (looksLikeLldpJson(cleaned)) {
|
|
1428
|
+
return finish(parseLldpJson(cleaned, options), emptyMessage(cleaned));
|
|
1471
1429
|
}
|
|
1430
|
+
if (looksLikeKeyvalue(cleaned)) {
|
|
1431
|
+
return finish(parseLldpKeyvalue(cleaned, options), emptyMessage(cleaned));
|
|
1432
|
+
}
|
|
1433
|
+
return finish(parseLldpText(cleaned, options), emptyMessage(cleaned));
|
|
1472
1434
|
}
|
|
1473
|
-
|
|
1474
|
-
|
|
1435
|
+
var lldpParser = {
|
|
1436
|
+
id: "lldp",
|
|
1437
|
+
kind: "lldp",
|
|
1438
|
+
label: "LLDP neighbors (any vendor)",
|
|
1439
|
+
parse: parseLldpNeighbors
|
|
1440
|
+
};
|
|
1441
|
+
|
|
1442
|
+
// server/src/neighbors/registry.ts
|
|
1443
|
+
var parsers = [];
|
|
1444
|
+
function registerNeighborParser(parser) {
|
|
1445
|
+
const index = parsers.findIndex((item) => item.id === parser.id);
|
|
1446
|
+
if (index >= 0) parsers.splice(index, 1);
|
|
1447
|
+
parsers.push(parser);
|
|
1475
1448
|
}
|
|
1476
1449
|
|
|
1450
|
+
// server/src/neighbors/index.ts
|
|
1451
|
+
registerNeighborParser(ciscoCdpParser);
|
|
1452
|
+
registerNeighborParser(lldpParser);
|
|
1453
|
+
|
|
1477
1454
|
// server/src/diagramData.ts
|
|
1478
1455
|
var DATA_LIMITS = {
|
|
1479
1456
|
maxChars: 512 * 1024,
|
|
@@ -1485,6 +1462,30 @@ var DATA_LIMITS = {
|
|
|
1485
1462
|
maxCellChars: 2e3,
|
|
1486
1463
|
maxTitleChars: 200
|
|
1487
1464
|
};
|
|
1465
|
+
var DATA_FIELDS = [
|
|
1466
|
+
"localDevice",
|
|
1467
|
+
"localPort",
|
|
1468
|
+
"remoteDevice",
|
|
1469
|
+
"remotePort",
|
|
1470
|
+
"localGroup",
|
|
1471
|
+
"remoteGroup",
|
|
1472
|
+
"localType",
|
|
1473
|
+
"remoteType",
|
|
1474
|
+
"localClass",
|
|
1475
|
+
"remoteClass",
|
|
1476
|
+
"localDiagram",
|
|
1477
|
+
"remoteDiagram",
|
|
1478
|
+
"id",
|
|
1479
|
+
"label",
|
|
1480
|
+
"group",
|
|
1481
|
+
"type",
|
|
1482
|
+
"styleClass",
|
|
1483
|
+
"diagram",
|
|
1484
|
+
"source",
|
|
1485
|
+
"target"
|
|
1486
|
+
];
|
|
1487
|
+
var DIRECTIONS = /* @__PURE__ */ new Set(["TD", "TB", "LR", "RL", "BT"]);
|
|
1488
|
+
var SHAPES = /* @__PURE__ */ new Set(["links", "nodes", "edges", "graph"]);
|
|
1488
1489
|
var SHAPE_FIELDS = {
|
|
1489
1490
|
links: [
|
|
1490
1491
|
{ field: "localDevice", label: "Local device", required: true },
|
|
@@ -1526,430 +1527,430 @@ var EDGE_FIELDS = SHAPE_FIELDS.edges;
|
|
|
1526
1527
|
function dataFail(status, message, code, details) {
|
|
1527
1528
|
return Object.assign(new Error(message), { status, code, details });
|
|
1528
1529
|
}
|
|
1529
|
-
|
|
1530
|
-
// server/src/diagramSightings.ts
|
|
1531
|
-
import path from "node:path";
|
|
1532
|
-
var FILE_NAME = "diagram-sightings.json";
|
|
1533
|
-
var chain = Promise.resolve();
|
|
1534
|
-
var depth = 0;
|
|
1535
|
-
var suppress = 0;
|
|
1536
|
-
function sightingsPath() {
|
|
1537
|
-
return path.join(getMetaPath(), FILE_NAME);
|
|
1538
|
-
}
|
|
1539
|
-
function emptyCatalog() {
|
|
1540
|
-
return { version: 1, updatedAt: 0, files: [], pending: [] };
|
|
1541
|
-
}
|
|
1542
|
-
function isPending(value) {
|
|
1543
|
-
if (!value || typeof value !== "object") return false;
|
|
1544
|
-
const item = value;
|
|
1545
|
-
return typeof item.from === "string" && (item.to === null || typeof item.to === "string") && (item.match === "content" || item.match === "basename" || item.match === "ambiguous") && Array.isArray(item.candidates) && item.candidates.every((candidate) => typeof candidate === "string") && typeof item.noticedAt === "number";
|
|
1546
|
-
}
|
|
1547
|
-
function isSighting(value) {
|
|
1548
|
-
if (!value || typeof value !== "object") return false;
|
|
1549
|
-
const item = value;
|
|
1550
|
-
return typeof item.path === "string" && typeof item.hash === "string" && typeof item.seenAt === "number" && (item.missingAt == null || typeof item.missingAt === "number");
|
|
1551
|
-
}
|
|
1552
|
-
function normalizeCatalog(value) {
|
|
1553
|
-
if (!value || typeof value !== "object") return null;
|
|
1554
|
-
const record = value;
|
|
1555
|
-
if (record.version !== 1 || !Array.isArray(record.files) || !Array.isArray(record.pending)) return null;
|
|
1556
|
-
if (typeof record.updatedAt !== "number") return null;
|
|
1557
|
-
return {
|
|
1558
|
-
version: 1,
|
|
1559
|
-
updatedAt: record.updatedAt,
|
|
1560
|
-
files: record.files.filter(isSighting).map((file) => ({
|
|
1561
|
-
path: normalizeRelative(file.path),
|
|
1562
|
-
hash: file.hash,
|
|
1563
|
-
seenAt: file.seenAt,
|
|
1564
|
-
...file.missingAt != null ? { missingAt: file.missingAt } : {}
|
|
1565
|
-
})),
|
|
1566
|
-
pending: record.pending.filter(isPending).map((item) => ({
|
|
1567
|
-
from: normalizeRelative(item.from),
|
|
1568
|
-
to: item.to ? normalizeRelative(item.to) : null,
|
|
1569
|
-
match: item.match,
|
|
1570
|
-
candidates: item.candidates.map((candidate) => normalizeRelative(candidate)).filter(Boolean),
|
|
1571
|
-
noticedAt: item.noticedAt
|
|
1572
|
-
}))
|
|
1573
|
-
};
|
|
1574
|
-
}
|
|
1575
|
-
async function load() {
|
|
1530
|
+
function readDataSource(source) {
|
|
1576
1531
|
try {
|
|
1577
|
-
|
|
1532
|
+
const loaded = loadYaml(source);
|
|
1533
|
+
if ("error" in loaded) return null;
|
|
1534
|
+
const raw = loaded.doc.toJS();
|
|
1535
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
|
|
1536
|
+
const nerdgram = raw.nerdgram;
|
|
1537
|
+
if (!nerdgram || typeof nerdgram !== "object" || Array.isArray(nerdgram)) return null;
|
|
1538
|
+
const block = nerdgram.source;
|
|
1539
|
+
if (!block || typeof block !== "object" || Array.isArray(block)) return null;
|
|
1540
|
+
return configFromPlain(block);
|
|
1578
1541
|
} catch {
|
|
1579
|
-
return
|
|
1580
|
-
}
|
|
1581
|
-
}
|
|
1582
|
-
async function save(catalog) {
|
|
1583
|
-
const files = [...catalog.files].sort((a, b) => a.path.localeCompare(b.path));
|
|
1584
|
-
const pending = [...catalog.pending].sort((a, b) => a.from.localeCompare(b.from));
|
|
1585
|
-
await atomicWriteJson(sightingsPath(), { ...catalog, files, pending });
|
|
1586
|
-
}
|
|
1587
|
-
function exclusive(fn) {
|
|
1588
|
-
if (depth > 0) return fn();
|
|
1589
|
-
const run = chain.then(async () => {
|
|
1590
|
-
depth += 1;
|
|
1591
|
-
try {
|
|
1592
|
-
return await fn();
|
|
1593
|
-
} finally {
|
|
1594
|
-
depth -= 1;
|
|
1595
|
-
}
|
|
1596
|
-
});
|
|
1597
|
-
chain = run.then(
|
|
1598
|
-
() => void 0,
|
|
1599
|
-
() => void 0
|
|
1600
|
-
);
|
|
1601
|
-
return run;
|
|
1602
|
-
}
|
|
1603
|
-
async function noteDiagramSighting(snapshot) {
|
|
1604
|
-
if (suppress > 0) return;
|
|
1605
|
-
const filePath = normalizeRelative(snapshot.path);
|
|
1606
|
-
if (!filePath || !snapshot.hash) return;
|
|
1607
|
-
await exclusive(async () => {
|
|
1608
|
-
if (suppress > 0) return;
|
|
1609
|
-
const now = Date.now();
|
|
1610
|
-
const catalog = await load();
|
|
1611
|
-
const files = catalog.files.filter((file) => file.path !== filePath && file.missingAt == null);
|
|
1612
|
-
files.push({ path: filePath, hash: snapshot.hash, seenAt: now });
|
|
1613
|
-
for (const old of catalog.files) {
|
|
1614
|
-
if (old.missingAt == null || old.path === filePath) continue;
|
|
1615
|
-
if (!catalog.pending.some((item) => item.from === old.path)) continue;
|
|
1616
|
-
files.push(old);
|
|
1617
|
-
}
|
|
1618
|
-
const pending = catalog.pending.filter((item) => item.from !== filePath);
|
|
1619
|
-
const updatedAt = catalog.updatedAt === 0 ? now : catalog.updatedAt;
|
|
1620
|
-
await save({ version: 1, updatedAt, files, pending });
|
|
1621
|
-
});
|
|
1622
|
-
}
|
|
1623
|
-
|
|
1624
|
-
// server/src/history.ts
|
|
1625
|
-
import fs from "node:fs/promises";
|
|
1626
|
-
import path2 from "node:path";
|
|
1627
|
-
function versionDirFor(relativePath) {
|
|
1628
|
-
return path2.join(getVersionsRoot(), safeStorageKey(relativePath));
|
|
1542
|
+
return null;
|
|
1543
|
+
}
|
|
1629
1544
|
}
|
|
1630
|
-
function
|
|
1631
|
-
|
|
1545
|
+
function isSafeDataPath(relativePath) {
|
|
1546
|
+
if (typeof relativePath !== "string" || !relativePath || relativePath.includes("\0")) return false;
|
|
1547
|
+
const normalized = relativePath.replace(/\\/g, "/").replace(/^\/+|\/+$/g, "");
|
|
1548
|
+
if (!normalized) return false;
|
|
1549
|
+
const parts = normalized.split("/");
|
|
1550
|
+
if (parts.some((part) => part.length === 0 || part === "." || part === ".." || part.startsWith("."))) return false;
|
|
1551
|
+
const base = normalized.split("/").pop() ?? "";
|
|
1552
|
+
return isDataFileName(base);
|
|
1632
1553
|
}
|
|
1633
|
-
|
|
1634
|
-
const
|
|
1635
|
-
if (!
|
|
1636
|
-
|
|
1554
|
+
function configFromPlain(block) {
|
|
1555
|
+
const file = typeof block.file === "string" ? block.file : "";
|
|
1556
|
+
if (!isSafeDataPath(file)) return null;
|
|
1557
|
+
if (block.format !== "csv" && block.format !== "json" && block.format !== "cdp" && block.format !== "lldp" && block.format !== "network") {
|
|
1558
|
+
return null;
|
|
1637
1559
|
}
|
|
1638
|
-
return
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1560
|
+
if (typeof block.shape !== "string" || !SHAPES.has(block.shape)) return null;
|
|
1561
|
+
const direction = typeof block.direction === "string" ? block.direction.toUpperCase() : "TD";
|
|
1562
|
+
if (!DIRECTIONS.has(direction)) return null;
|
|
1563
|
+
const mapping = mappingFromPlain(block.mapping);
|
|
1564
|
+
if (!mapping) return null;
|
|
1565
|
+
const config = {
|
|
1566
|
+
file,
|
|
1567
|
+
format: block.format,
|
|
1568
|
+
shape: block.shape,
|
|
1569
|
+
mapping,
|
|
1570
|
+
direction,
|
|
1571
|
+
grouping: block.grouping !== false,
|
|
1572
|
+
diagramType: "flowchart"
|
|
1644
1573
|
};
|
|
1574
|
+
if (typeof block.edgesFile === "string" && block.edgesFile) {
|
|
1575
|
+
if (!isSafeDataPath(block.edgesFile)) return null;
|
|
1576
|
+
config.edgesFile = block.edgesFile;
|
|
1577
|
+
}
|
|
1578
|
+
if (block.edgesFormat === "csv" || block.edgesFormat === "json") config.edgesFormat = block.edgesFormat;
|
|
1579
|
+
const edgesMapping = mappingFromPlain(block.edgesMapping);
|
|
1580
|
+
if (block.edgesMapping != null && !edgesMapping) return null;
|
|
1581
|
+
if (edgesMapping && Object.keys(edgesMapping).length > 0) config.edgesMapping = edgesMapping;
|
|
1582
|
+
if (block.format === "network") {
|
|
1583
|
+
if (block.view !== "l3" && block.view !== "l2") return null;
|
|
1584
|
+
config.view = block.view;
|
|
1585
|
+
config.includeDown = block.includeDown === true;
|
|
1586
|
+
if (block.vlan != null && block.vlan !== "") {
|
|
1587
|
+
if (typeof block.vlan !== "number" || !Number.isInteger(block.vlan) || block.vlan < 1 || block.vlan > 4094) return null;
|
|
1588
|
+
config.vlan = block.vlan;
|
|
1589
|
+
}
|
|
1590
|
+
if (block.groupBy != null && block.groupBy !== "") {
|
|
1591
|
+
if (block.groupBy !== "vrf" && block.groupBy !== "site" && block.groupBy !== "role" && block.groupBy !== "platform") {
|
|
1592
|
+
return null;
|
|
1593
|
+
}
|
|
1594
|
+
config.groupBy = block.groupBy;
|
|
1595
|
+
}
|
|
1596
|
+
} else if (block.groupBy != null && block.groupBy !== "") {
|
|
1597
|
+
if (block.groupBy !== "platform" && block.groupBy !== "capability") return null;
|
|
1598
|
+
config.groupBy = block.groupBy;
|
|
1599
|
+
}
|
|
1600
|
+
if (typeof block.localDevice === "string" && block.localDevice.trim()) {
|
|
1601
|
+
config.localDevice = block.localDevice.trim();
|
|
1602
|
+
}
|
|
1603
|
+
return config;
|
|
1645
1604
|
}
|
|
1646
|
-
function
|
|
1647
|
-
if (
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1605
|
+
function mappingFromPlain(value) {
|
|
1606
|
+
if (value == null) return {};
|
|
1607
|
+
if (!isRecord3(value)) return null;
|
|
1608
|
+
const mapping = {};
|
|
1609
|
+
for (const field of DATA_FIELDS) {
|
|
1610
|
+
const column = value[field];
|
|
1611
|
+
if (column == null || column === "") continue;
|
|
1612
|
+
if (typeof column !== "string") return null;
|
|
1613
|
+
mapping[field] = column;
|
|
1651
1614
|
}
|
|
1652
|
-
|
|
1653
|
-
id: revision.id,
|
|
1654
|
-
createdAt: revision.createdAt,
|
|
1655
|
-
mtimeMs: revision.mtimeMs,
|
|
1656
|
-
size: revision.size,
|
|
1657
|
-
hash: revision.hash
|
|
1658
|
-
};
|
|
1659
|
-
if (typeof revision.name === "string" && revision.name.trim()) meta.name = revision.name;
|
|
1660
|
-
if (typeof revision.note === "string" && revision.note.trim()) meta.note = revision.note;
|
|
1661
|
-
if (typeof revision.namedAt === "string" && revision.namedAt.trim()) meta.namedAt = revision.namedAt;
|
|
1662
|
-
return meta;
|
|
1663
|
-
}
|
|
1664
|
-
function isNamedRevision(revision) {
|
|
1665
|
-
return typeof revision.name === "string" && revision.name.trim().length > 0;
|
|
1666
|
-
}
|
|
1667
|
-
async function saveIndex(dir, index) {
|
|
1668
|
-
await atomicWriteJson(indexPath(dir), index);
|
|
1669
|
-
}
|
|
1670
|
-
async function prune(dir, index) {
|
|
1671
|
-
const named = index.revisions.filter((revision) => isNamedRevision(revision));
|
|
1672
|
-
const unnamed = index.revisions.filter((revision) => !isNamedRevision(revision));
|
|
1673
|
-
unnamed.sort((a, b) => a.createdAt.localeCompare(b.createdAt));
|
|
1674
|
-
while (unnamed.length > REVISION_RETENTION) {
|
|
1675
|
-
const dropped = unnamed.shift();
|
|
1676
|
-
if (!dropped) break;
|
|
1677
|
-
await fs.rm(path2.join(dir, `${dropped.id}.mmd`), { force: true });
|
|
1678
|
-
}
|
|
1679
|
-
index.revisions = [...named, ...unnamed];
|
|
1680
|
-
}
|
|
1681
|
-
async function snapshotPrevious(relativePath, previous) {
|
|
1682
|
-
const normalized = normalizeRelative(relativePath);
|
|
1683
|
-
const dir = versionDirFor(normalized);
|
|
1684
|
-
await fs.mkdir(dir, { recursive: true });
|
|
1685
|
-
const index = await loadIndex(dir, normalized);
|
|
1686
|
-
const id = createId();
|
|
1687
|
-
await atomicWriteText(path2.join(dir, `${id}.mmd`), previous.content);
|
|
1688
|
-
index.path = normalized;
|
|
1689
|
-
index.revisions.push({
|
|
1690
|
-
id,
|
|
1691
|
-
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1692
|
-
mtimeMs: previous.mtimeMs,
|
|
1693
|
-
size: previous.size,
|
|
1694
|
-
hash: previous.hash
|
|
1695
|
-
});
|
|
1696
|
-
await prune(dir, index);
|
|
1697
|
-
await saveIndex(dir, index);
|
|
1698
|
-
return id;
|
|
1615
|
+
return mapping;
|
|
1699
1616
|
}
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
import path3 from "node:path";
|
|
1703
|
-
var SAME_INSTANCE_MESSAGE = "This diagram is open in another window.";
|
|
1704
|
-
function lockStorageKey(relativePath) {
|
|
1705
|
-
return safeStorageKey(relativePath, ".json");
|
|
1617
|
+
function isRecord3(value) {
|
|
1618
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
1706
1619
|
}
|
|
1707
|
-
function
|
|
1708
|
-
|
|
1620
|
+
function yamlText(match) {
|
|
1621
|
+
const normalized = match.body.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
1622
|
+
if (!match.indent) return normalized;
|
|
1623
|
+
return normalized.split("\n").map((line) => line.startsWith(match.indent) ? line.slice(match.indent.length) : line).join("\n");
|
|
1624
|
+
}
|
|
1625
|
+
function loadYaml(source) {
|
|
1626
|
+
const bom = source.startsWith("\uFEFF") ? "\uFEFF" : "";
|
|
1627
|
+
const text = bom ? source.slice(1) : source;
|
|
1628
|
+
const match = matchFrontMatter(text);
|
|
1629
|
+
if (!match) return { doc: new Document({}), body: text, bom };
|
|
1630
|
+
const doc = parseDocument(yamlText(match), { schema: "core", prettyErrors: false });
|
|
1631
|
+
if (doc.errors.length > 0) return { error: "The YAML header could not be parsed." };
|
|
1632
|
+
if (!isMap(doc.contents)) return { error: "Front matter must be a YAML mapping." };
|
|
1633
|
+
return { doc, body: text.slice(match.length), bom };
|
|
1709
1634
|
}
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1635
|
+
|
|
1636
|
+
// server/src/exporters/limits.ts
|
|
1637
|
+
var EXPORT_MAX_INPUT_BYTES = DATA_LIMITS.maxChars;
|
|
1638
|
+
var EXPORT_MAX_DEVICES = DATA_LIMITS.maxNodes;
|
|
1639
|
+
var EXPORT_MAX_LINKS = DATA_LIMITS.maxEdges;
|
|
1640
|
+
function exporterFail(status, message, code = "invalid") {
|
|
1641
|
+
return Object.assign(new Error(message), { status, code });
|
|
1714
1642
|
}
|
|
1715
|
-
function
|
|
1716
|
-
if (!
|
|
1717
|
-
|
|
1718
|
-
|
|
1643
|
+
function assertExportInputSize(bytes) {
|
|
1644
|
+
if (!Number.isFinite(bytes) || bytes < 0) {
|
|
1645
|
+
throw exporterFail(400, "The network document size is not valid.");
|
|
1646
|
+
}
|
|
1647
|
+
if (bytes > EXPORT_MAX_INPUT_BYTES) {
|
|
1648
|
+
throw exporterFail(413, "The network document is larger than 512 KB.", "too_large");
|
|
1649
|
+
}
|
|
1719
1650
|
}
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
...hostname ? { hostname } : {}
|
|
1734
|
-
};
|
|
1651
|
+
|
|
1652
|
+
// server/src/exporters/helpers.ts
|
|
1653
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
1654
|
+
import { stringify as stringifyYaml, parse as parseYaml } from "yaml";
|
|
1655
|
+
var CHANGE_ME = "CHANGE_ME";
|
|
1656
|
+
var HEADER_LINES = [
|
|
1657
|
+
"Generated by NerdGram from this diagram's network data.",
|
|
1658
|
+
"Credentials, images, and other values the diagram does not know are placeholders.",
|
|
1659
|
+
"Missing facts were omitted or marked. NerdGram did not connect to a device.",
|
|
1660
|
+
"Replace every CHANGE_ME before you use this file. Do not commit a real secret."
|
|
1661
|
+
];
|
|
1662
|
+
function isRecord4(value) {
|
|
1663
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
1735
1664
|
}
|
|
1736
|
-
function
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
const legacy = !optionalText(record.instanceId);
|
|
1740
|
-
const sameInstance2 = Boolean(instanceId) && record.instanceId === instanceId;
|
|
1741
|
-
if ((sameInstance2 || legacy) && record.pid !== process.pid && !isProcessAlive(record.pid)) return true;
|
|
1742
|
-
return false;
|
|
1743
|
-
}
|
|
1744
|
-
function sameInstance(record, identity) {
|
|
1745
|
-
return !optionalText(record.instanceId) || record.instanceId === identity.instanceId;
|
|
1746
|
-
}
|
|
1747
|
-
function diagramLabel(relativePath) {
|
|
1748
|
-
const base = relativePath.split("/").pop() || relativePath;
|
|
1749
|
-
return base.replace(/\.mmd$/i, "") || base;
|
|
1750
|
-
}
|
|
1751
|
-
function formatLockSince(iso) {
|
|
1752
|
-
const date = new Date(iso);
|
|
1753
|
-
if (Number.isNaN(date.getTime())) return iso;
|
|
1754
|
-
return new Intl.DateTimeFormat("en-US", { hour: "numeric", minute: "2-digit" }).format(date);
|
|
1755
|
-
}
|
|
1756
|
-
function lockedMessage(record, identity) {
|
|
1757
|
-
if (sameInstance(record, identity)) return SAME_INSTANCE_MESSAGE;
|
|
1758
|
-
const name = record.instanceName || record.hostname || "another instance";
|
|
1759
|
-
const host = record.hostname && record.instanceName && record.hostname !== record.instanceName ? ` (${record.hostname})` : "";
|
|
1760
|
-
return `${diagramLabel(record.path)} is open on ${name}${host} since ${formatLockSince(record.acquiredAt)}. Try again when it's closed.`;
|
|
1761
|
-
}
|
|
1762
|
-
async function readLock(relativePath) {
|
|
1763
|
-
const parsed = await readJsonFile(lockFile(relativePath));
|
|
1764
|
-
if (!isLockRecord(parsed)) return null;
|
|
1765
|
-
const record = normalizeLock(parsed);
|
|
1766
|
-
const identity = await getInstanceIdentity();
|
|
1767
|
-
return { ...record, stale: lockIsStale(record, Date.now(), identity.instanceId) };
|
|
1768
|
-
}
|
|
1769
|
-
function holderDetails(record, identity) {
|
|
1770
|
-
return {
|
|
1771
|
-
acquiredAt: record.acquiredAt,
|
|
1772
|
-
heartbeatAt: record.heartbeatAt,
|
|
1773
|
-
staleAfterMs: LOCK_STALE_MS,
|
|
1774
|
-
instanceId: record.instanceId ?? null,
|
|
1775
|
-
instanceName: record.instanceName ?? null,
|
|
1776
|
-
hostname: record.hostname ?? null,
|
|
1777
|
-
sameInstance: sameInstance(record, identity)
|
|
1778
|
-
};
|
|
1665
|
+
function pushWarning(warnings, path, message) {
|
|
1666
|
+
if (warnings.some((item) => item.path === path && item.message === message)) return;
|
|
1667
|
+
warnings.push({ path, message });
|
|
1779
1668
|
}
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
return httpFail(423, lockedMessage(record, identity), "locked", {
|
|
1783
|
-
lock: holderDetails(record, identity)
|
|
1784
|
-
});
|
|
1669
|
+
function sortWarnings(warnings) {
|
|
1670
|
+
return [...warnings].sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : a.message < b.message ? -1 : a.message > b.message ? 1 : 0);
|
|
1785
1671
|
}
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1672
|
+
function commentBlock(style, lines) {
|
|
1673
|
+
if (style === "xml") {
|
|
1674
|
+
const body = lines.map((line) => ` ${line}`).join("\n");
|
|
1675
|
+
return `<!--
|
|
1676
|
+
${body}
|
|
1677
|
+
-->
|
|
1678
|
+
`;
|
|
1679
|
+
}
|
|
1680
|
+
const mark = style === "slash" ? "//" : "#";
|
|
1681
|
+
return lines.map((line) => line ? `${mark} ${line}` : mark).join("\n") + "\n";
|
|
1791
1682
|
}
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
var DIAGRAM_EXT = ".mmd";
|
|
1795
|
-
function isDiagramFile(name) {
|
|
1796
|
-
return name.toLowerCase().endsWith(DIAGRAM_EXT);
|
|
1683
|
+
function renderYaml(value) {
|
|
1684
|
+
return stringifyYaml(value, { lineWidth: 0 });
|
|
1797
1685
|
}
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
}
|
|
1806
|
-
}
|
|
1807
|
-
async function buildTree(relativePath = "") {
|
|
1808
|
-
ensureDataRoot();
|
|
1809
|
-
const abs = resolveSafePath(relativePath || ".");
|
|
1810
|
-
const entries = await readDirSafe(abs);
|
|
1811
|
-
const nodes = [];
|
|
1812
|
-
for (const name of entries) {
|
|
1813
|
-
if (name === META_DIR_NAME || name.startsWith(".")) continue;
|
|
1814
|
-
const childAbs = path4.join(abs, name);
|
|
1815
|
-
const stat = await fs2.stat(childAbs);
|
|
1816
|
-
const rel = toRelativePath(childAbs);
|
|
1817
|
-
if (stat.isDirectory()) {
|
|
1818
|
-
nodes.push({
|
|
1819
|
-
name,
|
|
1820
|
-
path: rel,
|
|
1821
|
-
type: "folder",
|
|
1822
|
-
mtimeMs: stat.mtimeMs,
|
|
1823
|
-
children: await buildTree(rel)
|
|
1824
|
-
});
|
|
1825
|
-
} else if (stat.isFile() && isDiagramFile(name)) {
|
|
1826
|
-
nodes.push({
|
|
1827
|
-
name,
|
|
1828
|
-
path: rel,
|
|
1829
|
-
type: "file",
|
|
1830
|
-
mtimeMs: stat.mtimeMs,
|
|
1831
|
-
size: stat.size
|
|
1832
|
-
});
|
|
1833
|
-
}
|
|
1686
|
+
function assertExportShape(document) {
|
|
1687
|
+
if (document.devices.length > EXPORT_MAX_DEVICES || document.links.length > EXPORT_MAX_LINKS) {
|
|
1688
|
+
throw exporterFail(
|
|
1689
|
+
413,
|
|
1690
|
+
`A design export can have at most ${EXPORT_MAX_DEVICES} devices and ${EXPORT_MAX_LINKS} links.`,
|
|
1691
|
+
"too_large"
|
|
1692
|
+
);
|
|
1834
1693
|
}
|
|
1835
|
-
nodes.sort((a, b) => {
|
|
1836
|
-
if (a.type !== b.type) return a.type === "folder" ? -1 : 1;
|
|
1837
|
-
return a.name.localeCompare(b.name, void 0, { sensitivity: "base" });
|
|
1838
|
-
});
|
|
1839
|
-
return nodes;
|
|
1840
1694
|
}
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
}
|
|
1860
|
-
}
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
}
|
|
1866
|
-
|
|
1867
|
-
}
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
const
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
name: path4.basename(abs),
|
|
1876
|
-
content: disk.content,
|
|
1877
|
-
mtimeMs: disk.mtimeMs,
|
|
1878
|
-
size: disk.size,
|
|
1879
|
-
hash: disk.hash
|
|
1880
|
-
};
|
|
1695
|
+
function safeLabel(label) {
|
|
1696
|
+
const trimmed = label.trim();
|
|
1697
|
+
if (!trimmed || /^-+$/.test(trimmed)) return "nerdgram";
|
|
1698
|
+
return trimmed;
|
|
1699
|
+
}
|
|
1700
|
+
function fileName(label, extension) {
|
|
1701
|
+
const stem = safeLabel(label).replace(/[\\/]/g, "-");
|
|
1702
|
+
if (!extension) return stem;
|
|
1703
|
+
return `${stem}.${extension}`;
|
|
1704
|
+
}
|
|
1705
|
+
function cidr(address, prefix) {
|
|
1706
|
+
return `${address}/${prefix}`;
|
|
1707
|
+
}
|
|
1708
|
+
var SHORT_FOR_LONG = [
|
|
1709
|
+
{ long: "GigabitEthernet", short: "Gi" },
|
|
1710
|
+
{ long: "TenGigabitEthernet", short: "Te" },
|
|
1711
|
+
{ long: "FastEthernet", short: "Fa" },
|
|
1712
|
+
{ long: "FortyGigabitEthernet", short: "Fo" },
|
|
1713
|
+
{ long: "HundredGigE", short: "Hu" },
|
|
1714
|
+
{ long: "TwentyFiveGigE", short: "Twe" },
|
|
1715
|
+
{ long: "AppGigabitEthernet", short: "Ap" },
|
|
1716
|
+
{ long: "Port-channel", short: "Po" },
|
|
1717
|
+
{ long: "Loopback", short: "Lo" },
|
|
1718
|
+
{ long: "Vlan", short: "Vl" },
|
|
1719
|
+
{ long: "Tunnel", short: "Tu" },
|
|
1720
|
+
{ long: "Serial", short: "Se" },
|
|
1721
|
+
{ long: "Ethernet", short: "Eth" }
|
|
1722
|
+
];
|
|
1723
|
+
function expandInterface(name) {
|
|
1724
|
+
for (const pair of SHORT_FOR_LONG) {
|
|
1725
|
+
const match = new RegExp(`^${pair.short}(?=\\d)`, "i").exec(name);
|
|
1726
|
+
if (match) return `${pair.long}${name.slice(match[0].length)}`;
|
|
1727
|
+
}
|
|
1728
|
+
return name;
|
|
1881
1729
|
}
|
|
1882
|
-
|
|
1883
|
-
const
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
await assertWriteAllowed(relative, options.lockToken);
|
|
1887
|
-
const disk = await readDisk(abs);
|
|
1888
|
-
const decision = evaluateWrite(disk, { ...options, content });
|
|
1889
|
-
if (decision.action === "reject") {
|
|
1890
|
-
if (decision.code === "missing") {
|
|
1891
|
-
throw httpFail(409, "This diagram is no longer on disk.", "missing");
|
|
1730
|
+
function shortenInterface(name) {
|
|
1731
|
+
for (const pair of SHORT_FOR_LONG) {
|
|
1732
|
+
if (name.toLowerCase().startsWith(pair.long.toLowerCase())) {
|
|
1733
|
+
return `${pair.short}${name.slice(pair.long.length)}`;
|
|
1892
1734
|
}
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1735
|
+
}
|
|
1736
|
+
return name;
|
|
1737
|
+
}
|
|
1738
|
+
function resolveLinks(document, warnings) {
|
|
1739
|
+
return document.links.map((link, index) => {
|
|
1740
|
+
const a = resolveInterface(link.a.device, link.a.interface, document.interfaces);
|
|
1741
|
+
const b = resolveInterface(link.b.device, link.b.interface, document.interfaces);
|
|
1742
|
+
if (a.unresolved) {
|
|
1743
|
+
pushWarning(
|
|
1744
|
+
warnings,
|
|
1745
|
+
`/links/${index}/a/interface`,
|
|
1746
|
+
`Link interface "${link.a.interface}" on ${link.a.device} did not match an interface name. The link name was kept.`
|
|
1747
|
+
);
|
|
1748
|
+
}
|
|
1749
|
+
if (b.unresolved) {
|
|
1750
|
+
pushWarning(
|
|
1751
|
+
warnings,
|
|
1752
|
+
`/links/${index}/b/interface`,
|
|
1753
|
+
`Link interface "${link.b.interface}" on ${link.b.device} did not match an interface name. The link name was kept.`
|
|
1754
|
+
);
|
|
1755
|
+
}
|
|
1756
|
+
if (link.inferred) {
|
|
1757
|
+
pushWarning(
|
|
1758
|
+
warnings,
|
|
1759
|
+
`/links/${index}`,
|
|
1760
|
+
`Link ${link.a.device}:${a.name} to ${link.b.device}:${b.name} is inferred and is not a proven cable.`
|
|
1899
1761
|
);
|
|
1900
1762
|
}
|
|
1901
|
-
if (!disk.exists) throw httpFail(409, "This diagram changed on disk.", "conflict");
|
|
1902
|
-
throw httpFail(409, "This diagram changed on disk since it was last loaded.", "conflict", {
|
|
1903
|
-
current: {
|
|
1904
|
-
path: relative,
|
|
1905
|
-
content: disk.content,
|
|
1906
|
-
mtimeMs: disk.mtimeMs,
|
|
1907
|
-
size: disk.size,
|
|
1908
|
-
hash: disk.hash
|
|
1909
|
-
}
|
|
1910
|
-
});
|
|
1911
|
-
}
|
|
1912
|
-
if (decision.action === "unchanged" && disk.exists) {
|
|
1913
|
-
await noteDiagramSighting({ path: relative, hash: disk.hash });
|
|
1914
1763
|
return {
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1764
|
+
index,
|
|
1765
|
+
aDevice: link.a.device,
|
|
1766
|
+
aInterface: a.name,
|
|
1767
|
+
bDevice: link.b.device,
|
|
1768
|
+
bInterface: b.name,
|
|
1769
|
+
inferred: link.inferred,
|
|
1770
|
+
unresolved: a.unresolved || b.unresolved,
|
|
1771
|
+
label: link.label
|
|
1920
1772
|
};
|
|
1921
|
-
}
|
|
1922
|
-
let revisionId;
|
|
1923
|
-
if (decision.action === "overwrite" && disk.exists) {
|
|
1924
|
-
revisionId = await snapshotPrevious(relative, disk);
|
|
1925
|
-
}
|
|
1926
|
-
await fs2.mkdir(path4.dirname(abs), { recursive: true });
|
|
1927
|
-
await atomicWriteText(abs, content);
|
|
1928
|
-
await dropThumbnail(relative);
|
|
1929
|
-
const stat = await fs2.stat(abs);
|
|
1930
|
-
const search = await import("./chunk-R3RZTEM3.js");
|
|
1931
|
-
await search.rememberSearchEntry(relative, content, stat.mtimeMs, stat.size);
|
|
1932
|
-
await noteDiagramSighting({ path: relative, hash: hashContent(content) });
|
|
1933
|
-
noteSync({ kind: "content", summary: `Update ${relative}` });
|
|
1934
|
-
return {
|
|
1935
|
-
path: relative,
|
|
1936
|
-
mtimeMs: stat.mtimeMs,
|
|
1937
|
-
size: stat.size,
|
|
1938
|
-
hash: hashContent(content),
|
|
1939
|
-
revisionId
|
|
1940
|
-
};
|
|
1773
|
+
});
|
|
1941
1774
|
}
|
|
1942
|
-
function
|
|
1943
|
-
|
|
1944
|
-
|
|
1775
|
+
function resolveInterface(device, rawName, interfaces) {
|
|
1776
|
+
const owned = interfaces.filter((item) => item.device === device);
|
|
1777
|
+
const exact = owned.find((item) => item.name === rawName);
|
|
1778
|
+
if (exact) return { name: exact.name, unresolved: false };
|
|
1779
|
+
const folded = owned.find((item) => item.name.toLowerCase() === rawName.toLowerCase());
|
|
1780
|
+
if (folded) return { name: folded.name, unresolved: false };
|
|
1781
|
+
const expanded = expandInterface(rawName);
|
|
1782
|
+
const fromShort = owned.find((item) => item.name.toLowerCase() === expanded.toLowerCase());
|
|
1783
|
+
if (fromShort) return { name: fromShort.name, unresolved: false };
|
|
1784
|
+
const fromLong = owned.find((item) => shortenInterface(item.name).toLowerCase() === rawName.toLowerCase());
|
|
1785
|
+
if (fromLong) return { name: fromLong.name, unresolved: false };
|
|
1786
|
+
return { name: rawName, unresolved: true };
|
|
1787
|
+
}
|
|
1788
|
+
function same(left, right) {
|
|
1789
|
+
if (left == null || left === "") return true;
|
|
1790
|
+
return left.trim().toLowerCase() === right.trim().toLowerCase();
|
|
1791
|
+
}
|
|
1792
|
+
function specificity(row) {
|
|
1793
|
+
return (row.platform ? 1 : 0) + (row.role ? 1 : 0) + (row.model ? 1 : 0);
|
|
1794
|
+
}
|
|
1795
|
+
function lookupMapping(builtIn, device, override) {
|
|
1796
|
+
const extra = (override ?? []).map((row) => ({
|
|
1797
|
+
platform: row.platform,
|
|
1798
|
+
role: row.role,
|
|
1799
|
+
model: row.model,
|
|
1800
|
+
values: Object.fromEntries(Object.entries(row).filter(([key]) => key !== "platform" && key !== "role" && key !== "model"))
|
|
1801
|
+
}));
|
|
1802
|
+
const ranked = [
|
|
1803
|
+
...extra.map((row, index) => ({ row, index, override: true })),
|
|
1804
|
+
...builtIn.map((row, index) => ({ row, index, override: false }))
|
|
1805
|
+
];
|
|
1806
|
+
const matches = ranked.filter(
|
|
1807
|
+
(item) => same(item.row.platform, device.platform) && same(item.row.role, device.role) && same(item.row.model, device.model)
|
|
1808
|
+
);
|
|
1809
|
+
if (matches.length === 0) return null;
|
|
1810
|
+
matches.sort((a, b) => {
|
|
1811
|
+
const score = specificity(b.row) - specificity(a.row);
|
|
1812
|
+
if (score !== 0) return score;
|
|
1813
|
+
if (a.override !== b.override) return a.override ? -1 : 1;
|
|
1814
|
+
return a.index - b.index;
|
|
1945
1815
|
});
|
|
1816
|
+
return matches[0]?.row.values ?? null;
|
|
1817
|
+
}
|
|
1818
|
+
function mappingRowsFromUnknown(value) {
|
|
1819
|
+
if (value == null) return null;
|
|
1820
|
+
if (!Array.isArray(value)) return null;
|
|
1821
|
+
const rows = [];
|
|
1822
|
+
for (const item of value) {
|
|
1823
|
+
if (!isRecord4(item)) continue;
|
|
1824
|
+
const row = {};
|
|
1825
|
+
for (const [key, raw] of Object.entries(item)) {
|
|
1826
|
+
if (typeof raw === "string") row[key] = raw;
|
|
1827
|
+
else if (typeof raw === "number" || typeof raw === "boolean") row[key] = String(raw);
|
|
1828
|
+
}
|
|
1829
|
+
if (Object.keys(row).length > 0) rows.push(row);
|
|
1830
|
+
}
|
|
1831
|
+
return rows;
|
|
1832
|
+
}
|
|
1833
|
+
function mappingForTarget(document, id) {
|
|
1834
|
+
if (!isRecord4(document)) return null;
|
|
1835
|
+
return mappingRowsFromUnknown(document[id]);
|
|
1836
|
+
}
|
|
1837
|
+
function parseMappingText(text) {
|
|
1838
|
+
const trimmed = text.replace(/^\uFEFF/, "").trim();
|
|
1839
|
+
if (!trimmed) return null;
|
|
1840
|
+
try {
|
|
1841
|
+
return JSON.parse(trimmed);
|
|
1842
|
+
} catch {
|
|
1843
|
+
return parseYaml(trimmed);
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
function safeGroupName(name) {
|
|
1847
|
+
const cleaned = name.trim().toLowerCase().replace(/[^a-z0-9_]+/g, "_").replace(/^_+|_+$/g, "");
|
|
1848
|
+
if (!cleaned) return "ungrouped";
|
|
1849
|
+
if (/^\d/.test(cleaned)) return `g_${cleaned}`;
|
|
1850
|
+
return cleaned;
|
|
1851
|
+
}
|
|
1852
|
+
function stableUuid(seed) {
|
|
1853
|
+
const hex = createHash("sha256").update(seed).digest("hex");
|
|
1854
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-4${hex.slice(13, 16)}-a${hex.slice(17, 20)}-${hex.slice(20, 32)}`;
|
|
1855
|
+
}
|
|
1856
|
+
function zipStore(files) {
|
|
1857
|
+
const locals = [];
|
|
1858
|
+
const centrals = [];
|
|
1859
|
+
let offset = 0;
|
|
1860
|
+
for (const file of files) {
|
|
1861
|
+
const name = Buffer.from(file.name.replace(/\\/g, "/"), "utf8");
|
|
1862
|
+
const data = Buffer.from(file.text, "utf8");
|
|
1863
|
+
const crc = crc32(data);
|
|
1864
|
+
const local = Buffer.alloc(30 + name.length);
|
|
1865
|
+
local.writeUInt32LE(67324752, 0);
|
|
1866
|
+
local.writeUInt16LE(20, 4);
|
|
1867
|
+
local.writeUInt16LE(0, 6);
|
|
1868
|
+
local.writeUInt16LE(0, 8);
|
|
1869
|
+
local.writeUInt16LE(0, 10);
|
|
1870
|
+
local.writeUInt16LE(0, 12);
|
|
1871
|
+
local.writeUInt32LE(crc, 14);
|
|
1872
|
+
local.writeUInt32LE(data.length, 18);
|
|
1873
|
+
local.writeUInt32LE(data.length, 22);
|
|
1874
|
+
local.writeUInt16LE(name.length, 26);
|
|
1875
|
+
local.writeUInt16LE(0, 28);
|
|
1876
|
+
name.copy(local, 30);
|
|
1877
|
+
locals.push(local, data);
|
|
1878
|
+
const central = Buffer.alloc(46 + name.length);
|
|
1879
|
+
central.writeUInt32LE(33639248, 0);
|
|
1880
|
+
central.writeUInt16LE(20, 4);
|
|
1881
|
+
central.writeUInt16LE(20, 6);
|
|
1882
|
+
central.writeUInt16LE(0, 8);
|
|
1883
|
+
central.writeUInt16LE(0, 10);
|
|
1884
|
+
central.writeUInt16LE(0, 12);
|
|
1885
|
+
central.writeUInt16LE(0, 14);
|
|
1886
|
+
central.writeUInt32LE(crc, 16);
|
|
1887
|
+
central.writeUInt32LE(data.length, 20);
|
|
1888
|
+
central.writeUInt32LE(data.length, 24);
|
|
1889
|
+
central.writeUInt16LE(name.length, 28);
|
|
1890
|
+
central.writeUInt16LE(0, 30);
|
|
1891
|
+
central.writeUInt16LE(0, 32);
|
|
1892
|
+
central.writeUInt16LE(0, 34);
|
|
1893
|
+
central.writeUInt16LE(0, 36);
|
|
1894
|
+
central.writeUInt32LE(0, 38);
|
|
1895
|
+
central.writeUInt32LE(offset, 42);
|
|
1896
|
+
name.copy(central, 46);
|
|
1897
|
+
centrals.push(central);
|
|
1898
|
+
offset += local.length + data.length;
|
|
1899
|
+
}
|
|
1900
|
+
const centralBuf = Buffer.concat(centrals);
|
|
1901
|
+
const end = Buffer.alloc(22);
|
|
1902
|
+
end.writeUInt32LE(101010256, 0);
|
|
1903
|
+
end.writeUInt16LE(0, 4);
|
|
1904
|
+
end.writeUInt16LE(0, 6);
|
|
1905
|
+
end.writeUInt16LE(files.length, 8);
|
|
1906
|
+
end.writeUInt16LE(files.length, 10);
|
|
1907
|
+
end.writeUInt32LE(centralBuf.length, 12);
|
|
1908
|
+
end.writeUInt32LE(offset, 16);
|
|
1909
|
+
end.writeUInt16LE(0, 20);
|
|
1910
|
+
return Buffer.concat([...locals, centralBuf, end]);
|
|
1911
|
+
}
|
|
1912
|
+
var CRC_TABLE = (() => {
|
|
1913
|
+
const table = new Uint32Array(256);
|
|
1914
|
+
for (let index = 0; index < 256; index += 1) {
|
|
1915
|
+
let crc = index;
|
|
1916
|
+
for (let bit = 0; bit < 8; bit += 1) crc = crc & 1 ? 3988292384 ^ crc >>> 1 : crc >>> 1;
|
|
1917
|
+
table[index] = crc >>> 0;
|
|
1918
|
+
}
|
|
1919
|
+
return table;
|
|
1920
|
+
})();
|
|
1921
|
+
function crc32(data) {
|
|
1922
|
+
let crc = 4294967295;
|
|
1923
|
+
for (const byte of data) crc = CRC_TABLE[(crc ^ byte) & 255] ^ crc >>> 8;
|
|
1924
|
+
return (crc ^ 4294967295) >>> 0;
|
|
1946
1925
|
}
|
|
1947
1926
|
|
|
1948
1927
|
export {
|
|
1949
1928
|
canonicalIp,
|
|
1950
1929
|
validateNetwork,
|
|
1951
1930
|
dataFail,
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1931
|
+
readDataSource,
|
|
1932
|
+
EXPORT_MAX_INPUT_BYTES,
|
|
1933
|
+
EXPORT_MAX_DEVICES,
|
|
1934
|
+
EXPORT_MAX_LINKS,
|
|
1935
|
+
exporterFail,
|
|
1936
|
+
assertExportInputSize,
|
|
1937
|
+
CHANGE_ME,
|
|
1938
|
+
HEADER_LINES,
|
|
1939
|
+
isRecord4 as isRecord,
|
|
1940
|
+
pushWarning,
|
|
1941
|
+
sortWarnings,
|
|
1942
|
+
commentBlock,
|
|
1943
|
+
renderYaml,
|
|
1944
|
+
assertExportShape,
|
|
1945
|
+
safeLabel,
|
|
1946
|
+
fileName,
|
|
1947
|
+
cidr,
|
|
1948
|
+
resolveLinks,
|
|
1949
|
+
lookupMapping,
|
|
1950
|
+
mappingRowsFromUnknown,
|
|
1951
|
+
mappingForTarget,
|
|
1952
|
+
parseMappingText,
|
|
1953
|
+
safeGroupName,
|
|
1954
|
+
stableUuid,
|
|
1955
|
+
zipStore
|
|
1955
1956
|
};
|