-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (63 loc) · 1.51 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
75
76
77
78
79
80
#!/usr/bin/env node
const HyperDHT = require('hyperdht');
const net = require('net');
const argv = require('minimist')(process.argv.slice(2));
const libNet = require('@hyper-cmd/lib-net');
const libUtils = require('@hyper-cmd/lib-utils');
const libKeys = require('@hyper-cmd/lib-keys');
const connPiper = libNet.connPiper;
const helpMsg = 'Usage:\nhyperproxy -i identity.json -s peer_key';
if (argv.help) {
console.log(helpMsg);
process.exit(-1);
}
const conf = {};
if (argv.s) {
conf.peer = libUtils.resolveHostToKey([], argv.s);
}
if (!conf.keepAlive) {
conf.keepAlive = 5000;
}
const peer = conf.peer;
if (!peer) {
console.error('Error: peer is invalid');
process.exit(-1);
}
let keyPair = null;
if (argv.i) {
keyPair = libUtils.resolveIdentity([], argv.i);
if (!keyPair) {
console.error('Error: identity file invalid');
process.exit(-1);
}
keyPair = libKeys.parseKeyPair(keyPair);
}
const dht = new HyperDHT({
keyPair,
});
const proxy = net.createServer((c) => {
return connPiper(
c,
() => {
const stream = dht.connect(Buffer.from(peer, 'hex'));
stream.setKeepAlive(conf.keepAlive);
return stream;
},
{},
{}
);
});
proxy.listen(0, function () {
console.log(proxy.address());
// const { port } = proxy.address()
// spawn('ssh', sshArgs(username, port), {
// stdio: 'inherit'
// }).once('exit', function (code) {
// process.exit(code)
// })
});
process.once('SIGINT', () => {
dht.destroy().then(() => {
process.exit();
});
});