-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
72 lines (60 loc) · 2.26 KB
/
utils.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
const fs = require('fs');
var sessions = [];
var messages = [];
function session(container, username, password, started_by, id, dev) {
// username must be only letters
// password must be alphanumeric, with !@#$%^&*()_-+= allowed
// id must be a number
var path = "containers";
if (dev) path = "dev";
if (container == undefined || !fs.existsSync(`/root/node/gaggle/docker/${path}/${container}`)) {
throw "Invalid container";
}
if (username == undefined || !/^[a-zA-Z]+$/.test(username) || username.length > 20 || username.length < 3) {
throw "Invalid username";
}
if (password == undefined || !/^[ A-Za-z0-9_@./#&+-]*$/.test(password) || password.length > 20 || password.length < 6) {
throw "Invalid password";
}
if (started_by == undefined || !/^[a-zA-Z]+$/.test(started_by) || started_by.length < 1) {
throw "Invalid started_by";
}
if (id == undefined || !/^[0-9]+$/.test(id)) {
throw "Invalid id";
}
this.container = container;
this.username = username;
this.password = password;
this.started_by = started_by;
this.id = id;
this.time = Date.now();
}
function message(username, msg, max_length) {
if (typeof(username) !== "string") throw "Invalid username";
if (typeof(msg) !== "string" || msg.length < 1) throw "Invalid message";
if (msg.length > max_length) throw "Message is too long.";
this.msg = msg;
this.username = username;
this.time = Date.now();
}
function start_session(id) {
var session = sessions[id];
var container = session.container;
var username = session.username;
var password = session.password;
var id = session.id;
var exec = require('child_process').exec;
var command = `/bin/bash /root/node/gaggle/docker/start.sh "${container}" "${username}" "${password}" ${id}`;
exec(command, (error, stdout, stderr) => {
if (error) throw `Exec error: ${error}`;
});
}
function stop_session(id) {
if (sessions[id] == undefined) throw "Session not found";
var exec = require('child_process').exec;
var command = `/bin/bash /root/node/gaggle/docker/stop.sh ${id}`;
exec(command, (error, stdout, stderr) => {
if (error) throw `Exec error: ${error}`;
});
}
module.exports = { sessions, messages, session, message, start_session, stop_session };