cfworker拉各种大文件一把梭

准备一个cf账号,一个域名并接入

添加一个A记录,随便解析,打开小云朵。

回到首页,点开左边的worker和pages。

根据提示,创建一个应用程序→部署→编辑代码,全选覆盖并保存部署。

addEventListener(‘fetch’, event => {
event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
const newUrl = request.url.replace(“https://你的域名/https://”, “https://”)
const response = await fetch(newUrl)

// you may want to add caching headers here to improve performance
// as well as handling errors if the upstream server is down
return response
}

【注意替换域名,要拉的如果是http,则修改域名后面对应的https】

点开刚部署的worker,在触发器→自定义域里添加刚刚的域名。

接着点下面的添加路由,填入刚解析的域名,示范:a.b.com/*,然后保存。

最后测试下,https://域名/https://speed.hetzner.de/100MB.bin

应用场景:拉github,拉civitai/huggingface模型,省去开代理的麻烦。

addEventListener(‘fetch’, event => {
event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
let targetUrl = new URL(request.url).pathname.slice(1);
if (targetUrl === ”) {
return new Response(‘Hello, world!’, {status: 200});
}
if (!targetUrl.includes(‘http://’) && !targetUrl.includes(‘https://’)) {
targetUrl = ‘http://’ + targetUrl;
}
try {
const response = await fetch(targetUrl, {headers: request.headers});
const downloadResponse = new Response(response.body, response);
downloadResponse.headers.set(‘Content-Disposition’, ‘attachment’);
return downloadResponse;
} catch (error) {
return new Response(Request to ${targetUrl} failed: ${error}, {status: 502});
}
}

此条目发表在日常分类目录。将固定链接加入收藏夹。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注