-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·423 lines (382 loc) · 9.98 KB
/
index.js
File metadata and controls
executable file
·423 lines (382 loc) · 9.98 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
const app = require("express")();
var http = require("http").createServer(app);
const favicon = require("express-favicon");
const path = require("path");
const bodyParser = require("body-parser");
var io = require("socket.io")(http);
const { Sequelize, Model, DataTypes } = require("sequelize");
const sequelize = new Sequelize({
dialect: "sqlite",
storage: "database.sqlite",
logging: false,
});
const port = 3000;
//Preprocessors
app.use(
bodyParser.urlencoded({
extended: false,
})
);
app.use(bodyParser.json());
// Setup Database
class logs extends Model {}
logs.init(
{
time: DataTypes.TIME,
log: DataTypes.STRING,
},
{ sequelize, modelName: "logs" }
);
class slaves extends Model {}
slaves.init(
{
avgspeed: DataTypes.DOUBLE,
blocks: DataTypes.INTEGER,
slaveid: {
// needs to be unique
type: DataTypes.INTEGER,
unique: true,
},
},
{ sequelize, modelName: "slaves" }
);
class queue extends Model {}
queue.init(
{
hash: DataTypes.STRING,
submitDate: DataTypes.INTEGER,
},
{ sequelize, modelName: "queue" }
);
class finished extends Model {}
finished.init(
{
hash: DataTypes.STRING,
plaintext: DataTypes.STRING,
crackDate: DataTypes.INTEGER,
},
{ sequelize, modelName: "finished" }
);
//TODO: Make settings table for sequelize
class settings extends Model {}
settings.init(
{
settingid: DataTypes.STRING,
settingvalue: DataTypes.STRING,
datatype: DataTypes.STRING,
},
{ sequelize, modelName: "settings" }
);
settings.sync();
finished.sync();
queue.sync();
slaves.sync();
logs.sync();
// Needed functions
function generateAscii() {
let output = [];
for (let i = 32; i < 126; i++){
output.push(String.fromCharCode(i));
}
return output;
}
async function log(info) {
/**
* receives a log, stores it to the database, and prints it to the display
*/
let time = Date.now();
let logText = `[${time}]: ${info}`;
//database.logs.push(logText);
const logdata = await logs.create({
log: info,
time: time,
});
console.log(logText);
}
function checkOldLoads() {
for (let e in global.work.loads) {
if (
global.work.loads[e].time / 1000 + global.settings.loadTimeout <
Date.now() / 1000
) {
return global.work.loads[e];
}
}
return false;
}
function getLevel(id, arraySize) {
id++;
let level = 0;
let beggining = 0;
while (beggining < id) {
beggining = 0;
for (let i = 1; i <= level; i++) {
beggining = arraySize ** i + beggining;
//console.log(`${level}:${beggining}`);
}
level++;
//console.log(`${level}:${beggining}`);
}
if (beggining === 0) {
return level;
} else {
return level - 1;
}
}
function isIdUnique(id) {
return slaves.count({ where: { slaveid: id } }).then(count => {
if (count != 0) {
return false;
}
return true;
});
}
async function loadtables() {
let count = await settings.count().then(count => {
return count;
});
if (count !== 0) {
await settings.findAll().then(table => {
for (let i in table) {
if (table[i].datatype === "number") {
global.settings[table[i].settingid] = parseInt(table[i].settingvalue);
} else if (table[i].datatype === "string") {
global.settings[table[i].settingid] = String(table[i].settingvalue);
}
}
});
} else {
global.settings = {
loadTimeout: 60, // Seconds
blockSize: 131072, // size of each workload to send slaves
start: 0,
stop: 131072,
};
updatesettings();
}
}
async function updatesettings() {
for (i in global.settings) {
if (
(await settings.count({ where: { settingid: String(i) } }).then(count => {
return count;
})) === 0
) {
settings.create({
settingvalue: String(global.settings[i]),
datatype: typeof global.settings[i],
settingid: String(i),
});
} else {
settings.update(
{
settingvalue: String(global.settings[i]),
datatype: typeof global.settings[i],
},
{ where: { settingid: String(i) } }
);
}
}
}
global.settings = {};
global.work = {
hash: "",
currentCount: 0,
loads: {},
chars: generateAscii(),
averageSpeed: 0,
};
loadtables();
//socket.io
io.on("connection", socket => {
/**
* Run when a new web client connects
*/
//log("New User Connected");
socket.emit("start", "");
socket.on("sync", async function(msg) {
let finishQueue = {};
let hashQueue = [];
await finished.findAll().then(hash => {
for (let i in hash) {
finishQueue[hash[i].hash] = hash[i].plaintext;
}
});
await queue.findAll().then(hash => {
for (let i in hash) {
hashQueue.push(hash[i].hash);
}
});
socket.emit("sync", { finishQueue: finishQueue, hashQueue: hashQueue });
});
socket.on("hashSubmit", async function(msg) {
let hash = msg.hash.toLowerCase();
let count = await finished
.count({ where: { hash: hash } })
.then(count => {
return count;
});
if (count === 0) {
if (global.work.hash === "") {
console.log(msg);
console.log(hash);
global.work.hash = hash;
queue.create({ hash: hash, submitDate: Date.now() });
io.emit("start", "");
} else {
// global.work.hashQueue.push(msg.hash);
queue.create({ hash: hash, submitDate: Date.now() });
}
log(`Hash ${hash} enqueued`);
}
});
socket.on("requestID", message => {
let creating = true;
while (creating) {
let newid = Math.floor(Math.random() * 99999999) + 1;
let slave = slaves.findAll({ where: { slaveid: newid } });
if (isIdUnique(newid)) {
slaves.create({ UUID: newid, blocks: 0, avgspeed: 0 });
socket.emit("setid", { id: newid });
creating = false;
}
}
});
socket.on("ping", async function(msg) {
socket.emit("pingReply", {
// Sends an init with previous ssids to the new client
time: Date.getTime(),
});
});
socket.on("finishedWork", async function(msg) {
let newSpeed =
global.settings.blockSize /
(Date.now() / 1000 - global.work.loads[msg.start].time / 1000);
global.work.currentCount += global.settings.blockSize;
global.work.averageSpeed = (global.work.averageSpeed + newSpeed) / 2;
delete global.work.loads[msg.start];
if (msg.cracked !== false) {
log(`Hash is ${msg.cracked}`);
global.settings.start = 0;
global.settings.stop = global.settings.blockSize - 1;
//global.work.foundHashes[global.work.hash] = msg.cracked;
finished.create({
hash: global.work.hash,
plaintext: msg.cracked,
crackDate: Date.now(),
});
queue.destroy({ where: { hash: global.work.hash } });
let payload = {};
payload[global.work.hash] = msg.cracked;
io.emit("foundHashes", { hashes: payload });
if (
await queue.count().then(count => {
return count > 0;
})
) {
console.log("Here");
global.work.hash = await queue.findOne().then(hash => {
return hash.hash;
});
//global.work.hash = global.work.hashQueue.pop();
} else {
global.work.hash = "";
}
}
});
socket.on("requestWork", async function(msg) {
if (global.work.hash === "") {
if (
await queue.count().then(count => {
return count > 0;
})
) {
global.work.hash = await queue.findOne().then(one => {
return one.hash;
});
} else {
socket.emit("work", "nowork");
}
} else {
let load = checkOldLoads();
if (load !== false) {
log("Resending old work");
load.time = Date.now();
} else {
load = {
hash: global.work.hash,
start: global.settings.start,
stop: global.settings.stop,
time: Date.now(),
};
global.settings.start += global.settings.blockSize;
global.settings.stop += global.settings.blockSize;
}
global.work.loads[load.start] = load;
load.chars = global.work.chars;
socket.emit("work", load);
}
});
});
function sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
async function update() {
while (true) {
await sleep(1000);
let level = await getLevel(global.settings.start, global.work.chars.length);
let speed = global.work.currentCount;
global.work.currentCount = 0;
let hash = global.work.hash;
await io.emit("displayUpdate", {
totalspeed: speed,
level: level,
curhash: hash,
});
updatesettings();
}
}
update();
//Setting up each page
app.get("/", (req, res) => {
/**
* When a user connects, they are send the index.html file
*/
res.sendFile(__dirname + "/public/html/index.html");
});
app.get("*.html", (req, res) => {
/**
* When a css file is requested, /public/css is searched
*/
res.sendFile(__dirname + `/public/html/${req.path.split("/")[1]}`);
console.log(`/public/html/${req.path.split("/")[1]}`);
});
app.get("*.css", (req, res) => {
/**
* When a css file is requested, /public/css is searched
*/
res.sendFile(__dirname + `/public/css/${req.path.split("/")[1]}`);
console.log(`/public/css/${req.path.split("/")[1]}`);
});
app.get("*.js", (req, res) => {
/**
* when a js file is requested, /public/scripts is searched
*/
res.sendFile(__dirname + `/public/js/${req.path.split("/")[1]}`);
console.log(`/public/js/${req.path.split("/")[1]}`);
});
app.use(favicon(__dirname + "/public/images/favicon.png")); // Send favicon when requested
app.use((req, res) => {
/**
* Used when requested webpage is not found.
*/
res.status(404); // set page to 404
log(
// Log that a user requested invalid page
`${req.body.host} connected to the server. They requested ${req.path}, but this page was unavailable.`
);
res.send("Error 404"); // Send Error 404 to user
});
http.listen(port, () => console.log(`Running on port ${port}.`));