badmfck-api-server 3.2.1 → 3.2.3
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 += "/";
|
@@ -76,11 +127,11 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
76
127
|
};
|
77
128
|
}
|
78
129
|
async controllers(req) {
|
79
|
-
this.checkAuthentication(req);
|
130
|
+
await this.checkAuthentication(req);
|
80
131
|
return {};
|
81
132
|
}
|
82
133
|
async changeLogLevel(req) {
|
83
|
-
this.checkAuthentication(req);
|
134
|
+
await this.checkAuthentication(req);
|
84
135
|
const val = logMap[req.data.level];
|
85
136
|
if (!val)
|
86
137
|
throw { ...DefaultErrors_1.default.BAD_REQUEST, details: "bad log level" };
|
@@ -88,7 +139,7 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
88
139
|
return { data: (await LogService_1.REQ_CURRENT_LOG_LEVEL.request()) };
|
89
140
|
}
|
90
141
|
async logs(req) {
|
91
|
-
this.checkAuthentication(req);
|
142
|
+
await this.checkAuthentication(req);
|
92
143
|
const services = await LogService_1.REQ_LOG_UNIQUE_SERVICES.request();
|
93
144
|
const log = await LogService_1.REQ_LOG.request({
|
94
145
|
lastID: req.data.lastID ?? 0
|
@@ -101,17 +152,17 @@ class Monitor extends BaseEndpoint_1.BaseEndpoint {
|
|
101
152
|
};
|
102
153
|
}
|
103
154
|
async netlog(req) {
|
104
|
-
this.checkAuthentication(req);
|
155
|
+
await this.checkAuthentication(req);
|
105
156
|
const log = await APIService_1.REQ_HTTP_LOG.request(req.data);
|
106
157
|
return { data: log };
|
107
158
|
}
|
108
159
|
async metrics(req) {
|
109
|
-
this.checkAuthentication(req);
|
160
|
+
await this.checkAuthentication(req);
|
110
161
|
const result = await MonitorService_1.REQ_EP_STAT.request(req.data);
|
111
162
|
return { data: result };
|
112
163
|
}
|
113
164
|
async serverStat(req) {
|
114
|
-
this.checkAuthentication(req);
|
165
|
+
await this.checkAuthentication(req);
|
115
166
|
let stat = {
|
116
167
|
cpuUsage: os_1.default.cpus(),
|
117
168
|
memoryTotal: os_1.default.totalmem(),
|