This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuserOperation.js
109 lines (102 loc) · 3.38 KB
/
userOperation.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (c) 2020 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 { LocalCache as LocalCache } from "@serverless-dns/cache-wrapper";
import { BlocklistFilter } from "@serverless-dns/blocklist-wrapper";
export class UserOperation {
constructor() {
this.userConfigCache = false;
this.blocklistFilter = new BlocklistFilter();
}
/**
* @param {*} param
* @param {*} param.dnsResolverUrl
* @param {*} param.request
* @param {*} param.isDnsMsg
* @returns
*/
async RethinkModule(param) {
return this.loadUser(param);
}
loadUser(param) {
let response = {};
response.isException = false;
response.exceptionStack = "";
response.exceptionFrom = "";
response.data = {};
response.data.userBlocklistInfo = {};
response.data.userBlocklistInfo.dnsResolverUrl = "";
try {
if (!param.isDnsMsg) {
return response;
}
if (!this.userConfigCache) {
this.userConfigCache = new LocalCache(
"User-Config-Cache",
1000,
);
}
let userBlocklistInfo = {};
userBlocklistInfo.from = "Cache";
let blocklistFlag = getBlocklistFlag(param.request.url);
let currentUser = this.userConfigCache.Get(blocklistFlag);
if (!currentUser) {
currentUser = {};
currentUser.userBlocklistFlagUint = "";
currentUser.flagVersion = 0;
currentUser.userServiceListUint = false;
let response = this.blocklistFilter.unstamp(blocklistFlag);
currentUser.userBlocklistFlagUint = response.userBlocklistFlagUint;
currentUser.flagVersion = response.flagVersion;
if (currentUser.userBlocklistFlagUint !== "") {
currentUser.userServiceListUint = this.blocklistFilter
.flagIntersection(
currentUser.userBlocklistFlagUint,
this.blocklistFilter.wildCardUint,
);
} else {
blocklistFlag = "";
}
userBlocklistInfo.from = "Generated";
this.userConfigCache.Put(blocklistFlag, currentUser);
}
userBlocklistInfo.userBlocklistFlagUint =
currentUser.userBlocklistFlagUint;
userBlocklistInfo.flagVersion = currentUser.flagVersion;
userBlocklistInfo.userServiceListUint = currentUser.userServiceListUint;
response.data.userBlocklistInfo = userBlocklistInfo;
response.data.dnsResolverUrl = param.dnsResolverUrl;
} catch (e) {
response.isException = true;
response.exceptionStack = e.stack;
response.exceptionFrom = "UserOperation loadUser";
console.error("Error At : UserOperation -> loadUser");
console.error(e.stack);
}
return response;
}
}
/**
* Get the blocklist flag from `Request` URL
* DNS over TLS flag from SNI should be rewritten to `url`'s pathname
* @param {String} url - Request URL string
* @returns
*/
function getBlocklistFlag(url) {
let blocklistFlag = "";
let reqUrl = new URL(url);
// Check if pathname has `/dns-query`
let tmpsplit = reqUrl.pathname.split("/");
if (tmpsplit.length > 1) {
if (tmpsplit[1].toLowerCase() == "dns-query") {
blocklistFlag = tmpsplit[2] || "";
} else {
blocklistFlag = tmpsplit[1] || "";
}
}
return blocklistFlag;
}