Skip to content

Commit 72838f5

Browse files
committed
add default allow thing
1 parent 0e1b604 commit 72838f5

6 files changed

Lines changed: 60 additions & 14 deletions

File tree

src/cli/daemon.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import "dotenv/config";
22
import { enable, logger } from "@libp2p/logger";
33
import { Multiaddr } from "@multiformats/multiaddr";
44
import type { Libp2pOptions } from "libp2p";
5+
import { join, resolve } from "node:path";
56
import { parseArgs } from "node:util";
67
import { SupportedPrivateKey } from "../challenge.js";
78
import { ZZZYNC } from "../constants.js";
@@ -42,8 +43,9 @@ export const run: SubCommand["run"] = async (args: string[]) => {
4243
) {
4344
throw new Error(`--config directory must be named ".${command}"`);
4445
}
46+
const CONFIG_DIR = resolve(values.config);
4547
const { CONFIG_PATH, datastore, blockstore } = await setupConfig(
46-
values.config,
48+
CONFIG_DIR,
4749
"daemon",
4850
);
4951
const config: DaemonConfig = await import(CONFIG_PATH);
@@ -74,6 +76,13 @@ export const run: SubCommand["run"] = async (args: string[]) => {
7476
libp2p.privateKey = privateKey;
7577
}
7678

79+
const handlerOptions = config.handlerOptions ?? {};
80+
81+
if (handlerOptions.allow == null) {
82+
const { createAllow } = await import(join(__dirname, "default-allow.js"));
83+
handlerOptions.allow = createAllow(CONFIG_DIR);
84+
}
85+
7786
const helia = await createZzzyncServer({
7887
blockstore,
7988
datastore,

src/cli/default-allow.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { logger } from "@libp2p/logger";
2+
import { existsSync, readFileSync, watchFile } from "node:fs";
3+
import { join } from "node:path";
4+
import { SupportedPrivateKey } from "../challenge.js";
5+
import { ZZZYNC } from "../constants.js";
6+
import { AllowFn } from "../handler.js";
7+
import { contenthash } from "../utils.js";
8+
9+
const log = logger(`${ZZZYNC}:allow`);
10+
11+
export function createDefaultAllow(configDir: string): AllowFn {
12+
const allowPath = join(configDir, "allow");
13+
const allowed = new Set<string>();
14+
15+
watchFile(allowPath, { interval: 500 }, (cur, prev) => {
16+
if (cur.mtimeMs === prev.mtimeMs) {
17+
return;
18+
}
19+
log("allow list updated");
20+
allowed.clear();
21+
22+
if (!existsSync(allowPath)) return;
23+
24+
const data = readFileSync(allowPath, "utf8");
25+
for (const line of data.split("\n")) {
26+
allowed.add(line);
27+
}
28+
});
29+
30+
return (publicKey: SupportedPrivateKey["publicKey"]): boolean => {
31+
return allowed.has(contenthash(publicKey));
32+
};
33+
}

src/cli/upload.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ export const run: SubCommand["run"] = async (args: string[]) => {
6969
) {
7070
throw new Error(`--config directory must be named ".${command}"`);
7171
}
72+
const CONFIG_DIR = resolve(values.config);
7273
const { CONFIG_PATH, datastore, blockstore } = await setupConfig(
73-
values.config,
74+
CONFIG_DIR,
7475
"upload",
7576
);
7677
const config: UploadConfig<ZzzyncServices> = await import(CONFIG_PATH);

src/cli/utils.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Datastore } from "interface-datastore";
1010
import { base36 } from "multiformats/bases/base36";
1111
import { existsSync } from "node:fs";
1212
import { mkdir } from "node:fs/promises";
13-
import { join, resolve } from "node:path";
13+
import { join } from "node:path";
1414
import { dirname } from "node:path";
1515
import { fileURLToPath } from "node:url";
1616
import { SupportedPrivateKey } from "../challenge.js";
@@ -94,16 +94,13 @@ export const setupConfig = async (
9494
): Promise<
9595
{ CONFIG_PATH: string; blockstore: Blockstore; datastore: Datastore; }
9696
> => {
97-
const CONFIG_DIR = resolve(dir);
98-
await mkdir(CONFIG_DIR, { recursive: true });
97+
await mkdir(dir, { recursive: true });
9998

100-
const datastore = new LevelDatastore(join(CONFIG_DIR, `${space}/datastore`));
101-
const blockstore = new LevelBlockstore(
102-
join(CONFIG_DIR, `${space}/blockstore`),
103-
);
99+
const datastore = new LevelDatastore(join(dir, `${space}/datastore`));
100+
const blockstore = new LevelBlockstore(join(dir, `${space}/blockstore`));
104101

105102
const DEFAULT_CONFIG_PATH = join(__dirname, "daemon-config.js");
106-
const CUSTOM_CONFIG_PATH = join(CONFIG_DIR, `${space}/config.js`);
103+
const CUSTOM_CONFIG_PATH = join(dir, `${space}/config.js`);
107104
const CONFIG_PATH = existsSync(CUSTOM_CONFIG_PATH)
108105
? CUSTOM_CONFIG_PATH
109106
: DEFAULT_CONFIG_PATH;

src/handler.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ import defer from "p-defer";
3737
import * as varint from "uint8-varint";
3838
import { isUint8ArrayList, Uint8ArrayList } from "uint8arraylist";
3939
import { equals } from "uint8arrays";
40-
import { buildChallenge, generateNonce } from "./challenge.js";
40+
import {
41+
buildChallenge,
42+
generateNonce,
43+
SupportedPrivateKey,
44+
} from "./challenge.js";
4145
import {
4246
CODEC_DAG_PB,
4347
CODEC_IDENTITY,
@@ -218,7 +222,7 @@ export async function* readCarFile(
218222
}
219223

220224
export type AllowFn = (
221-
ipnsMultihash: IpnsMultihash,
225+
dialerPublicKey: SupportedPrivateKey["publicKey"],
222226
options?: AbortOptions,
223227
) => Promise<boolean> | boolean;
224228

@@ -274,7 +278,9 @@ export const createZzzyncHandler =
274278
}
275279
const dialerLibp2pKey = dialerPublicKey.toCID();
276280

277-
if (options.allow && !(await options.allow(dialerIpns, { signal }))) {
281+
if (
282+
options.allow && !(await options.allow(dialerPublicKey, { signal }))
283+
) {
278284
const error = new Error("ipns key not allowed");
279285
log.error(error.message);
280286
throw error;

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ export function publicKeyAsIpnsMultihash(
5656
return null;
5757
}
5858

59-
export function contenthash(publicKey: PublicKey) {
59+
export function contenthash(publicKey: PublicKey): string {
6060
return `/ipns/${publicKey.toCID().toString(base36)}`;
6161
}

0 commit comments

Comments
 (0)