-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
88 lines (75 loc) · 2.21 KB
/
index.js
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
86
87
88
import { resolveTxt } from "node:dns/promises";
import { readFile, writeFile, mkdir, open } from "node:fs/promises";
async function getTxtRecords(hostname) {
const records = await resolveTxt(hostname);
return records.map((recordParts) => recordParts.join(""));
}
async function getDmarcRecord(hostname) {
try {
const records = await getTxtRecords("_dmarc." + hostname);
const dmarcRecord = records.find((record) => record.includes("v=DMARC"));
return dmarcRecord ? parseDmarcRecord(dmarcRecord) : undefined;
} catch (error) {
return undefined;
}
}
function parseDmarcRecord(stringRecord) {
return {
v: /v=(\w+)/.exec(stringRecord)?.[1],
p: /p=(\w+)/.exec(stringRecord)?.[1],
};
}
function policyHeader(policy) {
switch (policy) {
case "none":
return "None ☹️";
case "quarantine":
return "Quarantine 🤔";
case "reject":
return "Reject ✅";
default:
return "DMARC manglar! ❌";
}
}
function getTimeElement() {
const now = new Date();
return `<time datetime="${now.toISOString()}">${now.toLocaleString(
"no",
)}</time>`;
}
async function main() {
const dmarcs = [];
const domainsFile = await open("./domains.txt");
for await (const domain of domainsFile.readLines()) {
const dmarc = await getDmarcRecord(domain);
dmarcs.push({
domain,
...(dmarc || {}),
});
}
const htmlContent = [undefined, "none", "quarantine", "reject"].flatMap(
(policy) => {
return [
"<div>",
`<h2>${policyHeader(policy)}</h2>`,
"<ul>",
...dmarcs
.filter(({ p }) => policy === p)
.sort((a, b) => a.domain.localeCompare(b.domain))
.map(
({ domain }) =>
`<li><a href="https://${domain}" target="_blank">${domain}</a></li>`,
),
"</ul>",
"</div>",
];
},
);
const htmlTemplate = await readFile("./src/index.html", "utf-8");
const populatedTemplate = htmlTemplate
.replace("{{CONTENT}}", htmlContent.join("\n"))
.replace("{{UPDATED_AT}}", getTimeElement());
await mkdir("./dist", { recursive: true });
await writeFile("./dist/index.html", populatedTemplate, "utf-8");
}
main();