@ipab/cli 0.1.0-rc.3 → 0.1.0-rc.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -10
- package/dist/cli.d.ts +3 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +188 -46
- package/dist/cli.js.map +1 -1
- package/dist/config-secret.d.ts +1 -1
- package/dist/config-secret.d.ts.map +1 -1
- package/dist/config-secret.js +2 -73
- package/dist/config-secret.js.map +1 -1
- package/dist/config.d.ts +2 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +23 -3
- package/dist/config.js.map +1 -1
- package/dist/context-types.d.ts +12 -0
- package/dist/context-types.d.ts.map +1 -0
- package/dist/context-types.js +2 -0
- package/dist/context-types.js.map +1 -0
- package/dist/context.d.ts +6 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +25 -0
- package/dist/context.js.map +1 -1
- package/dist/identity.d.ts +5 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +5 -0
- package/dist/identity.js.map +1 -1
- package/dist/ipc.d.ts +3 -1
- package/dist/ipc.d.ts.map +1 -1
- package/dist/ipc.js +34 -1
- package/dist/ipc.js.map +1 -1
- package/dist/kubo-install.d.ts +9 -0
- package/dist/kubo-install.d.ts.map +1 -0
- package/dist/kubo-install.js +361 -0
- package/dist/kubo-install.js.map +1 -0
- package/dist/kubo-peering.d.ts +11 -0
- package/dist/kubo-peering.d.ts.map +1 -0
- package/dist/kubo-peering.js +93 -0
- package/dist/kubo-peering.js.map +1 -0
- package/dist/kubo-peers.d.ts +12 -0
- package/dist/kubo-peers.d.ts.map +1 -0
- package/dist/kubo-peers.js +118 -0
- package/dist/kubo-peers.js.map +1 -0
- package/dist/kubo-release.d.ts +3 -0
- package/dist/kubo-release.d.ts.map +1 -0
- package/dist/kubo-release.js +12 -0
- package/dist/kubo-release.js.map +1 -0
- package/dist/kubo.d.ts +14 -5
- package/dist/kubo.d.ts.map +1 -1
- package/dist/kubo.js +186 -206
- package/dist/kubo.js.map +1 -1
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +6 -2
- package/dist/runtime.js.map +1 -1
- package/dist/setup.d.ts +2 -1
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +23 -18
- package/dist/setup.js.map +1 -1
- package/dist/state-storage-windows.d.ts +2 -0
- package/dist/state-storage-windows.d.ts.map +1 -0
- package/dist/state-storage-windows.js +114 -0
- package/dist/state-storage-windows.js.map +1 -0
- package/dist/state-storage.d.ts +3 -0
- package/dist/state-storage.d.ts.map +1 -0
- package/dist/state-storage.js +299 -0
- package/dist/state-storage.js.map +1 -0
- package/dist/state.d.ts +5 -3
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +64 -30
- package/dist/state.js.map +1 -1
- package/dist/swarm-files.d.ts +3 -2
- package/dist/swarm-files.d.ts.map +1 -1
- package/dist/swarm-files.js +25 -9
- package/dist/swarm-files.js.map +1 -1
- package/dist/swarm.d.ts +10 -5
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +225 -79
- package/dist/swarm.js.map +1 -1
- package/package.json +4 -4
- package/src/cli.ts +224 -60
- package/src/config-secret.ts +2 -84
- package/src/config.ts +25 -3
- package/src/context-types.ts +11 -0
- package/src/context.ts +40 -0
- package/src/identity.ts +13 -0
- package/src/ipc.ts +50 -1
- package/src/kubo-install.ts +452 -0
- package/src/kubo-peering.ts +126 -0
- package/src/kubo-peers.ts +145 -0
- package/src/kubo-release.ts +17 -0
- package/src/kubo.ts +254 -275
- package/src/runtime.ts +12 -1
- package/src/setup.ts +30 -17
- package/src/state-storage-windows.ts +134 -0
- package/src/state-storage.ts +329 -0
- package/src/state.ts +76 -35
- package/src/swarm-files.ts +26 -6
- package/src/swarm.ts +316 -97
package/src/kubo.ts
CHANGED
|
@@ -1,26 +1,17 @@
|
|
|
1
1
|
import { execFile, spawn } from "node:child_process";
|
|
2
2
|
import { promisify } from "node:util";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
createPrivateKey,
|
|
6
|
-
createPublicKey,
|
|
7
|
-
sign,
|
|
8
|
-
} from "node:crypto";
|
|
9
|
-
import {
|
|
10
|
-
chmod,
|
|
11
|
-
lstat,
|
|
12
|
-
open,
|
|
13
|
-
readFile,
|
|
14
|
-
rename,
|
|
15
|
-
rm,
|
|
16
|
-
writeFile,
|
|
17
|
-
} from "node:fs/promises";
|
|
3
|
+
import { createPrivateKey, createPublicKey, sign } from "node:crypto";
|
|
4
|
+
import { lstat, open, readFile, writeFile } from "node:fs/promises";
|
|
18
5
|
import { join } from "node:path";
|
|
19
6
|
import { createServer } from "node:net";
|
|
20
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
advertisedIps,
|
|
9
|
+
BlockUnavailable,
|
|
10
|
+
readWithPeers,
|
|
11
|
+
} from "./kubo-peers.js";
|
|
21
12
|
import { base58btc } from "multiformats/bases/base58";
|
|
22
13
|
import { boundedText, uniqueId } from "@ipab/bundle/pointer/scope";
|
|
23
|
-
import { protectedPath, secureNew } from "./identity.js";
|
|
14
|
+
import { protectedPath, protectedPaths, secureNew } from "./identity.js";
|
|
24
15
|
import {
|
|
25
16
|
privateDirectory,
|
|
26
17
|
readPrivate,
|
|
@@ -29,15 +20,26 @@ import {
|
|
|
29
20
|
} from "./swarm-files.js";
|
|
30
21
|
import { HostError, object } from "./errors.js";
|
|
31
22
|
import { nodeError } from "./authoring.js";
|
|
23
|
+
import {
|
|
24
|
+
peerArguments,
|
|
25
|
+
persistPeers,
|
|
26
|
+
reconcilePeers,
|
|
27
|
+
type PeeringPeer,
|
|
28
|
+
} from "./kubo-peering.js";
|
|
32
29
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
30
|
+
import { kuboBinary } from "./kubo-install.js";
|
|
31
|
+
export { kuboBinary };
|
|
32
|
+
export { KUBO_VERSION } from "./kubo-release.js";
|
|
33
|
+
// Windows Firewall creates an inbound rule for every program that listens on a
|
|
34
|
+
// non-loopback address, so each new managed binary location prompts for
|
|
35
|
+
// authorization. Binding only 127.0.0.1 creates no rule at all, which keeps
|
|
36
|
+
// same-host private networks (and automated tests) unattended. A LAN swarms
|
|
37
|
+
// still advertises private IPv4 addresses as peers dial *in* to that listener.
|
|
38
|
+
export type ListenMode = "lan" | "loopback";
|
|
39
|
+
function swarmAddress(listen: ListenMode, port: number): string {
|
|
40
|
+
return `/ip4/${listen === "loopback" ? "127.0.0.1" : "0.0.0.0"}/tcp/${port}`;
|
|
40
41
|
}
|
|
42
|
+
const run = promisify(execFile);
|
|
41
43
|
export interface NodeRecord {
|
|
42
44
|
scope: string;
|
|
43
45
|
peerId: string;
|
|
@@ -47,6 +49,7 @@ export interface NodeRecord {
|
|
|
47
49
|
auth: string;
|
|
48
50
|
epoch?: number;
|
|
49
51
|
pid?: number;
|
|
52
|
+
listen?: ListenMode;
|
|
50
53
|
}
|
|
51
54
|
export interface KuboNode {
|
|
52
55
|
repo: string;
|
|
@@ -59,7 +62,7 @@ export interface KuboNode {
|
|
|
59
62
|
}
|
|
60
63
|
function nodeRecord(value: unknown, scope: string): NodeRecord {
|
|
61
64
|
const record = object(value, "protected managed-node record");
|
|
62
|
-
const { peerId, api, gateway, swarm, auth, pid, epoch } = record;
|
|
65
|
+
const { peerId, api, gateway, swarm, auth, pid, epoch, listen } = record;
|
|
63
66
|
if (
|
|
64
67
|
Object.keys(record).some(
|
|
65
68
|
(key) =>
|
|
@@ -72,6 +75,7 @@ function nodeRecord(value: unknown, scope: string): NodeRecord {
|
|
|
72
75
|
"auth",
|
|
73
76
|
"pid",
|
|
74
77
|
"epoch",
|
|
78
|
+
"listen",
|
|
75
79
|
].includes(key),
|
|
76
80
|
) ||
|
|
77
81
|
record.scope !== scope ||
|
|
@@ -89,7 +93,10 @@ function nodeRecord(value: unknown, scope: string): NodeRecord {
|
|
|
89
93
|
(pid !== undefined &&
|
|
90
94
|
(typeof pid !== "number" || !Number.isSafeInteger(pid) || pid <= 0)) ||
|
|
91
95
|
(epoch !== undefined &&
|
|
92
|
-
(typeof epoch !== "number" ||
|
|
96
|
+
(typeof epoch !== "number" ||
|
|
97
|
+
!Number.isSafeInteger(epoch) ||
|
|
98
|
+
epoch < 1)) ||
|
|
99
|
+
(listen !== undefined && listen !== "lan" && listen !== "loopback")
|
|
93
100
|
)
|
|
94
101
|
throw new HostError(
|
|
95
102
|
"Invalid protected managed-node record; old node formats are unsupported.",
|
|
@@ -103,148 +110,9 @@ function nodeRecord(value: unknown, scope: string): NodeRecord {
|
|
|
103
110
|
auth,
|
|
104
111
|
...(pid !== undefined ? { pid } : {}),
|
|
105
112
|
...(epoch !== undefined ? { epoch } : {}),
|
|
113
|
+
...(listen !== undefined ? { listen } : {}),
|
|
106
114
|
};
|
|
107
115
|
}
|
|
108
|
-
export async function kuboBinary(stateDir: string): Promise<string> {
|
|
109
|
-
const directory = await privateDirectory(stateDir, "kubo-bin");
|
|
110
|
-
return withPrivateLock(directory, async () => {
|
|
111
|
-
const executable = join(
|
|
112
|
-
directory,
|
|
113
|
-
process.platform === "win32" ? "ipfs.exe" : "ipfs",
|
|
114
|
-
);
|
|
115
|
-
const installed = await readPrivate<Installation>(
|
|
116
|
-
directory,
|
|
117
|
-
"installation.json",
|
|
118
|
-
);
|
|
119
|
-
if (installed) {
|
|
120
|
-
await protectedPath(executable);
|
|
121
|
-
if (
|
|
122
|
-
installed.version !== KUBO_VERSION ||
|
|
123
|
-
installed.hash !== hash(await readFile(executable))
|
|
124
|
-
)
|
|
125
|
-
throw new HostError(
|
|
126
|
-
"Kubo installation integrity mismatch; do not use this binary.",
|
|
127
|
-
);
|
|
128
|
-
return executable;
|
|
129
|
-
}
|
|
130
|
-
const platforms: Partial<Record<NodeJS.Platform, string>> = {
|
|
131
|
-
win32: "windows",
|
|
132
|
-
darwin: "darwin",
|
|
133
|
-
linux: "linux",
|
|
134
|
-
};
|
|
135
|
-
const platform = platforms[process.platform];
|
|
136
|
-
if (!platform || !["x64", "arm64"].includes(process.arch))
|
|
137
|
-
throw new HostError("Unsupported Kubo platform.");
|
|
138
|
-
const arch = process.arch === "x64" ? "amd64" : "arm64";
|
|
139
|
-
const filename = `kubo_v${KUBO_VERSION}_${platform}-${arch}.${platform === "windows" ? "zip" : "tar.gz"}`;
|
|
140
|
-
// Official release metadata supplies the immutable archive digest; HTTPS is the trust boundary.
|
|
141
|
-
const release = await fetch(
|
|
142
|
-
`https://api.github.com/repos/ipfs/kubo/releases/tags/v${KUBO_VERSION}`,
|
|
143
|
-
{
|
|
144
|
-
redirect: "error",
|
|
145
|
-
signal: AbortSignal.timeout(30_000),
|
|
146
|
-
headers: { accept: "application/vnd.github+json" },
|
|
147
|
-
},
|
|
148
|
-
);
|
|
149
|
-
if (!release.ok)
|
|
150
|
-
throw new HostError(
|
|
151
|
-
`Could not verify official Kubo release (HTTP ${release.status}).`,
|
|
152
|
-
);
|
|
153
|
-
const metadata = JSON.parse(await boundedText(release)) as {
|
|
154
|
-
draft: boolean;
|
|
155
|
-
prerelease: boolean;
|
|
156
|
-
assets: { name: string; digest: string; browser_download_url: string }[];
|
|
157
|
-
};
|
|
158
|
-
const asset = metadata.assets.find((item) => item.name === filename);
|
|
159
|
-
if (
|
|
160
|
-
metadata.draft ||
|
|
161
|
-
metadata.prerelease ||
|
|
162
|
-
!asset ||
|
|
163
|
-
!/^sha256:[a-f0-9]{64}$/.test(asset.digest) ||
|
|
164
|
-
asset.browser_download_url !==
|
|
165
|
-
`https://github.com/ipfs/kubo/releases/download/v${KUBO_VERSION}/${filename}`
|
|
166
|
-
)
|
|
167
|
-
throw new HostError("Official stable Kubo archive/digest unavailable.");
|
|
168
|
-
const archive = join(
|
|
169
|
-
directory,
|
|
170
|
-
`${uniqueId()}.${platform === "windows" ? "zip" : "tar.gz"}`,
|
|
171
|
-
);
|
|
172
|
-
const stage = await privateDirectory(directory, `extract-${uniqueId()}`);
|
|
173
|
-
try {
|
|
174
|
-
const response = await fetch(asset.browser_download_url, {
|
|
175
|
-
signal: AbortSignal.timeout(180_000),
|
|
176
|
-
});
|
|
177
|
-
if (!response.ok || !response.body)
|
|
178
|
-
throw new HostError("Kubo download failed.");
|
|
179
|
-
const handle = await open(archive, "wx", 0o600);
|
|
180
|
-
let total = 0;
|
|
181
|
-
const digest = createHash("sha256");
|
|
182
|
-
try {
|
|
183
|
-
await secureNew(archive);
|
|
184
|
-
const stream: ReadableStream<Uint8Array> = response.body;
|
|
185
|
-
for await (const chunk of stream) {
|
|
186
|
-
total += chunk.length;
|
|
187
|
-
if (total > 150 * 1024 * 1024)
|
|
188
|
-
throw new HostError("Kubo archive exceeds limit.");
|
|
189
|
-
digest.update(chunk);
|
|
190
|
-
await handle.write(chunk);
|
|
191
|
-
}
|
|
192
|
-
await handle.sync();
|
|
193
|
-
} finally {
|
|
194
|
-
await handle.close();
|
|
195
|
-
}
|
|
196
|
-
if (`sha256:${digest.digest("hex")}` !== asset.digest)
|
|
197
|
-
throw new HostError("Kubo archive checksum mismatch.");
|
|
198
|
-
const listing = await run("tar", ["-tf", archive], {
|
|
199
|
-
timeout: 30_000,
|
|
200
|
-
maxBuffer: 1024 * 1024,
|
|
201
|
-
windowsHide: true,
|
|
202
|
-
});
|
|
203
|
-
if (
|
|
204
|
-
listing.stdout
|
|
205
|
-
.split(/\r?\n/)
|
|
206
|
-
.filter(Boolean)
|
|
207
|
-
.some(
|
|
208
|
-
(path) =>
|
|
209
|
-
!/^kubo\/[A-Za-z0-9._/-]+$/.test(path) ||
|
|
210
|
-
path.split("/").includes(".."),
|
|
211
|
-
)
|
|
212
|
-
)
|
|
213
|
-
throw new HostError("Unsafe Kubo archive layout.");
|
|
214
|
-
await run("tar", ["-xf", archive, "-C", stage], {
|
|
215
|
-
timeout: 30_000,
|
|
216
|
-
windowsHide: true,
|
|
217
|
-
});
|
|
218
|
-
const extracted = join(
|
|
219
|
-
stage,
|
|
220
|
-
"kubo",
|
|
221
|
-
process.platform === "win32" ? "ipfs.exe" : "ipfs",
|
|
222
|
-
);
|
|
223
|
-
if (
|
|
224
|
-
!(await lstat(extracted)).isFile() ||
|
|
225
|
-
(await lstat(extracted)).isSymbolicLink()
|
|
226
|
-
)
|
|
227
|
-
throw new HostError("Invalid Kubo executable.");
|
|
228
|
-
await secureNew(extracted);
|
|
229
|
-
if (process.platform !== "win32") await chmod(extracted, 0o700);
|
|
230
|
-
const version = await run(extracted, ["version", "--number"], {
|
|
231
|
-
timeout: 10_000,
|
|
232
|
-
windowsHide: true,
|
|
233
|
-
});
|
|
234
|
-
if (version.stdout.trim() !== KUBO_VERSION)
|
|
235
|
-
throw new HostError("Kubo executable version mismatch.");
|
|
236
|
-
await rename(extracted, executable);
|
|
237
|
-
await writePrivate(directory, "installation.json", {
|
|
238
|
-
version: KUBO_VERSION,
|
|
239
|
-
hash: hash(await readFile(executable)),
|
|
240
|
-
});
|
|
241
|
-
return executable;
|
|
242
|
-
} finally {
|
|
243
|
-
await rm(stage, { recursive: true, force: true });
|
|
244
|
-
await rm(archive, { force: true });
|
|
245
|
-
}
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
116
|
async function freePort(): Promise<number> {
|
|
249
117
|
const server = createServer();
|
|
250
118
|
return new Promise((resolve, reject) => {
|
|
@@ -266,22 +134,56 @@ async function freePort(): Promise<number> {
|
|
|
266
134
|
export async function prepareNode(
|
|
267
135
|
stateDir: string,
|
|
268
136
|
scope: string,
|
|
137
|
+
listen: ListenMode = "lan",
|
|
269
138
|
): Promise<KuboNode> {
|
|
270
139
|
if (!/^[a-f0-9]{64}$/.test(scope))
|
|
271
140
|
throw new HostError("Invalid service-bound local Scope identity.");
|
|
272
141
|
const binary = await kuboBinary(stateDir);
|
|
273
|
-
return prepareWithBinary(stateDir, scope, binary);
|
|
142
|
+
return prepareWithBinary(stateDir, scope, binary, listen);
|
|
143
|
+
}
|
|
144
|
+
export async function checkCachedNode(
|
|
145
|
+
stateDir: string,
|
|
146
|
+
scope: string,
|
|
147
|
+
epoch: number,
|
|
148
|
+
peer: string,
|
|
149
|
+
) {
|
|
150
|
+
if (!/^[a-f0-9]{64}$/.test(scope))
|
|
151
|
+
throw new HostError("Invalid service-bound local Scope identity.");
|
|
152
|
+
const saved = await readPrivate<unknown>(
|
|
153
|
+
join(stateDir, "swarm-nodes", scope),
|
|
154
|
+
"node.json",
|
|
155
|
+
);
|
|
156
|
+
if (saved === undefined) return;
|
|
157
|
+
const node = nodeRecord(saved, scope);
|
|
158
|
+
if (node.peerId !== peer || (node.epoch !== undefined && node.epoch > epoch))
|
|
159
|
+
throw new HostError(
|
|
160
|
+
"Cached membership does not match the recorded node identity or epoch; retry with --force.",
|
|
161
|
+
);
|
|
274
162
|
}
|
|
275
163
|
async function prepareWithBinary(
|
|
276
164
|
stateDir: string,
|
|
277
165
|
scope: string,
|
|
278
166
|
binary: string,
|
|
167
|
+
listen: ListenMode,
|
|
168
|
+
signal?: AbortSignal,
|
|
279
169
|
): Promise<KuboNode> {
|
|
280
|
-
const parent =
|
|
281
|
-
const directory =
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
170
|
+
const parent = join(stateDir, "swarm-nodes");
|
|
171
|
+
const directory = join(parent, scope);
|
|
172
|
+
const repo = join(directory, "repo");
|
|
173
|
+
try {
|
|
174
|
+
await protectedPaths(
|
|
175
|
+
[parent, directory, repo].map((path) => ({ path, directory: true })),
|
|
176
|
+
);
|
|
177
|
+
} catch (cause) {
|
|
178
|
+
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
179
|
+
await privateDirectory(stateDir, "swarm-nodes");
|
|
180
|
+
await privateDirectory(parent, scope);
|
|
181
|
+
await privateDirectory(directory, "repo");
|
|
182
|
+
}
|
|
183
|
+
const prepare = async () => {
|
|
184
|
+
let record = await readPrivate<NodeRecord>(directory, "node.json", [
|
|
185
|
+
{ path: join(repo, "config") },
|
|
186
|
+
]);
|
|
285
187
|
if (record === undefined) {
|
|
286
188
|
try {
|
|
287
189
|
await lstat(join(repo, "swarm.key"));
|
|
@@ -298,6 +200,7 @@ async function prepareWithBinary(
|
|
|
298
200
|
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
299
201
|
await run(binary, ["init", "--profile=server"], {
|
|
300
202
|
env,
|
|
203
|
+
signal,
|
|
301
204
|
timeout: 30_000,
|
|
302
205
|
windowsHide: true,
|
|
303
206
|
});
|
|
@@ -334,11 +237,12 @@ async function prepareWithBinary(
|
|
|
334
237
|
gateway: await freePort(),
|
|
335
238
|
swarm: await freePort(),
|
|
336
239
|
auth: uniqueId(),
|
|
240
|
+
listen,
|
|
337
241
|
};
|
|
338
242
|
config.Addresses = {
|
|
339
243
|
API: `/ip4/127.0.0.1/tcp/${record.api}`,
|
|
340
244
|
Gateway: `/ip4/127.0.0.1/tcp/${record.gateway}`,
|
|
341
|
-
Swarm: [
|
|
245
|
+
Swarm: [swarmAddress(listen, record.swarm)],
|
|
342
246
|
};
|
|
343
247
|
config.Bootstrap = [];
|
|
344
248
|
config.Swarm.AddrFilters = [];
|
|
@@ -365,20 +269,15 @@ async function prepareWithBinary(
|
|
|
365
269
|
});
|
|
366
270
|
await secureNew(join(repo, "config"));
|
|
367
271
|
await writePrivate(directory, "node.json", record);
|
|
272
|
+
await protectedPath(join(repo, "config"));
|
|
368
273
|
}
|
|
369
|
-
await protectedPath(join(repo, "config"));
|
|
370
274
|
record = nodeRecord(record, scope);
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
item.address,
|
|
378
|
-
),
|
|
379
|
-
)
|
|
380
|
-
.map((item) => item.address)
|
|
381
|
-
.slice(0, 15);
|
|
275
|
+
// A repo created before listen modes existed binds 0.0.0.0, so it is LAN.
|
|
276
|
+
record.listen ??= "lan";
|
|
277
|
+
if (record.listen !== listen)
|
|
278
|
+
throw new HostError(
|
|
279
|
+
"Managed node already listens in a different mode; existing repos are not rebound.",
|
|
280
|
+
);
|
|
382
281
|
return {
|
|
383
282
|
repo,
|
|
384
283
|
peerId: record.peerId,
|
|
@@ -386,11 +285,12 @@ async function prepareWithBinary(
|
|
|
386
285
|
gateway: `http://127.0.0.1:${record.gateway}`,
|
|
387
286
|
auth: record.auth,
|
|
388
287
|
...(record.pid !== undefined ? { pid: record.pid } : {}),
|
|
389
|
-
addresses:
|
|
288
|
+
addresses: advertisedIps(record.listen).map(
|
|
390
289
|
(ip) => `/ip4/${ip}/tcp/${record.swarm}/p2p/${record.peerId}`,
|
|
391
290
|
),
|
|
392
291
|
};
|
|
393
|
-
}
|
|
292
|
+
};
|
|
293
|
+
return withPrivateLock(directory, prepare, signal);
|
|
394
294
|
}
|
|
395
295
|
export async function peerProof(
|
|
396
296
|
node: KuboNode,
|
|
@@ -451,9 +351,9 @@ async function rpc(
|
|
|
451
351
|
: AbortSignal.timeout(timeout),
|
|
452
352
|
});
|
|
453
353
|
}
|
|
454
|
-
async function ready(node: KuboNode): Promise<boolean> {
|
|
354
|
+
async function ready(node: KuboNode, signal?: AbortSignal): Promise<boolean> {
|
|
455
355
|
try {
|
|
456
|
-
const response = await rpc(node, "id");
|
|
356
|
+
const response = await rpc(node, "id", signal);
|
|
457
357
|
if (response.status === 401 || response.status === 403) {
|
|
458
358
|
await response.body?.cancel();
|
|
459
359
|
throw new HostError(
|
|
@@ -471,6 +371,7 @@ async function ready(node: KuboNode): Promise<boolean> {
|
|
|
471
371
|
);
|
|
472
372
|
return true;
|
|
473
373
|
} catch (cause) {
|
|
374
|
+
signal?.throwIfAborted();
|
|
474
375
|
if (cause instanceof HostError) throw cause;
|
|
475
376
|
return false;
|
|
476
377
|
}
|
|
@@ -499,19 +400,49 @@ export async function startNode(
|
|
|
499
400
|
scope: string,
|
|
500
401
|
key: string,
|
|
501
402
|
epoch: number,
|
|
502
|
-
|
|
503
|
-
|
|
403
|
+
listen: ListenMode = "lan",
|
|
404
|
+
membership?: {
|
|
405
|
+
self: string;
|
|
406
|
+
peers: readonly PeeringPeer[];
|
|
407
|
+
signal?: AbortSignal;
|
|
408
|
+
},
|
|
504
409
|
): Promise<KuboNode> {
|
|
505
410
|
if (!/^[a-f0-9]{64}$/.test(scope))
|
|
506
411
|
throw new HostError("Invalid service-bound local Scope identity.");
|
|
507
412
|
if (!/^[a-f0-9]{64}$/.test(key)) throw new HostError("Invalid Swarm key.");
|
|
508
|
-
const
|
|
509
|
-
|
|
413
|
+
const signal = membership?.signal;
|
|
414
|
+
signal?.throwIfAborted();
|
|
415
|
+
const binary = await kuboBinary(stateDir, signal ? { signal } : {});
|
|
416
|
+
signal?.throwIfAborted();
|
|
417
|
+
const node = await prepareWithBinary(stateDir, scope, binary, listen, signal);
|
|
418
|
+
if (membership && node.peerId !== membership.self)
|
|
419
|
+
throw new HostError(
|
|
420
|
+
"Approved PeerID differs from this local Request's Kubo identity.",
|
|
421
|
+
);
|
|
510
422
|
const directory = join(stateDir, "swarm-nodes", scope);
|
|
511
|
-
|
|
423
|
+
const start = async () => {
|
|
424
|
+
signal?.throwIfAborted();
|
|
512
425
|
const record = (await readPrivate<NodeRecord>(directory, "node.json"))!;
|
|
426
|
+
if (record.epoch !== undefined && record.epoch > epoch)
|
|
427
|
+
throw new HostError(
|
|
428
|
+
"Refusing to restore an older Swarm epoch; refresh membership.",
|
|
429
|
+
);
|
|
513
430
|
if (record.epoch !== undefined && record.epoch !== epoch)
|
|
514
431
|
await stopNode(node);
|
|
432
|
+
const online = await ready(node, signal);
|
|
433
|
+
if (!online && record.pid !== undefined) {
|
|
434
|
+
try {
|
|
435
|
+
process.kill(record.pid, 0);
|
|
436
|
+
throw new HostError(
|
|
437
|
+
"Owned node process is still running but not ready; retry before changing its configuration.",
|
|
438
|
+
);
|
|
439
|
+
} catch (cause) {
|
|
440
|
+
if (!nodeError(cause, "ESRCH")) throw cause;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
const call = (command: string, budget: AbortSignal) =>
|
|
444
|
+
rpc(node, command, signal ? AbortSignal.any([signal, budget]) : budget);
|
|
445
|
+
const desired = membership?.peers ?? [];
|
|
515
446
|
const keyPath = join(node.repo, "swarm.key");
|
|
516
447
|
if (record.epoch === epoch) {
|
|
517
448
|
await protectedPath(keyPath);
|
|
@@ -521,12 +452,16 @@ export async function startNode(
|
|
|
521
452
|
)
|
|
522
453
|
throw new HostError("Swarm key file does not match approved epoch.");
|
|
523
454
|
} else {
|
|
455
|
+
await persistPeers(node.repo, desired, online ? call : undefined);
|
|
524
456
|
await writeFile(keyPath, `/key/swarm/psk/1.0.0/\n/base16/\n${key}\n`, {
|
|
525
457
|
mode: 0o600,
|
|
526
458
|
});
|
|
527
459
|
await secureNew(keyPath);
|
|
528
460
|
}
|
|
529
|
-
if (
|
|
461
|
+
if (record.epoch === epoch)
|
|
462
|
+
await persistPeers(node.repo, desired, online ? call : undefined);
|
|
463
|
+
if (!online) {
|
|
464
|
+
signal?.throwIfAborted();
|
|
530
465
|
const logfile = join(directory, "daemon.log");
|
|
531
466
|
const log = await open(logfile, "a", 0o600);
|
|
532
467
|
await secureNew(logfile);
|
|
@@ -559,8 +494,9 @@ export async function startNode(
|
|
|
559
494
|
await writePrivate(directory, "node.json", record);
|
|
560
495
|
}
|
|
561
496
|
for (let i = 0; ; i++) {
|
|
497
|
+
signal?.throwIfAborted();
|
|
562
498
|
if (failure) throw failure;
|
|
563
|
-
if (await ready(node)) break;
|
|
499
|
+
if (await ready(node, signal)) break;
|
|
564
500
|
if (i >= 150 || child.exitCode !== null)
|
|
565
501
|
throw new HostError(
|
|
566
502
|
"Owned Kubo startup failed; inspect protected daemon.log.",
|
|
@@ -581,52 +517,10 @@ export async function startNode(
|
|
|
581
517
|
}
|
|
582
518
|
record.epoch = epoch;
|
|
583
519
|
await writePrivate(directory, "node.json", record);
|
|
584
|
-
|
|
585
|
-
for (const address of addresses) {
|
|
586
|
-
validateAddress(address);
|
|
587
|
-
const target = address.split("/").at(-1)!;
|
|
588
|
-
if (target === node.peerId) continue;
|
|
589
|
-
const group = groups.get(target) ?? [];
|
|
590
|
-
if (group.length < 16 && !group.includes(address)) group.push(address);
|
|
591
|
-
groups.set(target, group);
|
|
592
|
-
}
|
|
593
|
-
const budget = AbortSignal.timeout(10_000);
|
|
594
|
-
await Promise.all(
|
|
595
|
-
[...groups.values()].map(async (group) => {
|
|
596
|
-
const connected = new AbortController();
|
|
597
|
-
await Promise.all(
|
|
598
|
-
group.map(async (address) => {
|
|
599
|
-
try {
|
|
600
|
-
const response = await rpc(
|
|
601
|
-
node,
|
|
602
|
-
`swarm/connect?arg=${encodeURIComponent(address)}`,
|
|
603
|
-
AbortSignal.any([budget, connected.signal]),
|
|
604
|
-
);
|
|
605
|
-
const ok = response.ok;
|
|
606
|
-
if (!ok) {
|
|
607
|
-
const reason = JSON.parse(await boundedText(response)) as {
|
|
608
|
-
Message?: string;
|
|
609
|
-
};
|
|
610
|
-
diagnostic?.(
|
|
611
|
-
`Swarm peer dial failed at ${address}: ${reason.Message?.slice(0, 512) ?? `HTTP ${response.status}`}`,
|
|
612
|
-
);
|
|
613
|
-
}
|
|
614
|
-
await response.body?.cancel();
|
|
615
|
-
if (ok) connected.abort();
|
|
616
|
-
} catch (cause) {
|
|
617
|
-
if (!connected.signal.aborted)
|
|
618
|
-
diagnostic?.(
|
|
619
|
-
`Swarm peer dial unavailable at ${address}: ${cause instanceof Error ? cause.message : "network error"}`,
|
|
620
|
-
);
|
|
621
|
-
// An offline address must not hide a reachable address of the same PeerID.
|
|
622
|
-
// Content reads still report actual network failure, not authorization failure.
|
|
623
|
-
}
|
|
624
|
-
}),
|
|
625
|
-
);
|
|
626
|
-
}),
|
|
627
|
-
);
|
|
520
|
+
await reconcilePeers(call, desired);
|
|
628
521
|
return node;
|
|
629
|
-
}
|
|
522
|
+
};
|
|
523
|
+
return withPrivateLock(directory, start, signal);
|
|
630
524
|
}
|
|
631
525
|
export function validateAddress(address: string, expectedPeer?: string): void {
|
|
632
526
|
const match =
|
|
@@ -643,48 +537,133 @@ export function validateAddress(address: string, expectedPeer?: string): void {
|
|
|
643
537
|
"LAN/VPN profile requires IPv4 TCP multiaddr bound to its PeerID.",
|
|
644
538
|
);
|
|
645
539
|
}
|
|
646
|
-
export function kuboSource(
|
|
540
|
+
export function kuboSource(
|
|
541
|
+
node: KuboNode,
|
|
542
|
+
addresses: readonly string[] = [],
|
|
543
|
+
diagnostic?: (line: string) => void,
|
|
544
|
+
detailed = false,
|
|
545
|
+
) {
|
|
546
|
+
const groups = new Map<string, string[]>();
|
|
547
|
+
for (const address of new Set(addresses)) {
|
|
548
|
+
validateAddress(address);
|
|
549
|
+
const target = address.split("/").at(-1)!;
|
|
550
|
+
if (target === node.peerId) continue;
|
|
551
|
+
const group = groups.get(target) ?? [];
|
|
552
|
+
if (group.length < 16)
|
|
553
|
+
group.push(address.slice(0, -`/p2p/${target}`.length));
|
|
554
|
+
groups.set(target, group);
|
|
555
|
+
}
|
|
556
|
+
let fallbackUsed = false;
|
|
557
|
+
const connected = async (signal: AbortSignal) => {
|
|
558
|
+
const response = await rpc(node, "swarm/peers", signal);
|
|
559
|
+
if (!response.ok) {
|
|
560
|
+
await response.body?.cancel();
|
|
561
|
+
throw new HostError(`HTTP ${response.status}`);
|
|
562
|
+
}
|
|
563
|
+
const data = object(
|
|
564
|
+
JSON.parse(await boundedText(response)) as unknown,
|
|
565
|
+
"owned Kubo connections",
|
|
566
|
+
);
|
|
567
|
+
if (data.Peers === null) return new Set<string>();
|
|
568
|
+
if (!Array.isArray(data.Peers))
|
|
569
|
+
throw new HostError("Invalid owned Kubo connections.");
|
|
570
|
+
return new Set(
|
|
571
|
+
data.Peers.map((entry: unknown) => {
|
|
572
|
+
const { Peer } = object(entry, "owned Kubo connection");
|
|
573
|
+
if (typeof Peer !== "string")
|
|
574
|
+
throw new HostError("Invalid owned Kubo connection.");
|
|
575
|
+
return Peer;
|
|
576
|
+
}),
|
|
577
|
+
);
|
|
578
|
+
};
|
|
579
|
+
const connect = async (
|
|
580
|
+
id: string,
|
|
581
|
+
addrs: readonly string[],
|
|
582
|
+
signal: AbortSignal,
|
|
583
|
+
) => {
|
|
584
|
+
const response = await rpc(
|
|
585
|
+
node,
|
|
586
|
+
`swarm/connect?${peerArguments(id, addrs)}`,
|
|
587
|
+
signal,
|
|
588
|
+
);
|
|
589
|
+
try {
|
|
590
|
+
if (!response.ok) throw new HostError(`HTTP ${response.status}`);
|
|
591
|
+
// swarm/connect may stream the result. Do not treat headers alone as success.
|
|
592
|
+
await boundedText(response);
|
|
593
|
+
} finally {
|
|
594
|
+
await response.body?.cancel();
|
|
595
|
+
}
|
|
596
|
+
};
|
|
647
597
|
return {
|
|
648
598
|
block: {
|
|
649
599
|
async get(
|
|
650
600
|
cid: string,
|
|
651
601
|
options: { signal?: AbortSignal } = {},
|
|
652
602
|
): Promise<Uint8Array> {
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
603
|
+
return readWithPeers(
|
|
604
|
+
groups,
|
|
605
|
+
async (signal, offline) => {
|
|
606
|
+
const response = await rpc(
|
|
607
|
+
node,
|
|
608
|
+
`block/get?arg=${encodeURIComponent(cid)}${offline ? "&offline=true" : ""}`,
|
|
609
|
+
signal,
|
|
610
|
+
30_000,
|
|
611
|
+
);
|
|
612
|
+
if (!response.ok || !response.body) {
|
|
613
|
+
const error = `Swarm block unavailable (HTTP ${response.status}).`;
|
|
614
|
+
if (offline && [404, 500].includes(response.status)) {
|
|
615
|
+
const detail = object(
|
|
616
|
+
JSON.parse(await boundedText(response)) as unknown,
|
|
617
|
+
"owned Kubo block error",
|
|
618
|
+
).Message;
|
|
619
|
+
if (
|
|
620
|
+
typeof detail === "string" &&
|
|
621
|
+
detail.startsWith("block was not found locally (offline):")
|
|
622
|
+
)
|
|
623
|
+
throw new BlockUnavailable(error);
|
|
624
|
+
}
|
|
625
|
+
await response.body?.cancel();
|
|
626
|
+
if (!offline && [404, 500].includes(response.status))
|
|
627
|
+
throw new BlockUnavailable(error);
|
|
628
|
+
throw new HostError(error);
|
|
629
|
+
}
|
|
630
|
+
const stream: ReadableStream<Uint8Array> = response.body;
|
|
631
|
+
const reader = stream.getReader();
|
|
632
|
+
const chunks: Uint8Array[] = [];
|
|
633
|
+
let size = 0;
|
|
634
|
+
try {
|
|
635
|
+
for (;;) {
|
|
636
|
+
const part = await reader.read();
|
|
637
|
+
if (part.done) break;
|
|
638
|
+
size += part.value.length;
|
|
639
|
+
if (size > 2 * 1024 * 1024) {
|
|
640
|
+
await reader.cancel();
|
|
641
|
+
throw new HostError("Kubo block exceeds limit.");
|
|
642
|
+
}
|
|
643
|
+
chunks.push(part.value);
|
|
644
|
+
}
|
|
645
|
+
} finally {
|
|
646
|
+
reader.releaseLock();
|
|
647
|
+
}
|
|
648
|
+
const bytes = new Uint8Array(size);
|
|
649
|
+
let offset = 0;
|
|
650
|
+
for (const chunk of chunks) {
|
|
651
|
+
bytes.set(chunk, offset);
|
|
652
|
+
offset += chunk.length;
|
|
653
|
+
}
|
|
654
|
+
return bytes;
|
|
655
|
+
},
|
|
656
|
+
connected,
|
|
657
|
+
connect,
|
|
658
|
+
() => {
|
|
659
|
+
if (fallbackUsed) return false;
|
|
660
|
+
fallbackUsed = true;
|
|
661
|
+
return true;
|
|
662
|
+
},
|
|
656
663
|
options.signal,
|
|
657
|
-
|
|
664
|
+
diagnostic,
|
|
665
|
+
detailed,
|
|
658
666
|
);
|
|
659
|
-
if (!response.ok || !response.body)
|
|
660
|
-
throw new HostError(
|
|
661
|
-
`Swarm block unavailable (HTTP ${response.status}).`,
|
|
662
|
-
);
|
|
663
|
-
const stream: ReadableStream<Uint8Array> = response.body;
|
|
664
|
-
const reader = stream.getReader();
|
|
665
|
-
const chunks: Uint8Array[] = [];
|
|
666
|
-
let size = 0;
|
|
667
|
-
try {
|
|
668
|
-
for (;;) {
|
|
669
|
-
const part = await reader.read();
|
|
670
|
-
if (part.done) break;
|
|
671
|
-
size += part.value.length;
|
|
672
|
-
if (size > 2 * 1024 * 1024) {
|
|
673
|
-
await reader.cancel();
|
|
674
|
-
throw new HostError("Kubo block exceeds limit.");
|
|
675
|
-
}
|
|
676
|
-
chunks.push(part.value);
|
|
677
|
-
}
|
|
678
|
-
} finally {
|
|
679
|
-
reader.releaseLock();
|
|
680
|
-
}
|
|
681
|
-
const bytes = new Uint8Array(size);
|
|
682
|
-
let offset = 0;
|
|
683
|
-
for (const chunk of chunks) {
|
|
684
|
-
bytes.set(chunk, offset);
|
|
685
|
-
offset += chunk.length;
|
|
686
|
-
}
|
|
687
|
-
return bytes;
|
|
688
667
|
},
|
|
689
668
|
},
|
|
690
669
|
};
|