-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
129 lines (115 loc) · 3.41 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
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
128
129
// Importing required modules
const HyperDHT = require("hyperdht"); // HyperDHT module for DHT functionality
const net = require("net"); // Node.js net module for creating network clients and servers
const libNet = require("@holesail/hyper-cmd-lib-net"); // Custom network library
const libKeys = require("hyper-cmd-lib-keys"); // To generate a random preSeed for server seed.
const b4a = require("b4a");
class holesailServer {
constructor() {
this.dht = new HyperDHT();
this.stats = {};
this.server = null;
this.keyPair = null;
this.buffer = null;
this.seed = null;
this.connection = null;
}
keyPairGenerator(buffer) {
// Used to generate a seed
// if buffer key is provided by the user, allows to keep the same keyPair everytime.
if (buffer) {
this.buffer = buffer;
} else {
this.buffer = libKeys.randomBytes(32).toString("hex");
}
// generate a seed from the buffer key
this.seed = Buffer.from(this.buffer, "hex");
// generate a keypair from the seed
this.keyPair = HyperDHT.keyPair(this.seed);
return this.keyPair;
}
// start the client on port and the address specified
serve(args, callback) {
this.secure = args.secure === true;
// generate the keypair
this.keyPairGenerator(args.buffSeed);
// this is needed for the secure mode to work and is implemented by HyperDHT
if (this.secure) {
var privateFirewall = (remotePublicKey) => {
return !b4a.equals(remotePublicKey, this.keyPair.publicKey);
};
} else {
var privateFirewall = false;
}
if (!args.udp) {
this.handleTCP(args, privateFirewall);
} else {
this.handleUDP(args, privateFirewall);
}
// start listening on the keyPair
this.server.listen(this.keyPair).then(() => {
if (typeof callback === "function") {
callback(); // Invoke the callback after the server has started
}
});
}
// Handle TCP connections
handleTCP(args, privateFirewall) {
this.server = this.dht.createServer(
{
firewall: privateFirewall,
reusableSocket: true,
},
(c) => {
// Connection handling using custom connection piper function
this.connection = libNet.connPiper(
c,
() => {
return net.connect({
port: +args.port,
host: args.address,
allowHalfOpen: true,
});
},
{ isServer: true, compress: false },
this.stats,
);
},
);
}
// Handle UDP connections
handleUDP(args, privateFirewall) {
this.server = this.dht.createServer({
privateFirewall,
reusableSocket: true,
});
this.server.on("connection", (c) => {
this.connection = libNet.udpPiper(
c,
() => {
return libNet.udpConnect({
port: +args.port,
host: args.address,
});
},
{ isServer: true, compress: false },
this.stats,
);
});
}
// destroy the dht instance
// TODO: Fix issue with server not destroying but only DHT connection after destroy() is called.
destroy() {
this.dht.destroy();
return 0;
}
// Return the public/connection key
getPublicKey() {
if (this.secure) {
return b4a.toString(this.seed, "hex");
} else {
return this.keyPair.publicKey.toString("hex");
}
}
} // end server Class
module.exports = holesailServer;