-
Notifications
You must be signed in to change notification settings - Fork 0
/
commonFunctions.js
152 lines (134 loc) · 5.7 KB
/
commonFunctions.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
142
143
144
145
146
147
148
149
150
151
152
const WebSocket = require('ws');
const fs = require('fs');
//Persistent Global Statuses
var statuses = new Object();
module.exports = function () {
this.wss = undefined;
this.broadcastStatus = (wss, broadcastMessage) => {
wss.broadcast(JSON.stringify(broadcastMessage));
}
this.updateStatus = (message) => {
var connection = new WebSocket("ws://statusdisplay.jacobwhite.us:8080")
connection.onopen = (error) => {
connection.send(JSON.stringify(message));
}
}
this.generateCode = (length) => {
var result = '';
var characters = 'abcdefghjkmnpqrstuvwxyz23456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result.toUpperCase();
}
this.loadStatus = () => {
let rawdata = fs.readFileSync('status.json');
let json = JSON.parse(rawdata);
global.status = json.status == undefined ? "" : json.status;
global.color = json.color == undefined ? "black" : json.color;
console.log("loaded status ", JSON.stringify(json));
}
this.saveStatus = () => {
json = {
status: global.status,
color: global.color
}
fs.writeFileSync('status.json', JSON.stringify(json));
}
this.serveFile = (res, file) => {
// console.log("serving file:", file);
fs.readFile(file, function (err, data) {
var mimetype = "text/html";
let extension = file.substr(file.lastIndexOf('.') + 1);
if (err) {
res.writeHead(404);
res.write("not found");
}
else {
switch (extension) {
case "odf":
mimetype = "font/opentype";
break;
case "mp3":
mimetype = "audio/mpeg";
break;
case "html":
mimetype = "text/html";
break;
case "css":
mimetype = "text/css";
break;
case "js":
mimetype = "text/javascript";
break;
}
res.setHeader("Access-Control-Allow-Origin", "*");
res.writeHead(200, { 'Content-Type': mimetype });
res.write(data);
res.end();
}
});
}
this.getHttpServer = () => {
const rlf = require("rate-limiter-flexible");
const opts = {
points:60,
duration:60,
};
const rateLimiter = new rlf.RateLimiterMemory(opts);
const http = require('http');
const url = require('url');
const querystring = require('querystring');
const server = http.createServer((req, res) => {
let parsedURL = url.parse(req.url);
if (parsedURL.pathname == "/") {
let queryParameters = querystring.parse(url.parse(req.url).query);
if (queryParameters['serverConnections'] != undefined || (queryParameters['status'] != undefined || queryParameters['color'] != undefined) && queryParameters['displayCode'] != undefined || queryParameters['statuses'] != undefined) {
// rateLimiter.consume(queryParameters['displayCode'], 1)
// .then((rateLimiterRes) => {
if (queryParameters['serverConnections'] != undefined && this.wss != undefined) {
console.log(this.wss._server.connections);
res.statuCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('{"connections": "' + this.wss._server.connections +'"}');
}
else if(queryParameters['statuses'] != undefined) {
console.log("statuses:", global.statuses);
res.statuCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(global.statuses));
}
else {
var broadcastMessage = {
type: "status",
status: queryParameters['status'],
color: queryParameters['color'],
displayCode: queryParameters['displayCode'],
chat_id: queryParameters['chat_id']
}
let s = {
status: broadcastMessage.status,
color: broadcastMessage.color
}
global.statuses[queryParameters['displayCode']] = s;
this.broadcastStatus(this.wss, broadcastMessage);
this.updateStatus(broadcastMessage);
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('{"result":"success","status":"' + queryParameters['status'] + '","color":"' + queryParameters['color'] + '"}');
}
// })
// .catch((rateLimiterRes) => { });
}
else {
this.serveFile(res, "html/index.html");
}
}
else {
this.serveFile(res, "html" + parsedURL.pathname);
}
});
return server
}
}