-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (121 loc) · 3.83 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
let opts;
try {
opts = require('./config.json');
} catch (error) {
console.warn("No ssl set. Please run `node configure.js`!")
opts = {};
}
const fs = require('fs');
const express = require('express');
const mime = require('mime')
const application = express()
const cors = require('cors');
const ws = require('ws')
const ssl = opts["ssl"] && opts["ssl"].key && opts["ssl"].cert
const factory = ssl ? require('https') : require('http');
const echo_chambers = require('./echo-chambers-protocol.js');
opts.debug = true; //TODO: Delete later
opts["ssl"] = ssl ? {
"key": fs.readFileSync(opts["ssl"].key),
"cert": fs.readFileSync(opts["ssl"].cert),
} : opts["ssl"];
const router = express.Router();
router.all('*', (req, res, next) => {
if (opts.debug) console.log(req.socket.remoteAddress, new Date(), req.url);
try {
if(req.path.indexOf(".") > -1){
res.setHeader('Content-Type', mime.lookup(req.path));
} else {
res.setHeader('Content-Type', 'text/html')
}
} catch(e) {
console.log(e);
}
next()
})
router.use('/js', express.static('www/js'))
router.use('/img', express.static('www/img'))
router.use('/sounds', express.static('www/sounds'))
router.use('/favicon.ico', express.static('www/favicon.ico'))
function streamBufferChunked(buffer, req, res) {
let chunkSize = 1024*64;
let range = (req.headers.range) ? req.headers.range.replace(/bytes=/, "").split("-") : [];
range[0] = range[0] ? parseInt(range[0], 10) : 0;
range[1] = range[1] ? parseInt(range[1], 10) : range[0] + chunkSize;
if(range[1] > buffer.length - 1) {
range[1] = buffer.length - 1;
}
range = {start: range[0], end: range[1]};
res.status(206)
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
res.setHeader('Pragma', 'no-cache')
res.setHeader('Expires', 0)
res.setHeader('Content-Type', mime.lookup(req.path))
res.setHeader('Accept-Ranges', 'bytes')
res.setHeader('Content-Range', 'bytes ' + range.start + '-' + range.end + '/' + buffer.length)
res.setHeader('Content-Length', range.start == range.end ? 0 : range.end - range.start + 1)
res.send(buffer.subarray(range.start, range.end + 1));
}
const share_folder = "/voice/";
router.get(share_folder+':resource', (req, res, next) => {
try {
const file_path = './www' +share_folder+ req.params.resource;
const resource = fs.readFileSync(file_path)
if (!resource) next()
streamBufferChunked(Buffer.from(resource, 'base64'), req, res)
} catch (e) {
console.log(e)
next()
}
});
router.get('/', (req, res, next) => {
try {
res.send(fs.readFileSync("./www/index.html"))
res.end()
} catch (e) {
console.log(e)
next()
}
});
application.use(cors({
origin: '*'
}));
if(ssl) {
application.all('*', ensureSecure);
require('http').createServer(application).listen(80)
}
application.use(function(req, res, next) {
req.getUrl = function() {
return req.protocol + "://" + req.get('host') + req.originalUrl;
}
return next();
});
application.use('/', router);
// Server creation starts here
const server = factory.createServer(opts.ssl, application);
server.listen(ssl ? 443 : 80, err => {
if (err) {
console.log('Well, this didn\'t work...');
process.exit();
}
console.log('Server is listening....');
});
const wss_protocol = echo_chambers.create();
const wss = new ws.Server({
server,
path: "/websockets"
});
wss.on('connection', (webSocket, req) => {
webSocket.remoteAddress = req.socket.remoteAddress;
webSocket.hostname = req.socket.remoteAddress;
wss_protocol(webSocket)
});
function ensureSecure(req, res, next){
if(req.secure){
// OK, continue
return next();
};
// handle port numbers if you need non defaults
// res.redirect('https://' + req.host + req.url); // express 3.x
res.redirect('https://' + req.hostname + req.url); // express 4.x
}