.:. 草榴社區 » 技術討論區 » 解决Github或Github pages 无法访问
本頁主題: 解决Github或Github pages 无法访问字體大小 寬屏顯示 只看樓主 最新點評 熱門評論 時間順序
这易逝的容颜


級別:俠客 ( 9 )
發帖:1897
威望:120 點
金錢:340 USD
貢獻:1023 點
註冊:2019-03-11

解决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 里面,保存
複製代碼
  1. const config = {
  2.   basic: {
  3.     upstream: 'https://github.com/',
  4.     mobileRedirect: 'https://github.com/',
  5.   },
  6.   firewall: {
  7.     blockedRegion: ['KP', 'SY', 'PK', 'CU'],
  8.     blockedIPAddress: [],
  9.     scrapeShield: true,
  10.   },
  11.   routes: {
  12.     TW: 'https://github.com/',
  13.   },
  14.   optimization: {
  15.     cacheEverything: false,
  16.     cacheTtl: 5,
  17.     mirage: true,
  18.     polish: 'off',
  19.     minify: {
  20.       javascript: true,
  21.       css: true,
  22.       html: true,
  23.     },
  24.   },
  25. };
  26. async function isMobile(userAgent) {
  27.   const agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod'];
  28.   return agents.any((agent) => userAgent.indexOf(agent) > 0);
  29. }
  30. async function fetchAndApply(request) {
  31.   const region = request.headers.get('cf-ipcountry') || '';
  32.   const ipAddress = request.headers.get('cf-connecting-ip') || '';
  33.   const userAgent = request.headers.get('user-agent') || '';
  34.   if (region !== '' && config.firewall.blockedRegion.includes(region.toUpperCase())) {
  35.     return new Response(
  36.       'Access denied: booster.js is not available in your region.',
  37.       {
  38.         status: 403,
  39.       },
  40.     );
  41.   } if (ipAddress !== '' && config.firewall.blockedIPAddress.includes(ipAddress)) {
  42.     return new Response(
  43.       'Access denied: Your IP address is blocked by booster.js.',
  44.       {
  45.         status: 403,
  46.       },
  47.     );
  48.   }
  49.   const requestURL = new URL(request.url);
  50.   let upstreamURL = null;
  51.   if (userAgent && isMobile(userAgent) === true) {
  52.     upstreamURL = new URL(config.basic.mobileRedirect);
  53.   } else if (region && region.toUpperCase() in config.routes) {
  54.     upstreamURL = new URL(config.routes[region.toUpperCase()]);
  55.   } else {
  56.     upstreamURL = new URL(config.basic.upstream);
  57.   }
  58.   requestURL.protocol = upstreamURL.protocol;
  59.   requestURL.host = upstreamURL.host;
  60.   requestURL.pathname = upstreamURL.pathname + requestURL.pathname;
  61.   let newRequest;
  62.   if (request.method === 'GET' || request.method === 'HEAD') {
  63.     newRequest = new Request(requestURL, {
  64.       cf: {
  65.         cacheEverything: config.optimization.cacheEverything,
  66.         cacheTtl: config.optimization.cacheTtl,
  67.         mirage: config.optimization.mirage,
  68.         polish: config.optimization.polish,
  69.         minify: config.optimization.minify,
  70.         scrapeShield: config.firewall.scrapeShield,
  71.       },
  72.       method: request.method,
  73.       headers: request.headers,
  74.     });
  75.   } else {
  76.     const requestBody = await request.text();
  77.     newRequest = new Request(requestURL, {
  78.       cf: {
  79.         cacheEverything: config.optimization.cacheEverything,
  80.         cacheTtl: config.optimization.cacheTtl,
  81.         mirage: config.optimization.mirage,
  82.         polish: config.optimization.polish,
  83.         minify: config.optimization.minify,
  84.         scrapeShield: config.firewall.scrapeShield,
  85.       },
  86.       method: request.method,
  87.       headers: request.headers,
  88.       body: requestBody,
  89.     });
  90.   }
  91.   const fetchedResponse = await fetch(newRequest);
  92.   const modifiedResponseHeaders = new Headers(fetchedResponse.headers);
  93.   if (modifiedResponseHeaders.has('x-pjax-url')) {
  94.     const pjaxURL = new URL(modifiedResponseHeaders.get('x-pjax-url'));
  95.     pjaxURL.protocol = requestURL.protocol;
  96.     pjaxURL.host = requestURL.host;
  97.     pjaxURL.pathname = pjaxURL.path.replace(requestURL.pathname, '/');
  98.     modifiedResponseHeaders.set(
  99.       'x-pjax-url',
  100.       pjaxURL.href,
  101.     );
  102.   }
  103.   return new Response(
  104.     fetchedResponse.body,
  105.     {
  106.       headers: modifiedResponseHeaders,
  107.       status: fetchedResponse.status,
  108.       statusText: fetchedResponse.statusText,
  109.     },
  110.   );
  111. }
  112. // eslint-disable-next-line no-restricted-globals
  113. addEventListener('fetch', (event) => {
  114.   event.respondWith(fetchAndApply(event.request));
  115. });
