This repository has been archived by the owner on Jul 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
57 lines (51 loc) · 1.63 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
var SharedContext = require('@serverless-dns/globalcontext').SharedContext
var SingleRequest = require('@serverless-dns/single-request').SingleRequest
var Modules = require("@serverless-dns/free-user").Modules
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
async function handleRequest(event) {
return proxyRequest(event)
}
let commonContext = new SharedContext()
async function proxyRequest(event) {
let thisRequest = new SingleRequest()
let res
try {
if (event.request.method === "OPTIONS") {
res = new Response()
res.headers.set('Content-Type', 'application/json')
res.headers.set('Access-Control-Allow-Origin', '*')
res.headers.set('Access-Control-Allow-Headers', '*')
return res
}
let Caller
for (let i = 0; i <= Modules.length - 1; i++) {
Caller = new Modules[i]()
await Caller.RethinkModule(commonContext, thisRequest, event)
if (thisRequest.StopProcessing) {
if (thisRequest.IsException) {
thisRequest.DnsExceptionResponse()
}
else if (thisRequest.IsInvalidFlagBlock == true) {
thisRequest.CustomResponse("Invalid Flag", "User Invalid Flag Block")
}
break
}
}
}
catch (e) {
//thisRequest.exception = e
//thisRequest.DnsExceptionResponse()
res = new Response(JSON.stringify(e.stack))
res.headers.set('Content-Type', 'application/json')
res.headers.set('Access-Control-Allow-Origin', '*')
res.headers.set('Access-Control-Allow-Headers', '*')
res.headers.append('Vary', 'Origin')
res.headers.set('server', 'bravedns')
res.headers.delete('expect-ct')
res.headers.delete('cf-ray')
return res
}
return thisRequest.httpResponse
}