namirasoft-node 1.4.101 → 1.4.102
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.
- package/dist/OTPOperation.js +3 -3
- package/dist/OTPOperation.js.map +1 -1
- package/package.json +43 -43
- package/src/AnomalyDetector.ts +84 -84
- package/src/BaseApplication.ts +440 -440
- package/src/BaseApplicationLink.ts +6 -6
- package/src/BaseController.ts +225 -225
- package/src/BaseCron.ts +104 -104
- package/src/BaseDatabase.ts +45 -45
- package/src/BaseEmailService.ts +38 -38
- package/src/BaseFilterItemBuilder.ts +189 -189
- package/src/BaseFilterItemBuilderDatabase.ts +45 -45
- package/src/BaseFilterItemBuilderObject.ts +91 -91
- package/src/BaseTable.ts +137 -137
- package/src/BaseTableColumnOptions.ts +8 -8
- package/src/CommandOperation.ts +32 -32
- package/src/GmailService.ts +22 -22
- package/src/IPOperation.ts +38 -38
- package/src/Meta.ts +36 -36
- package/src/OTPOperation.ts +94 -94
- package/src/RequestHeaderService.ts +27 -27
- package/src/SMTPService.ts +26 -26
- package/src/ServerToServerOperation.ts +23 -23
- package/src/Timer.ts +17 -17
- package/src/Validator.ts +15 -15
- package/src/index.ts +21 -21
package/dist/OTPOperation.js
CHANGED
|
@@ -55,13 +55,13 @@ class OTPOperation {
|
|
|
55
55
|
static onSafeVerify(x_otp, x_otp_time, x_otp_ver_tried_count, code, handler, errorHandler) {
|
|
56
56
|
return __awaiter(this, void 0, void 0, function* () {
|
|
57
57
|
if (!x_otp || !x_otp_time)
|
|
58
|
-
throw namirasoft_core_1.ErrorOperation.getHTTP(
|
|
58
|
+
throw namirasoft_core_1.ErrorOperation.getHTTP(410, "The OTP has not been generated yet. Please request first.");
|
|
59
59
|
if (x_otp_time != null)
|
|
60
60
|
x_otp_time = new Date(x_otp_time);
|
|
61
61
|
if (x_otp_time < namirasoft_core_1.TimeOperation.minutesAgo(OTPOperation.OTP_EXPIRE_TIME, new Date()))
|
|
62
|
-
namirasoft_core_1.ErrorOperation.throwHTTP(410, "The OTP code expired. Please request again.");
|
|
62
|
+
namirasoft_core_1.ErrorOperation.throwHTTP(410, "The OTP code has been expired. Please request again.");
|
|
63
63
|
if (x_otp_ver_tried_count > OTPOperation.OTP_VER_MAX_TRY_COUNT)
|
|
64
|
-
namirasoft_core_1.ErrorOperation.throwHTTP(429, "The
|
|
64
|
+
namirasoft_core_1.ErrorOperation.throwHTTP(429, "The number of allowed attempts has been exceeded. Please try again later.");
|
|
65
65
|
if (x_otp !== code) {
|
|
66
66
|
yield errorHandler();
|
|
67
67
|
namirasoft_core_1.ErrorOperation.throwHTTP(401, "Wrong code.");
|
package/dist/OTPOperation.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OTPOperation.js","sourceRoot":"","sources":["../src/OTPOperation.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAAgE;AAEhE,MAAa,YAAY;IASrB,MAAM,CAAC,QAAQ,CAAC,SAAiB,CAAC,EAAE,QAAgB,CAAC;QAEjD,IAAI,CAAC,MAAM;YACP,MAAM,GAAG,CAAC,CAAC;QACf,IAAI,CAAC,KAAK;YACN,KAAK,GAAG,CAAC,CAAC;QACd,IAAI,GAAG,GAAG,EAAE,CAAC;QACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE;YAC1B,GAAG,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;QACpD,IAAI,GAAG,GAAG,EAAE,CAAC;QACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE;YAC3B,GAAG,GAAG,GAAG,GAAG,EAAE,GAAG,GAAG,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC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EAClB,CAAC;gBACG,MAAM,YAAY,EAAE,CAAC;gBACrB,gCAAc,CAAC,SAAS,CAAC,GAAG,EAAE,aAAa,CAAC,CAAC;YACjD,CAAC;YAED,OAAO,MAAM,OAAO,CAAC;gBACjB,KAAK,EAAE,IAAI;gBACX,UAAU,EAAE,IAAI;gBAChB,UAAU,EAAE,IAAI;aACnB,CAAC,CAAC;QACP,CAAC;KAAA;;AA3FL,oCA4FC;AA1FiB,8BAAiB,GAAW,IAAI,CAAC;AACjC,4BAAe,GAAW,CAAC,CAAC;AAC5B,gCAAmB,GAAW,CAAC,CAAC;AAEhC,kCAAqB,GAAW,CAAC,CAAC;AAClC,kCAAqB,GAAW,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "namirasoft-node",
|
|
3
|
-
"title": "Namirasoft Node NPM Package",
|
|
4
|
-
"description": "Namira Software Corporation Node NPM Package",
|
|
5
|
-
"icon": "logo.png",
|
|
6
|
-
"logo": "https://static.namirasoft.com/image/namirasoft/site/logo/base.png",
|
|
7
|
-
"language": "ts",
|
|
8
|
-
"framework": "npm",
|
|
9
|
-
"application": "package",
|
|
10
|
-
"private": false,
|
|
11
|
-
"version": "1.4.
|
|
12
|
-
"author": "Amir Abolhasani",
|
|
13
|
-
"license": "MIT",
|
|
14
|
-
"main": "./dist/index.js",
|
|
15
|
-
"types": "./dist/index.d.ts",
|
|
16
|
-
"scripts": {
|
|
17
|
-
"build": ""
|
|
18
|
-
},
|
|
19
|
-
"dependencies": {
|
|
20
|
-
"@supercharge/request-ip": "^1.2.0",
|
|
21
|
-
"@types/cors": "^2.8.18",
|
|
22
|
-
"@types/express": "^5.0.2",
|
|
23
|
-
"@types/node": "^22.15.
|
|
24
|
-
"@types/node-cron": "^3.0.11",
|
|
25
|
-
"@types/nodemailer": "^6.4.17",
|
|
26
|
-
"@types/nodemailer-smtp-transport": "^2.7.8",
|
|
27
|
-
"@types/serve-index": "^1.9.4",
|
|
28
|
-
"@types/swagger-jsdoc": "^6.0.4",
|
|
29
|
-
"@types/swagger-ui-express": "^4.1.8",
|
|
30
|
-
"cors": "^2.8.5",
|
|
31
|
-
"express": "^5.1.0",
|
|
32
|
-
"joi": "^17.13.3",
|
|
33
|
-
"namirasoft-core": "^1.4.
|
|
34
|
-
"namirasoft-log": "^1.4.30",
|
|
35
|
-
"namirasoft-schema": "^1.4.25",
|
|
36
|
-
"node-cron": "^4.0.7",
|
|
37
|
-
"nodemailer": "^7.0.3",
|
|
38
|
-
"nodemailer-smtp-transport": "^2.7.4",
|
|
39
|
-
"request-ip": "^3.3.0",
|
|
40
|
-
"serve-index": "^1.9.1",
|
|
41
|
-
"swagger-jsdoc": "^6.2.8",
|
|
42
|
-
"swagger-ui-express": "^5.0.1"
|
|
43
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "namirasoft-node",
|
|
3
|
+
"title": "Namirasoft Node NPM Package",
|
|
4
|
+
"description": "Namira Software Corporation Node NPM Package",
|
|
5
|
+
"icon": "logo.png",
|
|
6
|
+
"logo": "https://static.namirasoft.com/image/namirasoft/site/logo/base.png",
|
|
7
|
+
"language": "ts",
|
|
8
|
+
"framework": "npm",
|
|
9
|
+
"application": "package",
|
|
10
|
+
"private": false,
|
|
11
|
+
"version": "1.4.102",
|
|
12
|
+
"author": "Amir Abolhasani",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": ""
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@supercharge/request-ip": "^1.2.0",
|
|
21
|
+
"@types/cors": "^2.8.18",
|
|
22
|
+
"@types/express": "^5.0.2",
|
|
23
|
+
"@types/node": "^22.15.24",
|
|
24
|
+
"@types/node-cron": "^3.0.11",
|
|
25
|
+
"@types/nodemailer": "^6.4.17",
|
|
26
|
+
"@types/nodemailer-smtp-transport": "^2.7.8",
|
|
27
|
+
"@types/serve-index": "^1.9.4",
|
|
28
|
+
"@types/swagger-jsdoc": "^6.0.4",
|
|
29
|
+
"@types/swagger-ui-express": "^4.1.8",
|
|
30
|
+
"cors": "^2.8.5",
|
|
31
|
+
"express": "^5.1.0",
|
|
32
|
+
"joi": "^17.13.3",
|
|
33
|
+
"namirasoft-core": "^1.4.76",
|
|
34
|
+
"namirasoft-log": "^1.4.30",
|
|
35
|
+
"namirasoft-schema": "^1.4.25",
|
|
36
|
+
"node-cron": "^4.0.7",
|
|
37
|
+
"nodemailer": "^7.0.3",
|
|
38
|
+
"nodemailer-smtp-transport": "^2.7.4",
|
|
39
|
+
"request-ip": "^3.3.0",
|
|
40
|
+
"serve-index": "^1.9.1",
|
|
41
|
+
"swagger-jsdoc": "^6.2.8",
|
|
42
|
+
"swagger-ui-express": "^5.0.1"
|
|
43
|
+
}
|
|
44
44
|
}
|
package/src/AnomalyDetector.ts
CHANGED
|
@@ -1,85 +1,85 @@
|
|
|
1
|
-
export class AnomalyDetector
|
|
2
|
-
{
|
|
3
|
-
static Main: AnomalyDetector = new AnomalyDetector();
|
|
4
|
-
// configuration
|
|
5
|
-
MAX_STORAGE_SIZE = 10000;
|
|
6
|
-
MAX_STORAGE_PER_IP = 25;
|
|
7
|
-
MIN_AVG_TIME = 200;
|
|
8
|
-
MIN_DATA_REQUIRE_FOR_AVG = 4;
|
|
9
|
-
MIN_AVG_TIME_PER_URL = 400;
|
|
10
|
-
MIN_DATA_REQUIRE_FOR_AVG_PER_URL = 3;
|
|
11
|
-
TIME_FADE_RATE = 0.9;
|
|
12
|
-
|
|
13
|
-
Storage: { [ip: string]: { url: string, time: number }[] } = {};
|
|
14
|
-
Storageـurl: { [ip: string]: { [url: string]: { time: number }[] } } = {};
|
|
15
|
-
Orders: string[] = [];
|
|
16
|
-
|
|
17
|
-
private weightedAverage(array: { time: number }[], fade_rate: number)
|
|
18
|
-
{
|
|
19
|
-
let sum = 0;
|
|
20
|
-
let sum_coef = 0;
|
|
21
|
-
for (let i = 1; i < array.length; i++)
|
|
22
|
-
{
|
|
23
|
-
let diff = array[i].time - array[i - 1].time;
|
|
24
|
-
let coef = Math.pow(fade_rate, (array.length - (i + 1)));
|
|
25
|
-
sum += diff * coef;
|
|
26
|
-
sum_coef += coef;
|
|
27
|
-
}
|
|
28
|
-
return sum / sum_coef;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
isAnomaly(ip: string, url: string): boolean
|
|
32
|
-
{
|
|
33
|
-
if (!this.Storage[ip])
|
|
34
|
-
{
|
|
35
|
-
this.Storage[ip] = [];
|
|
36
|
-
this.Storageـurl[ip] = {};
|
|
37
|
-
this.Orders.push(ip);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (this.Orders.length > this.MAX_STORAGE_SIZE)
|
|
41
|
-
{
|
|
42
|
-
let index = this.Orders.shift();
|
|
43
|
-
if (index)
|
|
44
|
-
{
|
|
45
|
-
delete this.Storage[index];
|
|
46
|
-
delete this.Storageـurl[index];
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (this.Storage[ip].length >= this.MAX_STORAGE_PER_IP)
|
|
51
|
-
{
|
|
52
|
-
let record = this.Storage[ip].shift(); // remove first element
|
|
53
|
-
if (record)
|
|
54
|
-
this.Storageـurl[ip][record.url].shift(); // remove first element of url array
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
this.Storage[ip].push({
|
|
58
|
-
url: url,
|
|
59
|
-
time: +new Date()
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
if (!this.Storageـurl[ip][url])
|
|
63
|
-
this.Storageـurl[ip][url] = [];
|
|
64
|
-
this.Storageـurl[ip][url].push({
|
|
65
|
-
time: +new Date()
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
// check conditions
|
|
69
|
-
// simple
|
|
70
|
-
if (this.Storage[ip].length >= this.MIN_DATA_REQUIRE_FOR_AVG)
|
|
71
|
-
{
|
|
72
|
-
let avg = this.weightedAverage(this.Storage[ip], this.TIME_FADE_RATE);
|
|
73
|
-
if (avg < this.MIN_AVG_TIME)
|
|
74
|
-
return true;
|
|
75
|
-
}
|
|
76
|
-
// by requst url
|
|
77
|
-
if (this.Storageـurl[ip][url].length >= this.MIN_DATA_REQUIRE_FOR_AVG_PER_URL)
|
|
78
|
-
{
|
|
79
|
-
let avg = this.weightedAverage(this.Storageـurl[ip][url], this.TIME_FADE_RATE);
|
|
80
|
-
if (avg < this.MIN_AVG_TIME_PER_URL)
|
|
81
|
-
return true;
|
|
82
|
-
}
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
1
|
+
export class AnomalyDetector
|
|
2
|
+
{
|
|
3
|
+
static Main: AnomalyDetector = new AnomalyDetector();
|
|
4
|
+
// configuration
|
|
5
|
+
MAX_STORAGE_SIZE = 10000;
|
|
6
|
+
MAX_STORAGE_PER_IP = 25;
|
|
7
|
+
MIN_AVG_TIME = 200;
|
|
8
|
+
MIN_DATA_REQUIRE_FOR_AVG = 4;
|
|
9
|
+
MIN_AVG_TIME_PER_URL = 400;
|
|
10
|
+
MIN_DATA_REQUIRE_FOR_AVG_PER_URL = 3;
|
|
11
|
+
TIME_FADE_RATE = 0.9;
|
|
12
|
+
|
|
13
|
+
Storage: { [ip: string]: { url: string, time: number }[] } = {};
|
|
14
|
+
Storageـurl: { [ip: string]: { [url: string]: { time: number }[] } } = {};
|
|
15
|
+
Orders: string[] = [];
|
|
16
|
+
|
|
17
|
+
private weightedAverage(array: { time: number }[], fade_rate: number)
|
|
18
|
+
{
|
|
19
|
+
let sum = 0;
|
|
20
|
+
let sum_coef = 0;
|
|
21
|
+
for (let i = 1; i < array.length; i++)
|
|
22
|
+
{
|
|
23
|
+
let diff = array[i].time - array[i - 1].time;
|
|
24
|
+
let coef = Math.pow(fade_rate, (array.length - (i + 1)));
|
|
25
|
+
sum += diff * coef;
|
|
26
|
+
sum_coef += coef;
|
|
27
|
+
}
|
|
28
|
+
return sum / sum_coef;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
isAnomaly(ip: string, url: string): boolean
|
|
32
|
+
{
|
|
33
|
+
if (!this.Storage[ip])
|
|
34
|
+
{
|
|
35
|
+
this.Storage[ip] = [];
|
|
36
|
+
this.Storageـurl[ip] = {};
|
|
37
|
+
this.Orders.push(ip);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (this.Orders.length > this.MAX_STORAGE_SIZE)
|
|
41
|
+
{
|
|
42
|
+
let index = this.Orders.shift();
|
|
43
|
+
if (index)
|
|
44
|
+
{
|
|
45
|
+
delete this.Storage[index];
|
|
46
|
+
delete this.Storageـurl[index];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (this.Storage[ip].length >= this.MAX_STORAGE_PER_IP)
|
|
51
|
+
{
|
|
52
|
+
let record = this.Storage[ip].shift(); // remove first element
|
|
53
|
+
if (record)
|
|
54
|
+
this.Storageـurl[ip][record.url].shift(); // remove first element of url array
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
this.Storage[ip].push({
|
|
58
|
+
url: url,
|
|
59
|
+
time: +new Date()
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
if (!this.Storageـurl[ip][url])
|
|
63
|
+
this.Storageـurl[ip][url] = [];
|
|
64
|
+
this.Storageـurl[ip][url].push({
|
|
65
|
+
time: +new Date()
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// check conditions
|
|
69
|
+
// simple
|
|
70
|
+
if (this.Storage[ip].length >= this.MIN_DATA_REQUIRE_FOR_AVG)
|
|
71
|
+
{
|
|
72
|
+
let avg = this.weightedAverage(this.Storage[ip], this.TIME_FADE_RATE);
|
|
73
|
+
if (avg < this.MIN_AVG_TIME)
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
// by requst url
|
|
77
|
+
if (this.Storageـurl[ip][url].length >= this.MIN_DATA_REQUIRE_FOR_AVG_PER_URL)
|
|
78
|
+
{
|
|
79
|
+
let avg = this.weightedAverage(this.Storageـurl[ip][url], this.TIME_FADE_RATE);
|
|
80
|
+
if (avg < this.MIN_AVG_TIME_PER_URL)
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
85
|
}
|