-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathdomain.ts
66 lines (48 loc) · 1.94 KB
/
domain.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
61
62
63
64
65
import { writeFile, readFile } from 'node:fs/promises';
import path from 'node:path';
import type { CustomHelpers, ErrorReport } from 'joi';
import got from 'got';
import validator from 'validator';
import { scopedLogger } from '../logger.js';
const logger = scopedLogger('malware-domain');
export const sourceList = [
'https://phishing.army/download/phishing_army_blocklist.txt',
'https://osint.digitalside.it/Threat-Intel/lists/latestdomains.txt',
'https://urlhaus.abuse.ch/downloads/hostfile/',
];
export const domainListPath = path.join(path.resolve(), 'data/DOMAIN_BLACKLIST.json');
let domainListArray = new Set<string>();
const isFulfilled = <T>(input: PromiseSettledResult<T>): input is PromiseFulfilledResult<T> => input.status === 'fulfilled';
export const query = async (url: string): Promise<string[]> => {
const { body } = await got(url, {
timeout: { request: 5000 },
});
const result = body.split(/r?\n?\s+/).filter(l => validator.isFQDN(l));
return result;
};
export const updateList = async (): Promise<void> => {
const result = await Promise.allSettled(sourceList.map(source => query(source)));
const list = [ ...new Set(result.flatMap((r) => {
if (isFulfilled(r)) {
return r.value;
}
logger.error('Error in domain updateList()', r.reason);
return [];
})) ].map(d => d.toLowerCase());
await writeFile(domainListPath, JSON.stringify(list), { encoding: 'utf8' });
};
export const populateMemList = async (): Promise<void> => {
const data = await readFile(domainListPath, 'utf8');
domainListArray = new Set(JSON.parse(data) as string[]);
};
export const validate = (target: string): boolean => !domainListArray.has(target.toLowerCase());
export const joiValidate = (value: string, helpers?: CustomHelpers): string | ErrorReport | Error => {
const isValid = validate(value);
if (!isValid) {
if (helpers) {
return helpers.error('domain.blacklisted');
}
throw new Error('domain.blacklisted');
}
return String(value);
};