-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
64 lines (48 loc) · 1.67 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
/*
* Cloudflare-worker-proxy
*
*/
const cookiename = '__cfmainstream'
const html = `<html><form>` +
`<input placeholder="https://domain.tld" name="__url"/> ` +
`<button>open</button></form></html>`
var request, url, mainstream
function getCookie(cookies, name) {
let match, re = /(.*?)=(.*?)(?:\s*;\s*|$)/g
while (match = re.exec(cookies))
if(match[1] == name)
return match[2]
}
async function getResponse() {
let opts = await getOpts()
let response = await fetch(mainstream.origin + url.pathname, opts)
response = new Response(response.body, response)
if(url.searchParams.get('__url'))
response.headers.append('Set-Cookie',
`${cookiename}=${mainstream.origin}; secure; httpOnly`)
return response
}
async function getOpts() {
let headers = {}
let body = !['GET', 'HEAD'].includes(request.method) ?
await request.text() : null
for(const header of request.headers.entries()) {
let [name, value] = header
if(value.indexOf(url.hostname) != -1)
value = value.replaceAll(url.hostname, mainstream.hostname)
headers[name] = value
}
return {method: request.method, headers: headers, body: body}
}
addEventListener("fetch", event => {
request = event.request
url = new URL(request.url)
mainstream = url.searchParams.get('__url')
if(!mainstream)
mainstream = getCookie(request.headers.get("Cookie"), cookiename)
if(!mainstream || !/^https?:\/\//.test(mainstream))
return event.respondWith(new Response(html,
{headers: {'Content-Type': 'text/html'}}))
mainstream = new URL(mainstream)
return event.respondWith(getResponse())
})