-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (116 loc) · 4.15 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
130
131
132
133
134
135
136
137
138
139
140
141
var app = require("./dist/server/WebServer");
var process = require("process");
var portfinder = require("portfinder");
var os = require("os");
var colors = require("colors/safe");
var http = require("http");
var https = require("https");
const url = require('url');
var WSServer = require("./dist/server/WSServerClient").default;
var WSProxyServer = require("./dist/server/WSProxyServer").default;
var WebSocketServer = require("ws").Server;
var { createProxyMiddleware } = require('http-proxy-middleware');
var fs = require("fs");
var netFaces = os.networkInterfaces();
var crypto = require("crypto");
function createCert() {
var certPath = "./generated-cert-1";
if(fs.existsSync(certPath)) {
return JSON.parse(fs.readFileSync(certPath, { encoding: "utf8", flag: "r" }));
}
var forge = require('node-forge');
var pki = forge.pki;
// generate a key pair or use one you have already
var keys = pki.rsa.generateKeyPair(2048);
// create a new certificate
var cert = pki.createCertificate();
// fill the required fields
cert.publicKey = keys.publicKey;
cert.serialNumber = '01';
cert.validity.notBefore = new Date();
cert.validity.notAfter = new Date();
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 20);
// use your own attributes here, or supply a csr (check the docs)
var attrs = [{
name: 'commonName',
value: 'dev.web-atoms.in'
}, {
name: 'countryName',
value: 'IN'
}, {
shortName: 'ST',
value: 'Maharashtra'
}, {
name: 'localityName',
value: 'Navi Mumbai'
}, {
name: 'organizationName',
value: 'NeuroSpeech Technologies Pvt Ltd'
}, {
shortName: 'OU',
value: 'Test'
}];
// here we set subject and issuer as the same one
cert.setSubject(attrs);
cert.setIssuer(attrs);
// the actual certificate signing
cert.sign(keys.privateKey);
// now convert the Forge certificate to PEM format
var pem = pki.certificateToPem(cert);
var pkey = pki.privateKeyToPem(keys.privateKey)
var c = { key: pkey, cert: pem };
fs.writeFileSync( certPath, JSON.stringify(c), "utf8");
return c;
}
function listen(port, ssl) {
var server = ssl ? https.createServer(createCert(), app.default) : http.createServer(app.default);
var wss = new WebSocketServer({ noServer: true });
const dss = new WebSocketServer({ noServer: true });
WSProxyServer.configure(dss, port);
WSServer.configure(wss);
server.on("upgrade", function upgrade(request, socket, head) {
const pathname = url.parse(request.url).pathname;
if (pathname === "/__debug" || pathname.startsWith('/__debug/')) {
dss.handleUpgrade(request, socket, head, function done(ws) {
dss.emit('connection', ws, request);
});
} else if (pathname === '/__listen') {
wss.handleUpgrade(request, socket, head, function done(ws) {
wss.emit('connection', ws, request);
});
} else {
console.error("Forwarding further.. " + pathname);
// socket.destroy();
}
});
server.listen(port,(err) => {
if(err) {
return console.log(err);
}
Object.keys(netFaces).forEach(function (dev) {
netFaces[dev].forEach(function (details) {
if (details.family === 'IPv4') {
if (ssl) {
console.log((' https://' + details.address + ':' + colors.cyan(port.toString())));
} else {
console.log((' http://' + details.address + ':' + colors.cyan(port.toString())));
}
}
});
});
return console.log(colors.green("Server has started "));
});
}
portfinder.basePort = 8080;
portfinder.getPort(function (err, port) {
if (err) {
throw err;
}
listen(port);
});
portfinder.getPort(function (err, port) {
if (err) {
throw err;
}
listen(port, true);
});