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
/
cc.js
206 lines (197 loc) · 6.37 KB
/
cc.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* 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/.
*/
let debug = false;
export class CommandControl {
constructor() {
this.latestTimestamp = "";
}
/**
* @param {Object} param
* @param {Request} param.request
* @param {*} param.blocklistFilter
* @param {*} param.latestTimestamp
* @param {*} param.isDnsMsg
* @returns
*/
async RethinkModule(param) {
if(debug) console.log("In CommandControl")
this.latestTimestamp = param.latestTimestamp;
let response = {};
response.isException = false;
response.exceptionStack = "";
response.exceptionFrom = "";
response.data = {};
response.data.stopProcessing = false;
if (param.request.method === "GET") {
response = this.commandOperation(
param.request.url,
param.blocklistFilter,
param.isDnsMsg
);
}
return response;
}
commandOperation(url, blocklistFilter, isDnsMsg) {
let response = {};
response.isException = false;
response.exceptionStack = "";
response.exceptionFrom = "";
response.data = {};
try {
response.data.stopProcessing = true;
response.data.httpResponse;
const reqUrl = new URL(url);
const queryString = reqUrl.searchParams;
const pathSplit = reqUrl.pathname.split("/");
let command = pathSplit[1];
if (!command) {
const d = reqUrl.host.split("."); // ex: xyz.max.rethinkdns.com
command = (d.length > 3 && d[2] === "rethinkdns") ? d[0] : ""
}
const weburl = command == ""
? "https://rethinkdns.com/configure"
: "https://rethinkdns.com/configure?s=added#" + command;
if (command == "listtob64") {
response.data.httpResponse = listToB64(
queryString,
blocklistFilter,
);
} else if (command == "b64tolist") {
response.data.httpResponse = b64ToList(
queryString,
blocklistFilter,
);
} else if (command == "dntolist") {
response.data.httpResponse = domainNameToList(
queryString,
blocklistFilter,
this.latestTimestamp,
);
} else if (command == "dntouint") {
response.data.httpResponse = domainNameToUint(
queryString,
blocklistFilter,
);
} else if (command == "config" || command == "configure") {
let b64UserFlag = "";
if (pathSplit.length >= 3) {
b64UserFlag = pathSplit[2];
}
response.data.httpResponse = configRedirect(
b64UserFlag,
reqUrl.origin,
this.latestTimestamp
);
} else if (!isDnsMsg) {
response.data.httpResponse = Response.redirect(weburl, 302);
} else if (queryString.has("dns")) {
response.data.stopProcessing = false;
} else {
response.data.httpResponse = new Response(null, {
status: 400,
statusText: "Bad Request",
});
}
} catch (e) {
response.isException = true;
response.exceptionStack = e.stack;
response.exceptionFrom = "CommandControl commandOperation";
response.data.httpResponse = new Response(
JSON.stringify(response.exceptionStack),
);
response.data.httpResponse.headers.set(
"Content-Type",
"application/json",
);
}
return response;
}
}
function configRedirect(b64UserFlag, requestUrlOrigin, latestTimestamp) {
let base = "https://rethinkdns.com/configure";
let query = "?v=ext&u=" + requestUrlOrigin + "&tstamp=" +
latestTimestamp + "#" + b64UserFlag;
return Response.redirect(base + query, 302);
}
function domainNameToList(queryString, blocklistFilter, latestTimestamp) {
let domainName = queryString.get("dn") || "";
let returndata = {};
returndata.domainName = domainName;
returndata.version = latestTimestamp;
returndata.list = {};
var searchResult = blocklistFilter.hadDomainName(domainName);
if (searchResult) {
let list;
let listDetail = {};
for (let entry of searchResult) {
list = blocklistFilter.getTag(entry[1]);
listDetail = {};
for (let listValue of list) {
listDetail[listValue] = blocklistFilter.blocklistFileTag[listValue];
}
returndata.list[entry[0]] = listDetail;
}
} else {
returndata.list = false;
}
let response = new Response(JSON.stringify(returndata));
response.headers.set("Content-Type", "application/json");
return response;
}
function domainNameToUint(queryString, blocklistFilter) {
let domainName = queryString.get("dn") || "";
let returndata = {};
returndata.domainName = domainName;
returndata.list = {};
var searchResult = blocklistFilter.hadDomainName(domainName);
if (searchResult) {
for (let entry of searchResult) {
returndata.list[entry[0]] = entry[1];
}
} else {
returndata.list = false;
}
let response = new Response(JSON.stringify(returndata));
response.headers.set("Content-Type", "application/json");
return response;
}
function listToB64(queryString, blocklistFilter) {
let list = queryString.get("list") || [];
let flagVersion = parseInt(queryString.get("flagversion")) || 0;
let returndata = {};
returndata.command = "List To B64String";
returndata.inputList = list;
returndata.flagVersion = flagVersion;
returndata.b64String = blocklistFilter.getB64FlagFromTag(
list.split(","),
flagVersion,
);
let response = new Response(JSON.stringify(returndata));
response.headers.set("Content-Type", "application/json");
return response;
}
function b64ToList(queryString, blocklistFilter) {
let b64 = queryString.get("b64") || "";
let returndata = {};
returndata.command = "Base64 To List";
returndata.inputB64 = b64;
let response = blocklistFilter.unstamp(b64);
if (response.userBlocklistFlagUint.length > 0) {
returndata.list = blocklistFilter.getTag(response.userBlocklistFlagUint);
returndata.listDetail = {};
for (let listValue of returndata.list) {
returndata.listDetail[listValue] =
blocklistFilter.blocklistFileTag[listValue];
}
} else {
returndata.list = "Invalid B64 String";
}
response = new Response(JSON.stringify(returndata));
response.headers.set("Content-Type", "application/json");
return response;
}