-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpub.js
More file actions
113 lines (89 loc) · 2.17 KB
/
pub.js
File metadata and controls
113 lines (89 loc) · 2.17 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
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
#!/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 goodbye = require('graceful-goodbye')
const connRemoteCtrl = libNet.connRemoteCtrl
const helpMsg = 'Usage:\nhypertele-pub -l port_local ?-c conf.json ?--seed seed'
if (argv.help) {
console.log(helpMsg)
process.exit(-1)
}
if (!+argv.l) {
console.error('Error: proxy port invalid')
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)
}
const debug = argv.debug
const seed = Buffer.from(conf.seed, 'hex')
const dht = new HyperDHT({ connectionKeepAlive: conf.keepAlive })
const keyPair = HyperDHT.keyPair(seed)
const stats = { cid: 0 }
const clients = {}
const local = net.createServer((socket) => {
socket.on('data', d => {
const cids = Object.keys(clients)
cids.forEach(cid => {
const c = clients[cid]
c.send(d)
})
})
})
local.listen(+argv.l, '0.0.0.0')
const server = dht.createServer({
firewall: (remotePublicKey, remoteHandshakePayload) => {
if (conf.allow && !libKeys.checkAllowList(conf.allow, remotePublicKey)) {
return true
}
return false
},
reusableSocket: true
}, c => {
const cid = stats.cid++
const ops = connRemoteCtrl(c, {
onDestroy: () => {
delete clients[cid]
},
debug,
isServer: true,
compress: conf.compress
}, stats)
clients[cid] = ops
})
server.listen(keyPair).then(() => {
console.log('hypertele:', keyPair.publicKey.toString('hex'))
})
if (debug) {
setInterval(() => {
console.log('connection stats', stats)
}, 5000)
}
goodbye(async () => {
await new Promise(resolve => {
local.close(resolve)
})
await dht.destroy()
})