1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/* jshint esversion: 2024, module: true */
/**
* Structured Editor for systemd-networkd configuration
* @class StructuredEditor
*/
class StructuredEditor {
constructor(container) {
this.container = container;
this.config = null;
this.currentFile = "";
this.systemdNetworkModule = null;
}
/**
* Load configuration from text
* @param {string} configText - Configuration text
* @param {string} filename - File name
*/
async loadConfiguration(configText, filename) {
// Dynamically import the systemd-network module
if (!this.systemdNetworkModule) {
this.systemdNetworkModule = await import("./systemd-network.js");
}
const { NetworkConfiguration } = this.systemdNetworkModule;
this.config = NetworkConfiguration.fromSystemdConfiguration(configText);
this.currentFile = filename;
this.render();
}
/**
* Render the structured editor
*/
render() {
if (!this.config) {
this.container.innerHTML =
'<div class="error-message">No configuration loaded</div>';
return;
}
this.container.innerHTML = this._createEditorHTML();
this._attachEventListeners();
}
/**
* Create editor HTML structure
* @private
* @returns {string}
*/
_createEditorHTML() {
return `
<div class="structured-editor">
<div class="editor-sections">
${this._createMatchSection()}
${this._createLinkSection()}
${this._createNetworkSection()}
${this._createDHCPSection()}
${this._createAddressSections()}
${this._createRouteSections()}
</div>
<div class="editor-actions">
<button class="button secondary" id="addAddressSection">Add Address</button>
<button class="button secondary" id="addRouteSection">Add Route</button>
</div>
</div>
`;
}
_createMatchSection() {
const match = this.config.Match;
return `
<div class="config-section">
<h4>[Match]</h4>
<div class="config-table">
${this._createInputRow("MACAddress", match.MACAddress?.join(" ") || "", "Space-separated MAC addresses")}
${this._createInputRow("Name", match.Name?.join(" ") || "", "Interface names")}
${this._createInputRow("Driver", match.Driver?.join(" ") || "", "Driver names")}
${this._createInputRow("Type", match.Type?.join(" ") || "", "Interface types")}
</div>
</div>
`;
}
_createLinkSection() {
const link = this.config.Link;
return `
<div class="config-section">
<h4>[Link]</h4>
<div class="config-table">
${this._createInputRow("MACAddress", link.MACAddress || "", "Hardware address")}
${this._createInputRow("MTUBytes", link.MTUBytes || "", "Maximum transmission unit")}
${this._createSelectRow("WakeOnLan", link.WakeOnLan || "", ["", "phy", "unicast", "broadcast", "arp", "magic"], "Wake-on-LAN")}
</div>
</div>
`;
}
_createNetworkSection() {
const network = this.config.Network;
return `
<div class="config-section">
<h4>[Network]</h4>
<div class="config-table">
${this._createInputRow("Description", network.Description || "", "Interface description")}
${this._createSelectRow("DHCP", network.DHCP?.join(" ") || "", ["", "yes", "no", "ipv4", "ipv6"], "DHCP client")}
${this._createInputRow("DNS", network.DNS?.join(" ") || "", "DNS servers")}
${this._createInputRow("NTP", network.NTP?.join(" ") || "", "NTP servers")}
${this._createSelectRow("IPv6PrivacyExtensions", network.IPv6PrivacyExtensions || "", ["", "yes", "no", "prefer-public"], "IPv6 privacy extensions")}
</div>
</div>
`;
}
_createDHCPSection() {
const dhcp = this.config.DHCP;
return `
<div class="config-section">
<h4>[DHCP]</h4>
<div class="config-table">
${this._createSelectRow("UseDNS", dhcp.UseDNS || "", ["", "yes", "no"], "Use DNS from DHCP")}
${this._createSelectRow("UseNTP", dhcp.UseNTP || "", ["", "yes", "no"], "Use NTP from DHCP")}
${this._createInputRow("RouteMetric", dhcp.RouteMetric || "", "Route metric")}
</div>
</div>
`;
}
_createAddressSections() {
if (!this.config.Address || this.config.Address.length === 0) {
return '<div class="config-section"><h4>[Address]</h4><p class="no-items">No address sections</p></div>';
}
return this.config.Address.map(
(addr, index) => `
<div class="config-section">
<h4>[Address] ${index > 0 ? `#${index + 1}` : ""}</h4>
<div class="config-table">
${this._createInputRow("Address", addr.Address || "", "IP address with prefix")}
${this._createInputRow("Peer", addr.Peer || "", "Peer address")}
<button class="button small warning remove-section" data-type="address" data-index="${index}">Remove</button>
</div>
</div>
`,
).join("");
}
_createRouteSections() {
if (!this.config.Route || this.config.Route.length === 0) {
return '<div class="config-section"><h4>[Route]</h4><p class="no-items">No route sections</p></div>';
}
return this.config.Route.map(
(route, index) => `
<div class="config-section">
<h4>[Route] ${index > 0 ? `#${index + 1}` : ""}</h4>
<div class="config-table">
${this._createInputRow("Gateway", route.Gateway || "", "Gateway address")}
${this._createInputRow("Destination", route.Destination || "", "Destination prefix")}
${this._createInputRow("Metric", route.Metric || "", "Route metric")}
<button class="button small warning remove-section" data-type="route" data-index="${index}">Remove</button>
</div>
</div>
`,
).join("");
}
_createInputRow(key, value, description) {
return `
<div class="config-row">
<label class="config-label" title="${description}">
<abbr title="${description}">${key}</abbr>:
</label>
<input type="text"
class="config-input"
data-section="${this._getCurrentSection()}"
data-key="${key}"
value="${value}"
placeholder="${description}">
</div>
`;
}
_createSelectRow(key, value, options, description) {
const optionsHTML = options
.map(
(opt) =>
`<option value="${opt}" ${opt === value ? "selected" : ""}>${opt || "(not set)"}</option>`,
)
.join("");
return `
<div class="config-row">
<label class="config-label" title="${description}">
<abbr title="${description}">${key}</abbr>:
</label>
<select class="config-select" data-section="${this._getCurrentSection()}" data-key="${key}">
${optionsHTML}
</select>
</div>
`;
}
/**
* Get current section name for event handling
* @private
* @returns {string}
*/
_getCurrentSection() {
// This is a simplified implementation - you might want to track the current section more precisely
return "network";
}
/**
* Attach event listeners to the editor
* @private
*/
_attachEventListeners() {
// Input changes
this.container.querySelectorAll(".config-input").forEach((input) => {
input.addEventListener("input", (e) => this._onInputChange(e));
});
// Select changes
this.container.querySelectorAll(".config-select").forEach((select) => {
select.addEventListener("change", (e) => this._onSelectChange(e));
});
// Add sections
this.container
.querySelector("#addAddressSection")
?.addEventListener("click", () => {
this._addAddressSection();
});
this.container
.querySelector("#addRouteSection")
?.addEventListener("click", () => {
this._addRouteSection();
});
// Remove sections
this.container.querySelectorAll(".remove-section").forEach((btn) => {
btn.addEventListener("click", (e) => this._onRemoveSection(e));
});
}
/**
* Handle input changes
* @private
* @param {Event} event
*/
_onInputChange(event) {
const input = event.target;
const section = input.dataset.section;
const key = input.dataset.key;
const value = input.value;
this._updateConfigValue(section, key, value);
}
/**
* Handle select changes
* @private
* @param {Event} event
*/
_onSelectChange(event) {
const select = event.target;
const section = select.dataset.section;
const key = select.dataset.key;
const value = select.value;
this._updateConfigValue(section, key, value);
}
/**
* Update configuration value
* @private
* @param {string} section - Section name
* @param {string} key - Key name
* @param {string} value - Value
*/
_updateConfigValue(section, key, value) {
if (!this.config) return;
// Simplified implementation - you'll want to expand this based on your systemd-network.js structure
console.log(`Update ${section}.${key} = ${value}`);
// Example update logic - you'll need to implement this based on your actual data structure
switch (section) {
case "match":
if (["MACAddress", "Name", "Driver", "Type"].includes(key)) {
this.config.Match[key] = value.split(" ").filter((v) => v.trim());
} else {
this.config.Match[key] = value;
}
break;
case "link":
this.config.Link[key] = value;
break;
case "network":
if (["DNS", "NTP", "DHCP", "Domains", "IPForward"].includes(key)) {
this.config.Network[key] = value.split(" ").filter((v) => v.trim());
} else {
this.config.Network[key] = value;
}
break;
case "dhcp":
this.config.DHCP[key] = value;
break;
}
}
/**
* Add a new address section
* @private
*/
async _addAddressSection() {
if (!this.systemdNetworkModule) {
this.systemdNetworkModule = await import("./systemd-network.js");
}
const { AddressSection } = this.systemdNetworkModule;
this.config.Address.push(new AddressSection());
this.render();
}
/**
* Add a new route section
* @private
*/
async _addRouteSection() {
if (!this.systemdNetworkModule) {
this.systemdNetworkModule = await import("./systemd-network.js");
}
const { RouteSection } = this.systemdNetworkModule;
this.config.Route.push(new RouteSection());
this.render();
}
/**
* Remove a section
* @private
* @param {Event} event
*/
_onRemoveSection(event) {
const btn = event.target;
const type = btn.dataset.type;
const index = parseInt(btn.dataset.index);
if (type === "address" && this.config.Address) {
this.config.Address.splice(index, 1);
} else if (type === "route" && this.config.Route) {
this.config.Route.splice(index, 1);
}
this.render();
}
/**
* Get current configuration as text
* @returns {string}
*/
getConfigurationText() {
return this.config ? this.config.toSystemdConfiguration() : "";
}
/**
* Get current configuration object
* @returns {Object|null}
*/
getConfiguration() {
return this.config;
}
}
export { StructuredEditor };
|