forked from NexusNull/bot-web-interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
127 lines (112 loc) · 4.55 KB
/
main.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
/**
* Created by Nexus on 29.07.2017.
*/
const express = require('express');
const Client = require("./Client");
const path = require("path");
const Publisher = require("./Publisher");
const sha512 = require('js-sha512').sha512;
class BotWebInterface {
constructor(config = {}) {
this.port = config.port || 2080;
if (config.password) {
if (typeof config.password != "string")
throw new TypeError("config.password needs to be of type string");
else
this.password = config.password;
} else {
this.password = null;
}
this.clients = [];
this.defaultStructure = [];
this.router = express.Router();
this.app = express();
this.server = require('http').createServer(this.app);
this.io = require('socket.io')(this.server);
this.publisher = new Publisher();
this.setRoutes();
this.openSocket()
this.server.listen(this.port);
}
setRoutes() {
this.router.use('/', express.static(__dirname + '/public'));
this.router.use("/sha512.js", function (req, res) {
res.sendFile(path.resolve(require.resolve("js-sha512") + "/../../build/sha512.min.js"));
});
console.log(path.resolve(require.resolve("socket.io-client") + "/../../dist/socket.io.min.js"))
this.router.use("/socket.io.js", function (req, res) {
res.sendFile(path.resolve(require.resolve("socket.io-client") + "/../../dist/socket.io.min.js"));
});
this.router.use("/prompt-boxes.js", function (req, res) {
res.sendFile(path.resolve(require.resolve("prompt-boxes") + "/../../../dist/prompt-boxes.min.js"));
});
this.router.use("/prompt-boxes.css", function (req, res) {
res.sendFile(path.resolve(require.resolve("prompt-boxes") + "/../../../dist/prompt-boxes.min.css"));
});
this.app.use(this.router);
this.app.use(function (req, res) {
res.status(404).send(" 404: Page not found");
});
}
openSocket() {
this.io.sockets.on('connection', (socket) => {
const client = new Client(socket, this, this.clients.length);
if (this.password != null) {
var puzzle = Math.floor(Math.random() * 1000000) + Math.floor(Math.random() * 1000000) + new Date();
var difficulty = 8;
socket.emit("authRequired", {puzzle: puzzle, difficulty: difficulty});
} else {
socket.emit("noAuthRequired");
}
socket.on("auth", (auth) => {
if (this.password == null) {
this.publisher.clientJoined(client);
this.clients.push(client);
return;
}
if (auth.solution && (auth.solution + "").length < 20) {
/*
This is here because I like playing around with different things.
*/
let hash = sha512.digest(puzzle + auth.solution);
let match = true;
for (let i = 0; i < difficulty; i += 8) {
var byte;
if (difficulty - i > 8) {
byte = hash[Math.floor(i / 8)];
} else {
byte = hash[Math.floor(i / 8)] >> 8 - (difficulty - i);
}
if (byte !== 0) {
match = false;
break;
}
}
if (!match)
socket.disconnect();
} else {
socket.disconnect();
}
if (auth.password === this.password) {
this.publisher.clientJoined(client);
this.clients.push(client);
} else {
puzzle = Math.floor(Math.random() * 1000000) + Math.floor(Math.random() * 1000000) + new Date();
socket.emit("authRequired", {puzzle: puzzle, difficulty: difficulty});
}
});
socket.on("disconnect", () => {
this.removeClient(client);
})
});
}
removeClient(client) {
for (let i in this.clients) {
if (this.clients[i] === client) {
this.publisher.clientLeft(client);
delete this.clients[i];
}
}
}
}
module.exports = BotWebInterface;