-
Notifications
You must be signed in to change notification settings - Fork 5
/
serverUtil.js
140 lines (124 loc) · 4.69 KB
/
serverUtil.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
import fs from "fs";
import { createRequire } from "module";
import { fileURLToPath } from "url";
import { dirname } from "path";
import { customAlphabet } from 'nanoid'
const require = createRequire(import.meta.url);
const SHA1 = require("crypto-js/sha1")
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Flags
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
export const FLAGS = {
SUCCESS: {
ASY_FILE_CREATED: "ASY_FILE_CREATED",
ASY_OUTPUT_CREATED: "ASY_OUTPUT_CREATED",
ASY_RUN_NO_OUTPUT: "ASY_RUN_NO_OUTPUT"
},
FAILURE: {
ASY_WRITE_FILE_ERR: ["ASY_WRITE_FILE_ERR", "An error occurred inside the server while writing the asy file."],
ASY_CODE_COMPILE_ERR: ["ASY_CODE_COMPILE_ERR", "Asymptote code compile error."],
PROCESS_SPAWN_ERR: ["PROCESS_SPAWN_ERR", "An error occurred inside the server while spawning child process."],
PROCESS_TIMEDOUT_ERR: ["PROCESS_TIMEDOUT_ERR", "Process timed out."],
PROCESS_TERMINATED_ERR: ["PROCESS_TERMINATED_ERR", "Process terminated."],
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% usrID
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
const uAlphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lAlphabets = "abcdefghijklmnopqrstuvwxyz";
const digits = "0123456789"
const nanoid = customAlphabet(digits + lAlphabets + uAlphabets, 4);
export function usrID(ip) {
let count = 0;
const relativeIPBasedPath = SHA1(ip).toString();
const clientsPath = __dirname + "/clients";
const files = fs.readdirSync(clientsPath);
files.forEach((element) => {
(element.includes(relativeIPBasedPath))? count++ : null;
});
return (count >= 50)? "-1": nanoid();
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% usrDirMgr
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
export function usrDirMgr(req, serverDir, id) {
const usrDirName = SHA1(req.ip).toString() + id;
return {
usrDirName: usrDirName,
usrDirPath: "/" + usrDirName,
usrRelDirPath: "/clients/" + usrDirName,
usrAbsDirPath: serverDir + "/clients/" + usrDirName,
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% writePing
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
export function writePing(dir) {
const pingFilePath = dir + "/ping";
fs.writeFile(pingFilePath, "", (err) => {
if (err) {
console.log("error in writing ping file!");
}
});
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% makeDir
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
export function makeDir(dir) {
fs.mkdirSync(dir);
writePing(dir);
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% dateTime
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
export function dateTime() {
const dateObject = new Date();
const year = dateObject.getFullYear();
const month = dateObject.getMonth() + 1;
const day = dateObject.getDate();
const hour = dateObject.getHours();
const minute = dateObject.getMinutes();
const second = dateObject.getSeconds();
const date = year + "/" + month + "/" + day;
const time = hour + ":" + minute + ":" + second;
return {
date: date,
time: time
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% removeDir
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
export function removeDir(path) {
if (fs.existsSync(path)) {
const files = fs.readdirSync(path)
if (files.length > 0) {
files.forEach(function(filename) {
if (fs.statSync(path + "/" + filename).isDirectory()) {
removeDir(path + "/" + filename);
} else {
fs.unlinkSync(path + "/" + filename);
}
})
fs.rmdirSync(path);
} else {
fs.rmdirSync(path);
}
} else {
console.log("Directory path not found!");
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% drop root permission
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
export function dropRootPermission(port) {
let uid = parseInt(process.env.ASYMPTOTE_UID);
let gid = parseInt(process.env.ASYMPTOTE_GID);
if (uid === 0 || gid === 0) {
const user = process.env.ASYMPTOTE_USER;
console.log(`Cannot run as uid 0 or gid 0; please first adduser`, user);
process.exit(-1);
}
const home = process.env.ASYMPTOTE_HOME;
process.env.HOME = home;
process.env.ASYMPTOTE_HOME = home+"/.asy";
process.setgid(gid);
process.setuid(uid);
console.log(`\nAsymptote Web Application started on port ${port} with uid ${uid} and gid ${gid}`);
console.log("Using home directory",home);
}