-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.ts
84 lines (70 loc) · 1.99 KB
/
proxy.ts
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
import { serve, type ConnInfo } from "https://deno.land/[email protected]/http/server.ts";
const log = (line:string, text:string) => {
console.log(line)
return `${text}\n${line}`
}
const logHeaders = (headers:Headers, text:string) => {
let lines = ''
for (const header of headers) {
lines = log(` ${header[0]}: ${header[1]}`, lines)
}
return `${text}${lines}`
}
const forward = async () => {
try {
const headers = new Headers()
headers.set('x-twintag-denodeploy-trace', `${Math.floor(Date.now())}`)
const rsp = await fetch('https://worker-proxy.sosvertigo.workers.dev', {
headers: headers,
})
return new Response(rsp.body, {
headers: rsp.headers,
})
} catch(err) {
return new Response(err, {status:502})
}
}
const test = async (url:string, method:string) => {
try {
const u = new URL(url)
const headers = new Headers()
headers.set('host', u.host)
headers.set('x-twintag-cloudflare-trace', `${Math.floor(Date.now())}`)
// log request
let text = log('\nChecking ...\n', '')
text = log(`${method} ${url}`, text)
text = logHeaders(headers, text)
// execute fetch
const rsp = await fetch(url, {
method: method,
headers: headers,
})
// log response
text = log(`Status ${rsp.status} `, text)
text = logHeaders(rsp.headers, text)
// log body length
const body = await rsp.text()
text = log(`${body.length} body bytes`, text)
return text
} catch(err) {
console.error(err)
return `${err}`
}
}
async function handler(req: Request, connInfo: ConnInfo): Promise<Response> {
console.log(req, connInfo)
const url = new URL(req.url)
if (url.pathname === '/test') {
return await forward()
}
if (url.pathname === '/ngrok') {
const text = await test('https://7588-87-67-226-224.eu.ngrok.io/', 'POST')
return new Response(text, { status: 200 } )
}
return new Response(null, { status: 404 } )
}
serve(handler, {
onListen: ({ port, hostname }) => {
console.log("Listening on", hostname, 'port', port)
}
});