badmfck-api-server 1.2.2 → 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/apiServer/APIService.js +1 -1
- package/dist/apiServer/MysqlService.d.ts +1 -0
- package/dist/apiServer/MysqlService.js +56 -6
- package/dist/apiServer/helper/Validator.d.ts +18 -0
- package/dist/apiServer/helper/Validator.js +60 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -1
- package/package.json +1 -1
@@ -37,6 +37,7 @@ export declare class MysqlService extends BaseService {
|
|
37
37
|
pool: Pool | null;
|
38
38
|
options: MysqlServiceOptions;
|
39
39
|
serviceStarted: boolean;
|
40
|
+
timeoutID: any;
|
40
41
|
queries: never[];
|
41
42
|
constructor(options: MysqlServiceOptions);
|
42
43
|
static executeQuery(query: MySqlQuery | MySqlQuery[]): Promise<MysqlResult[]>;
|
@@ -7,6 +7,7 @@ exports.MysqlService = exports.executeQuery = exports.REQ_MYSQL_QUERY = void 0;
|
|
7
7
|
const mysql_1 = __importDefault(require("mysql"));
|
8
8
|
const BaseService_1 = require("./BaseService");
|
9
9
|
const badmfck_signal_1 = require("badmfck-signal");
|
10
|
+
const crypto_1 = require("crypto");
|
10
11
|
exports.REQ_MYSQL_QUERY = new badmfck_signal_1.Req("REQ_MYSQL_QUERY");
|
11
12
|
const executeQuery = async (query) => { return await exports.REQ_MYSQL_QUERY.request(query); };
|
12
13
|
exports.executeQuery = executeQuery;
|
@@ -16,10 +17,17 @@ class MysqlService extends BaseService_1.BaseService {
|
|
16
17
|
pool = null;
|
17
18
|
options;
|
18
19
|
serviceStarted = false;
|
20
|
+
timeoutID;
|
19
21
|
queries = [];
|
20
22
|
constructor(options) {
|
21
23
|
super("mysql");
|
22
24
|
this.options = options;
|
25
|
+
if (this.options.host.startsWith("_"))
|
26
|
+
this.options.host = decrypt(this.options.host) ?? this.options.host;
|
27
|
+
if (this.options.user.startsWith("_"))
|
28
|
+
this.options.user = decrypt(this.options.user) ?? this.options.user;
|
29
|
+
if (this.options.database.startsWith("_"))
|
30
|
+
this.options.database = decrypt(this.options.database) ?? this.options.database;
|
23
31
|
}
|
24
32
|
static async executeQuery(query) { return await exports.REQ_MYSQL_QUERY.request(query); }
|
25
33
|
async init() {
|
@@ -44,7 +52,9 @@ class MysqlService extends BaseService_1.BaseService {
|
|
44
52
|
if (!ok) {
|
45
53
|
if (this.options.onLog)
|
46
54
|
this.options.onLog(2, "Mysql server not connected, retrying in 3 sec");
|
47
|
-
|
55
|
+
if (this.timeoutID)
|
56
|
+
clearTimeout(this.timeoutID);
|
57
|
+
this.timeoutID = setTimeout(() => { this.recreatePool(); }, 3000);
|
48
58
|
}
|
49
59
|
else {
|
50
60
|
this.serviceStarted = true;
|
@@ -136,10 +146,6 @@ class MysqlService extends BaseService_1.BaseService {
|
|
136
146
|
});
|
137
147
|
return;
|
138
148
|
}
|
139
|
-
this.pool.on("error", (evt) => {
|
140
|
-
if (this.options.onLog)
|
141
|
-
this.options.onLog(2, `${evt}`);
|
142
|
-
});
|
143
149
|
this.pool.getConnection((err, conn) => {
|
144
150
|
if (err) {
|
145
151
|
if (this.options.onLog)
|
@@ -250,6 +256,19 @@ class MysqlService extends BaseService_1.BaseService {
|
|
250
256
|
if (this.options.onLog)
|
251
257
|
this.options.onLog(1, "Connecting to mysql: \n HOST: " + this.options.host + '\n USER:' + this.options.user + '\n PORT:' + this.options.port);
|
252
258
|
let err = false;
|
259
|
+
if (this.pool) {
|
260
|
+
try {
|
261
|
+
this.pool.removeAllListeners();
|
262
|
+
this.pool.end(err => {
|
263
|
+
if (err && this.options.onLog)
|
264
|
+
this.options.onLog(2, err);
|
265
|
+
});
|
266
|
+
}
|
267
|
+
catch (e) {
|
268
|
+
if (this.options.onLog)
|
269
|
+
this.options.onLog(2, e);
|
270
|
+
}
|
271
|
+
}
|
253
272
|
try {
|
254
273
|
this.pool = mysql_1.default.createPool({
|
255
274
|
connectionLimit: this.options.connectionLimit,
|
@@ -266,7 +285,12 @@ class MysqlService extends BaseService_1.BaseService {
|
|
266
285
|
this.options.onLog(2, "Can't connect to MYSQL!");
|
267
286
|
err = true;
|
268
287
|
}
|
269
|
-
|
288
|
+
if (!err && this.pool) {
|
289
|
+
this.pool.on("error", (evt) => {
|
290
|
+
if (this.options.onLog)
|
291
|
+
this.options.onLog(2, `${evt}`);
|
292
|
+
});
|
293
|
+
}
|
270
294
|
return new Promise((res, rej) => {
|
271
295
|
if (err) {
|
272
296
|
res(false);
|
@@ -289,4 +313,30 @@ class MysqlService extends BaseService_1.BaseService {
|
|
289
313
|
}
|
290
314
|
}
|
291
315
|
exports.MysqlService = MysqlService;
|
316
|
+
const secret_key = "AKLWkajw%^&dgwqhw#98453i23bfk23rn2knknglrgjeit";
|
317
|
+
const secret_iv = "rthadrfk23rn2kn#*FNKA@gt44df3tslrgj##!it";
|
318
|
+
const key = (0, crypto_1.createHash)('sha512').update(secret_key).digest('hex').substring(0, 32);
|
319
|
+
const iv = (0, crypto_1.createHash)('sha512').update(secret_iv).digest('hex').substring(0, 16);
|
320
|
+
const decrypt = (encrypted) => {
|
321
|
+
try {
|
322
|
+
encrypted = Buffer.from(encrypted, 'base64').toString('binary');
|
323
|
+
const decipher = (0, crypto_1.createDecipheriv)('aes-256-cbc', key, iv);
|
324
|
+
let decrypted = decipher.update(encrypted, 'binary', 'utf8');
|
325
|
+
decrypted += decipher.final('utf8');
|
326
|
+
return decrypted;
|
327
|
+
}
|
328
|
+
catch (e) {
|
329
|
+
return null;
|
330
|
+
}
|
331
|
+
};
|
332
|
+
const encrypt = (txt) => {
|
333
|
+
try {
|
334
|
+
let encipher = (0, crypto_1.createCipheriv)('aes-256-cbc', key, iv), encrypted = encipher.update(txt, 'utf8', 'binary');
|
335
|
+
encrypted += encipher.final('binary');
|
336
|
+
return Buffer.from(encrypted, 'binary').toString('base64');
|
337
|
+
}
|
338
|
+
catch (e) {
|
339
|
+
return null;
|
340
|
+
}
|
341
|
+
};
|
292
342
|
exports.default = MysqlService;
|
@@ -0,0 +1,18 @@
|
|
1
|
+
export interface IValidatorOptions {
|
2
|
+
min?: number;
|
3
|
+
max?: number;
|
4
|
+
type?: "string" | "boolean" | "number";
|
5
|
+
regex?: RegExp;
|
6
|
+
}
|
7
|
+
export declare enum ValidationReport {
|
8
|
+
OK = 1,
|
9
|
+
VALUE_IS_NULL = 2,
|
10
|
+
TYPE_MISSMATCH = 3,
|
11
|
+
VALUE_TOO_BIG = 4,
|
12
|
+
VALUE_TOO_SHORT = 5,
|
13
|
+
VALUE_INCORRECT = 6
|
14
|
+
}
|
15
|
+
export declare class Validator {
|
16
|
+
static validateObject(fields: string[], object: any): boolean;
|
17
|
+
static validateValue(value: string | number | boolean, opt?: IValidatorOptions): ValidationReport;
|
18
|
+
}
|
@@ -0,0 +1,60 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.Validator = exports.ValidationReport = void 0;
|
4
|
+
var ValidationReport;
|
5
|
+
(function (ValidationReport) {
|
6
|
+
ValidationReport[ValidationReport["OK"] = 1] = "OK";
|
7
|
+
ValidationReport[ValidationReport["VALUE_IS_NULL"] = 2] = "VALUE_IS_NULL";
|
8
|
+
ValidationReport[ValidationReport["TYPE_MISSMATCH"] = 3] = "TYPE_MISSMATCH";
|
9
|
+
ValidationReport[ValidationReport["VALUE_TOO_BIG"] = 4] = "VALUE_TOO_BIG";
|
10
|
+
ValidationReport[ValidationReport["VALUE_TOO_SHORT"] = 5] = "VALUE_TOO_SHORT";
|
11
|
+
ValidationReport[ValidationReport["VALUE_INCORRECT"] = 6] = "VALUE_INCORRECT";
|
12
|
+
})(ValidationReport || (exports.ValidationReport = ValidationReport = {}));
|
13
|
+
class Validator {
|
14
|
+
static validateObject(fields, object) {
|
15
|
+
if (!object || typeof object !== "object")
|
16
|
+
return false;
|
17
|
+
for (let i of fields) {
|
18
|
+
if (!(i in object) || object[i] === undefined)
|
19
|
+
return false;
|
20
|
+
}
|
21
|
+
return true;
|
22
|
+
}
|
23
|
+
static validateValue(value, opt) {
|
24
|
+
if (!value)
|
25
|
+
return ValidationReport.VALUE_IS_NULL;
|
26
|
+
if (!opt)
|
27
|
+
return ValidationReport.OK;
|
28
|
+
if (!opt.type)
|
29
|
+
opt.type = "string";
|
30
|
+
if (opt.type === "boolean") {
|
31
|
+
if (typeof value === "boolean")
|
32
|
+
return ValidationReport.OK;
|
33
|
+
if (typeof value === "number" && (value === 1 || value === 0))
|
34
|
+
return ValidationReport.OK;
|
35
|
+
return ValidationReport.TYPE_MISSMATCH;
|
36
|
+
}
|
37
|
+
if (opt.type === "number") {
|
38
|
+
if (typeof value !== "number")
|
39
|
+
return ValidationReport.TYPE_MISSMATCH;
|
40
|
+
if (opt.max && value > opt.max)
|
41
|
+
return ValidationReport.VALUE_TOO_BIG;
|
42
|
+
if (opt.min && value < opt.min)
|
43
|
+
return ValidationReport.VALUE_TOO_BIG;
|
44
|
+
return ValidationReport.OK;
|
45
|
+
}
|
46
|
+
if (typeof value !== "string")
|
47
|
+
return ValidationReport.TYPE_MISSMATCH;
|
48
|
+
if (opt.max && value.length > opt.max)
|
49
|
+
return ValidationReport.VALUE_TOO_BIG;
|
50
|
+
if (opt.min && value.length < opt.min)
|
51
|
+
return ValidationReport.VALUE_TOO_SHORT;
|
52
|
+
if (opt.regex) {
|
53
|
+
const tmp = value.replaceAll(opt.regex, "");
|
54
|
+
if (tmp.length !== 0)
|
55
|
+
return ValidationReport.VALUE_INCORRECT;
|
56
|
+
}
|
57
|
+
return ValidationReport.OK;
|
58
|
+
}
|
59
|
+
}
|
60
|
+
exports.Validator = Validator;
|
package/dist/index.d.ts
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
import { APIService, Initializer } from "./apiServer/APIService";
|
2
2
|
import { LocalRequest } from "./apiServer/LocalRequest";
|
3
3
|
import { MysqlService } from "./apiServer/MysqlService";
|
4
|
-
|
4
|
+
import { Validator } from "./apiServer/helper/Validator";
|
5
|
+
export { APIService, Initializer, LocalRequest, MysqlService, Validator };
|
package/dist/index.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.MysqlService = exports.LocalRequest = exports.Initializer = exports.APIService = void 0;
|
3
|
+
exports.Validator = exports.MysqlService = exports.LocalRequest = exports.Initializer = exports.APIService = void 0;
|
4
4
|
const APIService_1 = require("./apiServer/APIService");
|
5
5
|
Object.defineProperty(exports, "APIService", { enumerable: true, get: function () { return APIService_1.APIService; } });
|
6
6
|
Object.defineProperty(exports, "Initializer", { enumerable: true, get: function () { return APIService_1.Initializer; } });
|
@@ -8,3 +8,5 @@ const LocalRequest_1 = require("./apiServer/LocalRequest");
|
|
8
8
|
Object.defineProperty(exports, "LocalRequest", { enumerable: true, get: function () { return LocalRequest_1.LocalRequest; } });
|
9
9
|
const MysqlService_1 = require("./apiServer/MysqlService");
|
10
10
|
Object.defineProperty(exports, "MysqlService", { enumerable: true, get: function () { return MysqlService_1.MysqlService; } });
|
11
|
+
const Validator_1 = require("./apiServer/helper/Validator");
|
12
|
+
Object.defineProperty(exports, "Validator", { enumerable: true, get: function () { return Validator_1.Validator; } });
|