教程:利用Cloudflare的Workers下载Github资源(转载代码)

660 阅读资源分享Github
0

github下载快的请无视:)(代码是转载的)

首先打开域名管理,点击上方的“Workers”,点击“管理Workers”,使用免费套餐即可,就是有调用限制。(域名删除后Workers的程序不会被删除)

选择“创建Workers”

在右边“脚本”一栏填入以下代码:(把原先的代码去了)

addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request).catch((err) => { return new Response(err.message) }))
})
 const html = `
  <html><head><h1 style="font-size:32px;font-family:verdana;color:red;">Github直链加速下载每日10w次调用 </h1></head><body>
    <input type="url" placeholder="url" id="url" style="border: 6px solid powderblue;color: blue; width: 60%; display: block;">
  <input 
  type="submit" id="submit" value="submit"
  style="width: 30%; text-align: center;font-size: 18px;border: 1px solid powderblue;"/>
  <div id="res"></div>
  <a id="a" href="""></a>
  <p>
<br>
<br>
<br>
怎么自己搭建这个界面?
<br>
https://scaleya.com/publication/github-download-faster-with-cloudflare-worker/
</p>
   <script>
  document.getElementById('submit').onclick=function(){
      let url  = document.getElementById('url').value;
      console.log('url: '+url);
      let a = document.getElementById('a');
      let div = document.getElementById('res');
      if(!url || !url.startsWith('http')){
          div.textContent="链接不合法: "+url;
          a.style="display:none";
      }else{
          div.textContent="";
          let res = (new URL(window.location.href)).origin+'?url='+encodeURIComponent(url);
          a.textContent=res;
          a.href=res;
          a.style="";
      }
  }
  </script>
  </body></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' } });
    }
}

然后点击下面的“保存并部署”即可,用你的4级域名去访问,像:

test.majianyu.workers.dev(如果请求多就删除这个测试页面了)

复制github下载连接,点击subnit,即可下载。

代码转载至:https://huhu.blue/github/


分享到:
回答于
0

谢谢分享。


分享到:
评论于
Avatar for Qi

对github资源网站(头像,用户资源)连接被重置有很大帮助。有时候教程图片都加载不了。

Login

Welcome! Login in to your account

Remember me Lost your password?

Don't have account. Register

Lost Password

Register