-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·84 lines (75 loc) · 2.1 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
83
const fs = require('fs');
const http = require('http');
const os = require('os');
const ifaces = os.networkInterfaces();
const listenPort = process.env.LISTEN_PORT || 8080;
let listenInterfaces = ['127.0.0.1', 'localhost'];
try {
listenInterfaces = [];
Object.keys(ifaces).forEach((ifname) => {
ifaces[ifname].forEach((iface) => {
if (iface.family !== 'IPv4') {
return;
}
listenInterfaces.push(iface.address);
});
});
} catch (err) {
if (listenInterfaces.length === 0) {
listenInterfaces = ['127.0.0.1', 'localhost'];
}
console.error('Error getting local interfaces:');
console.error(err);
}
try {
if (process.env.LISTEN_INTERFACE) {
listenInterfaces = JSON.parse(process.env.LISTEN_INTERFACE);
}
} catch (err) {
console.error('Could not parse environment variable LISTEN_INTERFACE. Using default:', listenInterfaces);
}
const allowedFiles = [
'',
'/',
'/index.html',
'/logic.js',
'/morseSound.js'
];
const httpListeningPromise = new Promise((resolve, reject) => {
http.createServer((req, res) => {
let getFile = req.url;
if (allowedFiles.includes(getFile)) {
if (getFile === '/' || getFile === '') {
getFile = '/index.html';
}
fs.readFile(__dirname + getFile, (err, data) => {
if (err) {
res.writeHead(404);
console.error(JSON.stringify(err, Object.getOwnPropertyNames(err)));
return res.end("File not found.");
}
console.log(`[HTTP]::GET '${req.url}'`);
res.writeHead(200);
res.end(data);
});
} else {
console.log(`[HTTP]::GET '${req.url}'`);
console.log(' Not on allow list: ', allowedFiles);
res.writeHead(401);
return res.end("Unauthorized");
}
}).listen(listenPort, listenInterfaces, (error) => {
if (error) {
return reject();
}
return resolve();
});
});
httpListeningPromise.then(() => {
console.log(`Connect using:`);
listenInterfaces.forEach((ipAddress) => {
console.log(`http://${ipAddress}:${listenPort}/`)
});
console.log('');
console.log(`Started waiting for connections`);
});