-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathking-cmd.ts
More file actions
75 lines (70 loc) · 2.47 KB
/
Copy pathking-cmd.ts
File metadata and controls
75 lines (70 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import waitFor from "p-wait-for";
import {ArgumentsCamelCase, Argv} from "yargs";
import {Logger} from "../logger.js";
import {StateHandler} from "../state-handler.js";
import {portsReachable} from "../utils.js";
import {KingConfig} from "../configs/king-config.js";
import {KingRatholeManager} from "../managers/king-rathole-manager.js";
import {initKingShutdownHandlers} from "../shutdown/king-shutdown.js";
import {KingSyncer} from "../tickers/king-syncer.js";
import {KingContext} from "../contexts/king-context.js";
export interface KingArguments {
councilHost: string;
rathole: string[];
host: string;
location: string;
noisePrivateKey?: string;
noisePublicKey?: string;
}
export const command = "king";
export const description = "Start ratking";
export async function handler (args: ArgumentsCamelCase) {
const logger = new Logger();
const config = new KingConfig(args as ArgumentsCamelCase<KingArguments>);
await portsReachable(config.ratholes);
const context = new KingContext(logger, config);
const ratholeManager = new KingRatholeManager(context);
const syncer = new KingSyncer(context);
const stateHandler = new StateHandler({
...context,
councilHost: config.councilHost,
stateChanged: async (state) => {
context.state = state;
await ratholeManager.stateChanged();
},
});
initKingShutdownHandlers({context, stateHandler, syncer, ratholeManager});
stateHandler.start();
await waitFor(() => stateHandler.hasState());
syncer.start();
logger.info("Ready", {"service.type": "ratking"});
}
export function builder (yargs: Argv) {
yargs.options("council-host", {
type: "string",
description: "Council host to syncronize from",
default: "http://localhost:8080",
});
yargs.options("host", {
type: "string",
description: "Host (domain or ip)",
});
yargs.options("rathole", {
type: "array",
description: "Rathole servers to open",
});
yargs.options("location", {
type: "string",
description: "Location identifier",
demandOption: true,
});
yargs.options("noise-private-key", {
type: "string",
description: "Base64 encoded Noise protocol private key (from rathole --genkey)",
});
yargs.options("noise-public-key", {
type: "string",
description: "Base64 encoded Noise protocol public key (from rathole --genkey)",
});
return yargs;
}