-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
82 lines (80 loc) · 3.2 KB
/
server.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
const {app} = require("./app.js");
const http = require("http");
const os = require("os");
const fs = require("fs");
const https = require("https");
const httpPort = process.env.PORT || 5656;
const httpsPort = process.env.PORT_HTTPS || 5657;
try {
const httpServer = http.createServer(app);
httpServer.listen(httpPort, "0.0.0.0", back => {
console.log("Http server listening on port : " + httpPort)
console.log("http://localhost:" + httpPort)
//print all local ip
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const net of interfaces[name]) {
console.log("http://" + net.address + ":" + httpPort)
}
}
});
let certsFolder = process.env.CERT_PATH || process.cwd() + '/certsFiles/';
//end with /
if (!certsFolder.endsWith('/')) {
certsFolder = certsFolder + '/';
}
if (fs.existsSync(certsFolder)) {
try {
const credentials = {};
const certFilesName = ['certificate.crt', 'fullchain.pem'];
const keyFilesName = ['key.key', 'privkey.pem', 'private.key', 'privatekey.pem'];
const caFilesName = ['ca.crt', 'chain.pem', 'chain.cert.pem'];
for (const certFileName of certFilesName) {
if (fs.existsSync(certsFolder + certFileName)) {
credentials.cert = fs.readFileSync(certsFolder + certFileName);
console.log('cert file found : ' + certsFolder + certFileName);
break;
}
}
if (!credentials.cert) {
console.error('cert file not found in : ' + certsFolder);
}
for (const keyFileName of keyFilesName) {
if (fs.existsSync(certsFolder + keyFileName)) {
credentials.key = fs.readFileSync(certsFolder + keyFileName);
console.log('key file found : ' + certsFolder + keyFileName);
break;
}
}
if (!credentials.key) {
console.error('key file not found in : ' + certsFolder);
}
for (const caFileName of caFilesName) {
if (fs.existsSync(certsFolder + caFileName)) {
credentials.ca = fs.readFileSync(certsFolder + caFileName);
console.log('ca file found : ' + certsFolder + caFileName);
break;
}
}
if (!credentials.ca) {
console.log('ca file not found in : ' + certsFolder);
}
const httpsServer = https.createServer(credentials, app);
httpsServer.listen(httpsPort, () => {
console.log("Https server listing on port : " + httpsPort)
console.log("https://localhost:" + httpsPort)
});
} catch (e) {
console.log("Can't start Https server");
console.log(e);
}
} else {
console.log("Can't start Https server, certs folder not found");
}
} catch (e) {
console.log("Can't start Http server");
console.log(e);
if (!process.env.TEST_MODE) {
process.exit(1);
}
}