forked from serverless-dns/serverless-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doh.js
49 lines (39 loc) · 1.29 KB
/
doh.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
/*
* 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 RethinkPlugin from "./plugin.js";
import * as util from "../commons/util.js";
import * as dnsutil from "../commons/dnsutil.js";
import IOState from "./io-state.js";
export function handleRequest(event) {
return proxyRequest(event);
}
async function proxyRequest(event) {
if (optionsRequest(event.request)) return util.respond204();
const io = new IOState();
try {
const plugin = new RethinkPlugin(event);
await util.timedSafeAsyncOp(
/* op*/ async () => plugin.executePlugin(io),
/* waitMs*/ dnsutil.requestTimeout(),
/* onTimeout*/ async () => errorResponse(io)
);
} catch (err) {
log.e("doh", "proxy-request error", err.stack);
errorResponse(io, err);
}
const ua = event.request.headers.get("User-Agent");
if (util.fromBrowser(ua)) io.setCorsHeadersIfNeeded();
return io.httpResponse;
}
function optionsRequest(request) {
return request.method === "OPTIONS";
}
function errorResponse(io, err = null) {
const eres = util.errResponse("doh.js", err);
io.dnsExceptionResponse(eres);
}