This repository was archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdnsAggCache.js
More file actions
154 lines (147 loc) · 4.65 KB
/
dnsAggCache.js
File metadata and controls
154 lines (147 loc) · 4.65 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* 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 DNSParserWrap from "./dnsParserWrap.js";
import DNSBlockOperation from "./dnsBlockOperation.js";
import { BlocklistFilter } from "@serverless-dns/blocklist-wrapper";
let debug = false;
export default class DNSAggCache {
constructor() {
this.dnsParser = new DNSParserWrap();
this.dnsBlockOperation = new DNSBlockOperation();
this.blocklistFilter = new BlocklistFilter();
this.wCache = null;
}
/**
* @param {*} param
* @param {*} param.userBlocklistInfo
* @param {*} param.request
* @param {*} param.requestBodyBuffer
* @param {*} param.isAggCacheReq
* @param {*} param.isDnsMsg
* @returns
*/
async RethinkModule(param) {
let response = {};
response.isException = false;
response.exceptionStack = "";
response.exceptionFrom = "";
response.data = null;
try {
if (!param.isDnsMsg) {
return response;
}
response.data = await this.aggCache(param);
} catch (e) {
response.isException = true;
response.exceptionStack = e.stack;
response.exceptionFrom = "DNSAggCache RethinkModule";
console.error("Error At : DNSAggCache -> RethinkModule");
console.error(e.stack);
}
return response;
}
async aggCache(param) {
let response = {};
response.reqDecodedDnsPacket = this.dnsParser.Decode(
param.requestBodyBuffer,
);
response.aggCacheResponse = {};
response.aggCacheResponse.type = "none";
if (param.isAggCacheReq && this.wCache === null) {
this.wCache = caches.default;
}
if (param.isAggCacheReq) {
const dn = (response.reqDecodedDnsPacket.questions.length > 0
? response.reqDecodedDnsPacket.questions[0].name
: "").trim().toLowerCase() +
":" + response.reqDecodedDnsPacket.questions[0].type;
let cacheResponse = await getCacheapi(this.wCache, param.request.url, dn);
if (debug) {
console.log("Cache Api Response");
console.log(cacheResponse);
}
if (cacheResponse) {
response.aggCacheResponse = await parseCacheapiResponse(
cacheResponse,
this.dnsParser,
this.dnsBlockOperation,
this.blocklistFilter,
param.userBlocklistInfo,
response.reqDecodedDnsPacket,
);
}
}
return response;
}
}
async function parseCacheapiResponse(
cacheResponse,
dnsParser,
dnsBlockOperation,
blocklistFilter,
userBlocklistInfo,
reqDecodedDnsPacket,
) {
let response = {};
response.type = "none";
response.data = {};
let metaData = JSON.parse(cacheResponse.headers.get("x-rethink-metadata"));
if (debug) {
console.log("Response Found at CacheApi");
console.log(JSON.stringify(metaData));
}
//check whether incoming request should be blocked by blocklist filter
if (
(reqDecodedDnsPacket.questions[0].type == "A" ||
reqDecodedDnsPacket.questions[0].type == "AAAA" ||
reqDecodedDnsPacket.questions[0].type == "CNAME" ||
reqDecodedDnsPacket.questions[0].type == "HTTPS" ||
reqDecodedDnsPacket.questions[0].type == "SVCB") &&
metaData.blocklistInfo &&
userBlocklistInfo.userBlocklistFlagUint !== ""
) {
metaData.blocklistInfo = new Map(Object.entries(metaData.blocklistInfo));
let blockResponse = dnsBlockOperation.checkDomainBlocking(
userBlocklistInfo.userBlocklistFlagUint,
userBlocklistInfo.userServiceListUint,
userBlocklistInfo.flagVersion,
metaData.blocklistInfo,
blocklistFilter,
reqDecodedDnsPacket.questions[0].name.trim().toLowerCase(),
);
if (blockResponse.isBlocked) {
response.type = "blocked";
response.data = blockResponse;
return response;
}
}
if (metaData.bodyUsed) {
const now = Date.now();
if (now <= (metaData.ttlEndTime)) {
response.type = "response";
response.data.decodedDnsPacket = dnsParser.Decode(
await cacheResponse.arrayBuffer(),
);
const outttl = Math.max(
Math.floor((metaData.ttlEndTime - now) / 1000),
1,
); // to verify ttl is not set to 0sec
for (let answer of response.data.decodedDnsPacket.answers) {
answer.ttl = outttl;
}
response.data.bodyBuffer = dnsParser.Encode(
response.data.decodedDnsPacket,
);
}
}
return response;
}
async function getCacheapi(wCache, reqUrl, key) {
let wCacheUrl = new URL((new URL(reqUrl)).origin + "/" + key);
return await wCache.match(wCacheUrl);
}