callback.js
2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const MODULE_NAME = 'DUMPER.CALLBACK';
const fs = require('fs');
const fsPromise = fs.promises;
const moment = require('moment');
const config = require('komodo-sdk/config');
const logger = require('tektrans-logger');
const path = require('path');
const mkdirIfNotExists = require('../mkdir-if-not-exists');
require('./init');
const vars = require('./vars');
const { baseDumpDir } = vars;
const baseCallbackDumpDir = path.join(baseDumpDir, 'callback');
if (!fs.existsSync(baseCallbackDumpDir)) {
fs.mkdirSync(baseCallbackDumpDir, { recursive: true });
}
const doNotDump = !config.partner.dump_callback;
module.exports = async (xid, req) => {
if (!req) return;
if (doNotDump) return;
logger.verbose(`${MODULE_NAME} E244F70E: Dumping`, {
xid,
});
const dumpDir = path.join(
baseCallbackDumpDir,
moment().format('YYYY-MM-DD'),
);
await mkdirIfNotExists(xid, dumpDir);
const dumpFileName = path.join(
dumpDir,
`callback_${xid}.txt`,
);
const data = `-------- BEGIN OF CALLBACK DUMP --------
${moment().format('YYYY-MM-DD HH:mm:ss')}
XID: ${xid}
REMOTE ADDR: ${req.ip}
USER AGENT: ${req.get('user-agent')}
CONTENT-TYPE: ${req.get('content-type')}
HTTP METHOD: ${req.method}
URL: ${req.url}
QUERY STRING: ${req.query && JSON.stringify(req.query, null, 2)}
BODY:
${typeof req.body === 'string' ? req.body : JSON.stringify(req.body, null, 2)}
-------- END OF CALLBACK DUMP --------
`;
try {
logger.verbose(`${MODULE_NAME} ED80B898: Writing file`, {
xid,
dumpFileName,
});
await fsPromise.writeFile(dumpFileName, data);
logger.verbose(`${MODULE_NAME} 362AEFC6: Writing last dump file`, { xid });
await fsPromise.writeFile(
path.join(baseDumpDir, 'last-callback.txt'),
data,
);
} catch (e) {
logger.warn(`${MODULE_NAME} 925869BC: Exception on writing dump file`, {
xid,
eCode: e.code,
eMessage: e.message,
});
}
};