解决Github或Github pages 无法访问

作为开发者,经常使用借助GitHub进行开发,但是最近防火墙发力导致本就半死不活的GitHub在河北彻底无法访问github.com站点,所以我决定搞一下,以下几个办法供参考【ps:不考虑那啥上网】

1.修改hosts

打开 http://tool.chinaz.com/dns?type=1&host=www.github.com&ip=
选择一个TTL最小的ip,比如我这里是新加坡的ip

之后打开这个路径C:WindowsSystem32driversetc找到hosts,将它复制一份到桌面

 

通过你的文本编辑器打开这个文件,并输入一行记录13.229.188.59 github.com,同理,如果需要 assets-cdn.github.com[CSS,JS加载慢,添加这个],avatars0.githubusercontent.com[用户头像不能访问,或者访问慢],avatars1.githubusercontent.com[用户头像不能访问,或者访问慢],使用上面的方法找到dns域名,填入即可。
这种方法并不是一劳永逸的,因为nds时刻在变,所以一旦不能访问,还是先找可访问的dns域名吧。

2.Cloudflare Workers 反代

利用Cloudflare Workers搭建一个GitHub镜像供自己使用

在 cloudflare 上创建一个 worker

将下面所有内容拷贝,覆盖粘贴到 worker 里面,保存

const config = {
  basic: {
    upstream: 'https://github.com/',
    mobileRedirect: 'https://github.com/',
  },

  firewall: {
    blockedRegion: ['KP', 'SY', 'PK', 'CU'],
    blockedIPAddress: [],
    scrapeShield: true,
  },

  routes: {
    TW: 'https://github.com/',
  },

  optimization: {
    cacheEverything: false,
    cacheTtl: 5,
    mirage: true,
    polish: 'off',
    minify: {
      javascript: true,
      css: true,
      html: true,
    },
  },
};

async function isMobile(userAgent) {
  const agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod'];
  return agents.any((agent) => userAgent.indexOf(agent) > 0);
}

async function fetchAndApply(request) {
  const region = request.headers.get('cf-ipcountry') || '';
  const ipAddress = request.headers.get('cf-connecting-ip') || '';
  const userAgent = request.headers.get('user-agent') || '';

  if (region !== '' && config.firewall.blockedRegion.includes(region.toUpperCase())) {
    return new Response(
      'Access denied: booster.js is not available in your region.',
      {
        status: 403,
      },
    );
  } if (ipAddress !== '' && config.firewall.blockedIPAddress.includes(ipAddress)) {
    return new Response(
      'Access denied: Your IP address is blocked by booster.js.',
      {
        status: 403,
      },
    );
  }

  const requestURL = new URL(request.url);
  let upstreamURL = null;

  if (userAgent && isMobile(userAgent) === true) {
    upstreamURL = new URL(config.basic.mobileRedirect);
  } else if (region && region.toUpperCase() in config.routes) {
    upstreamURL = new URL(config.routes[region.toUpperCase()]);
  } else {
    upstreamURL = new URL(config.basic.upstream);
  }

  requestURL.protocol = upstreamURL.protocol;
  requestURL.host = upstreamURL.host;
  requestURL.pathname = upstreamURL.pathname + requestURL.pathname;

  let newRequest;
  if (request.method === 'GET' || request.method === 'HEAD') {
    newRequest = new Request(requestURL, {
      cf: {
        cacheEverything: config.optimization.cacheEverything,
        cacheTtl: config.optimization.cacheTtl,
        mirage: config.optimization.mirage,
        polish: config.optimization.polish,
        minify: config.optimization.minify,
        scrapeShield: config.firewall.scrapeShield,
      },
      method: request.method,
      headers: request.headers,
    });
  } else {
    const requestBody = await request.text();
    newRequest = new Request(requestURL, {
      cf: {
        cacheEverything: config.optimization.cacheEverything,
        cacheTtl: config.optimization.cacheTtl,
        mirage: config.optimization.mirage,
        polish: config.optimization.polish,
        minify: config.optimization.minify,
        scrapeShield: config.firewall.scrapeShield,
      },
      method: request.method,
      headers: request.headers,
      body: requestBody,
    });
  }

  const fetchedResponse = await fetch(newRequest);

  const modifiedResponseHeaders = new Headers(fetchedResponse.headers);
  if (modifiedResponseHeaders.has('x-pjax-url')) {
    const pjaxURL = new URL(modifiedResponseHeaders.get('x-pjax-url'));
    pjaxURL.protocol = requestURL.protocol;
    pjaxURL.host = requestURL.host;
    pjaxURL.pathname = pjaxURL.path.replace(requestURL.pathname, '/');

    modifiedResponseHeaders.set(
      'x-pjax-url',
      pjaxURL.href,
    );
  }

  return new Response(
    fetchedResponse.body,
    {
      headers: modifiedResponseHeaders,
      status: fetchedResponse.status,
      statusText: fetchedResponse.statusText,
    },
  );
}

// eslint-disable-next-line no-restricted-globals
addEventListener('fetch', (event) => {
  event.respondWith(fetchAndApply(event.request));
});

之后通过worker 的子域名来访问GitHub

 

但是这种方法我是不大推荐的,因为这样不可以登录,也就是说不可以下载,如果想要下载则需要再新建一个worker然后输入以下代码

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' } });
    }
}

或者可用这位朋友的方法搭建加速链接

白嫖cloudflare搭建属于自己的git加速站-2022年2月20日更新

希望这篇文章能对您产生帮助,如果我真的帮到了您,那我呆某真是感到无比荣幸🤟

-> 加入科技玩家交流群组:点击加入 <-
注意:
1.文中二维码和链接可能带有邀请性质,请各位玩家自行抉择。
2.请勿通过链接填写qq号与密码、银行卡号与密码等个人隐私信息。
3.禁止纯拉人头,拉app注册等信息,发现必小黑屋。
4.同一种信息仅发一次,多发会被删除。
5.文章中源码或APP等,无法保证其绝对安全,需自行辨别。
6.文章关联方不想展示也可以微信站长“socutesheep”删除。
本文由 @呆逼 发布。如若转载,请注明出处: 科技玩家 » 解决Github或Github pages 无法访问

给TA买糖
共{{data.count}}人
人已买糖
学习笔记精选文章

Rclone实现GoogleDrive等多网盘挂载给vps变相扩容!搭配FileBrowser更香哦~

2022-1-26 0:04:58

教程玩家投稿

iperf3带宽测试工具安装及使用方法分享

2022-1-28 21:17:47

47 条回复 A文章作者 M管理员
贴心提醒
请认真对待作者付出,勿发表无意义言论,触发过滤规则的评论将无法提交,包含敏感词的评论会自动变成待审核状态哦。
  1. seatom

    又学习到了,写的非常好😁

  2.  19739

    nice啊 😎

  3. 哆啦B梦

    感谢分享,不断学习!!

  4.  未

    感谢分享!

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索