This repository has been archived by the owner on Apr 1, 2023. It is now read-only.
forked from HandyOSS/HandyHost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandyDvpn.js
368 lines (357 loc) · 10.6 KB
/
HandyDvpn.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import fs from 'fs';
import path from 'path';
import url from 'url';
import {Daemon} from './dvpnAPI/Daemon.js';
import {DVPNSetup} from './dvpnAPI/Setup.js';
import {UpdateHelper} from './dvpnAPI/UpdateHelper.js';
import {DVPNStats} from './dvpnAPI/Stats.js';
import {CommonUtils} from './CommonUtils.js';
import {spawn} from 'child_process';
export class HandyDVPN{
constructor(){
this.ioNamespaces = {};
this.redlistPortsPath = process.env.HOME+'/.HandyHost/ports.json';
this.daemon = new Daemon();
this.dvpnSetup = new DVPNSetup();
this.updateHelper = new UpdateHelper();
this.dvpnStats = new DVPNStats();
this.handyUtils = new CommonUtils();
try{
fs.mkdirSync(`${process.env.HOME}/.HandyHost/sentinelData`,{recursive:true})
}
catch(e){
//folder already exists
}
let acctName;
try{
acctName = fs.readFileSync(`${process.env.HOME}/.HandyHost/sentinelData/.nodeEnv`,'utf8');
}
catch(e){}
if(typeof acctName != "undefined"){
process.env.DVPN_ACCT_NAME = acctName.replace(/\n/g,'');
//try startup here since we have already inited
}
setTimeout(()=>{
//edge case: if I manually removed .sentinelnode for a clean install
//left my old instance running, ran the installer, then started up,
//the 2nd instance would fire ininConfigs on startup but never finish
//because the new instance would fail **almost** immediately.
//we add this timeout to give some buffer time..
this.dvpnSetup.initConfigs(); //check if dvpn configs exist, create them if not.
},100);
setTimeout(()=>{
//give some time to spin up before autostarting
this.dvpnSetup.autostartDVPN(this.ioNamespaces); //make sure io exists before we autostart
},5000);
}
addSocketNamespace(ioNamespace,serverName){
//console.log('init sia sockets');
this.ioNamespaces[serverName] = {namespace:ioNamespace};
this.ioNamespaces[serverName].namespace.adapter.on("create-room", (room) => {
if(room.indexOf('dvpn') == 0){
//start a Socket listener for this room
this.initSocketListener(room,serverName);
}
});
this.ioNamespaces[serverName].namespace.adapter.on("delete-room", (room) => {
//console.log(`room deleted ${room}`);
if(room.indexOf('dvpn') == 0){
//stop a Socket listener for this room
this.removeSocketListener(room,serverName);
}
});
/*this.ioNamespaces[serverName].adapter.on("join-room", (room, id) => {
console.log(`socket ${id} has joined room ${room}`);
});
this.ioNamespaces[serverName].adapter.on("leave-room", (room, id) => {
console.log(`socket ${id} has left room ${room}`);
});*/
this.ioNamespaces[serverName].namespace.on('connection',(socket)=>{
this.addSocketConnection(socket,serverName);
});
}
addSocketConnection(socket,serverName){
socket.emit('register');
socket.on('subscribe',()=>{
socket.join('dvpn');
this.checkForUpdates(serverName);
})
socket.on('getAppStatus',()=>{
let status = {};
this.updateHelper.checkForUpdates().then(data=>{
if(Object.keys(data).length > 0){
status.current = data.current;
status.latest = data.all[data.all.length-1];
if(data.current != data.all[data.all.length-1]){
status.isUpToDate = false;
}
else{
status.isUpToDate = true;
}
}
this.dvpnSetup.checkMachineStatus().then(isRunning=>{
status.active = isRunning;
socket.emit('versionStatus',status);
}).catch(err=>{
status.active = false;
socket.emit('versionStatus',status);
})
})
//separately check for handyhost updates
let handyhostStatus = {};
handyhostStatus.current = fs.readFileSync("./VERSION",'utf8').trim();
this.handyUtils.checkForUpdates().then(data=>{
console.log('hh version data??',data);
handyhostStatus.isUpToDate = data.isUpToDate
handyhostStatus.latest = data.latest;
handyhostStatus.current = data.local;
socket.emit('handyhostVersionStatus',handyhostStatus);
}).catch(err=>{
//couldnt ping github
socket.emit('handyhostVersionStatus',handyhostStatus);
})
})
}
checkForUpdates(serverName){
this.updateHelper.checkForUpdates().then(data=>{
if(Object.keys(data).length > 0){
if(data.current != data.all[data.all.length-1]){
this.ioNamespaces[serverName].namespace.to('dvpn').emit('updatesAvailable',data);
}
else{
this.ioNamespaces[serverName].namespace.to('dvpn').emit('nodeIsUpToDate');
}
}
})
this.handyUtils.checkForUpdates().then(data=>{
console.log('HandyHost versionData',data);
if(!data.isUpToDate){
this.ioNamespaces[serverName].namespace.to('dvpn').emit('HandyHostUpdatesAvailable',data);
}
else{
this.ioNamespaces[serverName].namespace.to('dvpn').emit('HandyHostIsUpToDate',data);
}
}).catch(error=>{
console.log('error checking for handyhost updates',error);
})
//this.ioNamespace.to('dvpn').emit('updatesAvailable',data);
}
retrieveAnalytics(serverName){
this.dvpnStats.getActiveSessionAnalytics().then(data=>{
this.ioNamespaces[serverName].namespace.to('dvpn').emit('sessionAnalytics',data.json,data.timeseries,data.sessions);
}).catch(error=>{
this.ioNamespaces[serverName].namespace.to('dvpn').emit('sessionAnalytics',{},{},{});
})
}
initSocketListener(room,serverName){
//TODO: add when we get more stats on nodes..
if(typeof this.ioNamespaces[serverName].updateCheckRoomInterval == "undefined"){
//spin up an interval to send out stats
this.ioNamespaces[serverName].updateCheckRoomInterval = setInterval(()=>{
this.checkForUpdates(serverName);
},60 * 1000 * 60); //hourly check for updates
}
if(typeof this.ioNamespaces[serverName].sessionAnalyticsInterval == "undefined"){
this.ioNamespaces[serverName].sessionAnalyticsInterval = setInterval(()=>{
this.retrieveAnalytics(serverName);
},30*1000); //every 30s
}
}
removeSocketListener(room,serverName){
//everybody left the room, kill the update interval
/*clearInterval(this.socketRoomInterval);
delete this.socketRoomInterval;*/
if(typeof this.ioNamespaces[serverName].updateCheckRoomInterval != "undefined"){
clearInterval(this.ioNamespaces[serverName].updateCheckRoomInterval);
delete this.ioNamespaces[serverName].updateCheckRoomInterval;
}
if(typeof this.ioNamespaces[serverName].sessionAnalyticsInterval != "undefined"){
clearInterval(this.ioNamespaces[serverName].sessionAnalyticsInterval);
delete this.ioNamespaces[serverName].sessionAnalyticsInterval;
}
}
sendSocketUpdates(){
/*
this.ioNamespace.to('dvpn').emit('update',{
chain:chainData,
wallet:walletData,
daemon: versionD
});
*/
}
api(path,requestBody,resolve,reject){
switch(`${path[1]}`){
case 'getState':
//check if its installed
this.dvpnStats.getState().then(data=>{
resolve(data);
}).catch(error=>{
reject(error);
})
break;
case 'initWallet':
this.initWallet(requestBody).then(data=>{
resolve(data);
}).catch(error=>{
reject(error);
})
break;
case 'getConfigs':
this.getConfigs().then(data=>{
resolve(data);
}).catch(error=>{
reject(error);
});
break;
case 'getPortsRedlist':
this.getPortsRedlist().then(data=>{
resolve(data);
}).catch(error=>{
reject(error);
})
break;
case 'updateNodeConfig':
this.updateNodeConfig(requestBody).then(data=>{
resolve(data);
}).catch(error=>{
reject(error);
})
break;
case 'getWallets':
this.getWallets(requestBody).then(data=>{
resolve(data);
}).catch(error=>{
reject(error);
})
break;
case 'launch':
this.launchNode(requestBody).then(data=>{
resolve(data);
}).catch(error=>{
reject(error);
})
break;
case 'rebuild':
this.rebuildNode().then(data=>{
resolve(data);
}).catch(error=>{
reject(error);
})
break;
case 'stop':
this.dvpnSetup.stopDVPN().then(()=>{
resolve({stop:true})
}).catch(error=>{
reject(error);
})
break;
case 'updateDVPN':
this.dvpnSetup.stopDVPN().then(()=>{
this.updateHelper.updateDVPN(this.ioNamespaces).then(()=>{
this.checkForUpdates();
resolve({finished:true});
}).catch(error=>{
reject({error})
})
})
break;
case 'getDashboardStats':
this.dvpnStats.getDashboardStats().then((data)=>{
resolve(data);
}).catch(error=>{
reject(error);
})
break;
case 'getActiveSessionAnalytics':
this.dvpnStats.getActiveSessions().then(data=>{
resolve(data);
}).catch(error=>{
reject(error);
})
break;
}
}
getConfigs(){
return new Promise((resolve,reject)=>{
let operator = '';
const operatorPath = `${process.env.HOME}/.HandyHost/sentinelData/.operator`;
if(fs.existsSync(operatorPath)){
operator = fs.readFileSync(operatorPath,'utf8');
}
this.dvpnSetup.getConfigs(true).then(data=>{
resolve({operator,config:data});
}).catch(error=>{
reject(error);
});
})
}
getPortsRedlist(){
return new Promise((resolve,reject)=>{
let output = {default:{},custom:{}};
if(fs.existsSync(this.redlistPortsPath)){
output = JSON.parse(fs.readFileSync(this.redlistPortsPath,'utf8'));
}
resolve(output);
})
}
rebuildNode(){
return this.dvpnSetup.rebuildDvpn(this.ioNamespaces);
}
launchNode(requestBody){
const {parsed,err} = this.parseRequestBody(requestBody);
if(typeof parsed == "undefined"){
return new Promise((resolve,reject)=>{
reject(err);
})
}
this.dvpnSetup.configureAutostart(parsed);
return this.dvpnSetup.launchDVPN(parsed.pw,this.ioNamespaces);
}
getWallets(requestBody){
const {parsed,err} = this.parseRequestBody(requestBody);
if(typeof parsed == "undefined"){
return new Promise((resolve,reject)=>{
reject(err);
})
}
return this.dvpnSetup.getKeyList(parsed.pw);
}
updateNodeConfig(requestBody){
const {parsed,err} = this.parseRequestBody(requestBody);
if(typeof parsed == "undefined"){
return new Promise((resolve,reject)=>{
reject(err);
})
}
if(typeof parsed.operator != "undefined"){
fs.writeFileSync(`${process.env.HOME}/.HandyHost/sentinelData/.operator`,parsed.operator,'utf8');
}
return this.dvpnSetup.updateConfigs(parsed.config)
}
initWallet(requestBody){
const {parsed,err} = this.parseRequestBody(requestBody);
if(typeof parsed == "undefined"){
return new Promise((resolve,reject)=>{
reject(err);
})
}
if(parsed.import){
//init from seed
return this.dvpnSetup.initWalletFromSeed(parsed.seed, parsed.pw, parsed.walletName);
}
else{
return this.dvpnSetup.initWallet(parsed.pw, parsed.walletName);
}
}
parseRequestBody(requestBody){
let parsed;
let err;
try{
parsed = JSON.parse(requestBody);
}
catch(e){
err = e;
}
return {parsed,err};
}
}