-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebserver.js
162 lines (133 loc) · 3.89 KB
/
webserver.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
153
154
155
156
157
158
159
160
161
162
var http = require("http")
var io = require("socket.io")
var fs = require("fs")
var EventEmitter = require("events")
var spawn = require("child_process").spawn
var soundData = require("./sounds/data.js")
var webserverEvents = new EventEmitter()
var clientCount = 0
module.exports = {
startServer: function(callback) {
print(null, "Starting webserver", true)
var server = http.createServer(function(req, res){
try {
if (req.url == "/") {
var html = fs.readFileSync("board/landing.html", "utf8")
html = html.replace("/*NEED_AUTH*/", (config.web.password == false) ? "none" : "block")
html = html.replace("<!--SEARCH_TEXT-->", config.web.text.search)
html = html.replace("<!--NEED_AUTH_TEXT-->", config.web.text.needAuth)
html = html.replace("<!--PICK_TEXT-->", config.web.text.pickSound)
html = html.replace("<!--LOADING_TEXT-->", config.web.text.loading)
res.write(html)
}
else if (req.url == "/style.css") {
res.write(fs.readFileSync("board/style.css", "utf8"))
}
else if (req.url == "/script.js") {
var js = fs.readFileSync("board/script.js", "utf8")
js = js.replace("/*JSON_SOUND_DATA*/", JSON.stringify(soundData))
js = js.replace("/*DISCONN_TEXT*/", config.web.text.disconnect)
js = js.replace("/*BLOCK_TEXT*/", config.web.text.blocked)
res.write(js)
}
else if (req.url == "/data.json") {
res.write(fs.readFileSync("sounds/data.js", "utf8").replace("module.exports = ", ""))
}
else if (req.url.substring(0, 5) == "/img/") {
var id = req.url.split("/")[req.url.split("/").length - 1]
if (getSoundById(id) || id == "noimage") {
res.write(fs.readFileSync("sounds/img/" + id + ".png"))
}
}
}
catch (e) {
console.log(e);
res.writeHead(500)
print(2, "HTTP server error for " + req.url)
}
res.end()
}).listen(config.web.port)
var socket = io.listen(server);
socket.on("connection", function(client) {
var user = {
"pid": 0,
"cooldown": false,
"timeout": false,
"auth": false,
"id": client.id,
"spam": {
"blocked": false,
"second": 0,
"requests": 0
}
}
clientCount++
webserverEvents.emit("connectionCange", clientCount)
if (config.web.password == false) {
user.auth = true
}
client.on("play", function(data) {
if (user.cooldown || !user.auth || user.spam.blocked) {
return
}
var sound = getSoundById(data)
if (!sound) {
return
}
user.cooldown = true
user.timeout = setTimeout(function () {
user.cooldown = false
client.emit("released")
}, (sound.duration + .5) * 1000);
webserverEvents.emit("playSound", {"soundId": data, "userId": user.id})
if (config.web.blockSpam != 0) {
if (user.spam.second != Math.round(new Date().getTime() / 1000)) {
user.spam.second = Math.round(new Date().getTime() / 1000)
}
else {
user.spam.requests++
if (user.spam.requests >= config.web.blockSpam) {
user.spam.blocked = true
client.emit("blocked")
}
}
}
})
client.on("kill", function() {
if (user.pid == 0 || !user.auth) {
return
}
try {
client.emit("released")
// No idea why, but fixes bug
spawn("kill", [user.pid + 1])
user.cooldown = false
user.pid = 0
clearTimeout(user.timeout)
} catch (e) {}
})
client.on("tryPassword", function(data) {
if (user.auth) {
client.emit("authDone")
}
else if (data === config.web.password) {
client.emit("authDone")
user.auth = true
}
})
client.on("disconnect", function() {
clientCount--
webserverEvents.emit("connectionCange", clientCount)
if (user.pid != 0) {
spawn("kill", [user.pid + 1])
user.pid = 0
}
})
webserverEvents.on("passEcec-" + user.id, function(pid) {
user.pid = pid
})
})
printStatus(1)
callback(webserverEvents)
}
}