我用来代理 apt 镜像,但是实际上任何网址都可以代理
// License: CC Zero 1.0 | https://creativecommons.org/publicdomain/zero/1.0/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
try{
var u = (new URL(request.url));
if (u.protocol == 'http:') {
u.protocol = 'https:';
return (new Response('', {status: 301, headers: {'Location': u.href}}));
}
var url = u.pathname.replace('/', '');
url = url.replace('/', '//');
url = url + u.search;
var d = await fetch(url, {method: request.method, headers: request.headers, body: request.body})
return (new Response(d.body, {status: d.status, headers: d.headers}))
}catch(e){
return (new Response(e, {status: 500}))
}
}
另外还有代理 websocket 的脚本
addEventListener(
'fetch',event => {
let url=new URL(event.request.url);
// 这里改成你的 v2ray ws 服务器的域名,不用加 http://,也不用加 https://
url.hostname='v2ray-server.example.com';
let request=new Request(url,event.request);
event.respondWith(
fetch(request)
)
}
)