@serfab/cadre-host 0.7.1
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 +101 -0
- package/dist/auth/duration.d.ts +1 -0
- package/dist/auth/duration.js +32 -0
- package/dist/auth/duration.js.map +1 -0
- package/dist/auth/index.d.ts +9 -0
- package/dist/auth/index.js +8 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/trust-circle-store.d.ts +26 -0
- package/dist/auth/trust-circle-store.js +129 -0
- package/dist/auth/trust-circle-store.js.map +1 -0
- package/dist/auth/trust-circle.d.ts +91 -0
- package/dist/auth/trust-circle.js +230 -0
- package/dist/auth/trust-circle.js.map +1 -0
- package/dist/auth/types.d.ts +81 -0
- package/dist/auth/types.js +24 -0
- package/dist/auth/types.js.map +1 -0
- package/dist/bin/host.d.ts +13 -0
- package/dist/bin/host.js +826 -0
- package/dist/bin/host.js.map +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/installer/browser.d.ts +14 -0
- package/dist/installer/browser.js +35 -0
- package/dist/installer/browser.js.map +1 -0
- package/dist/installer/config.d.ts +43 -0
- package/dist/installer/config.js +113 -0
- package/dist/installer/config.js.map +1 -0
- package/dist/installer/identity.d.ts +23 -0
- package/dist/installer/identity.js +36 -0
- package/dist/installer/identity.js.map +1 -0
- package/dist/installer/index.d.ts +83 -0
- package/dist/installer/index.js +204 -0
- package/dist/installer/index.js.map +1 -0
- package/dist/installer/paths.d.ts +33 -0
- package/dist/installer/paths.js +84 -0
- package/dist/installer/paths.js.map +1 -0
- package/dist/installer/platform.d.ts +9 -0
- package/dist/installer/platform.js +21 -0
- package/dist/installer/platform.js.map +1 -0
- package/dist/installer/service-host/index.d.ts +11 -0
- package/dist/installer/service-host/index.js +16 -0
- package/dist/installer/service-host/index.js.map +1 -0
- package/dist/installer/service-host/launchd.d.ts +16 -0
- package/dist/installer/service-host/launchd.js +79 -0
- package/dist/installer/service-host/launchd.js.map +1 -0
- package/dist/installer/service-host/nssm.d.ts +20 -0
- package/dist/installer/service-host/nssm.js +88 -0
- package/dist/installer/service-host/nssm.js.map +1 -0
- package/dist/installer/service-host/systemd.d.ts +16 -0
- package/dist/installer/service-host/systemd.js +78 -0
- package/dist/installer/service-host/systemd.js.map +1 -0
- package/dist/installer/service-host/template.d.ts +6 -0
- package/dist/installer/service-host/template.js +20 -0
- package/dist/installer/service-host/template.js.map +1 -0
- package/dist/installer/service-host/types.d.ts +40 -0
- package/dist/installer/service-host/types.js +6 -0
- package/dist/installer/service-host/types.js.map +1 -0
- package/dist/installer/wizard.d.ts +31 -0
- package/dist/installer/wizard.js +67 -0
- package/dist/installer/wizard.js.map +1 -0
- package/dist/nat/address-resolver.d.ts +22 -0
- package/dist/nat/address-resolver.js +31 -0
- package/dist/nat/address-resolver.js.map +1 -0
- package/dist/nat/ddns/duckdns.d.ts +12 -0
- package/dist/nat/ddns/duckdns.js +57 -0
- package/dist/nat/ddns/duckdns.js.map +1 -0
- package/dist/nat/ddns/index.d.ts +30 -0
- package/dist/nat/ddns/index.js +21 -0
- package/dist/nat/ddns/index.js.map +1 -0
- package/dist/nat/ddns/updater.d.ts +68 -0
- package/dist/nat/ddns/updater.js +194 -0
- package/dist/nat/ddns/updater.js.map +1 -0
- package/dist/nat/external-ip.d.ts +62 -0
- package/dist/nat/external-ip.js +114 -0
- package/dist/nat/external-ip.js.map +1 -0
- package/dist/nat/index.d.ts +22 -0
- package/dist/nat/index.js +16 -0
- package/dist/nat/index.js.map +1 -0
- package/dist/nat/nat-service.d.ts +101 -0
- package/dist/nat/nat-service.js +284 -0
- package/dist/nat/nat-service.js.map +1 -0
- package/dist/nat/nat-store.d.ts +19 -0
- package/dist/nat/nat-store.js +136 -0
- package/dist/nat/nat-store.js.map +1 -0
- package/dist/nat/port-mapper.d.ts +98 -0
- package/dist/nat/port-mapper.js +226 -0
- package/dist/nat/port-mapper.js.map +1 -0
- package/dist/nat/reachability.d.ts +19 -0
- package/dist/nat/reachability.js +29 -0
- package/dist/nat/reachability.js.map +1 -0
- package/dist/nat/secrets/file-store.d.ts +23 -0
- package/dist/nat/secrets/file-store.js +102 -0
- package/dist/nat/secrets/file-store.js.map +1 -0
- package/dist/nat/secrets/index.d.ts +36 -0
- package/dist/nat/secrets/index.js +56 -0
- package/dist/nat/secrets/index.js.map +1 -0
- package/dist/nat/secrets/keytar-store.d.ts +18 -0
- package/dist/nat/secrets/keytar-store.js +52 -0
- package/dist/nat/secrets/keytar-store.js.map +1 -0
- package/dist/nat/types.d.ts +120 -0
- package/dist/nat/types.js +24 -0
- package/dist/nat/types.js.map +1 -0
- package/dist/orchestrator/host-process-orchestrator.d.ts +87 -0
- package/dist/orchestrator/host-process-orchestrator.js +557 -0
- package/dist/orchestrator/host-process-orchestrator.js.map +1 -0
- package/dist/orchestrator/index.d.ts +3 -0
- package/dist/orchestrator/index.js +2 -0
- package/dist/orchestrator/index.js.map +1 -0
- package/dist/orchestrator/log-rotator.d.ts +25 -0
- package/dist/orchestrator/log-rotator.js +56 -0
- package/dist/orchestrator/log-rotator.js.map +1 -0
- package/dist/orchestrator/pid-liveness.d.ts +11 -0
- package/dist/orchestrator/pid-liveness.js +26 -0
- package/dist/orchestrator/pid-liveness.js.map +1 -0
- package/dist/orchestrator/port-allocator.d.ts +16 -0
- package/dist/orchestrator/port-allocator.js +38 -0
- package/dist/orchestrator/port-allocator.js.map +1 -0
- package/dist/orchestrator/state-store.d.ts +32 -0
- package/dist/orchestrator/state-store.js +44 -0
- package/dist/orchestrator/state-store.js.map +1 -0
- package/dist/orchestrator/types.d.ts +85 -0
- package/dist/orchestrator/types.js +31 -0
- package/dist/orchestrator/types.js.map +1 -0
- package/dist/server/error-handler.d.ts +26 -0
- package/dist/server/error-handler.js +95 -0
- package/dist/server/error-handler.js.map +1 -0
- package/dist/server/events/bus.d.ts +17 -0
- package/dist/server/events/bus.js +36 -0
- package/dist/server/events/bus.js.map +1 -0
- package/dist/server/events/sse-route.d.ts +22 -0
- package/dist/server/events/sse-route.js +65 -0
- package/dist/server/events/sse-route.js.map +1 -0
- package/dist/server/events/types.d.ts +25 -0
- package/dist/server/events/types.js +8 -0
- package/dist/server/events/types.js.map +1 -0
- package/dist/server/index.d.ts +67 -0
- package/dist/server/index.js +149 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/origin-guard.d.ts +24 -0
- package/dist/server/origin-guard.js +72 -0
- package/dist/server/origin-guard.js.map +1 -0
- package/dist/server/routes/nat.d.ts +16 -0
- package/dist/server/routes/nat.js +42 -0
- package/dist/server/routes/nat.js.map +1 -0
- package/dist/server/routes/nodes.d.ts +23 -0
- package/dist/server/routes/nodes.js +128 -0
- package/dist/server/routes/nodes.js.map +1 -0
- package/dist/server/routes/settings.d.ts +26 -0
- package/dist/server/routes/settings.js +97 -0
- package/dist/server/routes/settings.js.map +1 -0
- package/dist/server/routes/status.d.ts +42 -0
- package/dist/server/routes/status.js +76 -0
- package/dist/server/routes/status.js.map +1 -0
- package/dist/server/routes/trust-circle.d.ts +17 -0
- package/dist/server/routes/trust-circle.js +35 -0
- package/dist/server/routes/trust-circle.js.map +1 -0
- package/dist/server/routes/update.d.ts +16 -0
- package/dist/server/routes/update.js +29 -0
- package/dist/server/routes/update.js.map +1 -0
- package/dist/server/server.d.ts +40 -0
- package/dist/server/server.js +90 -0
- package/dist/server/server.js.map +1 -0
- package/dist/server/settings-store.d.ts +24 -0
- package/dist/server/settings-store.js +27 -0
- package/dist/server/settings-store.js.map +1 -0
- package/dist/server/static.d.ts +25 -0
- package/dist/server/static.js +177 -0
- package/dist/server/static.js.map +1 -0
- package/dist/ui/assets/index-BwU5mJGF.css +1 -0
- package/dist/ui/assets/index-C9uChk2P.js +14 -0
- package/dist/ui/assets/index-C9uChk2P.js.map +1 -0
- package/dist/ui/index.html +14 -0
- package/dist/update/apply.d.ts +37 -0
- package/dist/update/apply.js +102 -0
- package/dist/update/apply.js.map +1 -0
- package/dist/update/index.d.ts +101 -0
- package/dist/update/index.js +228 -0
- package/dist/update/index.js.map +1 -0
- package/dist/update/manifest.d.ts +48 -0
- package/dist/update/manifest.js +152 -0
- package/dist/update/manifest.js.map +1 -0
- package/dist/update/release-key.d.ts +24 -0
- package/dist/update/release-key.js +52 -0
- package/dist/update/release-key.js.map +1 -0
- package/dist/update/store.d.ts +22 -0
- package/dist/update/store.js +81 -0
- package/dist/update/store.js.map +1 -0
- package/dist/update/types.d.ts +90 -0
- package/dist/update/types.js +22 -0
- package/dist/update/types.js.map +1 -0
- package/dist/update/version.d.ts +18 -0
- package/dist/update/version.js +76 -0
- package/dist/update/version.js.map +1 -0
- package/package.json +80 -0
- package/service/README.md +86 -0
- package/service/cadre-host.service.tmpl +16 -0
- package/service/com.serfab.cadre-host.plist.tmpl +35 -0
- package/service/install-service.ps1 +98 -0
- package/service/uninstall-service.ps1 +19 -0
- package/src/__tests__/cli-invite.smoke.test.ts +187 -0
- package/src/__tests__/cli-nat.smoke.test.ts +207 -0
- package/src/__tests__/cli.smoke.test.ts +41 -0
- package/src/__tests__/orchestrator.test.ts +570 -0
- package/src/auth/__tests__/duration.test.ts +28 -0
- package/src/auth/__tests__/trust-circle-integration.test.ts +128 -0
- package/src/auth/__tests__/trust-circle-store.test.ts +115 -0
- package/src/auth/__tests__/trust-circle.test.ts +334 -0
- package/src/auth/duration.ts +33 -0
- package/src/auth/index.ts +19 -0
- package/src/auth/trust-circle-store.ts +155 -0
- package/src/auth/trust-circle.ts +304 -0
- package/src/auth/types.ts +96 -0
- package/src/bin/host.ts +905 -0
- package/src/index.ts +167 -0
- package/src/installer/__tests__/config.test.ts +86 -0
- package/src/installer/__tests__/identity.test.ts +34 -0
- package/src/installer/__tests__/installer.smoke.test.ts +124 -0
- package/src/installer/__tests__/paths.test.ts +47 -0
- package/src/installer/__tests__/service-host.test.ts +62 -0
- package/src/installer/__tests__/wizard.test.ts +60 -0
- package/src/installer/browser.ts +42 -0
- package/src/installer/config.ts +153 -0
- package/src/installer/identity.ts +49 -0
- package/src/installer/index.ts +303 -0
- package/src/installer/paths.ts +109 -0
- package/src/installer/platform.ts +27 -0
- package/src/installer/service-host/index.ts +21 -0
- package/src/installer/service-host/launchd.ts +90 -0
- package/src/installer/service-host/nssm.ts +100 -0
- package/src/installer/service-host/systemd.ts +88 -0
- package/src/installer/service-host/template.ts +21 -0
- package/src/installer/service-host/types.ts +48 -0
- package/src/installer/wizard.ts +92 -0
- package/src/nat/__tests__/address-resolver.test.ts +82 -0
- package/src/nat/__tests__/ddns-duckdns.test.ts +83 -0
- package/src/nat/__tests__/ddns-updater.test.ts +253 -0
- package/src/nat/__tests__/external-ip.test.ts +124 -0
- package/src/nat/__tests__/nat-service.test.ts +305 -0
- package/src/nat/__tests__/nat-store.test.ts +93 -0
- package/src/nat/__tests__/port-mapper.test.ts +191 -0
- package/src/nat/__tests__/reachability.test.ts +24 -0
- package/src/nat/__tests__/secrets.test.ts +156 -0
- package/src/nat/address-resolver.ts +51 -0
- package/src/nat/ddns/duckdns.ts +76 -0
- package/src/nat/ddns/index.ts +55 -0
- package/src/nat/ddns/updater.ts +243 -0
- package/src/nat/external-ip.ts +154 -0
- package/src/nat/index.ts +71 -0
- package/src/nat/nat-service.ts +375 -0
- package/src/nat/nat-store.ts +154 -0
- package/src/nat/port-mapper.ts +329 -0
- package/src/nat/reachability.ts +34 -0
- package/src/nat/secrets/file-store.ts +130 -0
- package/src/nat/secrets/index.ts +82 -0
- package/src/nat/secrets/keytar-store.ts +67 -0
- package/src/nat/types.ts +157 -0
- package/src/orchestrator/host-process-orchestrator.ts +626 -0
- package/src/orchestrator/index.ts +3 -0
- package/src/orchestrator/log-rotator.ts +53 -0
- package/src/orchestrator/pid-liveness.ts +24 -0
- package/src/orchestrator/port-allocator.ts +43 -0
- package/src/orchestrator/state-store.ts +72 -0
- package/src/orchestrator/types.ts +95 -0
- package/src/server/__tests__/bus.test.ts +38 -0
- package/src/server/__tests__/error-handler.test.ts +96 -0
- package/src/server/__tests__/nodes-route.test.ts +179 -0
- package/src/server/__tests__/origin-guard.test.ts +72 -0
- package/src/server/__tests__/publishers.test.ts +216 -0
- package/src/server/__tests__/server.smoke.test.ts +178 -0
- package/src/server/__tests__/settings-route.test.ts +177 -0
- package/src/server/__tests__/sse-route.test.ts +119 -0
- package/src/server/__tests__/status-route.test.ts +113 -0
- package/src/server/error-handler.ts +108 -0
- package/src/server/events/bus.ts +42 -0
- package/src/server/events/sse-route.ts +81 -0
- package/src/server/events/types.ts +22 -0
- package/src/server/index.ts +209 -0
- package/src/server/origin-guard.ts +84 -0
- package/src/server/routes/nat.ts +63 -0
- package/src/server/routes/nodes.ts +147 -0
- package/src/server/routes/settings.ts +115 -0
- package/src/server/routes/status.ts +108 -0
- package/src/server/routes/trust-circle.ts +49 -0
- package/src/server/routes/update.ts +43 -0
- package/src/server/server.ts +122 -0
- package/src/server/settings-store.ts +37 -0
- package/src/server/static.ts +191 -0
- package/src/update/__tests__/apply.test.ts +101 -0
- package/src/update/__tests__/manifest.test.ts +93 -0
- package/src/update/__tests__/store.test.ts +81 -0
- package/src/update/__tests__/update-service.integration.test.ts +221 -0
- package/src/update/__tests__/version.test.ts +31 -0
- package/src/update/apply.ts +148 -0
- package/src/update/index.ts +303 -0
- package/src/update/manifest.ts +172 -0
- package/src/update/release-key.ts +55 -0
- package/src/update/store.ts +101 -0
- package/src/update/types.ts +109 -0
- package/src/update/version.ts +80 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
2
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
|
|
6
|
+
import { NatStore } from '../nat-store.js';
|
|
7
|
+
import { NatError } from '../types.js';
|
|
8
|
+
|
|
9
|
+
let tmpRoot: string;
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-nat-store-'));
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('NatStore', () => {
|
|
20
|
+
it('returns defaults when file is missing', () => {
|
|
21
|
+
const store = new NatStore(tmpRoot);
|
|
22
|
+
const s = store.load();
|
|
23
|
+
expect(s.externalPort).toBe(4001);
|
|
24
|
+
expect(s.internalPort).toBe(4001);
|
|
25
|
+
expect(s.upnpEnabled).toBe(true);
|
|
26
|
+
expect(s.ddns.providerId).toBeNull();
|
|
27
|
+
expect(s.ddns.externallyManaged).toBe(false);
|
|
28
|
+
expect(s.ddns.intervalMs).toBe(5 * 60 * 1000);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('persists and reloads settings atomically', () => {
|
|
32
|
+
const store = new NatStore(tmpRoot);
|
|
33
|
+
store.update({ externalPort: 4002, upnpEnabled: false });
|
|
34
|
+
|
|
35
|
+
expect(existsSync(join(tmpRoot, 'nat.json'))).toBe(true);
|
|
36
|
+
expect(existsSync(join(tmpRoot, 'nat.json.tmp'))).toBe(false);
|
|
37
|
+
|
|
38
|
+
const reread = new NatStore(tmpRoot);
|
|
39
|
+
const s = reread.load();
|
|
40
|
+
expect(s.externalPort).toBe(4002);
|
|
41
|
+
expect(s.upnpEnabled).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('merges ddns subtree on update', () => {
|
|
45
|
+
const store = new NatStore(tmpRoot);
|
|
46
|
+
store.update({
|
|
47
|
+
ddns: {
|
|
48
|
+
providerId: 'duckdns',
|
|
49
|
+
hostname: 'foo.duckdns.org',
|
|
50
|
+
externallyManaged: false,
|
|
51
|
+
intervalMs: 60_000,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
store.update({ ddns: { externallyManaged: true } as never });
|
|
56
|
+
|
|
57
|
+
const s = store.load();
|
|
58
|
+
expect(s.ddns.providerId).toBe('duckdns');
|
|
59
|
+
expect(s.ddns.hostname).toBe('foo.duckdns.org');
|
|
60
|
+
expect(s.ddns.externallyManaged).toBe(true);
|
|
61
|
+
expect(s.ddns.intervalMs).toBe(60_000);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('persists version field', () => {
|
|
65
|
+
const store = new NatStore(tmpRoot);
|
|
66
|
+
store.update({ externalPort: 4001 });
|
|
67
|
+
const raw = JSON.parse(readFileSync(join(tmpRoot, 'nat.json'), 'utf8'));
|
|
68
|
+
expect(raw.version).toBe(1);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('throws on malformed JSON rather than silently wiping', () => {
|
|
72
|
+
writeFileSync(join(tmpRoot, 'nat.json'), 'not json', 'utf8');
|
|
73
|
+
const store = new NatStore(tmpRoot);
|
|
74
|
+
expect(() => store.load()).toThrow(NatError);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('rejects invalid port', () => {
|
|
78
|
+
const store = new NatStore(tmpRoot);
|
|
79
|
+
expect(() => store.update({ externalPort: 0 })).toThrow(NatError);
|
|
80
|
+
expect(() => store.update({ externalPort: 70000 })).toThrow(NatError);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('rejects too-small interval', () => {
|
|
84
|
+
const store = new NatStore(tmpRoot);
|
|
85
|
+
expect(() => store.update({ ddns: { intervalMs: 500 } as never })).toThrow(NatError);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('rejects empty string providerId/hostname', () => {
|
|
89
|
+
const store = new NatStore(tmpRoot);
|
|
90
|
+
expect(() => store.update({ ddns: { providerId: '' } as never })).toThrow(NatError);
|
|
91
|
+
expect(() => store.update({ ddns: { hostname: '' } as never })).toThrow(NatError);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { NatError } from '../types.js';
|
|
4
|
+
import {
|
|
5
|
+
PortMapperService,
|
|
6
|
+
type PortMapper,
|
|
7
|
+
type PortMappingResult,
|
|
8
|
+
} from '../port-mapper.js';
|
|
9
|
+
|
|
10
|
+
interface FakeOpts {
|
|
11
|
+
mapShouldThrow?: Error;
|
|
12
|
+
verifyResult?: boolean;
|
|
13
|
+
verifyShouldThrow?: Error;
|
|
14
|
+
externalIp?: string | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function makeFakeMapper(initial: FakeOpts = {}): PortMapper & {
|
|
18
|
+
calls: { map: number; verify: number; unmap: number; stop: number };
|
|
19
|
+
options: FakeOpts;
|
|
20
|
+
} {
|
|
21
|
+
const opts = { ...initial };
|
|
22
|
+
const calls = { map: 0, verify: 0, unmap: 0, stop: 0 };
|
|
23
|
+
return {
|
|
24
|
+
options: opts,
|
|
25
|
+
calls,
|
|
26
|
+
async map(o): Promise<PortMappingResult> {
|
|
27
|
+
calls.map++;
|
|
28
|
+
if (opts.mapShouldThrow) throw opts.mapShouldThrow;
|
|
29
|
+
return {
|
|
30
|
+
mode: 'auto-upnp',
|
|
31
|
+
externalIp: opts.externalIp ?? '203.0.113.1',
|
|
32
|
+
externalPort: o.externalPort,
|
|
33
|
+
internalPort: o.internalPort,
|
|
34
|
+
protocol: o.protocol ?? 'tcp',
|
|
35
|
+
leaseExpiresAt: new Date(Date.now() + (o.ttlMs ?? 60_000)),
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
async verify() {
|
|
39
|
+
calls.verify++;
|
|
40
|
+
if (opts.verifyShouldThrow) throw opts.verifyShouldThrow;
|
|
41
|
+
return opts.verifyResult ?? true;
|
|
42
|
+
},
|
|
43
|
+
async externalIp() {
|
|
44
|
+
return opts.externalIp ?? '203.0.113.1';
|
|
45
|
+
},
|
|
46
|
+
async unmap() {
|
|
47
|
+
calls.unmap++;
|
|
48
|
+
},
|
|
49
|
+
async stop() {
|
|
50
|
+
calls.stop++;
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function fakeTimers(): {
|
|
56
|
+
setTimer: (fn: () => void, ms: number) => NodeJS.Timeout;
|
|
57
|
+
clearTimer: (h: NodeJS.Timeout) => void;
|
|
58
|
+
fire(): void;
|
|
59
|
+
pending: number;
|
|
60
|
+
} {
|
|
61
|
+
let callback: (() => void) | null = null;
|
|
62
|
+
return {
|
|
63
|
+
setTimer: (fn) => {
|
|
64
|
+
callback = fn;
|
|
65
|
+
return {} as NodeJS.Timeout;
|
|
66
|
+
},
|
|
67
|
+
clearTimer: () => {
|
|
68
|
+
callback = null;
|
|
69
|
+
},
|
|
70
|
+
fire: () => {
|
|
71
|
+
const cb = callback;
|
|
72
|
+
callback = null;
|
|
73
|
+
cb?.();
|
|
74
|
+
},
|
|
75
|
+
get pending() { return callback ? 1 : 0; },
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
describe('PortMapperService', () => {
|
|
80
|
+
it('installs the mapping on start and records mode + externalIp', async () => {
|
|
81
|
+
const mapper = makeFakeMapper({ externalIp: '203.0.113.42' });
|
|
82
|
+
const timers = fakeTimers();
|
|
83
|
+
const svc = new PortMapperService({
|
|
84
|
+
mapper,
|
|
85
|
+
externalPort: 4001,
|
|
86
|
+
internalPort: 4001,
|
|
87
|
+
setTimer: timers.setTimer,
|
|
88
|
+
clearTimer: timers.clearTimer,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
await svc.start();
|
|
92
|
+
const state = svc.getState();
|
|
93
|
+
expect(state.mode).toBe('auto-upnp');
|
|
94
|
+
expect(state.routerExternalIp).toBe('203.0.113.42');
|
|
95
|
+
expect(state.externalPort).toBe(4001);
|
|
96
|
+
expect(state.lastError).toBeNull();
|
|
97
|
+
expect(mapper.calls.map).toBe(1);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('flips to mode=failed when the mapper throws', async () => {
|
|
101
|
+
const mapper = makeFakeMapper({ mapShouldThrow: new NatError('mapping_failed', 'router said no') });
|
|
102
|
+
const timers = fakeTimers();
|
|
103
|
+
const svc = new PortMapperService({
|
|
104
|
+
mapper,
|
|
105
|
+
externalPort: 4001,
|
|
106
|
+
internalPort: 4001,
|
|
107
|
+
setTimer: timers.setTimer,
|
|
108
|
+
clearTimer: timers.clearTimer,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
await svc.start();
|
|
112
|
+
expect(svc.getState().mode).toBe('failed');
|
|
113
|
+
expect(svc.getState().lastError).toContain('router said no');
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('unmaps and stops the mapper on stop()', async () => {
|
|
117
|
+
const mapper = makeFakeMapper();
|
|
118
|
+
const timers = fakeTimers();
|
|
119
|
+
const svc = new PortMapperService({
|
|
120
|
+
mapper,
|
|
121
|
+
externalPort: 4001,
|
|
122
|
+
internalPort: 4001,
|
|
123
|
+
setTimer: timers.setTimer,
|
|
124
|
+
clearTimer: timers.clearTimer,
|
|
125
|
+
});
|
|
126
|
+
await svc.start();
|
|
127
|
+
await svc.stop();
|
|
128
|
+
expect(mapper.calls.unmap).toBeGreaterThanOrEqual(1);
|
|
129
|
+
expect(mapper.calls.stop).toBeGreaterThanOrEqual(1);
|
|
130
|
+
expect(svc.getState().mode).toBe('disabled');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('stop() is idempotent', async () => {
|
|
134
|
+
const mapper = makeFakeMapper();
|
|
135
|
+
const timers = fakeTimers();
|
|
136
|
+
const svc = new PortMapperService({
|
|
137
|
+
mapper, externalPort: 4001, internalPort: 4001,
|
|
138
|
+
setTimer: timers.setTimer, clearTimer: timers.clearTimer,
|
|
139
|
+
});
|
|
140
|
+
await svc.start();
|
|
141
|
+
await svc.stop();
|
|
142
|
+
await svc.stop();
|
|
143
|
+
expect(mapper.calls.unmap).toBe(1);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('refresh() re-maps when verify fails', async () => {
|
|
147
|
+
const mapper = makeFakeMapper({ verifyResult: false });
|
|
148
|
+
const timers = fakeTimers();
|
|
149
|
+
const svc = new PortMapperService({
|
|
150
|
+
mapper, externalPort: 4001, internalPort: 4001,
|
|
151
|
+
setTimer: timers.setTimer, clearTimer: timers.clearTimer,
|
|
152
|
+
});
|
|
153
|
+
await svc.start();
|
|
154
|
+
expect(mapper.calls.map).toBe(1);
|
|
155
|
+
|
|
156
|
+
await svc.refresh();
|
|
157
|
+
// verify=false → re-map
|
|
158
|
+
expect(mapper.calls.map).toBe(2);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('refresh() re-issues the mapping when verify succeeds (extends lease)', async () => {
|
|
162
|
+
const mapper = makeFakeMapper({ verifyResult: true });
|
|
163
|
+
const timers = fakeTimers();
|
|
164
|
+
const svc = new PortMapperService({
|
|
165
|
+
mapper, externalPort: 4001, internalPort: 4001,
|
|
166
|
+
setTimer: timers.setTimer, clearTimer: timers.clearTimer,
|
|
167
|
+
});
|
|
168
|
+
await svc.start();
|
|
169
|
+
expect(mapper.calls.map).toBe(1);
|
|
170
|
+
await svc.refresh();
|
|
171
|
+
expect(mapper.calls.map).toBe(2);
|
|
172
|
+
expect(mapper.calls.verify).toBe(1);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('schedules a refresh timer', async () => {
|
|
176
|
+
const mapper = makeFakeMapper();
|
|
177
|
+
const timers = fakeTimers();
|
|
178
|
+
const svc = new PortMapperService({
|
|
179
|
+
mapper, externalPort: 4001, internalPort: 4001,
|
|
180
|
+
setTimer: timers.setTimer, clearTimer: timers.clearTimer,
|
|
181
|
+
});
|
|
182
|
+
await svc.start();
|
|
183
|
+
expect(timers.pending).toBe(1);
|
|
184
|
+
|
|
185
|
+
// Fire the timer — should re-arm after the inner refresh resolves.
|
|
186
|
+
timers.fire();
|
|
187
|
+
// Wait for the async refresh chain to complete.
|
|
188
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
189
|
+
expect(timers.pending).toBe(1);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { evaluateReachability } from '../reachability.js';
|
|
4
|
+
|
|
5
|
+
describe('evaluateReachability', () => {
|
|
6
|
+
it('returns cgnat when cgnatDetected is true (overrides portMode)', () => {
|
|
7
|
+
expect(evaluateReachability({ portMode: 'auto-upnp', cgnatDetected: true })).toBe('cgnat');
|
|
8
|
+
expect(evaluateReachability({ portMode: 'failed', cgnatDetected: true })).toBe('cgnat');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('returns reachable for auto-upnp / auto-natpmp', () => {
|
|
12
|
+
expect(evaluateReachability({ portMode: 'auto-upnp', cgnatDetected: false })).toBe('reachable');
|
|
13
|
+
expect(evaluateReachability({ portMode: 'auto-natpmp', cgnatDetected: false })).toBe('reachable');
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('returns unreachable when port mapping failed', () => {
|
|
17
|
+
expect(evaluateReachability({ portMode: 'failed', cgnatDetected: false })).toBe('unreachable');
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('returns unknown for manual / disabled', () => {
|
|
21
|
+
expect(evaluateReachability({ portMode: 'manual', cgnatDetected: false })).toBe('unknown');
|
|
22
|
+
expect(evaluateReachability({ portMode: 'disabled', cgnatDetected: false })).toBe('unknown');
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
2
|
+
import { mkdtempSync, rmSync, statSync, existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
|
|
6
|
+
import { KeytarSecretsStore } from '../secrets/keytar-store.js';
|
|
7
|
+
import { FileSecretsStore } from '../secrets/file-store.js';
|
|
8
|
+
import { ddnsAccount, type KeytarLike } from '../secrets/index.js';
|
|
9
|
+
import { NatError } from '../types.js';
|
|
10
|
+
|
|
11
|
+
function makeFakeKeytar(): {
|
|
12
|
+
keytar: KeytarLike;
|
|
13
|
+
store: Map<string, string>;
|
|
14
|
+
injectThrow: (op: keyof KeytarLike, err: Error) => void;
|
|
15
|
+
clearThrow: () => void;
|
|
16
|
+
} {
|
|
17
|
+
const store = new Map<string, string>();
|
|
18
|
+
const throws: Partial<Record<keyof KeytarLike, Error>> = {};
|
|
19
|
+
const key = (svc: string, acct: string) => `${svc}::${acct}`;
|
|
20
|
+
|
|
21
|
+
const keytar: KeytarLike = {
|
|
22
|
+
async setPassword(svc, acct, password) {
|
|
23
|
+
if (throws.setPassword) throw throws.setPassword;
|
|
24
|
+
store.set(key(svc, acct), password);
|
|
25
|
+
},
|
|
26
|
+
async getPassword(svc, acct) {
|
|
27
|
+
if (throws.getPassword) throw throws.getPassword;
|
|
28
|
+
return store.get(key(svc, acct)) ?? null;
|
|
29
|
+
},
|
|
30
|
+
async deletePassword(svc, acct) {
|
|
31
|
+
if (throws.deletePassword) throw throws.deletePassword;
|
|
32
|
+
return store.delete(key(svc, acct));
|
|
33
|
+
},
|
|
34
|
+
async findCredentials(svc) {
|
|
35
|
+
if (throws.findCredentials) throw throws.findCredentials;
|
|
36
|
+
const out: Array<{ account: string; password: string }> = [];
|
|
37
|
+
for (const [k, v] of store) {
|
|
38
|
+
const [s, a] = k.split('::');
|
|
39
|
+
if (s === svc) out.push({ account: a!, password: v });
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
return {
|
|
45
|
+
keytar,
|
|
46
|
+
store,
|
|
47
|
+
injectThrow: (op, err) => { throws[op] = err; },
|
|
48
|
+
clearThrow: () => { for (const k of Object.keys(throws)) delete throws[k as keyof KeytarLike]; },
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
describe('ddnsAccount()', () => {
|
|
53
|
+
it('formats provider + field into a stable key', () => {
|
|
54
|
+
expect(ddnsAccount('duckdns', 'token')).toBe('ddns:duckdns:token');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('rejects empty inputs', () => {
|
|
58
|
+
expect(() => ddnsAccount('', 'token')).toThrow(NatError);
|
|
59
|
+
expect(() => ddnsAccount('duckdns', '')).toThrow(NatError);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('KeytarSecretsStore', () => {
|
|
64
|
+
it('round-trips a credential', async () => {
|
|
65
|
+
const { keytar } = makeFakeKeytar();
|
|
66
|
+
const s = new KeytarSecretsStore('test-svc', keytar);
|
|
67
|
+
await s.set('a', 'v1');
|
|
68
|
+
expect(await s.get('a')).toBe('v1');
|
|
69
|
+
expect(await s.delete('a')).toBe(true);
|
|
70
|
+
expect(await s.get('a')).toBeNull();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('lists accounts', async () => {
|
|
74
|
+
const { keytar } = makeFakeKeytar();
|
|
75
|
+
const s = new KeytarSecretsStore('svc-1', keytar);
|
|
76
|
+
await s.set('one', 'v');
|
|
77
|
+
await s.set('two', 'v');
|
|
78
|
+
expect((await s.list()).sort()).toEqual(['one', 'two']);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('wraps keytar errors as NatError(secrets_unavailable)', async () => {
|
|
82
|
+
const fake = makeFakeKeytar();
|
|
83
|
+
const s = new KeytarSecretsStore('svc', fake.keytar);
|
|
84
|
+
fake.injectThrow('setPassword', new Error('dbus error'));
|
|
85
|
+
await expect(s.set('a', 'b')).rejects.toMatchObject({ code: 'secrets_unavailable' });
|
|
86
|
+
|
|
87
|
+
fake.clearThrow();
|
|
88
|
+
fake.injectThrow('getPassword', new Error('boom'));
|
|
89
|
+
await expect(s.get('a')).rejects.toMatchObject({ code: 'secrets_unavailable' });
|
|
90
|
+
|
|
91
|
+
fake.clearThrow();
|
|
92
|
+
fake.injectThrow('deletePassword', new Error('boom'));
|
|
93
|
+
await expect(s.delete('a')).rejects.toMatchObject({ code: 'secrets_unavailable' });
|
|
94
|
+
|
|
95
|
+
fake.clearThrow();
|
|
96
|
+
fake.injectThrow('findCredentials', new Error('boom'));
|
|
97
|
+
await expect(s.list()).rejects.toMatchObject({ code: 'secrets_unavailable' });
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('FileSecretsStore', () => {
|
|
102
|
+
let tmpRoot: string;
|
|
103
|
+
|
|
104
|
+
beforeEach(() => {
|
|
105
|
+
tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-secrets-'));
|
|
106
|
+
});
|
|
107
|
+
afterEach(() => {
|
|
108
|
+
try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('round-trips credentials through disk', async () => {
|
|
112
|
+
const s = new FileSecretsStore(tmpRoot);
|
|
113
|
+
await s.set('ddns:duckdns:token', 'TOKEN');
|
|
114
|
+
expect(await s.get('ddns:duckdns:token')).toBe('TOKEN');
|
|
115
|
+
|
|
116
|
+
const reread = new FileSecretsStore(tmpRoot);
|
|
117
|
+
expect(await reread.get('ddns:duckdns:token')).toBe('TOKEN');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('lists and deletes', async () => {
|
|
121
|
+
const s = new FileSecretsStore(tmpRoot);
|
|
122
|
+
await s.set('a', '1');
|
|
123
|
+
await s.set('b', '2');
|
|
124
|
+
expect((await s.list()).sort()).toEqual(['a', 'b']);
|
|
125
|
+
expect(await s.delete('a')).toBe(true);
|
|
126
|
+
expect(await s.delete('a')).toBe(false);
|
|
127
|
+
expect(await s.list()).toEqual(['b']);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('returns null for unknown account', async () => {
|
|
131
|
+
const s = new FileSecretsStore(tmpRoot);
|
|
132
|
+
expect(await s.get('nope')).toBeNull();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('throws on malformed file rather than silently wiping', async () => {
|
|
136
|
+
const s = new FileSecretsStore(tmpRoot);
|
|
137
|
+
await s.set('a', '1');
|
|
138
|
+
writeFileSync(s.filePath(), 'not json', 'utf8');
|
|
139
|
+
await expect(s.list()).rejects.toBeInstanceOf(NatError);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it.runIf(process.platform !== 'win32')('writes secrets file with mode 0600', async () => {
|
|
143
|
+
const s = new FileSecretsStore(tmpRoot);
|
|
144
|
+
await s.set('a', '1');
|
|
145
|
+
expect(existsSync(s.filePath())).toBe(true);
|
|
146
|
+
const mode = statSync(s.filePath()).mode & 0o777;
|
|
147
|
+
expect(mode).toBe(0o600);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('persists version field', async () => {
|
|
151
|
+
const s = new FileSecretsStore(tmpRoot);
|
|
152
|
+
await s.set('a', '1');
|
|
153
|
+
const raw = JSON.parse(readFileSync(s.filePath(), 'utf8'));
|
|
154
|
+
expect(raw.version).toBe(1);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build the multiaddrs that should be embedded in invites.
|
|
3
|
+
*
|
|
4
|
+
* Inputs are the host's currently-best understanding of its dialable surface:
|
|
5
|
+
* - DDNS hostname (`/dns4/<hostname>/...`) when configured AND reachable.
|
|
6
|
+
* - Raw external IP (`/ip4/<ip>/...`) when no DDNS but reachable.
|
|
7
|
+
* - Whatever libp2p reports otherwise — including any `/p2p-circuit/`
|
|
8
|
+
* addresses, once relay-client wiring lands (see follow-up tickets).
|
|
9
|
+
*
|
|
10
|
+
* This is intentionally a pure function so it's trivial to unit-test, and so
|
|
11
|
+
* the NatService can be the only place that knows about settings → multiaddrs.
|
|
12
|
+
*/
|
|
13
|
+
export interface BuildInviteAddressesInput {
|
|
14
|
+
peerId: string;
|
|
15
|
+
externalPort: number;
|
|
16
|
+
ddnsHostname: string | null;
|
|
17
|
+
externalIp: string | null;
|
|
18
|
+
reachable: boolean;
|
|
19
|
+
/** Multiaddrs reported by the underlying libp2p node. */
|
|
20
|
+
libp2pAddrs: string[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function buildInviteAddresses(input: BuildInviteAddressesInput): string[] {
|
|
24
|
+
const suffix = `/p2p/${input.peerId}`;
|
|
25
|
+
// DNS path-of-truth: stable across IP changes.
|
|
26
|
+
if (input.reachable && input.ddnsHostname) {
|
|
27
|
+
return [`/dns4/${input.ddnsHostname}/tcp/${input.externalPort}${suffix}`];
|
|
28
|
+
}
|
|
29
|
+
// Raw IPv4: works when we know our external IP.
|
|
30
|
+
if (input.reachable && input.externalIp && isIpv4(input.externalIp)) {
|
|
31
|
+
return [`/ip4/${input.externalIp}/tcp/${input.externalPort}${suffix}`];
|
|
32
|
+
}
|
|
33
|
+
// Fallback: whatever libp2p sees. Strip any addresses that lack the
|
|
34
|
+
// peer suffix and append it for clarity, but otherwise pass through.
|
|
35
|
+
return input.libp2pAddrs.map((a) => withPeerSuffix(a, input.peerId));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function withPeerSuffix(addr: string, peerId: string): string {
|
|
39
|
+
if (addr.includes(`/p2p/${peerId}`)) return addr;
|
|
40
|
+
if (addr.includes('/p2p/')) return addr; // already has *some* peer
|
|
41
|
+
return `${addr}/p2p/${peerId}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function isIpv4(s: string): boolean {
|
|
45
|
+
const m = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(s);
|
|
46
|
+
if (!m) return false;
|
|
47
|
+
return m.slice(1).every((octet) => {
|
|
48
|
+
const n = Number(octet);
|
|
49
|
+
return n >= 0 && n <= 255;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import debug from 'debug';
|
|
2
|
+
|
|
3
|
+
import { NatError } from '../types.js';
|
|
4
|
+
import type { DdnsProvider, DdnsProviderDeps } from './index.js';
|
|
5
|
+
|
|
6
|
+
const log = debug('cadre:host:nat-ddns:duckdns');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* DuckDNS provider.
|
|
10
|
+
*
|
|
11
|
+
* Update API: HTTPS GET
|
|
12
|
+
* https://www.duckdns.org/update?domains=<subdomain>&token=<token>&ip=<ip>
|
|
13
|
+
* Response is plain text — `OK` on success, `KO` on failure.
|
|
14
|
+
*
|
|
15
|
+
* `hostname` may be supplied as the bare subdomain (`foo`) or the FQDN
|
|
16
|
+
* (`foo.duckdns.org`); we strip the suffix.
|
|
17
|
+
*/
|
|
18
|
+
export const duckDnsProvider: DdnsProvider = {
|
|
19
|
+
id: 'duckdns',
|
|
20
|
+
displayName: 'DuckDNS',
|
|
21
|
+
configFields: [
|
|
22
|
+
{ key: 'token', label: 'DuckDNS token', secret: true },
|
|
23
|
+
],
|
|
24
|
+
async update(
|
|
25
|
+
config: Record<string, string>,
|
|
26
|
+
hostname: string,
|
|
27
|
+
ip: string,
|
|
28
|
+
deps: DdnsProviderDeps,
|
|
29
|
+
): Promise<void> {
|
|
30
|
+
const token = config['token'];
|
|
31
|
+
if (!token) {
|
|
32
|
+
throw new NatError('ddns_credentials_missing', 'DuckDNS requires a `token` config value');
|
|
33
|
+
}
|
|
34
|
+
const subdomain = stripSuffix(hostname);
|
|
35
|
+
if (!subdomain) {
|
|
36
|
+
throw new NatError('invalid_config', `DuckDNS hostname must be a valid subdomain (got: "${hostname}")`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const url = `https://www.duckdns.org/update?domains=${encodeURIComponent(subdomain)}&token=${encodeURIComponent(token)}&ip=${encodeURIComponent(ip)}`;
|
|
40
|
+
log('DuckDNS update %s → %s', subdomain, ip);
|
|
41
|
+
|
|
42
|
+
let res: Response;
|
|
43
|
+
try {
|
|
44
|
+
res = await deps.fetch(url);
|
|
45
|
+
} catch (err) {
|
|
46
|
+
throw new NatError(
|
|
47
|
+
'ddns_update_failed',
|
|
48
|
+
`DuckDNS request failed: ${(err as Error).message}`,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
if (!res.ok) {
|
|
52
|
+
throw new NatError(
|
|
53
|
+
'ddns_update_failed',
|
|
54
|
+
`DuckDNS HTTP ${res.status}: ${res.statusText}`,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
const text = (await res.text()).trim();
|
|
58
|
+
if (text !== 'OK') {
|
|
59
|
+
throw new NatError(
|
|
60
|
+
'ddns_update_failed',
|
|
61
|
+
`DuckDNS returned "${text || '(empty)'}" (expected "OK")`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const SUFFIX = '.duckdns.org';
|
|
68
|
+
|
|
69
|
+
function stripSuffix(hostname: string): string {
|
|
70
|
+
const trimmed = hostname.trim().toLowerCase();
|
|
71
|
+
if (trimmed.length === 0) return '';
|
|
72
|
+
if (trimmed.endsWith(SUFFIX)) {
|
|
73
|
+
return trimmed.slice(0, -SUFFIX.length);
|
|
74
|
+
}
|
|
75
|
+
return trimmed;
|
|
76
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { NatError, type DdnsConfigField } from '../types.js';
|
|
2
|
+
import { duckDnsProvider } from './duckdns.js';
|
|
3
|
+
|
|
4
|
+
/** Per-provider runtime dependencies (fetch override for tests). */
|
|
5
|
+
export interface DdnsProviderDeps {
|
|
6
|
+
fetch: typeof fetch;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* DDNS provider interface. Each provider knows:
|
|
11
|
+
* - Its display name + the fields needed to configure it (which fields are
|
|
12
|
+
* secrets vs. plain config goes into the SecretsStore / nat.json split).
|
|
13
|
+
* - How to push an IP update to the provider's API.
|
|
14
|
+
*
|
|
15
|
+
* v1 ships DuckDNS only. New providers drop in by exporting a `DdnsProvider`
|
|
16
|
+
* and adding an entry to `BUILTIN_PROVIDERS`.
|
|
17
|
+
*/
|
|
18
|
+
export interface DdnsProvider {
|
|
19
|
+
readonly id: string;
|
|
20
|
+
readonly displayName: string;
|
|
21
|
+
readonly configFields: ReadonlyArray<DdnsConfigField>;
|
|
22
|
+
/** Push `ip` for `hostname`. Throws NatError(ddns_update_failed) on failure. */
|
|
23
|
+
update(
|
|
24
|
+
config: Record<string, string>,
|
|
25
|
+
hostname: string,
|
|
26
|
+
ip: string,
|
|
27
|
+
deps: DdnsProviderDeps,
|
|
28
|
+
): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Built-in DDNS providers. */
|
|
32
|
+
export const BUILTIN_PROVIDERS: Readonly<Record<string, DdnsProvider>> = {
|
|
33
|
+
duckdns: duckDnsProvider,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** Look up a provider by ID, throwing NatError(ddns_provider_unknown) if missing. */
|
|
37
|
+
export function getProvider(providerId: string): DdnsProvider {
|
|
38
|
+
const p = BUILTIN_PROVIDERS[providerId];
|
|
39
|
+
if (!p) {
|
|
40
|
+
throw new NatError(
|
|
41
|
+
'ddns_provider_unknown',
|
|
42
|
+
`Unknown DDNS provider: ${providerId}. Built-in: ${Object.keys(BUILTIN_PROVIDERS).join(', ')}`,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
return p;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** List all built-in providers. */
|
|
49
|
+
export function listProviders(): DdnsProvider[] {
|
|
50
|
+
return Object.values(BUILTIN_PROVIDERS);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { duckDnsProvider } from './duckdns.js';
|
|
54
|
+
export { DdnsUpdater } from './updater.js';
|
|
55
|
+
export type { DdnsUpdaterOptions, DdnsUpdateOutcome } from './updater.js';
|