-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
134 lines (120 loc) · 3.66 KB
/
web.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
module.exports.setStatus = setStatus;
module.exports.setSettings = setSettings;
module.exports.setQuiz = setQuiz;
module.exports.setCommands = setCommands;
var status = {};
var settings = {};
var quiz = {};
var commands = {};
var http = require('http');
var fs = require('fs');
var Mustache = require('mustache');
function setQuiz(newQuiz){
quiz = newQuiz;
}
function setStatus(newStatus){
status = newStatus;
}
function setSettings(newSettings){
settings = newSettings;
http.createServer(processRequest).listen(settings.http.port);
}
function setCommands(newCommands){
commands = newCommands;
}
function processRequest(req, res){
if(!checkAuth(req)){
res.writeHead(401,{'WWW-Authenticate':'Basic realm=Authorization required'});
res.end('Unauthorized');
} else {
if(req.url == '/'){
serveModule('main', req, res);
} else if(req.url.startsWith('/files/')){
var path = req.url.split('/');
var filename = './web/' + path[1] + '/' + path[2];
serveFile(res, filename);
} else if(req.url.startsWith('/modules/')){
var modname = req.url.split('/')[2];
serveModule(modname, req, res);
}
}
}
var modules = {
'main': function(req, res){
httpPageHeaderFooter(res, '', [], 'main');
},
'showStatus': function(req, res){
httpPageHeaderFooter(res, '', ['status', 'commands'], 'showStatus');
},
'status': function(req, res){
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(status));
}
};
function serveModule(modname, req, res){
try {
if(modname in modules){
modules[modname](req, res); // internal module
} else {
var modfile = './web/'+modname.split('/')[0]+'.js';
delete require.cache[require.resolve(modfile)];
var module = require(modfile);
module.init(status, settings, quiz, commands);
module.run(req, res); // external module
}
} catch(e){
console.log('Exception in http handler: '+e);
res.writeHead(404);
res.end();
}
}
function serveFile(res, filename){
try {
var ext = filename.split(/\.(?=[^\.]+$)/)[1] || '';
var data = fs.readFileSync(filename);
var mime = 'text/plain';
switch(ext.toLowerCase()){
case 'html': mime = 'text/html'; break;
case 'gif': mime = 'image/gif'; break;
case 'png': mime = 'image/png'; break;
case 'jpg': case 'jpeg': mime = 'image/jpeg'; break;
case 'js': mime = 'text/javascript'; break;
case 'css': mime = 'text/css'; break;
}
res.writeHead(200,{'Content-Type':mime});
res.end(data);
} catch(e){
console.log('Exception in http handler: '+e);
res.writeHead(404);
res.end();
}
}
function checkAuth(req){
var header = req.headers['authorization'] || '';
var token = header.split(/\s+/).pop() || '';
var auth = new Buffer.from(token, 'base64').toString();
var parts = auth.split(/:/);
var username = parts[0];
var password = parts[1];
if(username != settings.http.login || password != settings.http.password){
return false;
}
return true;
}
function httpPageHeaderFooter(res, data = '', scripts = [], template = false, templateData = {}){
httpHeader(res);
var parsedTemplate = '';
var html = '<html><head><title>node-ircquiz</title><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>';
for(var i=0; i<scripts.length; i++){
html += '<script src="/files/' + scripts[i] + '.js"></script>';
}
if(template){
var templateFile = fs.readFileSync('./web/' + template + '.html', {encoding: 'utf-8'});
parsedTemplate = Mustache.render(templateFile, templateData);
}
html += '<link rel="stylesheet" type="text/css" href="/files/style.css"></head><body>' + data + parsedTemplate + '</body></html>';
res.end(html);
}
function httpHeader(res){
res.writeHead(200,{'Content-Type':'text/html; charset=utf-8'});
}