-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhypernat-client.js
More file actions
84 lines (76 loc) · 2.63 KB
/
hypernat-client.js
File metadata and controls
84 lines (76 loc) · 2.63 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
#!/usr/bin/env node
require('dotenv').config();
const Keychain = require('keypear');
const DHT = require("@hyperswarm/dht");
const pump = require("pump");
var net = require("net");
const udp = require('dgram');
const argv = require('yargs').argv;
const options = {
"schema": [
{
"mode": "client",
"proto": process.env.PROTO || "tcp",
"port": process.env.PORT || '3000',
"publicKey": process.env.PEER || argv.peer
}
]
}
const { base58_to_binary, binary_to_base58 } = require('base58-js')
const relay = async () => {
const node = new DHT({});
await node.ready();
return {
tcp: {
client: async (publicKey, port) => {
var server = net.createServer({allowHalfOpen: true},function (local) {
console.log('Connecting to tcp', port);
const socket = node.connect(publicKey, { reusableSocket: true });
pump(local, socket, local);
});
server.listen(port, "127.0.0.1");
console.log('Listening for local connections on tcp', port);
}
},
udp: {
client: async (publicKey, port) => {
console.log('Connecting to udp', port);
const conn = await node.connect(publicKey);
await new Promise(res => conn.on('open', res));
console.log('connection open');
var server = udp.createSocket('udp4');
let inport;
server.on('message', async (buf, rinfo) => {
if (!inport) {
console.log('setting port', rinfo);
inport = rinfo.port;
}
conn.rawStream.send(buf);
})
conn.rawStream.on('message', (buf) => {
server.send(buf, inport);
})
server.bind(port);
console.log('UDP stream ready, listening for packets on ', port);
}
}
}
}
const schema = options.schema;
const modes = {
client: async (settings) => {
const {proto, port, publicKey} = settings;
const keys = new Keychain(base58_to_binary(publicKey));
const key = keys.get(proto+port).publicKey;
const rel = await relay();
return (rel)[proto].client(key, port);
}
}
const run = async () => {
console.log('\033[31;1mHyperNAT client starting ...\033[0m');
console.log('\033[32;1mPEER:\033[0m', argv.peer)
for (forwarder of schema) {
await modes[forwarder.mode](forwarder);
}
}
run()