-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathderaCLI.js
More file actions
360 lines (325 loc) · 9.53 KB
/
deraCLI.js
File metadata and controls
360 lines (325 loc) · 9.53 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
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
//This is a Windows Service to launch diode in the background
var express = require('express');
var app = express();
var store = require('nconf');
var cors = require('cors')
store.use('file', { file: './config.json' });
var status = 0;
let killVar=0;
app.use(cors())
runningClients = [];
appRootDir = require('app-root-dir').get();
if(appRootDir.indexOf("daemon")){
appRootDir = `${appRootDir.slice(0,appRootDir.indexOf("daemon"))}`
}
const diodePath = appRootDir + 'I/bin/diode.exe';
const spawn = require( 'child_process' ).spawn;
let child;
//Stops diode
async function killDiodeCLI(){
killVar=1
child.kill();
}
getAddr();
//returns address and domain of the client with get request
app.get('/diode/address', function(req, res) {
res.send(JSON.stringify({address: global.addrs,domain: global.domain}) );
});
//returns hello world
app.get('/', function (req, res) {
res.send('Hello World!');
});
//Sets Diode Fleet
app.get('/diode/fleet/:fleet', function(req, res) {
setDiodeFleet(req.params.fleet);
res.send(JSON.stringify({status: status, set: true }) );
});
//sets isPublishActive to requested value
app.get('/setPublishActive/:value', function (req, res) {
value = req.params.value;
if(value == "true"||value == "false"){
store.set('isPublishActive', req.params.value);
store.save();
res.send(JSON.stringify({isPublishActive: req.params.value}));
}else{
res.send(JSON.stringify({error: "Invalid value"}));
}
});
//Sets Default Port, publishmode and remote address values
app.get('/setDefault/:ports/:mode/:remoteAddr', function (req, res) {
if(isValidMode(req.params.mode)){
store.set('defaultPorts', req.params.ports);
store.set('defaultMode', req.params.mode);
store.set('defaultRemoteAddr', req.params.remoteAddr);
store.save();
res.send(JSON.stringify({defaultPorts: req.params.ports,defaultMode: req.params.mode,defaultRemoteAddr: req.params.remoteAddr}));
}else{
res.send(JSON.stringify({error: "Invalid Mode"}));
}
});
//Sets Default Port and publishmode values
app.get('/setDefault/:ports/:mode/', function (req, res) {
if(isValidMode(req.params.mode)){
store.set('defaultPorts', req.params.ports);
store.set('defaultMode', req.params.mode);
store.save();
res.send(JSON.stringify({defaultPorts: req.params.ports,defaultMode: req.params.mode}));
}else{
res.send(JSON.stringify({error: "Invalid Mode"}));
}
});
//returns the status of the diode
app.get('/diode', function (req, res) {
if(status == 0){
res.send(JSON.stringify({status:status}));
}else{
res.send(JSON.stringify({status:status,pid:child.pid}));
}
});
//stops diode
app.get('/diode/stop', function (req, res) {
killDiodeCLI();
res.send(JSON.stringify({status:status}));
});
//publishes diode on the specified ports
app.get('/diode/publish/:ports/:mode', function (req, res) {
if(status == 0){
let ports = req.params.ports.split(',');
let mode = req.params.mode;
let remoteAddress = "0x0";
publishDiode(ports,mode,remoteAddress);
res.send(JSON.stringify({status:status, result:1}));
}else{
res.send(JSON.stringify({status:status, result:0}));
}
});
app.get('/diode/publish/:ports/:mode/:remoteAddress', function (req, res) {
if(status == 0){
let ports = req.params.ports.split(',');
let mode = req.params.mode;
let remoteAddress = req.params.remoteAddress;
publishDiode(ports,mode,remoteAddress);
res.send(JSON.stringify({status:status, result:1,pid:child.pid}));
}else{
res.send(JSON.stringify({status:status, result:0,pid:child.pid}));
}
});
//Bind to specified ports on specified address
app.get('/diode/bind/:ports/:address', function (req, res) {
if(status == 0){
let ports = req.params.ports.split(',');
let address = req.params.address;
bindDiode(ports,address);
res.send(JSON.stringify({status:status, result:1}));
}else{
res.send(JSON.stringify({status:status, result:0}));
}
});
//Add BNS record
app.get('/diode/addBNS/:bnsName', function (req, res) {
let bnsName = req.params.bnsName;
if(isValidBNSName(bnsName)){
addBNSRecord(bnsName);
res.send(JSON.stringify({status:status, result:1}));
}else{
res.send(JSON.stringify({status:status, result:0, error: "Invalid BNS Name"}));
}
}
);
app.listen(3000, function () {
console.log('Diode is running on port 3000!');
});
//starts Diode with default ports and mode
function startDiodeCLI(){
store.load();
defaultPorts=store.get('defaultPorts').split(',');
defaultMode=store.get('defaultMode');
defaultRemoteAddr=store.get('defaultRemoteAddr');
if(defaultPorts.length == 0){
return false;
}else{
publishDiode(defaultPorts,defaultMode,defaultRemoteAddr);
}
}
//Sets Diode Fleet
function setDiodeFleet(fleet){
console.log("Setting Diode Fleet");
let args = ['-retrytimes=1']
args.push('-retrywait=1m')
args.push('config')
args.push('-set')
args.push('fleet='+fleet)
child = spawn( diodePath, args);
status = 3;
console.log(child.pid);
runningClients.push(child.pid);
child.stdout.on( 'data', data => {
console.log( `stdout: ${data}` );
});
child.stderr.on( 'data', data => {
onError(data)
});
child.on('exit', (code) => {
console.log(`child process exited with code ${code}`);
runningClients.splice(runningClients.indexOf(child.pid),1);
status = 0;
});
}
//Checks if BNS name is valid by checking if it is longer than 8 characters
function isValidBNSName(bnsName){
if(bnsName.length > 8){
return true;
}
return false;
}
//Bind Diode to selected port and address
function bindDiode(ports,remoteAddress){
console.log("Binding Diode");
let args = ['-retrytimes=1']
args.push('-retrywait=1m')
args.push('-bind')
ports.forEach(port => {
args.push(port+":"+remoteAddress+":"+port)
});
child = spawn( diodePath, args);
console.log(child.pid);
runningClients.push(child.pid);
child.stdout.on( 'data', data => {
console.log( `stdout: ${data}` );
});
child.stderr.on( 'data', data => {
onError(data)
});
child.on('exit', (code) => {
console.log(`child process exited with code ${code}`);
runningClients.splice(runningClients.indexOf(child.pid),1);
status = 0;
});
}
//Publishes diode on the specified ports
function publishDiode(ports,mode,remoteAddress){
console.log("Publishing diode on ports: " + ports);
let args = ['-retrytimes=1']
args.push('-retrywait=1m')
args.push('publish')
ports.forEach(port => {
args.push('-'+mode)
if(mode == 'private') args.push(port+','+remoteAddress)
else args.push(port)
});
child = spawn( diodePath, args);
status = 3;
console.log(child.pid);
runningClients.push(child.pid);
child.stdout.on( 'data', data => {
console.log( `stdout: ${data}` );
});
child.stderr.on( 'data', data => {
onError(data)
});
child.on('exit', (code) => {
console.log(`child process exited with code ${code}`);
runningClients.splice(runningClients.indexOf(child.pid),1);
if(killVar){
killVar=0;
status = 0;
}else{
publishDiode(ports,mode,remoteAddress)
status = 1;
}
});
}
//Add BNS record
function addBNSRecord(Name){
console.log("Adding BNS Record");
let args = ['-retrytimes=1']
args.push('-retrywait=1m')
args.push('bns')
args.push('-register')
args.push(Name)
child = spawn( diodePath, args);
status = 3;
console.log(child.pid);
runningClients.push(child.pid);
child.stdout.on( 'data', data => {
console.log( `stdout: ${data}` );
});
child.stderr.on( 'data', data => {
onError(data)
}
);
child.on('exit', (code) => {
console.log(`child process exited with code ${code}`);
runningClients.splice(runningClients.indexOf(child.pid),1);
status = 0;
}
);
}
function onError(data) {
console.log( `stderr: ${data}` );
if(data.indexOf("Bind")!=-1){
status = 3;
}else if(data.indexOf("Port")!=-1){
status = 4;
}else if(data.indexOf("windows")!=-1){
getAddr();
}else if(data.indexOf("Diode")!=-1){
status = 1;
}else if(data.indexOf("validated")!=-1){
status = 2;
}
}
//Checks if mode is valid
function isValidMode(mode){
if(mode == 'private'||mode == 'public'||mode == 'protected'){
return true;
}else{
return false;
}
}
//Gets the address of the client
function getAddr(){
let args = ['time']
child = spawn( diodePath, args);
status = 3;
console.log(child.pid);
runningClients.push(child.pid);
child.stdout.on( 'data', data => {
console.log( `stdout: ${data}` );
});
child.stderr.on( 'data', data => {
onError(data)
if(data.indexOf("Client address")!=-1){
global.addrs = `${data.slice(data.indexOf("0x"),data.indexOf("0x")+42)}`;
}
if(data.indexOf("Client name")!=-1){
global.domain = `${data.slice(data.indexOf(": ")+2,data.indexOf(".diode"))}`;
console.log(global.domain);
}
});
child.on('exit', (code) => {
console.log(`child process exited with code ${code}`);
runningClients.splice(runningClients.indexOf(child.pid),1);
status = 0;
});
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
store.load();
setTimeout(publishOnStart, 20000);
async function publishOnStart(){
while(runningClients.length > 0){
console.log("Waiting for clients to exit");
await sleep(10000)
}
if(store.get('isPublishActive') == "true"){
if(startDiodeCLI()){
console.log("Diode started");
}else{
console.log("Diode failed to start");
}
}
}