namirasoft-node 1.3.74 → 1.3.75

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.
@@ -0,0 +1,4 @@
1
+ export declare class OTPOperation {
2
+ static run(command: string, cwd: string | undefined): Promise<void>;
3
+ static runSync(command: string, cwd: string | undefined): Promise<any>;
4
+ }
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.OTPOperation = void 0;
13
+ const { exec, execSync } = require('child_process');
14
+ class OTPOperation {
15
+ static run(command, cwd) {
16
+ return __awaiter(this, void 0, void 0, function* () {
17
+ yield exec(command, { cwd }, (error, stdout, stderr) => {
18
+ if (error) {
19
+ console.error(`error: ${error.message}`);
20
+ return;
21
+ }
22
+ if (stderr) {
23
+ console.error(`stderr: ${stderr}`);
24
+ return;
25
+ }
26
+ console.log(`stdout:\n${stdout}`);
27
+ });
28
+ });
29
+ }
30
+ static runSync(command, cwd) {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ try {
33
+ return yield execSync(command, { cwd, encoding: 'utf-8' });
34
+ }
35
+ catch (error) {
36
+ console.error(error.stdout);
37
+ throw error;
38
+ }
39
+ });
40
+ }
41
+ }
42
+ exports.OTPOperation = OTPOperation;
43
+ //# sourceMappingURL=CMDOperation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CMDOperation.js","sourceRoot":"","sources":["../src/CMDOperation.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;AACpD,MAAa,YAAY;IAErB,MAAM,CAAO,GAAG,CAAC,OAAe,EAAE,GAAuB;;YAErD,MAAM,IAAI,CAAC,OAAO,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE;gBAEnD,IAAI,KAAK,EACT;oBACI,OAAO,CAAC,KAAK,CAAC,UAAU,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;oBACzC,OAAO;iBACV;gBACD,IAAI,MAAM,EACV;oBACI,OAAO,CAAC,KAAK,CAAC,WAAW,MAAM,EAAE,CAAC,CAAC;oBACnC,OAAO;iBACV;gBACD,OAAO,CAAC,GAAG,CAAC,YAAY,MAAM,EAAE,CAAC,CAAC;YACtC,CAAC,CAAC,CAAC;QACP,CAAC;KAAA;IACD,MAAM,CAAO,OAAO,CAAC,OAAe,EAAE,GAAuB;;YAEzD,IACA;gBACI,OAAO,MAAM,QAAQ,CAAC,OAAO,EAAE,EAAE,GAAG,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;aAC9D;YAAC,OAAO,KAAU,EACnB;gBACI,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;gBAC5B,MAAM,KAAK,CAAC;aACf;QACL,CAAC;KAAA;CACJ;AA9BD,oCA8BC"}
@@ -0,0 +1,16 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { AttachmentLike } from "nodemailer/lib/mailer";
4
+ import { Readable } from "stream";
5
+ export declare class EmailService {
6
+ host: string;
7
+ username: string;
8
+ username_from: string;
9
+ password: string;
10
+ error_title: string;
11
+ error_recipients: string;
12
+ constructor(host: string, username: string, username_from: string, password: string, error_title: string, error_recipients: string);
13
+ sendExeption(error: Error, meta: any, callback?: (err: Error | null, info: any) => void): void;
14
+ sendError(title: string, message: string, callback?: (err: Error | null, info: any) => void): void;
15
+ send(to: string, subject: string, text: string, html?: string | Buffer | Readable | AttachmentLike | undefined, callback?: (err: Error | null, info: any) => void): void;
16
+ }
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.EmailService = void 0;
7
+ const nodemailer_1 = __importDefault(require("nodemailer"));
8
+ const nodemailer_smtp_transport_1 = __importDefault(require("nodemailer-smtp-transport"));
9
+ class EmailService {
10
+ constructor(host, username, username_from, password, error_title, error_recipients) {
11
+ this.host = host;
12
+ this.username = username;
13
+ this.username_from = username_from;
14
+ this.password = password;
15
+ this.error_title = error_title;
16
+ this.error_recipients = error_recipients;
17
+ }
18
+ sendExeption(error, meta, callback) {
19
+ let title = error.message;
20
+ let message = title;
21
+ if (meta)
22
+ message += "\r\n" + JSON.stringify(meta);
23
+ message += "\r\n" + error.stack;
24
+ this.sendError(title, message, callback);
25
+ }
26
+ sendError(title, message, callback) {
27
+ if (!title)
28
+ title = '';
29
+ let toks = this.error_recipients.split(',');
30
+ for (let i = 0; i < toks.length; i++) {
31
+ const email = toks[i];
32
+ this.send(email, this.error_title + " - " + title, message, undefined, callback);
33
+ }
34
+ }
35
+ send(to, subject, text, html, callback) {
36
+ if (!this.username)
37
+ return;
38
+ if (!this.password)
39
+ return;
40
+ let transform = {};
41
+ if (this.host === 'gmail')
42
+ transform = (0, nodemailer_smtp_transport_1.default)({
43
+ service: 'gmail',
44
+ host: 'smtp.gmail.com',
45
+ auth: {
46
+ user: this.username,
47
+ pass: this.password
48
+ }
49
+ });
50
+ else
51
+ transform = {
52
+ host: this.host,
53
+ port: 465,
54
+ secure: true,
55
+ auth: {
56
+ user: this.username,
57
+ pass: this.password
58
+ }
59
+ };
60
+ let transporter = nodemailer_1.default.createTransport(transform);
61
+ let mailOptions = {
62
+ from: this.username_from,
63
+ to,
64
+ subject,
65
+ text,
66
+ html
67
+ };
68
+ if (html)
69
+ mailOptions.html = html;
70
+ transporter.sendMail(mailOptions, function (error, info) {
71
+ if (callback)
72
+ callback(error, info);
73
+ else {
74
+ if (error)
75
+ console.log(error);
76
+ }
77
+ });
78
+ }
79
+ }
80
+ exports.EmailService = EmailService;
81
+ //# sourceMappingURL=EmailService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EmailService.js","sourceRoot":"","sources":["../src/EmailService.ts"],"names":[],"mappings":";;;;;;AAAA,4DAAoC;AACpC,0FAAsD;AAItD,MAAa,YAAY;IAQrB,YAAY,IAAY,EAAE,QAAgB,EAAE,aAAqB,EAAE,QAAgB,EAAE,WAAmB,EAAE,gBAAwB;QAE9H,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;IAC7C,CAAC;IACD,YAAY,CAAC,KAAY,EAAE,IAAS,EAAE,QAAiD;QAEnF,IAAI,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC;QAC1B,IAAI,OAAO,GAAG,KAAK,CAAC;QACpB,IAAI,IAAI;YACJ,OAAO,IAAI,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;QAC7C,OAAO,IAAI,MAAM,GAAG,KAAK,CAAC,KAAK,CAAC;QAChC,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;IAC7C,CAAC;IACD,SAAS,CAAC,KAAa,EAAE,OAAe,EAAE,QAAiD;QAEvF,IAAI,CAAC,KAAK;YACN,KAAK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AI;YACJ,IAAI;SACP,CAAC;QACF,IAAI,IAAI;YACJ,WAAW,CAAC,IAAI,GAAG,IAAI,CAAC;QAE5B,WAAW,CAAC,QAAQ,CAAC,WAAW,EAAE,UAAU,KAAK,EAAE,IAAI;YAEnD,IAAI,QAAQ;gBACR,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;iBAE1B;gBACI,IAAI,KAAK;oBACL,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;aAC1B;QACL,CAAC,CAAC,CAAC;IACP,CAAC;CACJ;AA3FD,oCA2FC"}
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "framework": "npm",
9
9
  "application": "package",
10
10
  "private": false,
11
- "version": "1.3.74",
11
+ "version": "1.3.75",
12
12
  "author": "Amir Abolhasani",
13
13
  "license": "MIT",
14
14
  "main": "./dist/index.js",
@@ -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
  }