複製代碼


之后通过worker 的子域名来访问GitHub
但是这种方法我是不大推荐的,因为这样不可以登录,也就是说不可以下载,如果想要下载则需要再新建一个worker然后输入以下代码
複製代碼
  1. addEventListener('fetch', event => {
  2.     event.respondWith(handleRequest(event.request).catch((err) => { return new Response(err.message) }))
  3. })
  4. const html = `
  5.   <html><head><h1 style=\"font-size:32px;font-family:verdana;color:red;\">Github直链加速下载每日10w次调用 </h1></head><body>
  6.     <input type=\"url\" placeholder=\"url\" id=\"url\" style=\"border: 6px solid powderblue;color: blue; width: 60%; display: block;\">
  7.   <input
  8.   type=\"submit\" id=\"submit\" value=\"submit\"
  9.   style=\"width: 30%; text-align: center;font-size: 18px;border: 1px solid powderblue;\"/>
  10.   <div id=\"res\"></div>
  11.   <a id=\"a\" href=\"\"\"></a>
  12.   <p>
  13. <br>
  14. <br>
  15. <br>
  16. 怎么自己搭建这个界面?
  17. <br>
  18. https://scaleya.com/publication/github-download-faster-with-cloudflare-worker/
  19. </p>
  20.   <script>
  21.   document.getElementById('submit').onclick=function(){
  22.       let url  = document.getElementById('url').value;
  23.       console.log('url: '+url);
  24.       let a = document.getElementById('a');
  25.       let div = document.getElementById('res');
  26.       if(!url || !url.startsWith('http')){
  27.           div.textContent=\"链接不合法: \"+url;
  28.           a.style=\"display:none\";
  29.       }else{
  30.           div.textContent=\"\";
  31.           let res = (new URL(window.location.href)).origin+'?url='+encodeURIComponent(url);
  32.           a.textContent=res;
  33.           a.href=res;
  34.           a.style=\"\";
  35.       }
  36.   }
  37.   </script>
  38.   </body></html>`;
  39. /**
  40. * Respond to the request
  41. * @param {Request} request
  42. */
  43. async function handleRequest(request) {
  44.     if (request.method === 'OPTIONS' && request.headers.has('access-control-request-headers')) {
  45.         return new Response(null, {
  46.             status: 204,
  47.             headers: new Headers({
  48.                 'access-control-allow-origin': '*',
  49.                 'access-control-allow-methods': 'GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS',
  50.                 'access-control-allow-headers': '*',
  51.                 'access-control-max-age': '1728000'
  52.             }),
  53.         })
  54.     }
  55.     let req_url = new URL(request.url);
  56.     if (req_url.pathname.startsWith('/ajax/')) {//ajax
  57.         let url = req_url.pathname.slice(6).replace(/^(https?):/+/, '$1://');
  58.         if (!url) return new Response(\"Only For Ajax\");
  59.         let res = await fetch(url, { method: request.method, headers: request.headers, body: request.body });
  60.         let h = new Headers(res.headers);
  61.         h.set('access-control-allow-origin', '*');
  62.         h.set('access-control-expose-headers', '*');
  63.         return new Response(res.body, { status: res.status, headers: h });
  64.     } else if (req_url.pathname === '/') {//download
  65.         let url = req_url.searchParams.get('url');
  66.         if (!url) return new Response(html, { status: 200, headers: { 'Content-Type': 'text/html; charset=utf-8' } });
  67.         let res;
  68.         if (request.headers.get('Range')) {
  69.             res = await fetch(url, { headers: { 'Range': request.headers.get('Range') } });
  70.         } else {
  71.             res = await fetch(url);
  72.         }
  73.         let h = new Headers(res.headers);
  74.         h.set('set-cookie', '');
  75.         h.set('access-control-allow-origin', '*');
  76.         h.set('access-control-expose-headers', '*');
  77.         return new Response(res.body, {
  78.             status: res.status,
  79.             headers: h,
  80.         })
  81.     } else {
  82.         return new Response(\"400 --\", { status: 400, headers: { 'Content-Type': 'text/html; charset=utf-8' } });
  83.     }
  84. }
複製代碼



赞(7)
DMCA / ABUSE REPORT | TOP Posted: 02-08 21:16 發表評論
.:. 草榴社區 » 技術討論區

電腦版 手機版 客戶端 DMCA
用時 0.01(s) x3, 07-16 22:32