-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.ts
More file actions
32 lines (25 loc) · 824 Bytes
/
Copy pathproxy.ts
File metadata and controls
32 lines (25 loc) · 824 Bytes
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
import * as flags from 'https://deno.land/std@0.198.0/flags/mod.ts'
const args = flags.parse(Deno.args, {
strings: ['url', 'port'],
collect: ['header'],
})
if (!args.url) throw new Error('--url not specified')
Deno.serve({ port: parseInt(args.port || '8080') }, (request) => {
const { pathname, search } = new URL(request.url)
const url = new URL('.' + pathname, args.url)
url.search = search
// url.pathname = pathname
const headers = new Headers(request.headers)
headers.set('Host', url.hostname)
for (const h of args.header ?? []) {
const [key, value] = h.split(':').map((v) => v.trim())
headers.set(key, value)
}
console.debug('proxy %o', url.toString(), headers)
return fetch(url, {
method: request.method,
headers,
body: request.body,
redirect: 'manual',
})
})