-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse_proxy.ts
64 lines (64 loc) · 1.92 KB
/
reverse_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
/**
* 主要中间件处理函数
*
* @param request 请求对象,包含请求的方法和URL等信息
* @param info 请求的额外信息,例如远程地址
* @param next 中间件链中的下一个函数,用于继续处理请求或结束请求处理
* @returns 返回一个响应对象
*/
export async function reverse_proxy(
url: URL,
requestHeaders: Headers,
request: Request,
): Promise<Response> {
try {
const req_info = {
method: request.method,
url: url,
headers: Object.fromEntries(requestHeaders),
};
console.log(
JSON.stringify(
{
// ...info,
request: req_info,
},
null,
4,
),
);
const response = await fetch(url, {
headers: requestHeaders,
method: request.method,
body: request.body,
/* 关闭重定向 */
/* 可以设定请求头中的字段"x-proxy-redirect"为"error" | "follow" |
"manual"来设定代理行为的重定向方式. */
redirect: (requestHeaders.get("x-proxy-redirect") ??
"manual") as RequestRedirect,
});
console.log(
JSON.stringify(
{
// ...info,
response: {
headers: Object.fromEntries(response.headers),
status: response.status,
},
request: req_info,
},
null,
4,
),
);
return new Response(response.body, {
headers: response.headers,
status: response.status,
});
} catch (error) {
console.error(error);
return new Response("bad gateway" + "\n" + String(error), {
status: 502,
});
}
}