@serfab/cadre-host 1.14.0 → 1.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -4
- package/dist/bin/host.js +32 -20
- package/dist/bin/host.js.map +1 -1
- package/dist/bin/nat-output.d.ts +5 -0
- package/dist/bin/nat-output.js +12 -1
- package/dist/bin/nat-output.js.map +1 -1
- package/dist/bin/ui-access.d.ts +22 -0
- package/dist/bin/ui-access.js +44 -0
- package/dist/bin/ui-access.js.map +1 -0
- package/dist/hosted/claim-details.d.ts +25 -3
- package/dist/hosted/claim-details.js +31 -37
- package/dist/hosted/claim-details.js.map +1 -1
- package/dist/hosted/hosted-node-service.d.ts +7 -0
- package/dist/hosted/hosted-node-service.js +16 -4
- package/dist/hosted/hosted-node-service.js.map +1 -1
- package/dist/hosted/hosted-node-watcher.d.ts +16 -4
- package/dist/hosted/hosted-node-watcher.js +45 -9
- package/dist/hosted/hosted-node-watcher.js.map +1 -1
- package/dist/hosted/node-status.d.ts +19 -3
- package/dist/hosted/node-status.js +21 -5
- package/dist/hosted/node-status.js.map +1 -1
- package/dist/hosted/types.d.ts +12 -5
- package/dist/hosted/types.js.map +1 -1
- package/dist/installer/config.d.ts +23 -0
- package/dist/installer/config.js +46 -0
- package/dist/installer/config.js.map +1 -1
- package/dist/installer/index.d.ts +2 -0
- package/dist/installer/index.js +7 -16
- package/dist/installer/index.js.map +1 -1
- package/dist/installer/wizard.d.ts +12 -2
- package/dist/installer/wizard.js +15 -7
- package/dist/installer/wizard.js.map +1 -1
- package/dist/nat/index.d.ts +2 -1
- package/dist/nat/index.js +1 -0
- package/dist/nat/index.js.map +1 -1
- package/dist/nat/nat-service.d.ts +22 -5
- package/dist/nat/nat-service.js +75 -26
- package/dist/nat/nat-service.js.map +1 -1
- package/dist/nat/nat-store.js +11 -0
- package/dist/nat/nat-store.js.map +1 -1
- package/dist/nat/network-mode.d.ts +19 -0
- package/dist/nat/network-mode.js +32 -0
- package/dist/nat/network-mode.js.map +1 -0
- package/dist/nat/reachability.d.ts +5 -4
- package/dist/nat/reachability.js +19 -6
- package/dist/nat/reachability.js.map +1 -1
- package/dist/nat/types.d.ts +26 -6
- package/dist/nat/types.js.map +1 -1
- package/dist/orchestrator/host-process-orchestrator.d.ts +3 -3
- package/dist/orchestrator/host-process-orchestrator.js +6 -15
- package/dist/orchestrator/host-process-orchestrator.js.map +1 -1
- package/dist/package-version.d.ts +1 -0
- package/dist/package-version.js +4 -0
- package/dist/package-version.js.map +1 -0
- package/dist/server/routes/settings.d.ts +1 -0
- package/dist/server/routes/settings.js +15 -1
- package/dist/server/routes/settings.js.map +1 -1
- package/dist/server/routes/status.js +2 -32
- package/dist/server/routes/status.js.map +1 -1
- package/dist/ui/assets/index-BHd8CWZA.css +1 -0
- package/dist/ui/assets/index-Ccr2GjPT.js +23 -0
- package/dist/ui/assets/{index-4FWuU8ye.js.map → index-Ccr2GjPT.js.map} +1 -1
- package/dist/ui/index.html +2 -2
- package/package.json +4 -5
- package/src/__tests__/cli.smoke.test.ts +1 -1
- package/src/__tests__/orchestrator-claim-secret.test.ts +1 -2
- package/src/__tests__/orchestrator-env-scrub.test.ts +160 -163
- package/src/__tests__/orchestrator-handle-contract.ts +1 -1
- package/src/__tests__/orchestrator-node-identity.test.ts +0 -5
- package/src/__tests__/orchestrator-push.test.ts +3 -3
- package/src/__tests__/orchestrator.test.ts +3 -5
- package/src/__tests__/ui-access.test.ts +30 -0
- package/src/bin/host.ts +30 -20
- package/src/bin/nat-output.ts +12 -1
- package/src/bin/ui-access.ts +50 -0
- package/src/hosted/__tests__/fake-orchestrator.test.ts +0 -1
- package/src/hosted/__tests__/fake-orchestrator.ts +263 -265
- package/src/hosted/__tests__/hosted-node-service.test.ts +708 -609
- package/src/hosted/__tests__/hosted-node-supervisor.test.ts +475 -474
- package/src/hosted/claim-details.ts +44 -37
- package/src/hosted/hosted-node-service.ts +23 -4
- package/src/hosted/hosted-node-watcher.ts +288 -250
- package/src/hosted/node-status.ts +31 -5
- package/src/hosted/types.ts +160 -153
- package/src/installer/__tests__/installer.smoke.test.ts +184 -182
- package/src/installer/__tests__/wizard.test.ts +76 -56
- package/src/installer/config.ts +66 -0
- package/src/installer/index.ts +252 -259
- package/src/installer/wizard.ts +101 -87
- package/src/nat/__tests__/nat-service.test.ts +608 -540
- package/src/nat/__tests__/nat-store.test.ts +130 -127
- package/src/nat/__tests__/network-mode.test.ts +38 -0
- package/src/nat/index.ts +91 -87
- package/src/nat/nat-service.ts +79 -26
- package/src/nat/nat-store.ts +249 -237
- package/src/nat/network-mode.ts +36 -0
- package/src/nat/reachability.ts +155 -143
- package/src/nat/types.ts +232 -209
- package/src/orchestrator/host-process-orchestrator.ts +1089 -1098
- package/src/package-version.ts +4 -0
- package/src/server/__tests__/fakes.ts +41 -40
- package/src/server/__tests__/hosted-nodes-route.test.ts +161 -160
- package/src/server/__tests__/settings-route.test.ts +31 -0
- package/src/server/routes/settings.ts +128 -115
- package/src/server/routes/status.ts +72 -102
- package/dist/ui/assets/index-4FWuU8ye.js +0 -19
- package/dist/ui/assets/index-Di9Q4d01.css +0 -1
package/src/nat/nat-store.ts
CHANGED
|
@@ -1,237 +1,249 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
2
|
-
import { dirname, join } from 'node:path';
|
|
3
|
-
import debug from 'debug';
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
NatError,
|
|
7
|
-
type ManualForward,
|
|
8
|
-
type ManualForwardPatch,
|
|
9
|
-
type NatDdnsSettings,
|
|
10
|
-
type NatSettingsFile,
|
|
11
|
-
type
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
private
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (
|
|
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
|
-
const
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
this.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
next.
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
...
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (value ===
|
|
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
|
-
if (
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if (
|
|
174
|
-
|
|
175
|
-
if (
|
|
176
|
-
|
|
177
|
-
if (typeof ddns
|
|
178
|
-
|
|
179
|
-
if (
|
|
180
|
-
return
|
|
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
|
-
'invalid_config',
|
|
228
|
-
|
|
229
|
-
)
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
}
|
|
237
|
-
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import debug from 'debug';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
NatError,
|
|
7
|
+
type ManualForward,
|
|
8
|
+
type ManualForwardPatch,
|
|
9
|
+
type NatDdnsSettings,
|
|
10
|
+
type NatSettingsFile,
|
|
11
|
+
type NetworkSetting,
|
|
12
|
+
type PortKind,
|
|
13
|
+
} from './types.js';
|
|
14
|
+
import { isDnsHostname } from './address-resolver.js';
|
|
15
|
+
|
|
16
|
+
const log = debug('cadre:host:nat-store');
|
|
17
|
+
|
|
18
|
+
const FILE_VERSION = 1;
|
|
19
|
+
|
|
20
|
+
/** Default DDNS update interval (5 minutes). */
|
|
21
|
+
const DEFAULT_DDNS_INTERVAL_MS = 5 * 60 * 1000;
|
|
22
|
+
|
|
23
|
+
const PORT_KINDS: ReadonlyArray<PortKind> = ['tcp', 'ws'];
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Atomic JSON store for `nat.json` — write to `<path>.tmp`, then rename.
|
|
27
|
+
*
|
|
28
|
+
* Concurrency assumption: only one cadre-host process owns a given rootDir.
|
|
29
|
+
*/
|
|
30
|
+
export class NatStore {
|
|
31
|
+
private readonly path: string;
|
|
32
|
+
private cache: NatSettingsFile | null = null;
|
|
33
|
+
|
|
34
|
+
constructor(rootDir: string) {
|
|
35
|
+
mkdirSync(rootDir, { recursive: true });
|
|
36
|
+
this.path = join(rootDir, 'nat.json');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
filePath(): string {
|
|
40
|
+
return this.path;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Load (with caching). A missing file returns defaults. */
|
|
44
|
+
load(): NatSettingsFile {
|
|
45
|
+
if (this.cache) return this.cache;
|
|
46
|
+
if (!existsSync(this.path)) {
|
|
47
|
+
this.cache = defaultSettings();
|
|
48
|
+
return this.cache;
|
|
49
|
+
}
|
|
50
|
+
let raw: string;
|
|
51
|
+
try {
|
|
52
|
+
raw = readFileSync(this.path, 'utf8');
|
|
53
|
+
} catch (err) {
|
|
54
|
+
throw new NatError(
|
|
55
|
+
'storage_error',
|
|
56
|
+
`failed to read nat file at ${this.path}: ${(err as Error).message}`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
let parsed: unknown;
|
|
60
|
+
try {
|
|
61
|
+
parsed = JSON.parse(raw);
|
|
62
|
+
} catch (err) {
|
|
63
|
+
throw new NatError(
|
|
64
|
+
'storage_error',
|
|
65
|
+
`nat file at ${this.path} is not valid JSON: ${(err as Error).message}`,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
const settings = fromParsed(parsed);
|
|
69
|
+
if (!settings) {
|
|
70
|
+
throw new NatError(
|
|
71
|
+
'storage_error',
|
|
72
|
+
`nat file at ${this.path} has unexpected shape`,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
this.cache = settings;
|
|
76
|
+
return this.cache;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Persist current settings atomically. */
|
|
80
|
+
save(state: NatSettingsFile): void {
|
|
81
|
+
mkdirSync(dirname(this.path), { recursive: true });
|
|
82
|
+
const next: NatSettingsFile = { ...state, version: FILE_VERSION };
|
|
83
|
+
const payload = JSON.stringify(next, null, 2);
|
|
84
|
+
const tmp = `${this.path}.tmp`;
|
|
85
|
+
writeFileSync(tmp, payload, { encoding: 'utf8' });
|
|
86
|
+
renameSync(tmp, this.path);
|
|
87
|
+
this.cache = next;
|
|
88
|
+
log('saved nat.json (upnp=%s, forwards=%d, ddns=%s) to %s',
|
|
89
|
+
next.upnpEnabled,
|
|
90
|
+
Object.keys(next.forwards).length,
|
|
91
|
+
next.ddns.providerId ?? '(none)',
|
|
92
|
+
this.path);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Patch and persist (validation lives here). `forwards` is edited through `setForward`. */
|
|
96
|
+
update(patch: Partial<Omit<NatSettingsFile, 'version' | 'forwards'>>): NatSettingsFile {
|
|
97
|
+
const current = this.load();
|
|
98
|
+
const merged: NatSettingsFile = {
|
|
99
|
+
...current,
|
|
100
|
+
...patch,
|
|
101
|
+
ddns: { ...current.ddns, ...(patch.ddns ?? {}) },
|
|
102
|
+
forwards: current.forwards,
|
|
103
|
+
version: FILE_VERSION,
|
|
104
|
+
};
|
|
105
|
+
validate(merged);
|
|
106
|
+
this.save(merged);
|
|
107
|
+
return merged;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Apply a manual-forward patch for one node: a number sets that port's
|
|
112
|
+
* external port, `null` clears it, an absent key leaves it alone. An entry
|
|
113
|
+
* with no ports left is removed.
|
|
114
|
+
*/
|
|
115
|
+
setForward(nodeId: string, patch: ManualForwardPatch): NatSettingsFile {
|
|
116
|
+
const current = this.load();
|
|
117
|
+
const next: ManualForward = { ...(current.forwards[nodeId] ?? {}) };
|
|
118
|
+
for (const kind of PORT_KINDS) {
|
|
119
|
+
const value = patch[kind];
|
|
120
|
+
if (value === undefined) continue;
|
|
121
|
+
if (value === null) delete next[kind];
|
|
122
|
+
else next[kind] = value;
|
|
123
|
+
}
|
|
124
|
+
const forwards = { ...current.forwards };
|
|
125
|
+
if (Object.keys(next).length === 0) delete forwards[nodeId];
|
|
126
|
+
else forwards[nodeId] = next;
|
|
127
|
+
const merged: NatSettingsFile = { ...current, forwards };
|
|
128
|
+
validate(merged);
|
|
129
|
+
this.save(merged);
|
|
130
|
+
return merged;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Drop a node's manual forward entry. No-op (no write) when there is none. */
|
|
134
|
+
deleteForward(nodeId: string): NatSettingsFile {
|
|
135
|
+
const current = this.load();
|
|
136
|
+
if (!(nodeId in current.forwards)) return current;
|
|
137
|
+
const forwards = { ...current.forwards };
|
|
138
|
+
delete forwards[nodeId];
|
|
139
|
+
const merged: NatSettingsFile = { ...current, forwards };
|
|
140
|
+
this.save(merged);
|
|
141
|
+
return merged;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function defaultSettings(): NatSettingsFile {
|
|
146
|
+
return {
|
|
147
|
+
version: FILE_VERSION,
|
|
148
|
+
network: 'auto',
|
|
149
|
+
upnpEnabled: true,
|
|
150
|
+
forwards: {},
|
|
151
|
+
ddns: defaultDdns(),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function defaultDdns(): NatDdnsSettings {
|
|
156
|
+
return {
|
|
157
|
+
providerId: null,
|
|
158
|
+
hostname: null,
|
|
159
|
+
externallyManaged: false,
|
|
160
|
+
intervalMs: DEFAULT_DDNS_INTERVAL_MS,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Build the settings from the known fields of a parsed file, or null when the
|
|
166
|
+
* shape is wrong. Unknown fields — including the `externalPort`/`internalPort`
|
|
167
|
+
* an older build wrote — are dropped, so the next save removes them.
|
|
168
|
+
*/
|
|
169
|
+
function fromParsed(v: unknown): NatSettingsFile | null {
|
|
170
|
+
if (!v || typeof v !== 'object') return null;
|
|
171
|
+
const obj = v as Record<string, unknown>;
|
|
172
|
+
if (obj.version !== FILE_VERSION) return null;
|
|
173
|
+
if (typeof obj.upnpEnabled !== 'boolean') return null;
|
|
174
|
+
// Absent in a file written before network modes existed; that reads as `auto`.
|
|
175
|
+
if (obj.network !== undefined && !isNetworkSetting(obj.network)) return null;
|
|
176
|
+
const ddns = obj.ddns as Record<string, unknown> | undefined;
|
|
177
|
+
if (!ddns || typeof ddns !== 'object') return null;
|
|
178
|
+
if (ddns.providerId !== null && typeof ddns.providerId !== 'string') return null;
|
|
179
|
+
if (ddns.hostname !== null && typeof ddns.hostname !== 'string') return null;
|
|
180
|
+
if (typeof ddns.externallyManaged !== 'boolean') return null;
|
|
181
|
+
if (typeof ddns.intervalMs !== 'number') return null;
|
|
182
|
+
const forwards = parseForwards(obj.forwards);
|
|
183
|
+
if (!forwards) return null;
|
|
184
|
+
return {
|
|
185
|
+
version: FILE_VERSION,
|
|
186
|
+
network: (obj.network as NetworkSetting | undefined) ?? 'auto',
|
|
187
|
+
upnpEnabled: obj.upnpEnabled,
|
|
188
|
+
forwards,
|
|
189
|
+
ddns: {
|
|
190
|
+
providerId: ddns.providerId as string | null,
|
|
191
|
+
hostname: ddns.hostname as string | null,
|
|
192
|
+
externallyManaged: ddns.externallyManaged,
|
|
193
|
+
intervalMs: ddns.intervalMs,
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** `forwards` is absent in a file written before manual forwards existed; that reads as none. */
|
|
199
|
+
function parseForwards(v: unknown): Record<string, ManualForward> | null {
|
|
200
|
+
if (v === undefined) return {};
|
|
201
|
+
if (!v || typeof v !== 'object' || Array.isArray(v)) return null;
|
|
202
|
+
const out: Record<string, ManualForward> = {};
|
|
203
|
+
for (const [nodeId, entry] of Object.entries(v as Record<string, unknown>)) {
|
|
204
|
+
if (!entry || typeof entry !== 'object') return null;
|
|
205
|
+
const rec = entry as Record<string, unknown>;
|
|
206
|
+
const forward: ManualForward = {};
|
|
207
|
+
for (const kind of PORT_KINDS) {
|
|
208
|
+
if (rec[kind] === undefined) continue;
|
|
209
|
+
if (typeof rec[kind] !== 'number') return null;
|
|
210
|
+
forward[kind] = rec[kind];
|
|
211
|
+
}
|
|
212
|
+
out[nodeId] = forward;
|
|
213
|
+
}
|
|
214
|
+
return out;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function isNetworkSetting(v: unknown): v is NetworkSetting {
|
|
218
|
+
return v === 'auto' || v === 'lan' || v === 'public';
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function validate(s: NatSettingsFile): void {
|
|
222
|
+
if (!isNetworkSetting(s.network)) {
|
|
223
|
+
throw new NatError('invalid_config', `network must be "auto", "lan" or "public", got "${String(s.network)}"`);
|
|
224
|
+
}
|
|
225
|
+
for (const [nodeId, forward] of Object.entries(s.forwards)) {
|
|
226
|
+
if (nodeId.length === 0) {
|
|
227
|
+
throw new NatError('invalid_config', 'a manual forward needs a node id');
|
|
228
|
+
}
|
|
229
|
+
for (const kind of PORT_KINDS) {
|
|
230
|
+
const port = forward[kind];
|
|
231
|
+
if (port === undefined) continue;
|
|
232
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
233
|
+
throw new NatError('invalid_config', `${kind} forward for ${nodeId} must be 1-65535, got ${port}`);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (!Number.isFinite(s.ddns.intervalMs) || s.ddns.intervalMs < 1000) {
|
|
238
|
+
throw new NatError(
|
|
239
|
+
'invalid_config',
|
|
240
|
+
`ddns.intervalMs must be >= 1000ms, got ${s.ddns.intervalMs}`,
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
if (s.ddns.providerId !== null && s.ddns.providerId.length === 0) {
|
|
244
|
+
throw new NatError('invalid_config', 'ddns.providerId must be a non-empty string or null');
|
|
245
|
+
}
|
|
246
|
+
if (s.ddns.hostname !== null && !isDnsHostname(s.ddns.hostname)) {
|
|
247
|
+
throw new NatError('invalid_config', `ddns.hostname must be a DNS name such as foo.duckdns.org or null, got "${s.ddns.hostname}"`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LAN or public-IP mode: whether this machine sits behind a router or holds a public IPv4
|
|
3
|
+
* on one of its interfaces. See `NetworkMode` in `types.ts` for what each changes.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { networkInterfaces, type NetworkInterfaceInfo } from 'node:os';
|
|
7
|
+
|
|
8
|
+
import { isPublicIpv4 } from './address-resolver.js';
|
|
9
|
+
import type { NetworkMode, NetworkSetting } from './types.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The first public IPv4 an interface holds, or null. A VPS's `eth0` holds its public address
|
|
13
|
+
* directly; a machine behind a router holds only private ones (its public address lives on the
|
|
14
|
+
* router). Docker bridges, VPNs and other private interfaces never qualify.
|
|
15
|
+
*
|
|
16
|
+
* NOTE: any interface counts, not only the one on the route to the internet, so a LAN machine
|
|
17
|
+
* with a public address on a second card or a tunnel reads as public (the `lan` setting fixes it);
|
|
18
|
+
* if that is reported, decide on the default route's source address instead (the UDP-connect probe
|
|
19
|
+
* in cadre-core's `primaryLanAddress`).
|
|
20
|
+
*/
|
|
21
|
+
export function publicInterfaceAddress(
|
|
22
|
+
interfaces: Record<string, NetworkInterfaceInfo[] | undefined> = networkInterfaces(),
|
|
23
|
+
): string | null {
|
|
24
|
+
for (const entries of Object.values(interfaces)) {
|
|
25
|
+
for (const entry of entries ?? []) {
|
|
26
|
+
if (entry.family === 'IPv4' && !entry.internal && isPublicIpv4(entry.address)) return entry.address;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The mode in effect: the setting, or for `auto`, public exactly when an interface holds a public IPv4. */
|
|
33
|
+
export function effectiveNetworkMode(setting: NetworkSetting, publicInterfaceIp: string | null): NetworkMode {
|
|
34
|
+
if (setting !== 'auto') return setting;
|
|
35
|
+
return publicInterfaceIp !== null ? 'public' : 'lan';
|
|
36
|
+
}
|