-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
291 lines (249 loc) · 8.1 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
const cookieParser = require('cookie-parser');
const exec = require('child_process').exec;
const express = require('express');
const app = express();
const auth = require('./auth.js');
const jwt = require("jsonwebtoken");
const bodyParser = require('body-parser');
const { password, explicit_password, ws_token, cookie_value, jwt_secret } = require('./config.json');
const webSocket = require('ws');
const crypto = require("crypto");
const fs = require('fs');
app.use(bodyParser.json());
app.use(cookieParser());
app.set("view engine", "ejs");
app.use('/static', express.static(__dirname + '/static'));
app.use('/assets', express.static(__dirname + '/assets'));
app.use(auth);
const server = require('http').createServer(app);
const wss = new webSocket.Server({ server:server });
const REQUEST = 0
const RESPONSE = 1
const IDENTIFY = 2
const EXECUTE = 3
async function request (req, client, outgoingData) {
outgoingData.ipFrom = String(req.ip).replace("::ffff:", "");
client.send(JSON.stringify(outgoingData));
result = new Promise(resolve => {
client.on("message", (message) => {
let data = JSON.parse(message);
if (data.op !== RESPONSE) {
return;
}
resolve(data.d);
});
client.removeEventListener("message");
});
return await result;
}
async function execute (req, command) {
const [ client ] = wss.clients;
if (client === undefined) {
result = new Promise(resolve => {
resolve({stdout: undefined, stderr: undefined});
});
return await result;
} else {
client.send(JSON.stringify({
op: EXECUTE,
d: command,
ipFrom: String(req.ip).replace("::ffff:", "")
}));
result = new Promise(resolve => {
client.on("message", (message) => {
let data = JSON.parse(message);
if (data.op !== EXECUTE) {
return;
}
resolve(data.d);
});
client.removeEventListener("message");
});
return await result;
}
}
wss.on('connection', (client) => {
console.log('Client Connected!');
const t = setTimeout(() => {
console.log('Client force kicked!');
client.close();
}, 30000);
client.send(JSON.stringify({op: IDENTIFY}));
client.on('message', (message) => {
let data = JSON.parse(message);
if (data.op === IDENTIFY) {
if (data.token === ws_token) {
clearTimeout(t);
client.setMaxListeners(0);
}
}
});
client.on('close', (reasonCode, description) => {
console.log('Client Disconnected!');
});
});
app.get('/', async (req, res) => {
const [ client ] = wss.clients;
if (client === undefined) {
res.render('offline');
} else {
data = await request(req, client, {
op: REQUEST,
d: "home"
});
res.render('home', data);
}
});
app.get("/:static_page(login|console|logs|editor|ec2|explicit|protocols|about)", async (req, res) => {
res.render(req.params.static_page);
});
app.get("/:ws_page(storage|processes/jesterbot|processes/pizzahat|processes/stealthybot|processes/dashboard|logs/messages|logs/network)", async (req, res) =>{
const [ client ] = wss.clients;
var routes = req.params.ws_page.split("/");
var endpoint = routes[routes.length - 1];
if (client === undefined) {
res.render('offline');
} else {
data = await request(req, client, {
op: REQUEST,
d: endpoint
});
res.render(req.params.ws_page, data);
}
});
app.get("/error", async (req, res) => {
let error = req.query;
error.link = "/";
error.linkMessage = "Back to homepage";
if (error.code == 403) {
error.name = "Forbidden";
if (error.route === "admin") {
error.message = "Admin is required for this endpoint";
error.link = "/login";
error.linkMessage = "Click here to sign in as admin";
} else {
error.message = "Superior admin is required for this endpoint";
error.link = "/explicit";
error.linkMessage = "Click here to sign in as superior admin";
}
} else if (error.code == 404) {
error.name = "Not found";
error.message = "You tried to visit a page that wasnt found";
} else if (error.code == 400) {
error.name = "Bad request";
error.message = "Your request was improperly formed";
}
res.render('error', {error});
});
app.get("/logs/usage", async (req, res) => {
var fileContent = fs.readFileSync("logs/usage.txt");
data = {
usage: fileContent
};
res.render('logs/usage', data);
});
app.get("/logs/processes", async (req, res) => {
jesterbotLogs = (await execute(req, "journalctl -b -u jesterbot.service")).stdout;
stealthybotLogs = (await execute(req, "journalctl -b -u stealthybot.service")).stdout;
dashboardLogs = (await execute(req, "journalctl -b -u raspberry-dashboard.service")).stdout;
res.render('logs/processes', {
processes: {
jesterbot: jesterbotLogs,
stealthybot: stealthybotLogs,
dashboard: dashboardLogs
}
});
});
app.get("/statistics", async (req, res) => {
var verified = false;
try {
jwt.verify(req.cookies[cookie_value] || "", "aoihfisoduhgoiahusSECRET_KEY");
verified = true;
} catch (error) {}
const [ client ] = wss.clients;
if (client === undefined) {
res.render('offline');
} else {
data = await request(req, client, {
op: REQUEST,
d: "statistics",
v: verified
});
res.render('statistics', data);
}
});
app.get("/logout", async (req, res) => {
res.clearCookie(cookie_value);
res.redirect("/login");
});
app.get("/pull", async(req, res) => {
res.redirect("/");
exec("git stash && git pull");
});
app.get("/reset", async (req, res) => {
var { password } = req.query;
var cookieValue = crypto.randomBytes(20).toString('hex');
var fileContent = JSON.parse(fs.readFileSync("./config.json"));
fileContent.cookie_value = cookieValue;
fileContent.password = password;
fs.writeFileSync("./config.json", JSON.stringify(fileContent, null, 4));
});
app.get("/restart", async (req, res) => {
var { unit } = req.query;
if (!unit) {
res.redirect("/");
await execute(req, 'sudo /sbin/reboot');
}
else {
res.redirect("/" + unit);
await execute(req, `sudo systemctl restart ${unit}.service`);
}
});
app.get("/execute", async (req, res) => {
var { cmd } = req.query;
var output = await execute(req, cmd);
const result = new Promise(resolve => {
if (output.stderr) {
resolve(output.stderr);
} else {
resolve(output.stdout);
}
});
res.status(200).json({message: await result + ""});
});
app.get('*', async (req, res) => {
res.redirect("/error?code=404");
});
app.post("/login", async (req, res) => {
if (req.body.password && req.body.password === password) {
res.cookie(cookie_value, jwt.sign({
username: "username"
}, jwt_secret, {
expiresIn: 10800
}), {
httpOnly: true,
expiresIn: 10800
});
res.status(200).json({ message: "s" });
} else {
res.status(400).json({ message: "Bad Argument" });
}
});
app.post("/explicit", async (req, res) => {
if (req.body.password && req.body.password === explicit_password) {
res.cookie("_fiojoweonfwouinwiunfuiw", jwt.sign({
username: "explicit_username"
}, jwt_secret, {
expiresIn: 10800
}), {
httpOnly: true,
expiresIn: 10800
});
res.status(200).json({ message: "s" });
} else {
res.status(400).json({ message: "Bad Argument" });
}
});
server.listen(8080, () => {
console.log("Listening at http://localhost:8080");
});