域名反代,实现加速某些网站或者接口。
通过Cloudflare Workers实现反代
addEventListener(
"fetch",event => {
let url=new URL(event.request.url);
url.hostname="raw.githubusercontent.com"; //你需要反代的域名
let request=new Request(url,event.request);
event. respondWith(
fetch(request)
)
}
)
通过Cloudflare Pages实现反代
addEventListener('fetch', event => {
event.respondWith(proxy(event));
});
async function proxy(event) {
const getReqHeader = (key) => event.request.headers.get(key);
let url = new URL(event.request.url);
url.hostname = "raw.githubusercontent.com";
let parameter = {
headers: {
'Host': 'raw.githubusercontent.com',
'User-Agent': getReqHeader("User-Agent"),
'Accept': getReqHeader("Accept"),
'Accept-Language': getReqHeader("Accept-Language"),
'Accept-Encoding': getReqHeader("Accept-Encoding"),
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0'
}
};
if (event.request.headers.has("Referer")) {
parameter.headers.Referer = getReqHeader("Referer");
}
if (event.request.headers.has("Origin")) {
parameter.headers.Origin = getReqHeader("Origin");
}
return fetch(new Request(url, event.request), parameter);
}
评论