-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbin.ts
189 lines (159 loc) · 4.82 KB
/
bin.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// TypeScript (.ts)
import { Command } from "commander";
import { createWriteStream, existsSync } from "fs";
import { readFile } from "fs/promises";
import { createKeyPair } from "./src/create-key-pair.js";
import { share } from "./src/p2p/share.js";
import { connect } from "./src/p2p/connect.js";
import kleur from "kleur";
import type { Identity, SerializedIdentity } from "./src/identity.js";
import { APP_VERSION, DEBUG } from "./env.js";
import { setLogLevel } from "./src/logger.js";
import { goodbye } from "./src/goodbye.js";
if (DEBUG) {
setLogLevel(30);
}
const IdentityPath = "./identity.json";
const program = new Command();
program
.name("p2p-socket")
.description("Use the hyperdht to share and connect to p2p-sockets")
.version(APP_VERSION);
addShareCommand(program);
addConnectCommand(program);
addIdCommand(program);
program.parse();
function addIdCommand(program) {
program
.command("create-id")
.description(
"Creates an identity.json file so that your shared p2p-sockets will have the same connect key. The identity.json file should be treated as a secret!"
)
.option(
"-s, --seed [seed...]",
"[optional] Passphrase to seed identity with",
[]
)
.action((options) => {
if (hasLocalIdentity()) {
console.error(
kleur.red(
`${IdentityPath} already exists. Please delete it if you want a new identity`
)
);
return;
}
const parsedSeed = options.seed ? options.seed.join("") : undefined;
const writeStream = createWriteStream(IdentityPath, {
encoding: "utf-8",
});
writeStream.once("close", () => {
console.log(
kleur
.green()
.bold()
.underline(`New identity created at ${IdentityPath}`)
);
});
const newKeyPair = createKeyPair(parsedSeed);
const identity: SerializedIdentity = {
keyPair: {
publicKey: newKeyPair.publicKey.toString("hex"),
secretKey: newKeyPair.secretKey.toString("hex"),
},
};
writeStream.write(JSON.stringify(identity, null, 2));
writeStream.close();
});
}
function addConnectCommand(program: Command) {
program
.command("connect")
.description("Connect to a p2p-socket")
.requiredOption("-k, --remote-key <key>", "[required] Remote share key")
.option("-h, --host <host>", "[optional] default: localhost", "localhost")
.option("-p, --port <port>", "[optional] default: 0", "0")
.action(async (options) => {
const { host, port, remoteKey } = options;
const { tcpServer, disconnect } = await connect({
tcp: {
host,
port: Number(port),
},
remotePublicKey: Buffer.from(remoteKey, "hex"),
});
goodbye(() => disconnect());
const address = tcpServer.address();
let listenPort: number = 0;
if (address && typeof address === "object") {
listenPort = address.port;
}
console.log(
kleur
.green()
.bold()
.underline(`P2P proxy reachable via ${host}:${listenPort}`)
);
});
}
function addShareCommand(program: Command) {
program
.command("share")
.description(
"Share something with the P2P network. Peers will use your publicKey to connect"
)
.requiredOption("-p, --port <port>", "[required] Port")
.option(
"-h, --host <host>",
"[optional] host default: localhost",
"localhost"
)
.action(async (options) => {
const identity = await getIdentity();
const { host, port } = options;
const proxy = await share({
tcp: {
host,
port: Number(port),
},
keyPair: identity.keyPair,
});
goodbye(() => proxy.unshare());
const remoteKey = proxy.hostAndKeyPair.publicKey.toString("hex");
console.log(
kleur
.dim()
.underline(`You are now sharing ${host}:${port} with P2P network`)
);
console.log();
console.log(kleur.bold(`Peers can connect to you by running:`));
console.log(
kleur
.italic()
.underline(
`npx p2p-socket@${APP_VERSION} connect --port ${port} --remote-key ${remoteKey}`
)
);
});
}
async function getIdentity(): Promise<Identity> {
if (hasLocalIdentity()) {
return getLocalIdentity();
}
return {
keyPair: createKeyPair(),
};
}
async function getLocalIdentity(): Promise<Identity> {
const dataBuffer = await readFile(IdentityPath);
const data: SerializedIdentity = JSON.parse(dataBuffer.toString("utf-8"));
return {
keyPair: {
publicKey: Buffer.from(data.keyPair.publicKey, "hex"),
secretKey: Buffer.from(data.keyPair.secretKey, "hex"),
},
};
}
function hasLocalIdentity() {
return existsSync(IdentityPath);
}