-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
executable file
·86 lines (76 loc) · 3.11 KB
/
server.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
import { existsSync, createReadStream, appendFile } from "fs";
import { dirname } from "path";
import { fileURLToPath } from "url";
import express from "express";
import expressStaticGzip from "express-static-gzip";
import { dateTime, dropRootPermission } from "./serverUtil.js";
import { reqTypeRouter, reqAnalyzer, delAnalyzer, usrConnect, requestResolver, writeAsyFile, downloadReq } from "./serverAnalyzer.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const defaultPort = 80;
const port = (process.env.ASYMPTOTE_PORT == undefined)? defaultPort: parseInt(process.env.ASYMPTOTE_PORT);
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Express Application
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
const app = express();
// Serving Static html File & Running Major Requests
// -------------------------------------------------
app.route("/")
.get(express.static(__dirname + "/build"))
.post(reqTypeRouter(), usrConnect(__dirname), reqAnalyzer(__dirname), writeAsyFile(__dirname), requestResolver())
.post(reqTypeRouter(), (req, res, next) => {
console.log(req.body);
next();
})
app.route("/delete")
.post(express.text(), delAnalyzer(__dirname));
// Serving Static Logo html File
// ----------------------------------------
app.route("/logo3d.html")
.get(expressStaticGzip(__dirname + "/build"));
// Serving Other Static Files
// ----------------------------------------
app.use("/static/", function(req, res, next) {
if (/\/(?:css|js|media)\//.test(req.originalUrl)) {
let urlMatched = /^\/static\/(?:css|js|media)\/(.+\.(?:css|js|map|svg)$)/g.exec(req.originalUrl);
if (urlMatched !== null && urlMatched[1] !== undefined) {
res.sendFile(__dirname + "/build" + urlMatched[0]);
}
}
})
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Iframe Request
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
app.use("/clients", (req, res, next) => {
if (req.method === "GET") {
const fileToServe = __dirname + req.originalUrl;
if (existsSync(fileToServe)) {
createReadStream(fileToServe).pipe(res);
}
} else {
next();
}
});
app.route("/clients")
.post(express.urlencoded({extended: true}), reqAnalyzer(__dirname) , downloadReq(__dirname))
app.listen(port);
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Drop Root Permissions
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dropRootPermission(port);
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Error Handling
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
process.on("uncaughtException", (err) => {
const diagnose = {
errorType: "An uncaught error moved to the top of the stack!",
registerTime: dateTime().time,
exception: err,
errorStack: err.stack,
},
diagnoseJSON = JSON.stringify(diagnose).replace(/\\n/g,'\n') + '\n';
const dest = __dirname + "/logs/uncaughtExceptions"
if (err) {
appendFile(dest, diagnoseJSON, (err) => {
if (err) {
console.log("An error occurred while writing " + dest + ".");
}
})
}
})