加速GitHub上release软件包的下载的方法

欢迎移步我的个人博客

需要自己搭建环境:

  1. 打开cloudflare主页后,选择Workers

加速GitHub上release软件包的下载的方法_第1张图片

  1. 创建worker

    加速GitHub上release软件包的下载的方法_第2张图片

  2. 将框中代码删除替换成如下代码后,点击部署即可。(该源码的Github地址)

    加速GitHub上release软件包的下载的方法_第3张图片

addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request).catch((err) => { return new Response(err.message) }))
})

const html = `
  
  
  
  
注:该工具只针对直链有效
`; /** * Respond to the request * @param {Request} request */ async function handleRequest(request) { if (request.method === 'OPTIONS' && request.headers.has('access-control-request-headers')) { return new Response(null, { status: 204, headers: new Headers({ 'access-control-allow-origin': '*', 'access-control-allow-methods': 'GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS', 'access-control-allow-headers': '*', 'access-control-max-age': '1728000' }), }) } let req_url = new URL(request.url); if (req_url.pathname.startsWith('/ajax/')) {//ajax let url = req_url.pathname.slice(6).replace(/^(https?):\/+/, '$1://'); if (!url) return new Response("Only For Ajax"); let res = await fetch(url, { method: request.method, headers: request.headers, body: request.body }); let h = new Headers(res.headers); h.set('access-control-allow-origin', '*'); h.set('access-control-expose-headers', '*'); return new Response(res.body, { status: res.status, headers: h }); } else if (req_url.pathname === '/') {//download let url = req_url.searchParams.get('url'); if (!url) return new Response(html, { status: 200, headers: { 'Content-Type': 'text/html; charset=utf-8' } }); let res; if (request.headers.get('Range')) { res = await fetch(url, { headers: { 'Range': request.headers.get('Range') } }); } else { res = await fetch(url); } let h = new Headers(res.headers); h.set('set-cookie', ''); h.set('access-control-allow-origin', '*'); h.set('access-control-expose-headers', '*'); return new Response(res.body, { status: res.status, headers: h, }) } else { return new Response("400 --", { status: 400, headers: { 'Content-Type': 'text/html; charset=utf-8' } }); } }
  1. 保存并部署成功后,点击小箭头打开网页

    加速GitHub上release软件包的下载的方法_第4张图片

  2. 输入链接即可下载。

你可能感兴趣的:(杂,GitHub)