-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
74 lines (69 loc) · 2.25 KB
/
index.js
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
const DHT = require("@hyperswarm/dht");
const crypto = require('hypercore-crypto')
const { unpack, pack } = require('msgpackr');
const runKey = async (key, args) => {
const node = new DHT();
return new Promise((pass, fail) => {
console.log('calling', key.toString('hex'), args);
const socket = node.connect(key);
socket.on('open', function () {
console.log('socket opened')
})
socket.on("data", (res) => {
socket.end();
const out = unpack(res);
if(out && out.error) {
fail(out.error);
throw out;
}
pass(out);
});
socket.on('error', error => fail({ error }));
socket.write(pack(args));
})
}
module.exports = (key = '') => {
return {
serve: (command, cb) => {
const node = new DHT();
const keyPair = crypto.keyPair(crypto.data(Buffer.from(key + command)));
console.log('serving', command, keyPair.publicKey.toString('hex'));
const server = node.createServer({});
server.on("connection", function (socket) {
console.log('connection', keyPair.publicKey.toString('hex'));
socket.on('error', function (e) { throw e });
socket.on("data", async data => {
try {
socket.write(pack(await cb(unpack(data))));
} catch (error) {
console.trace(error);
socket.write(pack({ error }));
}
socket.end();
});
});
server.listen(keyPair);
console.log('running listen...')
},
run: (command, args) => {
console.log('run', command);
const keyPair = crypto.keyPair(crypto.data(Buffer.from(key + command)));
return runKey(keyPair.publicKey, args)
},
runKey
}
}
async function exitHandler(options, exitCode) {
if (options.cleanup) console.log('DHT Cleanup');
if (exitCode || exitCode === 0) console.log(exitCode);
try {
await node.destroy([options])
} catch (e) {
}
if (options.exit) process.exit(1);
}
process.on('exit', exitHandler.bind(null, { cleanup: true }));
process.on('SIGINT', exitHandler.bind(null, { exit: true }));
process.on('SIGUSR1', exitHandler.bind(null, { exit: true }));
process.on('SIGUSR2', exitHandler.bind(null, { exit: true }));
process.on('uncaughtException', exitHandler.bind(null, { exit: true }));