forked from serverless-dns/serverless-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnsBlock.js
75 lines (61 loc) · 1.98 KB
/
dnsBlock.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
/*
* Copyright (c) 2021 RethinkDNS and its authors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import * as rdnsutil from "../dnsblockutil.js";
import * as dnsutil from "../../commons/dnsutil.js";
export class DnsBlocker {
constructor() {
this.log = log.withTags("DnsBlocker");
}
blockQuestion(rxid, req, blockInfo) {
const dnsPacket = req.dnsPacket;
const stamps = req.stamps;
if (!stamps) {
this.log.d(rxid, "q: no stamp");
return req;
}
if (!rdnsutil.hasBlockstamp(blockInfo)) {
this.log.d(rxid, "q: no user-set blockstamp");
return req;
}
if (!dnsutil.isQueryBlockable(dnsPacket)) {
this.log.d(rxid, "not a blockable dns-query");
return req;
}
const domains = dnsutil.extractDomains(dnsPacket);
const bres = this.block(domains, blockInfo, stamps);
return rdnsutil.copyOnlyBlockProperties(req, bres);
}
blockAnswer(rxid, res, blockInfo) {
const dnsPacket = res.dnsPacket;
const stamps = res.stamps;
// dnsPacket is null when cache only has metadata
if (!stamps || !dnsutil.hasAnswers(dnsPacket)) {
this.log.d(rxid, "ans: no stamp / dns-packet");
return res;
}
if (!rdnsutil.hasBlockstamp(blockInfo)) {
this.log.d(rxid, "ans: no user-set blockstamp");
return res;
}
if (!dnsutil.isAnswerBlockable(dnsPacket)) {
this.log.d(rxid, "ans not cloaked with cname/https/svcb");
return res;
}
const domains = dnsutil.extractDomains(dnsPacket);
const bres = this.block(domains, blockInfo, stamps);
return rdnsutil.copyOnlyBlockProperties(res, bres);
}
block(names, blockInfo, blockstamps) {
let r = rdnsutil.rdnsNoBlockResponse();
for (const n of names) {
r = rdnsutil.doBlock(n, blockInfo, blockstamps);
if (r.isBlocked) break;
}
return r;
}
}