-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
60 lines (53 loc) · 1.61 KB
/
index.ts
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
import * as fs from 'fs';
import * as xml2js from 'xml2js';
import * as dns from 'dns';
import * as glob from 'glob';
let xmlFileNameOrPattern = process.argv[2];
if (!xmlFileNameOrPattern) {
xmlFileNameOrPattern = "*.xml";
}
function handleRecord(record: any) {
const row = record.row[0];
const policy_evaluated = row.policy_evaluated[0];
const dkim = policy_evaluated.dkim[0];
const spf = policy_evaluated.spf[0];
if (dkim === 'fail' && spf === 'fail') {
const source_ip = row.source_ip[0];
handleFail(source_ip);
}
}
function handleFail(source_ip: string) {
dns.reverse(source_ip, (err, hostnames) => {
if (err) {
console.warn('could not find hostname for: ', source_ip);
} else {
console.log('hostnames: ', hostnames);
}
});
}
function parseFile(fileName: string) {
const parser = new xml2js.Parser();
fs.readFile(fileName, function (err, data) {
if (err) {
throw `Could not read file "${fileName}"`;
}
parser.parseString(<any>data, function (err: any, parsedXml: any) {
const records: any[] = parsedXml.feedback.record;
for (const record of records) {
handleRecord(record);
}
console.log('Done');
});
});
}
function findFile(filenamePattern: string) {
glob(filenamePattern, {}, (er, files) => {
if (er) {
throw `Could not find file matching "${filenamePattern}"`;
}
for (const fileName of files) {
parseFile(fileName);
}
});
}
findFile(xmlFileNameOrPattern);