serverpreconfigured 2.2.2 → 2.2.4
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/logs/logs.d.ts +4 -0
- package/dist/logs/logs.js +22 -3
- package/package.json +1 -1
- package/src/logs/logs.ts +24 -3
package/dist/logs/logs.d.ts
CHANGED
|
@@ -10,11 +10,15 @@ export interface SaveLogOptions {
|
|
|
10
10
|
userId?: number;
|
|
11
11
|
data: string;
|
|
12
12
|
severity: LogSeverity;
|
|
13
|
+
addPath?: string;
|
|
14
|
+
filePrefix?: string;
|
|
13
15
|
penTestSuspcion?: boolean;
|
|
14
16
|
req?: Request;
|
|
15
17
|
ip?: string;
|
|
16
18
|
url?: string;
|
|
17
19
|
}
|
|
20
|
+
export declare function stringfyError(err: any): string;
|
|
21
|
+
export declare function getIpFromRequest(req: Request): string;
|
|
18
22
|
export declare function saveInternalErrorLog(req: Request, error: any, options?: {
|
|
19
23
|
penTestSuspcion?: boolean;
|
|
20
24
|
severity?: LogSeverity;
|
package/dist/logs/logs.js
CHANGED
|
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.saveLog = exports.saveInternalErrorLog = exports.LogSeverity = exports.BASE_LOG_PATH = void 0;
|
|
15
|
+
exports.saveLog = exports.saveInternalErrorLog = exports.getIpFromRequest = exports.stringfyError = exports.LogSeverity = exports.BASE_LOG_PATH = void 0;
|
|
16
16
|
const fs_1 = __importDefault(require("fs"));
|
|
17
17
|
const path_1 = __importDefault(require("path"));
|
|
18
18
|
exports.BASE_LOG_PATH = './logs';
|
|
@@ -41,11 +41,24 @@ function stringfyError(err) {
|
|
|
41
41
|
}
|
|
42
42
|
return retData;
|
|
43
43
|
}
|
|
44
|
+
exports.stringfyError = stringfyError;
|
|
45
|
+
function getIpFromRequest(req) {
|
|
46
|
+
//@
|
|
47
|
+
let ips = (req.headers['cf-connecting-ip'] ||
|
|
48
|
+
req.headers['x-real-ip'] ||
|
|
49
|
+
req.headers['x-forwarded-for'] ||
|
|
50
|
+
req.ip || '');
|
|
51
|
+
if (typeof (ips) == 'string') {
|
|
52
|
+
ips = ips.split(',');
|
|
53
|
+
}
|
|
54
|
+
return ips[0].trim();
|
|
55
|
+
}
|
|
56
|
+
exports.getIpFromRequest = getIpFromRequest;
|
|
44
57
|
function saveInternalErrorLog(req, error, options) {
|
|
45
58
|
var _a;
|
|
46
59
|
return __awaiter(this, void 0, void 0, function* () {
|
|
47
60
|
try {
|
|
48
|
-
const ip = req
|
|
61
|
+
const ip = getIpFromRequest(req);
|
|
49
62
|
const url = req.originalUrl;
|
|
50
63
|
//@ts-ignore
|
|
51
64
|
const userId = (_a = req.user) === null || _a === void 0 ? void 0 : _a.id;
|
|
@@ -70,6 +83,12 @@ function saveLog(options) {
|
|
|
70
83
|
fs_1.default.mkdirSync(exports.BASE_LOG_PATH);
|
|
71
84
|
}
|
|
72
85
|
let basePath = exports.BASE_LOG_PATH;
|
|
86
|
+
if (options.addPath) {
|
|
87
|
+
basePath = path_1.default.join(basePath, options.addPath);
|
|
88
|
+
if (!fs_1.default.existsSync(basePath)) {
|
|
89
|
+
fs_1.default.mkdirSync(basePath);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
73
92
|
if (options.userId) {
|
|
74
93
|
basePath = path_1.default.join(basePath, options.userId.toString());
|
|
75
94
|
}
|
|
@@ -79,7 +98,7 @@ function saveLog(options) {
|
|
|
79
98
|
if (!fs_1.default.existsSync(basePath)) {
|
|
80
99
|
fs_1.default.mkdirSync(basePath);
|
|
81
100
|
}
|
|
82
|
-
let fileName = path_1.default.join(basePath, `${getDateString(new Date())}.csv`);
|
|
101
|
+
let fileName = path_1.default.join(basePath, `${options.filePrefix ? options.filePrefix + '_' : ""}${getDateString(new Date())}.csv`);
|
|
83
102
|
let data = "";
|
|
84
103
|
if (fs_1.default.existsSync(fileName)) {
|
|
85
104
|
data = fs_1.default.readFileSync(fileName).toString() + "\n";
|
package/package.json
CHANGED
package/src/logs/logs.ts
CHANGED
|
@@ -12,6 +12,8 @@ export interface SaveLogOptions{
|
|
|
12
12
|
userId?:number;
|
|
13
13
|
data:string;
|
|
14
14
|
severity:LogSeverity;
|
|
15
|
+
addPath?:string;
|
|
16
|
+
filePrefix?:string;
|
|
15
17
|
penTestSuspcion?:boolean;
|
|
16
18
|
req?:Request;
|
|
17
19
|
ip?:string;
|
|
@@ -19,7 +21,7 @@ export interface SaveLogOptions{
|
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
|
|
22
|
-
function stringfyError(err:any):string{
|
|
24
|
+
export function stringfyError(err:any):string{
|
|
23
25
|
const type=typeof(err);
|
|
24
26
|
if(type!=='object')
|
|
25
27
|
return err.toString();
|
|
@@ -36,9 +38,22 @@ function stringfyError(err:any):string{
|
|
|
36
38
|
}
|
|
37
39
|
return retData;
|
|
38
40
|
}
|
|
41
|
+
export function getIpFromRequest(req:Request){
|
|
42
|
+
//@
|
|
43
|
+
let ips = (
|
|
44
|
+
req.headers['cf-connecting-ip'] ||
|
|
45
|
+
req.headers['x-real-ip'] ||
|
|
46
|
+
req.headers['x-forwarded-for'] ||
|
|
47
|
+
req.ip || ''
|
|
48
|
+
);
|
|
49
|
+
if(typeof(ips)=='string'){
|
|
50
|
+
ips=ips.split(',');
|
|
51
|
+
}
|
|
52
|
+
return ips[0].trim();
|
|
53
|
+
}
|
|
39
54
|
export async function saveInternalErrorLog(req:Request,error:any,options?:{penTestSuspcion?:boolean,severity?:LogSeverity}){
|
|
40
55
|
try{
|
|
41
|
-
const ip=req
|
|
56
|
+
const ip=getIpFromRequest(req);
|
|
42
57
|
const url=req.originalUrl;
|
|
43
58
|
//@ts-ignore
|
|
44
59
|
const userId=req.user?.id;
|
|
@@ -60,6 +75,12 @@ export function saveLog(options:SaveLogOptions){
|
|
|
60
75
|
fs.mkdirSync(BASE_LOG_PATH);
|
|
61
76
|
}
|
|
62
77
|
let basePath=BASE_LOG_PATH;
|
|
78
|
+
if(options.addPath){
|
|
79
|
+
basePath=path.join(basePath,options.addPath);
|
|
80
|
+
if(!fs.existsSync(basePath)){
|
|
81
|
+
fs.mkdirSync(basePath);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
63
84
|
if(options.userId){
|
|
64
85
|
basePath=path.join(basePath,options.userId.toString());
|
|
65
86
|
}else{
|
|
@@ -68,7 +89,7 @@ export function saveLog(options:SaveLogOptions){
|
|
|
68
89
|
if(!fs.existsSync(basePath)){
|
|
69
90
|
fs.mkdirSync(basePath);
|
|
70
91
|
}
|
|
71
|
-
let fileName=path.join(basePath,`${getDateString(new Date())}.csv`);
|
|
92
|
+
let fileName=path.join(basePath,`${options.filePrefix?options.filePrefix+'_':""}${getDateString(new Date())}.csv`);
|
|
72
93
|
let data="";
|
|
73
94
|
if(fs.existsSync(fileName)){
|
|
74
95
|
data=fs.readFileSync(fileName).toString()+"\n";
|