badmfck-api-server 1.5.3 → 1.5.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -39,6 +39,7 @@ export declare class APIService extends BaseService {
39
39
  private version;
40
40
  private options;
41
41
  private monitor?;
42
+ private monitorIndexFile?;
42
43
  netLog: APIServiceNetworkLogItem[];
43
44
  constructor(options?: APIServiceOptions | null);
44
45
  init(): Promise<void>;
@@ -12,6 +12,9 @@ const DefaultErrors_1 = __importDefault(require("./structures/DefaultErrors"));
12
12
  const badmfck_signal_1 = require("badmfck-signal");
13
13
  const LogService_1 = require("./LogService");
14
14
  const Monitor_1 = require("./monitor/Monitor");
15
+ const path_1 = __importDefault(require("path"));
16
+ const crypto_1 = __importDefault(require("crypto"));
17
+ const fs_1 = __importDefault(require("fs"));
15
18
  function getDefaultOptions() {
16
19
  return {
17
20
  port: 8091,
@@ -44,9 +47,10 @@ async function Initializer(services) {
44
47
  exports.Initializer = Initializer;
45
48
  class APIService extends BaseService_1.BaseService {
46
49
  static nextLogID = 0;
47
- version = "1.5.3";
50
+ version = "1.5.5";
48
51
  options;
49
52
  monitor;
53
+ monitorIndexFile;
50
54
  netLog = [];
51
55
  constructor(options) {
52
56
  super('HTTP Service');
@@ -60,6 +64,11 @@ class APIService extends BaseService_1.BaseService {
60
64
  this.monitor = new Monitor_1.Monitor();
61
65
  this.options.endpoints.push(this.monitor);
62
66
  console.log("Service Monitor initialized");
67
+ console.log("monitor links:");
68
+ for (let i of this.options.monitor) {
69
+ const hash = crypto_1.default.createHash("sha256").update(i.login + i.password).digest().toString("hex");
70
+ console.log("/sm-" + hash);
71
+ }
63
72
  }
64
73
  }
65
74
  async init() {
@@ -189,6 +198,40 @@ class APIService extends BaseService_1.BaseService {
189
198
  }
190
199
  app.use((req, res, next) => {
191
200
  const tme = +new Date();
201
+ if (this.monitor && this.options && this.options.monitor) {
202
+ if (req.method === "GET") {
203
+ let monitorRequest = false;
204
+ for (let i of this.options.monitor) {
205
+ const hash = crypto_1.default.createHash("sha256").update(i.login + i.password).digest().toString("hex");
206
+ if (req.originalUrl.endsWith("/sm-" + hash)) {
207
+ monitorRequest = true;
208
+ break;
209
+ }
210
+ }
211
+ if (monitorRequest) {
212
+ const date = new Date();
213
+ try {
214
+ if (!this.monitorIndexFile)
215
+ this.monitorIndexFile = fs_1.default.readFileSync(path_1.default.resolve(__dirname, "monitor", "index.html"));
216
+ res.setHeader("Content-Type", "text/html");
217
+ res.setHeader("Content-Length", this.monitorIndexFile.byteLength);
218
+ res.status(200).send(this.monitorIndexFile);
219
+ }
220
+ catch (e) {
221
+ this.sendResponse(res, {
222
+ error: {
223
+ code: 10002,
224
+ message: "Internal server error",
225
+ details: `${e}`
226
+ },
227
+ data: null,
228
+ httpStatus: 500
229
+ }, tme, req.path);
230
+ }
231
+ return;
232
+ }
233
+ }
234
+ }
192
235
  this.sendResponse(res, {
193
236
  error: DefaultErrors_1.default.UNKNOWN_REQUEST,
194
237
  data: null,
@@ -1,5 +1,6 @@
1
1
  import { BaseEndpoint } from "../BaseEndpoint";
2
2
  import { HTTPRequestVO, TransferPacketVO } from "../structures/Interfaces";
3
+ export declare const indexFilePath: string;
3
4
  interface IStatObject {
4
5
  requests: Map<string, number>;
5
6
  errors: Map<string, number>;
@@ -3,11 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Monitor = void 0;
6
+ exports.Monitor = exports.indexFilePath = void 0;
7
7
  const APIService_1 = require("../APIService");
8
8
  const BaseEndpoint_1 = require("../BaseEndpoint");
9
9
  const LogService_1 = require("../LogService");
10
10
  const crypto_1 = __importDefault(require("crypto"));
11
+ const path_1 = __importDefault(require("path"));
12
+ exports.indexFilePath = path_1.default.resolve(__dirname, "index.html");
11
13
  class Monitor extends BaseEndpoint_1.BaseEndpoint {
12
14
  ignoreHttpLogging = true;
13
15
  startedAt = new Date();
@@ -0,0 +1,4 @@
1
+ <html>
2
+ <head></head>
3
+ <body>MONITOR!</body>
4
+ </html>
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "1.5.3",
3
+ "version": "1.5.5",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "type": "commonjs",
8
8
  "scripts": {
9
- "prepublish": "tsc",
10
- "build": "tsc",
9
+ "prepublish": "build",
10
+ "build": "tsc & cp ./src/apiServer/monitor/index.html ./dist/apiServer/monitor/index.html",
11
11
  "test": "echo \"Error: no test specified\" && exit 1"
12
12
  },
13
13
  "keywords": [