-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
283 lines (262 loc) · 6.32 KB
/
index.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// SPDX-License-Identifier: 0BSD
const FWD = "fwd";
const FWD_STREAMED = 0; // default
const FWD_BUFFERED = 2;
const FWD_CLONED = 1;
const HDR_CONTENT_LEN = "Content-Length";
const ODOH_METHOD = "POST";
const ODOH_TARGETHOST = "targethost";
const ODOH_TARGETPATH = "targetpath";
const ODOH_ENDPOINT_NAME = "RethinkDNS";
const ODOH_HDR_ACCEPT = { Accept: "application/oblivious-dns-message" };
const ODOH_HDR_CONTENT_TYPE = {
"Content-Type": "application/oblivious-dns-message",
};
const ODOH_HDR_CACHE_CONTROL = { "Cache-Control": "no-cache, no-store" };
// datatracker.ietf.org/doc/rfc9230/ (section 4.1)
const ODOH_HDR_PROXY_ERR = {
"Proxy-Status": ODOH_ENDPOINT_NAME + "; error=http_request_error",
};
function proxyStatusHdr(code) {
return { "Proxy-Status": `${ODOH_ENDPOINT_NAME}; received-status=${code}` };
}
function proxyStatusKV(code) {
return {
k: "Proxy-Status",
v: `${ODOH_ENDPOINT_NAME}; received-status=${code}`,
};
}
/**
* @param {string} txt
* @returns {Response}
*/
function r500(txt) {
return new Response(null, {
status: 500,
statusText: txt,
headers: { ...ODOH_HDR_PROXY_ERR },
});
}
/**
* @param {string} txt
* @returns {Response}
*/
function r400(txt) {
return new Response(null, {
status: 400,
statusText: txt,
headers: { ...ODOH_HDR_PROXY_ERR },
});
}
/**
* @param {Response} r
* @param {Request} fwdreq
* @returns {Response}
*/
function response(r, fwdreq) {
// rename the headers as Snippets snips some out (like the "Location" header)
const orighdrs = {};
let h = 0;
let f = 0;
for (const [k, v] of r.headers) {
orighdrs[`x-${k}`] = v;
h++;
}
for (const [k, v] of fwdreq.headers) {
orighdrs[`x-fwdreq-${k}`] = v;
f++;
}
orighdrs["x-orig-hdr-count"] = h + "";
orighdrs["x-fwdreq-hdr-count"] = f + "";
return new Response(r.body, {
status: r.status,
statusText: r.statusText,
headers: {
...orighdrs,
...ODOH_HDR_CONTENT_TYPE,
...proxyStatusHdr(r.status),
},
});
}
/**
* @param {URL} u
* @param {Request} r
* @returns {Request}
*/
function request(u, r) {
const len = hdr(r.headers, HDR_CONTENT_LEN, "0");
return new Request(u, {
method: ODOH_METHOD,
body: r.body,
headers: {
...ODOH_HDR_CONTENT_TYPE,
...ODOH_HDR_CACHE_CONTROL,
...ODOH_HDR_ACCEPT,
HDR_CONTENT_LEN: len,
},
});
}
/**
* @param {URL} u
* @param {Request} r
* @returns {Request}
*/
function clonedRequest(u, r) {
// to follow redirects
// stackoverflow.com/questions/55920957
return new Request(u, r);
}
/**
* @param {URL} u
* @param {ArrayBuffer} body
* @returns {Request}
*/
function bufferedRequest(u, body) {
let len = "0";
if (body) {
len = body.byteLength + "";
}
// to follow redirects
// stackoverflow.com/questions/55920957
return new Request(u, {
method: ODOH_METHOD,
body: body,
headers: {
...ODOH_HDR_CONTENT_TYPE,
...ODOH_HDR_CACHE_CONTROL,
...ODOH_HDR_ACCEPT,
HDR_CONTENT_LEN: len,
},
});
}
/**
* @param {Response} r
* @param {Request} fwdreq
* @returns {Response}
*/
function cloneResponse(r, fwdreq) {
const rr = new Response(r.body, r);
const pkv = proxyStatusKV(r.status);
rr.headers.set(pkv.k, pkv.v);
for (const [k, v] of fwdreq.headers) {
rr.headers.set(`x-fwdreq-${k}`, v);
}
return rr;
}
/**
*
* @param {Headers} h
* @param {string} k
* @param {string} d
*/
function hdr(h, k, d) {
if (!h || !k || h instanceof Headers === false) {
return d;
}
return h.get(k) || d;
}
/**
*
* @param {URL} url
* @param {string} name
* @returns {string}
*/
function param(url, name) {
if (!url || !name) {
return "";
}
return url.searchParams.get(name) || "";
}
/**
*
* @param {URL} url
* @param {string} name
* @returns {string}
*/
function at(url, name) {
if (!url || !name) {
return "";
}
const path = url.pathname;
if (!path || path === "/") {
return "";
}
const p = path.split("/");
let i = Number.MAX_SAFE_INTEGER;
if (name === ODOH_TARGETHOST) {
i = 1;
} else if (name === ODOH_TARGETPATH) {
i = 2;
} else if (name === FWD) {
i = 3;
}
if (p.length > i) {
return p[i];
}
return "";
}
function get(url, name) {
return param(url, name) || at(url, name);
}
function ensureLeadingSlash(path) {
return path.startsWith("/") ? path : `/${path}`;
}
/**
* Proxy the ODoH request to the target host/path and return the response.
* @param {Request} req
* @returns {Response}
*/
async function handleRequest(req) {
try {
// https://odoh.proxy/dns-query?targethost=&targetpath=
// https://odoh.proxy/{targethost}/{targetpath}
const u = new URL(req.url);
const host = get(u, ODOH_TARGETHOST);
const path0 = get(u, ODOH_TARGETPATH);
const fwd = get(u, FWD);
if (req.method !== ODOH_METHOD) {
return r500(`Only ${ODOH_METHOD}`);
}
if (!host) {
return r400(`Missing ${ODOH_TARGETHOST}`);
}
if (!path0) {
return r400(`Missing ${ODOH_TARGETPATH}`);
}
// ensure targetPath starts with a slash
const path = ensureLeadingSlash(path0);
const target = `https://${host}${path}`;
if (fwd == FWD_BUFFERED) {
// == string or number
// if redirected by the target:
// with "fetch(bufferedRequest())" => 1022 Snippets exceeded subrequests limit
const body = await req.arrayBuffer();
const fwdreq = bufferedRequest(target, body);
const downstream = await fetch(fwdreq);
return cloneResponse(downstream, fwdreq);
} else if (fwd == FWD_CLONED) {
// if redirected by the target:
// with "return cloneResponse()" => 405 Method Not Allowed
// with "return response()" => 301 Moved Permanently
const fwdreq = clonedRequest(target, req);
const downstream = await fetch(fwdreq);
return response(downstream, fwdreq);
} else {
// fwd == FWD_STREAMED
// if redirected by the target:
// with "return clonedResponse()" => 500 Internal Server Error
const fwdreq = request(target, req);
const downstream = await fetch(fwdreq);
return cloneResponse(downstream, fwdreq);
}
} catch (err) {
console.error(err);
return r500(err.message);
}
}
// developers.cloudflare.com/workers/runtime-apis/fetch-event/#syntax-module-worker
export default {
async fetch(r, env, ctx) {
return handleRequest(r);
},
};