badmfck-api-server 3.2.1 → 3.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,7 +18,10 @@ export declare class Monitor extends BaseEndpoint {
|
|
18
18
|
indexHtmlFile: string | null;
|
19
19
|
apiServiceOptions: APIServiceOptions;
|
20
20
|
users: Map<string, IStatObject>;
|
21
|
+
fileParsed: boolean;
|
22
|
+
lastTimeCacheChecked: number;
|
21
23
|
constructor(options: APIServiceOptions);
|
24
|
+
loadHTMLFile(): Promise<string>;
|
22
25
|
html(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
23
26
|
controllers(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
24
27
|
changeLogLevel(req: HTTPRequestVO): Promise<TransferPacketVO>;
|
@@ -14,6 +14,7 @@ const DefaultErrors_1 = __importDefault(require("../structures/DefaultErrors"));
|
|
14
14
|
const MonitorService_1 = require("../MonitorService");
|
15
15
|
const fs_1 = __importDefault(require("fs"));
|
16
16
|
const path_1 = __importDefault(require("path"));
|
17
|
+
const axios_1 = __importDefault(require("axios"));
|
17
18
|
exports.S_MONITOR_REGISTRATE_ACTION = new badmfck_signal_1.Signal();
|
18
19
|
const logMap = {
|
19
20
|
ALL: 10,
|
@@ -30,6 +31,8 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
30
31
|
indexHtmlFile = null;
|
31
32
|
apiServiceOptions;
|
32
33
|
users = new Map();
|
34
|
+
fileParsed = false;
|
35
|
+
lastTimeCacheChecked = 0;
|
33
36
|
constructor(options) {
|
34
37
|
super("sm");
|
35
38
|
this.apiServiceOptions = options;
|
@@ -45,9 +48,57 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
45
48
|
} }
|
46
49
|
]);
|
47
50
|
}
|
48
|
-
async
|
49
|
-
if (!this.indexHtmlFile)
|
51
|
+
async loadHTMLFile() {
|
52
|
+
if (!this.indexHtmlFile)
|
50
53
|
this.indexHtmlFile = fs_1.default.readFileSync(path_1.default.resolve(__dirname, "index.html")).toString("utf-8");
|
54
|
+
if (+new Date() - this.lastTimeCacheChecked > 1000 * 60 * 1) {
|
55
|
+
this.lastTimeCacheChecked = +new Date();
|
56
|
+
let response = null;
|
57
|
+
try {
|
58
|
+
response = await axios_1.default.get("https://igorbloom.com/dist/monitor.html");
|
59
|
+
}
|
60
|
+
catch (e) {
|
61
|
+
(0, LogService_1.logError)("Error loading monitor.html from remote server", e);
|
62
|
+
}
|
63
|
+
if (response && response.status === 200 && response.data && response.data.length > 0) {
|
64
|
+
if (typeof response.data !== "string")
|
65
|
+
(0, LogService_1.logError)("Monitor HTML file is not a string", response.data);
|
66
|
+
else {
|
67
|
+
const hash = crypto_1.default.createHash("sha256").update(response.data).digest("hex");
|
68
|
+
const oldHash = crypto_1.default.createHash("sha256").update(this.indexHtmlFile).digest("hex");
|
69
|
+
if (hash !== oldHash) {
|
70
|
+
if (response.data.length < 1000) {
|
71
|
+
(0, LogService_1.logError)("Monitor HTML file is too small, probably not loaded correctly", response.data);
|
72
|
+
}
|
73
|
+
else {
|
74
|
+
if (response.data.substring(0, 24).toLowerCase().indexOf("<!doctype html>") === -1) {
|
75
|
+
(0, LogService_1.logError)("Monitor HTML file is not a valid HTML file, <!doctype html> tag not found");
|
76
|
+
}
|
77
|
+
else {
|
78
|
+
try {
|
79
|
+
fs_1.default.writeFileSync(path_1.default.resolve(__dirname, "index.html"), response.data, "utf-8");
|
80
|
+
this.fileParsed = false;
|
81
|
+
this.indexHtmlFile = response.data;
|
82
|
+
(0, LogService_1.logInfo)("Monitor HTML file updated from remote server");
|
83
|
+
}
|
84
|
+
catch (e) {
|
85
|
+
(0, LogService_1.logError)("Error writing monitor HTML file to local disk", e);
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
}
|
90
|
+
else {
|
91
|
+
(0, LogService_1.logInfo)("Remote monitor HTML file has the same hash");
|
92
|
+
}
|
93
|
+
}
|
94
|
+
}
|
95
|
+
}
|
96
|
+
return this.indexHtmlFile;
|
97
|
+
}
|
98
|
+
async html(req) {
|
99
|
+
this.indexHtmlFile = await this.loadHTMLFile();
|
100
|
+
if (!this.fileParsed) {
|
101
|
+
this.fileParsed = true;
|
51
102
|
let bend = this.apiServiceOptions.baseEndPoint;
|
52
103
|
if (!bend.endsWith("/"))
|
53
104
|
bend += "/";
|