-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.js
127 lines (100 loc) · 2.87 KB
/
server.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
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
#!/usr/bin/env node
const HyperDHT = require('hyperdht')
const net = require('net')
const b4a = require('b4a')
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 goodbye = require('graceful-goodbye')
const connPiper = libNet.connPiper
const helpMsg = 'Usage:\nhypertele-server -l service_port -u unix_socket ?--address service_address ?-c conf.json ?--seed seed ?--cert-skip ?--private'
if (argv.help) {
console.log(helpMsg)
process.exit(-1)
}
if (!argv.u && !+argv.l) {
console.error('Error: proxy port invalid')
process.exit(-1)
}
if (argv.u && argv.l) {
console.error('Error: cannot listen to both a port and a Unix domain socket')
process.exit(-1)
}
const conf = {}
conf.keepAlive = 5000
if (argv.seed) {
conf.seed = argv.seed
}
if (argv.c) {
libUtils.readConf(conf, argv.c)
}
if (argv.compress) {
conf.compress = true
}
if (argv['cert-skip']) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
}
if (!conf.seed) {
console.error('Error: conf.seed invalid')
process.exit(-1)
}
if (conf.allow) {
conf.allow = libKeys.prepKeyList(conf.allow)
}
conf.private = false
if (argv.private) {
if (conf.allow) throw new Error('--private flag is not compatible with an allow list, as the private key derived from the seed is the capability')
conf.private = true
}
// Unofficial opt, only used for tests
let bootstrap = null
if (argv.bootstrap) {
bootstrap = [{ host: '127.0.0.1', port: argv.bootstrap }]
}
const debug = argv.debug
const seed = Buffer.from(conf.seed, 'hex')
const dht = new HyperDHT({
bootstrap,
connectionKeepAlive: conf.keepAlive
})
const keyPair = HyperDHT.keyPair(seed)
const stats = {}
const destIp = argv.address || '127.0.0.1'
const privateFirewall = (remotePublicKey) => {
return !b4a.equals(remotePublicKey, keyPair.publicKey)
}
const allowListFirewall = (remotePublicKey, remoteHandshakePayload) => {
if (conf.allow && !libKeys.checkAllowList(conf.allow, remotePublicKey)) {
return true
}
return false
}
const firewall = conf.private ? privateFirewall : allowListFirewall
const server = dht.createServer({
firewall,
reusableSocket: true
}, c => {
connPiper(c, () => {
return net.connect(
argv.u
? { path: argv.u }
: { port: +argv.l, host: destIp, allowHalfOpen: true }
)
}, { debug, isServer: true, compress: conf.compress }, stats)
})
server.listen(keyPair).then(() => {
if (conf.private) {
console.log(`hypertele (private): connect with seed ${b4a.toString(seed, 'hex')} (listening on ${b4a.toString(keyPair.publicKey, 'hex')})`)
} else {
console.log('hypertele:', keyPair.publicKey.toString('hex'))
}
})
if (debug) {
setInterval(() => {
console.log('connection stats', stats)
}, 5000)
}
goodbye(async () => {
await dht.destroy()
})