@serfab/cadre-host 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server/static.js +38 -38
- package/dist/ui/assets/index-C-iks41M.js.map +1 -1
- package/dist/ui/index.html +12 -12
- package/package.json +4 -4
- package/service/cadre-host.service.tmpl +16 -16
- package/service/com.serfab.cadre-host.plist.tmpl +35 -35
- package/service/install-service.ps1 +98 -98
- package/service/uninstall-service.ps1 +19 -19
- package/src/__tests__/build-targets.test.ts +26 -26
- package/src/__tests__/cli-invite.smoke.test.ts +192 -192
- package/src/__tests__/cli-nat.smoke.test.ts +212 -212
- package/src/__tests__/cli.smoke.test.ts +46 -46
- package/src/__tests__/global-setup.ts +51 -51
- package/src/__tests__/host-owner.smoke.test.ts +123 -123
- package/src/__tests__/orchestrator-env-scrub.test.ts +140 -140
- package/src/__tests__/orchestrator-node-identity.test.ts +204 -204
- package/src/__tests__/orchestrator-owner.test.ts +198 -198
- package/src/__tests__/orchestrator-pin-keys.test.ts +153 -153
- package/src/__tests__/orchestrator-push.test.ts +145 -145
- package/src/__tests__/orchestrator.test.ts +951 -951
- package/src/auth/__tests__/duration.test.ts +28 -28
- package/src/auth/__tests__/self-label.test.ts +85 -85
- package/src/auth/__tests__/trust-circle-store.test.ts +115 -115
- package/src/auth/__tests__/trust-circle.test.ts +445 -445
- package/src/auth/duration.ts +33 -33
- package/src/auth/index.ts +21 -21
- package/src/auth/self-label.ts +74 -74
- package/src/auth/trust-circle-store.ts +155 -155
- package/src/auth/types.ts +99 -99
- package/src/donation/__tests__/donation-service.test.ts +1068 -1068
- package/src/donation/__tests__/donation-supervisor.test.ts +510 -510
- package/src/donation/__tests__/fake-orchestrator.test.ts +168 -168
- package/src/donation/__tests__/fake-orchestrator.ts +225 -225
- package/src/donation/__tests__/grant-service.test.ts +213 -213
- package/src/donation/__tests__/grant-store.test.ts +113 -113
- package/src/donation/donation-service.ts +962 -962
- package/src/donation/donation-store.ts +171 -171
- package/src/donation/donation-supervisor.ts +388 -388
- package/src/donation/grant-service.ts +200 -200
- package/src/donation/grant-store.ts +152 -152
- package/src/donation/index.ts +64 -64
- package/src/donation/types.ts +243 -243
- package/src/installer/__tests__/identity.test.ts +34 -34
- package/src/installer/__tests__/paths.test.ts +47 -47
- package/src/installer/__tests__/service-host.test.ts +62 -62
- package/src/installer/__tests__/wizard.test.ts +64 -64
- package/src/installer/identity.ts +49 -49
- package/src/installer/paths.ts +109 -109
- package/src/installer/platform.ts +27 -27
- package/src/installer/service-host/index.ts +21 -21
- package/src/installer/service-host/template.ts +21 -21
- package/src/installer/wizard.ts +103 -103
- package/src/nat/__tests__/address-resolver.test.ts +82 -82
- package/src/nat/__tests__/ddns-duckdns.test.ts +83 -83
- package/src/nat/__tests__/ddns-updater.test.ts +253 -253
- package/src/nat/__tests__/external-ip.test.ts +124 -124
- package/src/nat/__tests__/nat-service.test.ts +497 -497
- package/src/nat/__tests__/nat-store.test.ts +93 -93
- package/src/nat/__tests__/port-mapper.test.ts +191 -191
- package/src/nat/__tests__/reachability.test.ts +24 -24
- package/src/nat/__tests__/secrets.test.ts +178 -178
- package/src/nat/address-resolver.ts +51 -51
- package/src/nat/ddns/duckdns.ts +76 -76
- package/src/nat/ddns/index.ts +55 -55
- package/src/nat/ddns/updater.ts +243 -243
- package/src/nat/external-ip.ts +154 -154
- package/src/nat/index.ts +71 -71
- package/src/nat/nat-service.ts +501 -501
- package/src/nat/nat-store.ts +154 -154
- package/src/nat/port-mapper.ts +329 -329
- package/src/nat/reachability.ts +34 -34
- package/src/nat/secrets/file-store.ts +130 -130
- package/src/nat/secrets/index.ts +112 -112
- package/src/nat/secrets/keytar-store.ts +67 -67
- package/src/nat/types.ts +159 -159
- package/src/orchestrator/__tests__/node-identity.test.ts +62 -62
- package/src/orchestrator/host-process-orchestrator.ts +1133 -1133
- package/src/orchestrator/index.ts +10 -10
- package/src/orchestrator/log-rotator.ts +53 -53
- package/src/orchestrator/node-identity.ts +40 -40
- package/src/orchestrator/pid-liveness.ts +24 -24
- package/src/orchestrator/port-allocator.ts +91 -91
- package/src/orchestrator/state-store.ts +90 -90
- package/src/orchestrator/types.ts +141 -141
- package/src/owner/__tests__/owner-node-client.test.ts +194 -194
- package/src/owner/index.ts +12 -12
- package/src/owner/owner-node-client.ts +259 -259
- package/src/push/__tests__/push.test.ts +90 -90
- package/src/push/index.ts +146 -146
- package/src/server/__tests__/bus.test.ts +38 -38
- package/src/server/__tests__/error-handler.test.ts +118 -118
- package/src/server/__tests__/grants-admin-route.test.ts +73 -73
- package/src/server/__tests__/grants-route.test.ts +263 -263
- package/src/server/__tests__/nodes-route.test.ts +259 -259
- package/src/server/__tests__/origin-guard.test.ts +72 -72
- package/src/server/__tests__/publishers.test.ts +216 -216
- package/src/server/__tests__/server.smoke.test.ts +253 -253
- package/src/server/__tests__/settings-route.test.ts +194 -194
- package/src/server/__tests__/sse-route.test.ts +119 -119
- package/src/server/__tests__/static.test.ts +145 -145
- package/src/server/__tests__/status-route.test.ts +134 -134
- package/src/server/__tests__/strands-route.test.ts +233 -233
- package/src/server/error-handler.ts +178 -178
- package/src/server/events/bus.ts +42 -42
- package/src/server/events/sse-route.ts +81 -81
- package/src/server/events/types.ts +23 -23
- package/src/server/index.ts +267 -267
- package/src/server/origin-guard.ts +84 -84
- package/src/server/routes/grants-admin.ts +41 -41
- package/src/server/routes/grants.ts +210 -210
- package/src/server/routes/nat.ts +63 -63
- package/src/server/routes/nodes.ts +170 -170
- package/src/server/routes/settings.ts +123 -123
- package/src/server/routes/status.ts +118 -118
- package/src/server/routes/strands.ts +54 -54
- package/src/server/routes/trust-circle.ts +49 -49
- package/src/server/routes/update.ts +43 -43
- package/src/server/server.ts +122 -122
- package/src/server/settings-store.ts +37 -37
- package/src/server/static.ts +207 -207
- package/src/strands/__tests__/strand-service.test.ts +165 -165
- package/src/strands/index.ts +14 -14
- package/src/strands/strand-service.ts +135 -135
- package/src/strands/types.ts +102 -102
- package/src/update/__tests__/apply.test.ts +158 -158
- package/src/update/__tests__/manifest.test.ts +128 -128
- package/src/update/__tests__/release-key.test.ts +42 -42
- package/src/update/__tests__/sign.test.ts +78 -78
- package/src/update/__tests__/store.test.ts +81 -81
- package/src/update/__tests__/update-service.integration.test.ts +256 -256
- package/src/update/__tests__/version.test.ts +31 -31
- package/src/update/apply.ts +190 -190
- package/src/update/index.ts +341 -341
- package/src/update/release-key.ts +71 -71
- package/src/update/sign.ts +74 -74
- package/src/update/store.ts +101 -101
- package/src/update/types.ts +109 -109
- package/src/update/version.ts +80 -80
|
@@ -1,497 +1,497 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
2
|
-
import { mkdtempSync, rmSync } from 'node:fs';
|
|
3
|
-
import { tmpdir } from 'node:os';
|
|
4
|
-
import { join } from 'node:path';
|
|
5
|
-
|
|
6
|
-
import { NatService, createNatHandlers } from '../nat-service.js';
|
|
7
|
-
import { ExternalIpDetector } from '../external-ip.js';
|
|
8
|
-
import type { PortMapper, PortMappingResult } from '../port-mapper.js';
|
|
9
|
-
import type { SecretsStore } from '../secrets/index.js';
|
|
10
|
-
import { ddnsAccount } from '../secrets/index.js';
|
|
11
|
-
import type { CadreNodeLike } from '../nat-service.js';
|
|
12
|
-
import { OwnerNodeUnavailableError } from '../../owner/owner-node-client.js';
|
|
13
|
-
|
|
14
|
-
class StubMapper implements PortMapper {
|
|
15
|
-
mapCalls = 0;
|
|
16
|
-
unmapCalls = 0;
|
|
17
|
-
stopCalls = 0;
|
|
18
|
-
externalIpValue: string | null = '203.0.113.10';
|
|
19
|
-
mapFails = false;
|
|
20
|
-
async map(o: { externalPort: number; internalPort: number; protocol?: 'tcp' | 'udp'; ttlMs?: number }): Promise<PortMappingResult> {
|
|
21
|
-
this.mapCalls++;
|
|
22
|
-
if (this.mapFails) throw new Error('router refused');
|
|
23
|
-
return {
|
|
24
|
-
mode: 'auto-upnp',
|
|
25
|
-
externalIp: this.externalIpValue,
|
|
26
|
-
externalPort: o.externalPort,
|
|
27
|
-
internalPort: o.internalPort,
|
|
28
|
-
protocol: o.protocol ?? 'tcp',
|
|
29
|
-
leaseExpiresAt: new Date(Date.now() + (o.ttlMs ?? 60_000)),
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
async verify() { return true; }
|
|
33
|
-
async externalIp() { return this.externalIpValue; }
|
|
34
|
-
async unmap() { this.unmapCalls++; }
|
|
35
|
-
async stop() { this.stopCalls++; }
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function makeSecrets(seed: Record<string, string> = {}): SecretsStore {
|
|
39
|
-
const m = new Map(Object.entries(seed));
|
|
40
|
-
return {
|
|
41
|
-
async set(a, v) { m.set(a, v); },
|
|
42
|
-
async get(a) { return m.has(a) ? m.get(a)! : null; },
|
|
43
|
-
async delete(a) { return m.delete(a); },
|
|
44
|
-
async list() { return [...m.keys()]; },
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function makeNode(peerId = '12D3KooWHost', addrs: string[] = ['/ip4/192.168.1.10/tcp/4001']): CadreNodeLike {
|
|
49
|
-
return {
|
|
50
|
-
getPeerId: () => peerId,
|
|
51
|
-
getMultiaddrs: () => addrs,
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* A node that mimics a freshly spawned owner child: `getPeerId` throws
|
|
57
|
-
* `OwnerNodeUnavailableError` for the first `failTimes` calls (admin channel
|
|
58
|
-
* not bound yet), then returns a real peer ID. Exposes the call count so tests
|
|
59
|
-
* can assert how many attempts the retry loop made.
|
|
60
|
-
*/
|
|
61
|
-
function makeFlakyNode(
|
|
62
|
-
failTimes: number,
|
|
63
|
-
peerId = '12D3KooWFlaky',
|
|
64
|
-
addrs: string[] = ['/ip4/192.168.1.10/tcp/4001'],
|
|
65
|
-
): { node: CadreNodeLike; peerIdCalls: () => number } {
|
|
66
|
-
let calls = 0;
|
|
67
|
-
const node: CadreNodeLike = {
|
|
68
|
-
getPeerId: async () => {
|
|
69
|
-
calls += 1;
|
|
70
|
-
if (calls <= failTimes) throw new OwnerNodeUnavailableError('admin channel not bound yet');
|
|
71
|
-
return peerId;
|
|
72
|
-
},
|
|
73
|
-
getMultiaddrs: async () => addrs,
|
|
74
|
-
};
|
|
75
|
-
return { node, peerIdCalls: () => calls };
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function makeDetector(opts: { router?: string | null; pub?: string | null }): ExternalIpDetector {
|
|
79
|
-
return new ExternalIpDetector({
|
|
80
|
-
routerProbe: opts.router === undefined ? undefined : async () => opts.router ?? null,
|
|
81
|
-
fetch: (async () => ({
|
|
82
|
-
ok: opts.pub != null,
|
|
83
|
-
status: opts.pub != null ? 200 : 500,
|
|
84
|
-
async text() { return opts.pub ?? ''; },
|
|
85
|
-
})) as unknown as typeof fetch,
|
|
86
|
-
publicIpUrls: ['https://stub.test'],
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
let tmpRoot: string;
|
|
91
|
-
beforeEach(() => {
|
|
92
|
-
tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-nat-svc-'));
|
|
93
|
-
});
|
|
94
|
-
afterEach(() => {
|
|
95
|
-
try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
describe('NatService', () => {
|
|
99
|
-
it('start() runs port mapping, detects IP, status reflects auto-upnp', async () => {
|
|
100
|
-
const mapper = new StubMapper();
|
|
101
|
-
const svc = new NatService({
|
|
102
|
-
rootDir: tmpRoot,
|
|
103
|
-
cadreNode: makeNode(),
|
|
104
|
-
secretsStore: makeSecrets(),
|
|
105
|
-
portMapper: mapper,
|
|
106
|
-
externalIpDetector: makeDetector({ router: '203.0.113.10', pub: '203.0.113.10' }),
|
|
107
|
-
});
|
|
108
|
-
await svc.start();
|
|
109
|
-
const s = svc.getStatus();
|
|
110
|
-
expect(s.portMode).toBe('auto-upnp');
|
|
111
|
-
expect(s.externalPort).toBe(4001);
|
|
112
|
-
expect(s.externalIp).toBe('203.0.113.10');
|
|
113
|
-
expect(s.directReachability).toBe('reachable');
|
|
114
|
-
expect(mapper.mapCalls).toBe(1);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('detects CGNAT and flags directReachability=cgnat', async () => {
|
|
118
|
-
const mapper = new StubMapper();
|
|
119
|
-
const svc = new NatService({
|
|
120
|
-
rootDir: tmpRoot,
|
|
121
|
-
cadreNode: makeNode(),
|
|
122
|
-
secretsStore: makeSecrets(),
|
|
123
|
-
portMapper: mapper,
|
|
124
|
-
externalIpDetector: makeDetector({ router: '100.64.0.5', pub: '203.0.113.10' }),
|
|
125
|
-
});
|
|
126
|
-
await svc.start();
|
|
127
|
-
const s = svc.getStatus();
|
|
128
|
-
expect(s.cgnatDetected).toBe(true);
|
|
129
|
-
expect(s.directReachability).toBe('cgnat');
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it('flips portMode to failed when mapper throws', async () => {
|
|
133
|
-
const mapper = new StubMapper();
|
|
134
|
-
mapper.mapFails = true;
|
|
135
|
-
const svc = new NatService({
|
|
136
|
-
rootDir: tmpRoot,
|
|
137
|
-
cadreNode: makeNode(),
|
|
138
|
-
secretsStore: makeSecrets(),
|
|
139
|
-
portMapper: mapper,
|
|
140
|
-
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
141
|
-
});
|
|
142
|
-
await svc.start();
|
|
143
|
-
expect(svc.getStatus().portMode).toBe('failed');
|
|
144
|
-
expect(svc.getStatus().directReachability).toBe('unreachable');
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it('stop() unmaps and is idempotent', async () => {
|
|
148
|
-
const mapper = new StubMapper();
|
|
149
|
-
const svc = new NatService({
|
|
150
|
-
rootDir: tmpRoot,
|
|
151
|
-
cadreNode: makeNode(),
|
|
152
|
-
secretsStore: makeSecrets(),
|
|
153
|
-
portMapper: mapper,
|
|
154
|
-
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
155
|
-
});
|
|
156
|
-
await svc.start();
|
|
157
|
-
await svc.stop();
|
|
158
|
-
await svc.stop();
|
|
159
|
-
expect(mapper.unmapCalls).toBe(1);
|
|
160
|
-
expect(mapper.stopCalls).toBe(1);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
it('getInviteAddresses → dns4 when DDNS configured + reachable', async () => {
|
|
164
|
-
const mapper = new StubMapper();
|
|
165
|
-
const secrets = makeSecrets({ [ddnsAccount('duckdns', 'token')]: 'T' });
|
|
166
|
-
const svc = new NatService({
|
|
167
|
-
rootDir: tmpRoot,
|
|
168
|
-
cadreNode: makeNode('12D3KooWHost'),
|
|
169
|
-
secretsStore: secrets,
|
|
170
|
-
portMapper: mapper,
|
|
171
|
-
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
172
|
-
});
|
|
173
|
-
await svc.start();
|
|
174
|
-
await svc.putDdns({
|
|
175
|
-
providerId: 'duckdns',
|
|
176
|
-
hostname: 'foo.duckdns.org',
|
|
177
|
-
config: { token: 'T' },
|
|
178
|
-
});
|
|
179
|
-
const addrs = await svc.getInviteAddresses();
|
|
180
|
-
expect(addrs).toEqual(['/dns4/foo.duckdns.org/tcp/4001/p2p/12D3KooWHost']);
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
it('getInviteAddresses → ip4 when reachable but no DDNS', async () => {
|
|
184
|
-
const mapper = new StubMapper();
|
|
185
|
-
const svc = new NatService({
|
|
186
|
-
rootDir: tmpRoot,
|
|
187
|
-
cadreNode: makeNode('12D3KooWHost'),
|
|
188
|
-
secretsStore: makeSecrets(),
|
|
189
|
-
portMapper: mapper,
|
|
190
|
-
externalIpDetector: makeDetector({ pub: '203.0.113.42' }),
|
|
191
|
-
});
|
|
192
|
-
await svc.start();
|
|
193
|
-
const addrs = await svc.getInviteAddresses();
|
|
194
|
-
expect(addrs).toEqual(['/ip4/203.0.113.42/tcp/4001/p2p/12D3KooWHost']);
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
it('getInviteAddresses → libp2p fallback when unreachable', async () => {
|
|
198
|
-
const mapper = new StubMapper();
|
|
199
|
-
mapper.mapFails = true;
|
|
200
|
-
const svc = new NatService({
|
|
201
|
-
rootDir: tmpRoot,
|
|
202
|
-
cadreNode: makeNode('12D3KooWHost', ['/ip4/10.0.0.5/tcp/4001']),
|
|
203
|
-
secretsStore: makeSecrets(),
|
|
204
|
-
portMapper: mapper,
|
|
205
|
-
externalIpDetector: makeDetector({ pub: null }),
|
|
206
|
-
});
|
|
207
|
-
await svc.start();
|
|
208
|
-
const addrs = await svc.getInviteAddresses();
|
|
209
|
-
expect(addrs).toEqual(['/ip4/10.0.0.5/tcp/4001/p2p/12D3KooWHost']);
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
it('putDdns persists secrets and updates status', async () => {
|
|
213
|
-
const secrets = makeSecrets();
|
|
214
|
-
const mapper = new StubMapper();
|
|
215
|
-
const svc = new NatService({
|
|
216
|
-
rootDir: tmpRoot,
|
|
217
|
-
cadreNode: makeNode(),
|
|
218
|
-
secretsStore: secrets,
|
|
219
|
-
portMapper: mapper,
|
|
220
|
-
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
221
|
-
});
|
|
222
|
-
await svc.start();
|
|
223
|
-
const status = await svc.putDdns({
|
|
224
|
-
providerId: 'duckdns',
|
|
225
|
-
hostname: 'h.duckdns.org',
|
|
226
|
-
config: { token: 'TOK' },
|
|
227
|
-
});
|
|
228
|
-
expect(status.ddns.providerId).toBe('duckdns');
|
|
229
|
-
expect(status.ddns.hostname).toBe('h.duckdns.org');
|
|
230
|
-
expect(await secrets.get(ddnsAccount('duckdns', 'token'))).toBe('TOK');
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
it('putDdns with unknown provider → ddns_provider_unknown', async () => {
|
|
234
|
-
const mapper = new StubMapper();
|
|
235
|
-
const svc = new NatService({
|
|
236
|
-
rootDir: tmpRoot,
|
|
237
|
-
cadreNode: makeNode(),
|
|
238
|
-
secretsStore: makeSecrets(),
|
|
239
|
-
portMapper: mapper,
|
|
240
|
-
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
241
|
-
});
|
|
242
|
-
await svc.start();
|
|
243
|
-
await expect(svc.putDdns({
|
|
244
|
-
providerId: 'no-such-provider',
|
|
245
|
-
hostname: 'x',
|
|
246
|
-
config: {},
|
|
247
|
-
})).rejects.toMatchObject({ code: 'ddns_provider_unknown' });
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
it('putSettings can disable UPnP and re-enable it', async () => {
|
|
251
|
-
const mapper = new StubMapper();
|
|
252
|
-
const svc = new NatService({
|
|
253
|
-
rootDir: tmpRoot,
|
|
254
|
-
cadreNode: makeNode(),
|
|
255
|
-
secretsStore: makeSecrets(),
|
|
256
|
-
portMapper: mapper,
|
|
257
|
-
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
258
|
-
});
|
|
259
|
-
await svc.start();
|
|
260
|
-
expect(mapper.mapCalls).toBe(1);
|
|
261
|
-
|
|
262
|
-
let s = await svc.putSettings({ upnpEnabled: false });
|
|
263
|
-
expect(s.portMode).toBe('disabled');
|
|
264
|
-
|
|
265
|
-
s = await svc.putSettings({ upnpEnabled: true });
|
|
266
|
-
expect(s.portMode).toBe('auto-upnp');
|
|
267
|
-
expect(mapper.mapCalls).toBeGreaterThanOrEqual(2);
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
it('testReachability re-runs IP detect and updates lastTestedAt', async () => {
|
|
271
|
-
const mapper = new StubMapper();
|
|
272
|
-
const svc = new NatService({
|
|
273
|
-
rootDir: tmpRoot,
|
|
274
|
-
cadreNode: makeNode(),
|
|
275
|
-
secretsStore: makeSecrets(),
|
|
276
|
-
portMapper: mapper,
|
|
277
|
-
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
278
|
-
});
|
|
279
|
-
await svc.start();
|
|
280
|
-
expect(svc.getStatus().lastTestedAt).toBeNull();
|
|
281
|
-
await svc.testReachability();
|
|
282
|
-
expect(svc.getStatus().lastTestedAt).not.toBeNull();
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
it('listDdnsProviders returns at least DuckDNS', () => {
|
|
286
|
-
const svc = new NatService({
|
|
287
|
-
rootDir: tmpRoot,
|
|
288
|
-
cadreNode: makeNode(),
|
|
289
|
-
secretsStore: makeSecrets(),
|
|
290
|
-
portMapper: new StubMapper(),
|
|
291
|
-
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
292
|
-
});
|
|
293
|
-
const providers = svc.listDdnsProviders();
|
|
294
|
-
const duck = providers.find((p) => p.id === 'duckdns');
|
|
295
|
-
expect(duck).toBeDefined();
|
|
296
|
-
expect(duck!.configFields[0]).toMatchObject({ key: 'token', secret: true });
|
|
297
|
-
});
|
|
298
|
-
});
|
|
299
|
-
|
|
300
|
-
describe('NatService — async channel node + onAddressesChanged', () => {
|
|
301
|
-
it('getInviteAddresses awaits async getPeerId/getMultiaddrs from the channel', async () => {
|
|
302
|
-
const asyncNode: CadreNodeLike = {
|
|
303
|
-
getPeerId: async () => '12D3KooWAsync',
|
|
304
|
-
getMultiaddrs: async () => ['/ip4/10.0.0.9/tcp/4001'],
|
|
305
|
-
};
|
|
306
|
-
const svc = new NatService({
|
|
307
|
-
rootDir: tmpRoot,
|
|
308
|
-
cadreNode: asyncNode,
|
|
309
|
-
secretsStore: makeSecrets(),
|
|
310
|
-
portMapper: new StubMapper(),
|
|
311
|
-
externalIpDetector: makeDetector({ pub: '203.0.113.50' }),
|
|
312
|
-
});
|
|
313
|
-
await svc.start();
|
|
314
|
-
const addrs = await svc.getInviteAddresses();
|
|
315
|
-
expect(addrs).toEqual(['/ip4/203.0.113.50/tcp/4001/p2p/12D3KooWAsync']);
|
|
316
|
-
});
|
|
317
|
-
|
|
318
|
-
it('throws node_unavailable when the node reports an empty peer ID (not ready)', async () => {
|
|
319
|
-
const notReadyNode: CadreNodeLike = {
|
|
320
|
-
getPeerId: async () => '',
|
|
321
|
-
getMultiaddrs: async () => [],
|
|
322
|
-
};
|
|
323
|
-
const svc = new NatService({
|
|
324
|
-
rootDir: tmpRoot,
|
|
325
|
-
cadreNode: notReadyNode,
|
|
326
|
-
secretsStore: makeSecrets(),
|
|
327
|
-
portMapper: new StubMapper(),
|
|
328
|
-
externalIpDetector: makeDetector({ pub: '203.0.113.50' }),
|
|
329
|
-
});
|
|
330
|
-
await svc.start();
|
|
331
|
-
await expect(svc.getInviteAddresses()).rejects.toMatchObject({ code: 'node_unavailable' });
|
|
332
|
-
});
|
|
333
|
-
|
|
334
|
-
it('fires onAddressesChanged on putSettings and testReachability', async () => {
|
|
335
|
-
const svc = new NatService({
|
|
336
|
-
rootDir: tmpRoot,
|
|
337
|
-
cadreNode: makeNode('12D3KooWHook'),
|
|
338
|
-
secretsStore: makeSecrets(),
|
|
339
|
-
portMapper: new StubMapper(),
|
|
340
|
-
externalIpDetector: makeDetector({ pub: '203.0.113.7' }),
|
|
341
|
-
});
|
|
342
|
-
await svc.start();
|
|
343
|
-
|
|
344
|
-
const fired: string[][] = [];
|
|
345
|
-
svc.onAddressesChanged((addrs) => { fired.push(addrs); });
|
|
346
|
-
|
|
347
|
-
await svc.putSettings({ externalPort: 4002 });
|
|
348
|
-
await svc.testReachability();
|
|
349
|
-
|
|
350
|
-
expect(fired.length).toBeGreaterThanOrEqual(2);
|
|
351
|
-
// The pushed addresses carry the node's peer ID.
|
|
352
|
-
expect(fired[fired.length - 1]!.every((a: string) => a.includes('12D3KooWHook'))).toBe(true);
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
it('onAddressesChanged unsubscribe stops further notifications', async () => {
|
|
356
|
-
const svc = new NatService({
|
|
357
|
-
rootDir: tmpRoot,
|
|
358
|
-
cadreNode: makeNode(),
|
|
359
|
-
secretsStore: makeSecrets(),
|
|
360
|
-
portMapper: new StubMapper(),
|
|
361
|
-
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
362
|
-
});
|
|
363
|
-
await svc.start();
|
|
364
|
-
let count = 0;
|
|
365
|
-
const off = svc.onAddressesChanged(() => { count++; });
|
|
366
|
-
await svc.testReachability();
|
|
367
|
-
off();
|
|
368
|
-
await svc.testReachability();
|
|
369
|
-
expect(count).toBe(1);
|
|
370
|
-
});
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
describe('NatService — initial invite-address push retry', () => {
|
|
374
|
-
it('delivers the initial push exactly once when the node is ready on the first attempt', async () => {
|
|
375
|
-
// The common production happy path: node bound fast, listener registered
|
|
376
|
-
// BEFORE start(). No retry needed — but the push must still fire (once).
|
|
377
|
-
const { node, peerIdCalls } = makeFlakyNode(0, '12D3KooWReady');
|
|
378
|
-
const svc = new NatService({
|
|
379
|
-
rootDir: tmpRoot,
|
|
380
|
-
cadreNode: node,
|
|
381
|
-
secretsStore: makeSecrets(),
|
|
382
|
-
portMapper: new StubMapper(),
|
|
383
|
-
externalIpDetector: makeDetector({ pub: '203.0.113.42' }),
|
|
384
|
-
initialPushRetryMs: 1,
|
|
385
|
-
initialPushTimeoutMs: 2_000,
|
|
386
|
-
});
|
|
387
|
-
|
|
388
|
-
const fired: string[][] = [];
|
|
389
|
-
svc.onAddressesChanged((addrs) => { fired.push(addrs); });
|
|
390
|
-
|
|
391
|
-
await svc.start();
|
|
392
|
-
|
|
393
|
-
expect(fired).toEqual([['/ip4/203.0.113.42/tcp/4001/p2p/12D3KooWReady']]);
|
|
394
|
-
expect(peerIdCalls()).toBe(1); // succeeded on the first attempt, no retry
|
|
395
|
-
});
|
|
396
|
-
|
|
397
|
-
it('retries past a not-yet-ready node and delivers the push exactly once', async () => {
|
|
398
|
-
const { node, peerIdCalls } = makeFlakyNode(3, '12D3KooWFlaky');
|
|
399
|
-
const svc = new NatService({
|
|
400
|
-
rootDir: tmpRoot,
|
|
401
|
-
cadreNode: node,
|
|
402
|
-
secretsStore: makeSecrets(),
|
|
403
|
-
portMapper: new StubMapper(),
|
|
404
|
-
externalIpDetector: makeDetector({ pub: '203.0.113.42' }),
|
|
405
|
-
initialPushRetryMs: 1,
|
|
406
|
-
initialPushTimeoutMs: 2_000,
|
|
407
|
-
});
|
|
408
|
-
|
|
409
|
-
// Listener registered BEFORE start() — this is the race the fix closes.
|
|
410
|
-
const fired: string[][] = [];
|
|
411
|
-
svc.onAddressesChanged((addrs) => { fired.push(addrs); });
|
|
412
|
-
|
|
413
|
-
await svc.start();
|
|
414
|
-
|
|
415
|
-
// Delivered exactly once, with the NAT-resolved address carrying the peer ID.
|
|
416
|
-
expect(fired).toEqual([['/ip4/203.0.113.42/tcp/4001/p2p/12D3KooWFlaky']]);
|
|
417
|
-
// It took the failing attempts plus the successful one (proves it retried).
|
|
418
|
-
expect(peerIdCalls()).toBe(4);
|
|
419
|
-
});
|
|
420
|
-
|
|
421
|
-
it('gives up after the bounded timeout but still resolves start() (best-effort)', async () => {
|
|
422
|
-
// Always unavailable — the retry budget elapses without a successful push.
|
|
423
|
-
const node: CadreNodeLike = {
|
|
424
|
-
getPeerId: async () => { throw new OwnerNodeUnavailableError('never ready'); },
|
|
425
|
-
getMultiaddrs: async () => [],
|
|
426
|
-
};
|
|
427
|
-
const svc = new NatService({
|
|
428
|
-
rootDir: tmpRoot,
|
|
429
|
-
cadreNode: node,
|
|
430
|
-
secretsStore: makeSecrets(),
|
|
431
|
-
portMapper: new StubMapper(),
|
|
432
|
-
externalIpDetector: makeDetector({ pub: '203.0.113.42' }),
|
|
433
|
-
initialPushRetryMs: 2,
|
|
434
|
-
initialPushTimeoutMs: 20,
|
|
435
|
-
});
|
|
436
|
-
|
|
437
|
-
const fired: string[][] = [];
|
|
438
|
-
svc.onAddressesChanged((addrs) => { fired.push(addrs); });
|
|
439
|
-
|
|
440
|
-
await svc.start(); // resolves despite the node never coming up
|
|
441
|
-
expect(fired).toEqual([]); // best-effort: nothing delivered
|
|
442
|
-
// The process is otherwise healthy — status still builds.
|
|
443
|
-
expect(svc.getStatus().portMode).toBe('auto-upnp');
|
|
444
|
-
});
|
|
445
|
-
|
|
446
|
-
it('does not retry on a non-node_unavailable error', async () => {
|
|
447
|
-
let calls = 0;
|
|
448
|
-
const node: CadreNodeLike = {
|
|
449
|
-
getPeerId: async () => { calls += 1; throw new Error('boom'); },
|
|
450
|
-
getMultiaddrs: async () => [],
|
|
451
|
-
};
|
|
452
|
-
const svc = new NatService({
|
|
453
|
-
rootDir: tmpRoot,
|
|
454
|
-
cadreNode: node,
|
|
455
|
-
secretsStore: makeSecrets(),
|
|
456
|
-
portMapper: new StubMapper(),
|
|
457
|
-
externalIpDetector: makeDetector({ pub: '203.0.113.42' }),
|
|
458
|
-
initialPushRetryMs: 1,
|
|
459
|
-
initialPushTimeoutMs: 2_000,
|
|
460
|
-
});
|
|
461
|
-
svc.onAddressesChanged(() => { /* registered before start */ });
|
|
462
|
-
|
|
463
|
-
await svc.start(); // does not hang retrying a non-transient failure
|
|
464
|
-
expect(calls).toBe(1);
|
|
465
|
-
});
|
|
466
|
-
});
|
|
467
|
-
|
|
468
|
-
describe('createNatHandlers', () => {
|
|
469
|
-
it('every handler delegates to the service', async () => {
|
|
470
|
-
const mapper = new StubMapper();
|
|
471
|
-
const svc = new NatService({
|
|
472
|
-
rootDir: tmpRoot,
|
|
473
|
-
cadreNode: makeNode(),
|
|
474
|
-
secretsStore: makeSecrets(),
|
|
475
|
-
portMapper: mapper,
|
|
476
|
-
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
477
|
-
});
|
|
478
|
-
await svc.start();
|
|
479
|
-
const h = createNatHandlers(svc);
|
|
480
|
-
|
|
481
|
-
expect((await h.getStatus()).portMode).toBe('auto-upnp');
|
|
482
|
-
expect((await h.listDdnsProviders()).find((p) => p.id === 'duckdns')).toBeDefined();
|
|
483
|
-
|
|
484
|
-
const status = await h.testReachability();
|
|
485
|
-
expect(status.lastTestedAt).not.toBeNull();
|
|
486
|
-
|
|
487
|
-
const after = await h.putSettings({ externalPort: 4002 });
|
|
488
|
-
expect(after.externalPort).toBe(4002);
|
|
489
|
-
|
|
490
|
-
const ddnsStatus = await h.putDdns({
|
|
491
|
-
providerId: 'duckdns',
|
|
492
|
-
hostname: 'a.duckdns.org',
|
|
493
|
-
config: { token: 'T' },
|
|
494
|
-
});
|
|
495
|
-
expect(ddnsStatus.ddns.hostname).toBe('a.duckdns.org');
|
|
496
|
-
});
|
|
497
|
-
});
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
2
|
+
import { mkdtempSync, rmSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
|
|
6
|
+
import { NatService, createNatHandlers } from '../nat-service.js';
|
|
7
|
+
import { ExternalIpDetector } from '../external-ip.js';
|
|
8
|
+
import type { PortMapper, PortMappingResult } from '../port-mapper.js';
|
|
9
|
+
import type { SecretsStore } from '../secrets/index.js';
|
|
10
|
+
import { ddnsAccount } from '../secrets/index.js';
|
|
11
|
+
import type { CadreNodeLike } from '../nat-service.js';
|
|
12
|
+
import { OwnerNodeUnavailableError } from '../../owner/owner-node-client.js';
|
|
13
|
+
|
|
14
|
+
class StubMapper implements PortMapper {
|
|
15
|
+
mapCalls = 0;
|
|
16
|
+
unmapCalls = 0;
|
|
17
|
+
stopCalls = 0;
|
|
18
|
+
externalIpValue: string | null = '203.0.113.10';
|
|
19
|
+
mapFails = false;
|
|
20
|
+
async map(o: { externalPort: number; internalPort: number; protocol?: 'tcp' | 'udp'; ttlMs?: number }): Promise<PortMappingResult> {
|
|
21
|
+
this.mapCalls++;
|
|
22
|
+
if (this.mapFails) throw new Error('router refused');
|
|
23
|
+
return {
|
|
24
|
+
mode: 'auto-upnp',
|
|
25
|
+
externalIp: this.externalIpValue,
|
|
26
|
+
externalPort: o.externalPort,
|
|
27
|
+
internalPort: o.internalPort,
|
|
28
|
+
protocol: o.protocol ?? 'tcp',
|
|
29
|
+
leaseExpiresAt: new Date(Date.now() + (o.ttlMs ?? 60_000)),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
async verify() { return true; }
|
|
33
|
+
async externalIp() { return this.externalIpValue; }
|
|
34
|
+
async unmap() { this.unmapCalls++; }
|
|
35
|
+
async stop() { this.stopCalls++; }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function makeSecrets(seed: Record<string, string> = {}): SecretsStore {
|
|
39
|
+
const m = new Map(Object.entries(seed));
|
|
40
|
+
return {
|
|
41
|
+
async set(a, v) { m.set(a, v); },
|
|
42
|
+
async get(a) { return m.has(a) ? m.get(a)! : null; },
|
|
43
|
+
async delete(a) { return m.delete(a); },
|
|
44
|
+
async list() { return [...m.keys()]; },
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function makeNode(peerId = '12D3KooWHost', addrs: string[] = ['/ip4/192.168.1.10/tcp/4001']): CadreNodeLike {
|
|
49
|
+
return {
|
|
50
|
+
getPeerId: () => peerId,
|
|
51
|
+
getMultiaddrs: () => addrs,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A node that mimics a freshly spawned owner child: `getPeerId` throws
|
|
57
|
+
* `OwnerNodeUnavailableError` for the first `failTimes` calls (admin channel
|
|
58
|
+
* not bound yet), then returns a real peer ID. Exposes the call count so tests
|
|
59
|
+
* can assert how many attempts the retry loop made.
|
|
60
|
+
*/
|
|
61
|
+
function makeFlakyNode(
|
|
62
|
+
failTimes: number,
|
|
63
|
+
peerId = '12D3KooWFlaky',
|
|
64
|
+
addrs: string[] = ['/ip4/192.168.1.10/tcp/4001'],
|
|
65
|
+
): { node: CadreNodeLike; peerIdCalls: () => number } {
|
|
66
|
+
let calls = 0;
|
|
67
|
+
const node: CadreNodeLike = {
|
|
68
|
+
getPeerId: async () => {
|
|
69
|
+
calls += 1;
|
|
70
|
+
if (calls <= failTimes) throw new OwnerNodeUnavailableError('admin channel not bound yet');
|
|
71
|
+
return peerId;
|
|
72
|
+
},
|
|
73
|
+
getMultiaddrs: async () => addrs,
|
|
74
|
+
};
|
|
75
|
+
return { node, peerIdCalls: () => calls };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function makeDetector(opts: { router?: string | null; pub?: string | null }): ExternalIpDetector {
|
|
79
|
+
return new ExternalIpDetector({
|
|
80
|
+
routerProbe: opts.router === undefined ? undefined : async () => opts.router ?? null,
|
|
81
|
+
fetch: (async () => ({
|
|
82
|
+
ok: opts.pub != null,
|
|
83
|
+
status: opts.pub != null ? 200 : 500,
|
|
84
|
+
async text() { return opts.pub ?? ''; },
|
|
85
|
+
})) as unknown as typeof fetch,
|
|
86
|
+
publicIpUrls: ['https://stub.test'],
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
let tmpRoot: string;
|
|
91
|
+
beforeEach(() => {
|
|
92
|
+
tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-nat-svc-'));
|
|
93
|
+
});
|
|
94
|
+
afterEach(() => {
|
|
95
|
+
try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('NatService', () => {
|
|
99
|
+
it('start() runs port mapping, detects IP, status reflects auto-upnp', async () => {
|
|
100
|
+
const mapper = new StubMapper();
|
|
101
|
+
const svc = new NatService({
|
|
102
|
+
rootDir: tmpRoot,
|
|
103
|
+
cadreNode: makeNode(),
|
|
104
|
+
secretsStore: makeSecrets(),
|
|
105
|
+
portMapper: mapper,
|
|
106
|
+
externalIpDetector: makeDetector({ router: '203.0.113.10', pub: '203.0.113.10' }),
|
|
107
|
+
});
|
|
108
|
+
await svc.start();
|
|
109
|
+
const s = svc.getStatus();
|
|
110
|
+
expect(s.portMode).toBe('auto-upnp');
|
|
111
|
+
expect(s.externalPort).toBe(4001);
|
|
112
|
+
expect(s.externalIp).toBe('203.0.113.10');
|
|
113
|
+
expect(s.directReachability).toBe('reachable');
|
|
114
|
+
expect(mapper.mapCalls).toBe(1);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('detects CGNAT and flags directReachability=cgnat', async () => {
|
|
118
|
+
const mapper = new StubMapper();
|
|
119
|
+
const svc = new NatService({
|
|
120
|
+
rootDir: tmpRoot,
|
|
121
|
+
cadreNode: makeNode(),
|
|
122
|
+
secretsStore: makeSecrets(),
|
|
123
|
+
portMapper: mapper,
|
|
124
|
+
externalIpDetector: makeDetector({ router: '100.64.0.5', pub: '203.0.113.10' }),
|
|
125
|
+
});
|
|
126
|
+
await svc.start();
|
|
127
|
+
const s = svc.getStatus();
|
|
128
|
+
expect(s.cgnatDetected).toBe(true);
|
|
129
|
+
expect(s.directReachability).toBe('cgnat');
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('flips portMode to failed when mapper throws', async () => {
|
|
133
|
+
const mapper = new StubMapper();
|
|
134
|
+
mapper.mapFails = true;
|
|
135
|
+
const svc = new NatService({
|
|
136
|
+
rootDir: tmpRoot,
|
|
137
|
+
cadreNode: makeNode(),
|
|
138
|
+
secretsStore: makeSecrets(),
|
|
139
|
+
portMapper: mapper,
|
|
140
|
+
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
141
|
+
});
|
|
142
|
+
await svc.start();
|
|
143
|
+
expect(svc.getStatus().portMode).toBe('failed');
|
|
144
|
+
expect(svc.getStatus().directReachability).toBe('unreachable');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('stop() unmaps and is idempotent', async () => {
|
|
148
|
+
const mapper = new StubMapper();
|
|
149
|
+
const svc = new NatService({
|
|
150
|
+
rootDir: tmpRoot,
|
|
151
|
+
cadreNode: makeNode(),
|
|
152
|
+
secretsStore: makeSecrets(),
|
|
153
|
+
portMapper: mapper,
|
|
154
|
+
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
155
|
+
});
|
|
156
|
+
await svc.start();
|
|
157
|
+
await svc.stop();
|
|
158
|
+
await svc.stop();
|
|
159
|
+
expect(mapper.unmapCalls).toBe(1);
|
|
160
|
+
expect(mapper.stopCalls).toBe(1);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('getInviteAddresses → dns4 when DDNS configured + reachable', async () => {
|
|
164
|
+
const mapper = new StubMapper();
|
|
165
|
+
const secrets = makeSecrets({ [ddnsAccount('duckdns', 'token')]: 'T' });
|
|
166
|
+
const svc = new NatService({
|
|
167
|
+
rootDir: tmpRoot,
|
|
168
|
+
cadreNode: makeNode('12D3KooWHost'),
|
|
169
|
+
secretsStore: secrets,
|
|
170
|
+
portMapper: mapper,
|
|
171
|
+
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
172
|
+
});
|
|
173
|
+
await svc.start();
|
|
174
|
+
await svc.putDdns({
|
|
175
|
+
providerId: 'duckdns',
|
|
176
|
+
hostname: 'foo.duckdns.org',
|
|
177
|
+
config: { token: 'T' },
|
|
178
|
+
});
|
|
179
|
+
const addrs = await svc.getInviteAddresses();
|
|
180
|
+
expect(addrs).toEqual(['/dns4/foo.duckdns.org/tcp/4001/p2p/12D3KooWHost']);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('getInviteAddresses → ip4 when reachable but no DDNS', async () => {
|
|
184
|
+
const mapper = new StubMapper();
|
|
185
|
+
const svc = new NatService({
|
|
186
|
+
rootDir: tmpRoot,
|
|
187
|
+
cadreNode: makeNode('12D3KooWHost'),
|
|
188
|
+
secretsStore: makeSecrets(),
|
|
189
|
+
portMapper: mapper,
|
|
190
|
+
externalIpDetector: makeDetector({ pub: '203.0.113.42' }),
|
|
191
|
+
});
|
|
192
|
+
await svc.start();
|
|
193
|
+
const addrs = await svc.getInviteAddresses();
|
|
194
|
+
expect(addrs).toEqual(['/ip4/203.0.113.42/tcp/4001/p2p/12D3KooWHost']);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('getInviteAddresses → libp2p fallback when unreachable', async () => {
|
|
198
|
+
const mapper = new StubMapper();
|
|
199
|
+
mapper.mapFails = true;
|
|
200
|
+
const svc = new NatService({
|
|
201
|
+
rootDir: tmpRoot,
|
|
202
|
+
cadreNode: makeNode('12D3KooWHost', ['/ip4/10.0.0.5/tcp/4001']),
|
|
203
|
+
secretsStore: makeSecrets(),
|
|
204
|
+
portMapper: mapper,
|
|
205
|
+
externalIpDetector: makeDetector({ pub: null }),
|
|
206
|
+
});
|
|
207
|
+
await svc.start();
|
|
208
|
+
const addrs = await svc.getInviteAddresses();
|
|
209
|
+
expect(addrs).toEqual(['/ip4/10.0.0.5/tcp/4001/p2p/12D3KooWHost']);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('putDdns persists secrets and updates status', async () => {
|
|
213
|
+
const secrets = makeSecrets();
|
|
214
|
+
const mapper = new StubMapper();
|
|
215
|
+
const svc = new NatService({
|
|
216
|
+
rootDir: tmpRoot,
|
|
217
|
+
cadreNode: makeNode(),
|
|
218
|
+
secretsStore: secrets,
|
|
219
|
+
portMapper: mapper,
|
|
220
|
+
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
221
|
+
});
|
|
222
|
+
await svc.start();
|
|
223
|
+
const status = await svc.putDdns({
|
|
224
|
+
providerId: 'duckdns',
|
|
225
|
+
hostname: 'h.duckdns.org',
|
|
226
|
+
config: { token: 'TOK' },
|
|
227
|
+
});
|
|
228
|
+
expect(status.ddns.providerId).toBe('duckdns');
|
|
229
|
+
expect(status.ddns.hostname).toBe('h.duckdns.org');
|
|
230
|
+
expect(await secrets.get(ddnsAccount('duckdns', 'token'))).toBe('TOK');
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it('putDdns with unknown provider → ddns_provider_unknown', async () => {
|
|
234
|
+
const mapper = new StubMapper();
|
|
235
|
+
const svc = new NatService({
|
|
236
|
+
rootDir: tmpRoot,
|
|
237
|
+
cadreNode: makeNode(),
|
|
238
|
+
secretsStore: makeSecrets(),
|
|
239
|
+
portMapper: mapper,
|
|
240
|
+
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
241
|
+
});
|
|
242
|
+
await svc.start();
|
|
243
|
+
await expect(svc.putDdns({
|
|
244
|
+
providerId: 'no-such-provider',
|
|
245
|
+
hostname: 'x',
|
|
246
|
+
config: {},
|
|
247
|
+
})).rejects.toMatchObject({ code: 'ddns_provider_unknown' });
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it('putSettings can disable UPnP and re-enable it', async () => {
|
|
251
|
+
const mapper = new StubMapper();
|
|
252
|
+
const svc = new NatService({
|
|
253
|
+
rootDir: tmpRoot,
|
|
254
|
+
cadreNode: makeNode(),
|
|
255
|
+
secretsStore: makeSecrets(),
|
|
256
|
+
portMapper: mapper,
|
|
257
|
+
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
258
|
+
});
|
|
259
|
+
await svc.start();
|
|
260
|
+
expect(mapper.mapCalls).toBe(1);
|
|
261
|
+
|
|
262
|
+
let s = await svc.putSettings({ upnpEnabled: false });
|
|
263
|
+
expect(s.portMode).toBe('disabled');
|
|
264
|
+
|
|
265
|
+
s = await svc.putSettings({ upnpEnabled: true });
|
|
266
|
+
expect(s.portMode).toBe('auto-upnp');
|
|
267
|
+
expect(mapper.mapCalls).toBeGreaterThanOrEqual(2);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it('testReachability re-runs IP detect and updates lastTestedAt', async () => {
|
|
271
|
+
const mapper = new StubMapper();
|
|
272
|
+
const svc = new NatService({
|
|
273
|
+
rootDir: tmpRoot,
|
|
274
|
+
cadreNode: makeNode(),
|
|
275
|
+
secretsStore: makeSecrets(),
|
|
276
|
+
portMapper: mapper,
|
|
277
|
+
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
278
|
+
});
|
|
279
|
+
await svc.start();
|
|
280
|
+
expect(svc.getStatus().lastTestedAt).toBeNull();
|
|
281
|
+
await svc.testReachability();
|
|
282
|
+
expect(svc.getStatus().lastTestedAt).not.toBeNull();
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('listDdnsProviders returns at least DuckDNS', () => {
|
|
286
|
+
const svc = new NatService({
|
|
287
|
+
rootDir: tmpRoot,
|
|
288
|
+
cadreNode: makeNode(),
|
|
289
|
+
secretsStore: makeSecrets(),
|
|
290
|
+
portMapper: new StubMapper(),
|
|
291
|
+
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
292
|
+
});
|
|
293
|
+
const providers = svc.listDdnsProviders();
|
|
294
|
+
const duck = providers.find((p) => p.id === 'duckdns');
|
|
295
|
+
expect(duck).toBeDefined();
|
|
296
|
+
expect(duck!.configFields[0]).toMatchObject({ key: 'token', secret: true });
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
describe('NatService — async channel node + onAddressesChanged', () => {
|
|
301
|
+
it('getInviteAddresses awaits async getPeerId/getMultiaddrs from the channel', async () => {
|
|
302
|
+
const asyncNode: CadreNodeLike = {
|
|
303
|
+
getPeerId: async () => '12D3KooWAsync',
|
|
304
|
+
getMultiaddrs: async () => ['/ip4/10.0.0.9/tcp/4001'],
|
|
305
|
+
};
|
|
306
|
+
const svc = new NatService({
|
|
307
|
+
rootDir: tmpRoot,
|
|
308
|
+
cadreNode: asyncNode,
|
|
309
|
+
secretsStore: makeSecrets(),
|
|
310
|
+
portMapper: new StubMapper(),
|
|
311
|
+
externalIpDetector: makeDetector({ pub: '203.0.113.50' }),
|
|
312
|
+
});
|
|
313
|
+
await svc.start();
|
|
314
|
+
const addrs = await svc.getInviteAddresses();
|
|
315
|
+
expect(addrs).toEqual(['/ip4/203.0.113.50/tcp/4001/p2p/12D3KooWAsync']);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it('throws node_unavailable when the node reports an empty peer ID (not ready)', async () => {
|
|
319
|
+
const notReadyNode: CadreNodeLike = {
|
|
320
|
+
getPeerId: async () => '',
|
|
321
|
+
getMultiaddrs: async () => [],
|
|
322
|
+
};
|
|
323
|
+
const svc = new NatService({
|
|
324
|
+
rootDir: tmpRoot,
|
|
325
|
+
cadreNode: notReadyNode,
|
|
326
|
+
secretsStore: makeSecrets(),
|
|
327
|
+
portMapper: new StubMapper(),
|
|
328
|
+
externalIpDetector: makeDetector({ pub: '203.0.113.50' }),
|
|
329
|
+
});
|
|
330
|
+
await svc.start();
|
|
331
|
+
await expect(svc.getInviteAddresses()).rejects.toMatchObject({ code: 'node_unavailable' });
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it('fires onAddressesChanged on putSettings and testReachability', async () => {
|
|
335
|
+
const svc = new NatService({
|
|
336
|
+
rootDir: tmpRoot,
|
|
337
|
+
cadreNode: makeNode('12D3KooWHook'),
|
|
338
|
+
secretsStore: makeSecrets(),
|
|
339
|
+
portMapper: new StubMapper(),
|
|
340
|
+
externalIpDetector: makeDetector({ pub: '203.0.113.7' }),
|
|
341
|
+
});
|
|
342
|
+
await svc.start();
|
|
343
|
+
|
|
344
|
+
const fired: string[][] = [];
|
|
345
|
+
svc.onAddressesChanged((addrs) => { fired.push(addrs); });
|
|
346
|
+
|
|
347
|
+
await svc.putSettings({ externalPort: 4002 });
|
|
348
|
+
await svc.testReachability();
|
|
349
|
+
|
|
350
|
+
expect(fired.length).toBeGreaterThanOrEqual(2);
|
|
351
|
+
// The pushed addresses carry the node's peer ID.
|
|
352
|
+
expect(fired[fired.length - 1]!.every((a: string) => a.includes('12D3KooWHook'))).toBe(true);
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it('onAddressesChanged unsubscribe stops further notifications', async () => {
|
|
356
|
+
const svc = new NatService({
|
|
357
|
+
rootDir: tmpRoot,
|
|
358
|
+
cadreNode: makeNode(),
|
|
359
|
+
secretsStore: makeSecrets(),
|
|
360
|
+
portMapper: new StubMapper(),
|
|
361
|
+
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
362
|
+
});
|
|
363
|
+
await svc.start();
|
|
364
|
+
let count = 0;
|
|
365
|
+
const off = svc.onAddressesChanged(() => { count++; });
|
|
366
|
+
await svc.testReachability();
|
|
367
|
+
off();
|
|
368
|
+
await svc.testReachability();
|
|
369
|
+
expect(count).toBe(1);
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
describe('NatService — initial invite-address push retry', () => {
|
|
374
|
+
it('delivers the initial push exactly once when the node is ready on the first attempt', async () => {
|
|
375
|
+
// The common production happy path: node bound fast, listener registered
|
|
376
|
+
// BEFORE start(). No retry needed — but the push must still fire (once).
|
|
377
|
+
const { node, peerIdCalls } = makeFlakyNode(0, '12D3KooWReady');
|
|
378
|
+
const svc = new NatService({
|
|
379
|
+
rootDir: tmpRoot,
|
|
380
|
+
cadreNode: node,
|
|
381
|
+
secretsStore: makeSecrets(),
|
|
382
|
+
portMapper: new StubMapper(),
|
|
383
|
+
externalIpDetector: makeDetector({ pub: '203.0.113.42' }),
|
|
384
|
+
initialPushRetryMs: 1,
|
|
385
|
+
initialPushTimeoutMs: 2_000,
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
const fired: string[][] = [];
|
|
389
|
+
svc.onAddressesChanged((addrs) => { fired.push(addrs); });
|
|
390
|
+
|
|
391
|
+
await svc.start();
|
|
392
|
+
|
|
393
|
+
expect(fired).toEqual([['/ip4/203.0.113.42/tcp/4001/p2p/12D3KooWReady']]);
|
|
394
|
+
expect(peerIdCalls()).toBe(1); // succeeded on the first attempt, no retry
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
it('retries past a not-yet-ready node and delivers the push exactly once', async () => {
|
|
398
|
+
const { node, peerIdCalls } = makeFlakyNode(3, '12D3KooWFlaky');
|
|
399
|
+
const svc = new NatService({
|
|
400
|
+
rootDir: tmpRoot,
|
|
401
|
+
cadreNode: node,
|
|
402
|
+
secretsStore: makeSecrets(),
|
|
403
|
+
portMapper: new StubMapper(),
|
|
404
|
+
externalIpDetector: makeDetector({ pub: '203.0.113.42' }),
|
|
405
|
+
initialPushRetryMs: 1,
|
|
406
|
+
initialPushTimeoutMs: 2_000,
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
// Listener registered BEFORE start() — this is the race the fix closes.
|
|
410
|
+
const fired: string[][] = [];
|
|
411
|
+
svc.onAddressesChanged((addrs) => { fired.push(addrs); });
|
|
412
|
+
|
|
413
|
+
await svc.start();
|
|
414
|
+
|
|
415
|
+
// Delivered exactly once, with the NAT-resolved address carrying the peer ID.
|
|
416
|
+
expect(fired).toEqual([['/ip4/203.0.113.42/tcp/4001/p2p/12D3KooWFlaky']]);
|
|
417
|
+
// It took the failing attempts plus the successful one (proves it retried).
|
|
418
|
+
expect(peerIdCalls()).toBe(4);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it('gives up after the bounded timeout but still resolves start() (best-effort)', async () => {
|
|
422
|
+
// Always unavailable — the retry budget elapses without a successful push.
|
|
423
|
+
const node: CadreNodeLike = {
|
|
424
|
+
getPeerId: async () => { throw new OwnerNodeUnavailableError('never ready'); },
|
|
425
|
+
getMultiaddrs: async () => [],
|
|
426
|
+
};
|
|
427
|
+
const svc = new NatService({
|
|
428
|
+
rootDir: tmpRoot,
|
|
429
|
+
cadreNode: node,
|
|
430
|
+
secretsStore: makeSecrets(),
|
|
431
|
+
portMapper: new StubMapper(),
|
|
432
|
+
externalIpDetector: makeDetector({ pub: '203.0.113.42' }),
|
|
433
|
+
initialPushRetryMs: 2,
|
|
434
|
+
initialPushTimeoutMs: 20,
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
const fired: string[][] = [];
|
|
438
|
+
svc.onAddressesChanged((addrs) => { fired.push(addrs); });
|
|
439
|
+
|
|
440
|
+
await svc.start(); // resolves despite the node never coming up
|
|
441
|
+
expect(fired).toEqual([]); // best-effort: nothing delivered
|
|
442
|
+
// The process is otherwise healthy — status still builds.
|
|
443
|
+
expect(svc.getStatus().portMode).toBe('auto-upnp');
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
it('does not retry on a non-node_unavailable error', async () => {
|
|
447
|
+
let calls = 0;
|
|
448
|
+
const node: CadreNodeLike = {
|
|
449
|
+
getPeerId: async () => { calls += 1; throw new Error('boom'); },
|
|
450
|
+
getMultiaddrs: async () => [],
|
|
451
|
+
};
|
|
452
|
+
const svc = new NatService({
|
|
453
|
+
rootDir: tmpRoot,
|
|
454
|
+
cadreNode: node,
|
|
455
|
+
secretsStore: makeSecrets(),
|
|
456
|
+
portMapper: new StubMapper(),
|
|
457
|
+
externalIpDetector: makeDetector({ pub: '203.0.113.42' }),
|
|
458
|
+
initialPushRetryMs: 1,
|
|
459
|
+
initialPushTimeoutMs: 2_000,
|
|
460
|
+
});
|
|
461
|
+
svc.onAddressesChanged(() => { /* registered before start */ });
|
|
462
|
+
|
|
463
|
+
await svc.start(); // does not hang retrying a non-transient failure
|
|
464
|
+
expect(calls).toBe(1);
|
|
465
|
+
});
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
describe('createNatHandlers', () => {
|
|
469
|
+
it('every handler delegates to the service', async () => {
|
|
470
|
+
const mapper = new StubMapper();
|
|
471
|
+
const svc = new NatService({
|
|
472
|
+
rootDir: tmpRoot,
|
|
473
|
+
cadreNode: makeNode(),
|
|
474
|
+
secretsStore: makeSecrets(),
|
|
475
|
+
portMapper: mapper,
|
|
476
|
+
externalIpDetector: makeDetector({ pub: '1.2.3.4' }),
|
|
477
|
+
});
|
|
478
|
+
await svc.start();
|
|
479
|
+
const h = createNatHandlers(svc);
|
|
480
|
+
|
|
481
|
+
expect((await h.getStatus()).portMode).toBe('auto-upnp');
|
|
482
|
+
expect((await h.listDdnsProviders()).find((p) => p.id === 'duckdns')).toBeDefined();
|
|
483
|
+
|
|
484
|
+
const status = await h.testReachability();
|
|
485
|
+
expect(status.lastTestedAt).not.toBeNull();
|
|
486
|
+
|
|
487
|
+
const after = await h.putSettings({ externalPort: 4002 });
|
|
488
|
+
expect(after.externalPort).toBe(4002);
|
|
489
|
+
|
|
490
|
+
const ddnsStatus = await h.putDdns({
|
|
491
|
+
providerId: 'duckdns',
|
|
492
|
+
hostname: 'a.duckdns.org',
|
|
493
|
+
config: { token: 'T' },
|
|
494
|
+
});
|
|
495
|
+
expect(ddnsStatus.ddns.hostname).toBe('a.duckdns.org');
|
|
496
|
+
});
|
|
497
|
+
});
|