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


級別:俠客 ( 9 )
發帖:1885
威望:115 點
金錢:1195 USD
貢獻:901 點
註冊: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 樓主 引用 | 發表評論
乱七嚟留种


級別:新手上路 ( 8 )
發帖:960
威望:92 點
金錢:10394 USD
貢獻:0 點
註冊:2015-10-08


1024
TOP Posted: 02-08 21:17 #1樓 引用 | 點評
椰林小蚂蚁


級別:光明使者 ( 14 )
發帖:18344
威望:10096 點
金錢:7282 USD
貢獻:2063 點
註冊:2020-02-11

给大佬献上膝盖
------------------------
X
TOP Posted: 02-08 21:17 #2樓 引用 | 點評
万向伦


級別:俠客 ( 9 )
發帖:1521
威望:153 點
金錢:1659 USD
貢獻:0 點
註冊:2019-01-02

支持技术帖!
TOP Posted: 02-08 21:18 #3樓 引用 | 點評
混江龙


級別:精靈王 ( 12 )
發帖:706
威望:832 點
金錢:24528 USD
貢獻:18200 點
註冊:2022-01-26

看看学习下
TOP Posted: 02-08 21:19 #4樓 引用 | 點評
爲伊人憔悴


級別:精靈王 ( 12 )
發帖:4697
威望:470 點
金錢:481 USD
貢獻:146800 點
註冊:2021-09-16

谢谢分享
TOP Posted: 02-08 21:24 #5樓 引用 | 點評
鼓掌要大声


級別:俠客 ( 9 )
發帖:9103
威望:278 點
金錢:48107 USD
貢獻:0 點
註冊:2020-09-15

大神收下我的膝盖
TOP Posted: 02-08 21:28 #6樓 引用 | 點評
簡体男人


級別:聖騎士 ( 11 )
發帖:3543
威望:624 點
金錢:417569 USD
貢獻:0 點
註冊:2021-09-19

妥妥的技术教程
------------------------
J
TOP Posted: 02-08 21:29 #7樓 引用 | 點評
东海小神龙


級別:新手上路 ( 8 )
發帖:422
威望:43 點
金錢:437 USD
貢獻:0 點
註冊:2021-11-29

感谢分享
TOP Posted: 02-08 21:31 #8樓 引用 | 點評
夜游宫


級別:天使 ( 14 )
發帖:92517
威望:190045 點
金錢:13403 USD
貢獻:21 點
註冊:2020-02-08
認證: 博彩區資深老幹部
2021-07-09

支持技术分享
------------------------
🐁 🐂 🐅 🐇 🐉 🐍 🐎 🐑 🐒 🐔 🐕 🐖
TOP Posted: 02-08 21:32 #9樓 引用 | 點評
漂泊四方


級別:騎士 ( 10 )
發帖:4760
威望:387 點
金錢:438 USD
貢獻:0 點
註冊:2020-07-24

感谢分享
TOP Posted: 02-08 23:30 #10樓 引用 | 點評
拾谎


級別:俠客 ( 9 )
發帖:2553
威望:256 點
金錢:4356 USD
貢獻:0 點
註冊:2019-07-16


谢谢分享
TOP Posted: 02-08 23:34 #11樓 引用 | 點評
zhao502


級別:精靈王 ( 12 )
發帖:4479
威望:363 點
金錢:224020663 USD
貢獻:33333 點
註冊:2010-03-14

感谢分享
TOP Posted: 02-08 23:47 #12樓 引用 | 點評
继续喝豆浆


級別:聖騎士 ( 11 )
發帖:6445
威望:583 點
金錢:3225 USD
貢獻:1314 點
註冊:2015-08-29

纯技术贴
TOP Posted: 02-09 00:05 #13樓 引用 | 點評
huatian1024


級別:光明使者 ( 14 )
發帖:16276
威望:37965 點
金錢:27571 USD
貢獻:100 點
註冊:2020-02-15

技术贴,感谢大佬分享
TOP Posted: 02-09 00:07 #14樓 引用 | 點評
xuejiao209


級別:騎士 ( 10 )
發帖:4008
威望:401 點
金錢:1708683 USD
貢獻:0 點
註冊:2017-04-01

感谢分享
TOP Posted: 02-09 05:01 #15樓 引用 | 點評
Shiyueruchu


級別:禁止發言 ( 8 )
發帖:4556
威望:462 點
金錢:-3667989 USD
貢獻:0 點
註冊:2021-06-02

涨知识了
TOP Posted: 02-09 05:57 #16樓 引用 | 點評
lovecan


級別:騎士 ( 10 )
發帖:4599
威望:468 點
金錢:6424 USD
貢獻:8 點
註冊:2011-10-03


很有意思
TOP Posted: 02-09 06:04 #17樓 引用 | 點評
口臭男孩


級別:禁止發言 ( 8 )
發帖:13933
威望:1384 點
金錢:29769 USD
貢獻:0 點
註冊:2015-07-28

感谢分享
TOP Posted: 02-09 06:21 #18樓 引用 | 點評
潇湘夜汐雨


級別:光明使者 ( 14 )
發帖:1852
威望:385 點
金錢:90924 USD
貢獻:520000 點
註冊:2018-05-25

感谢分享
TOP Posted: 02-09 07:00 #19樓 引用 | 點評
拐子


級別:俠客 ( 9 )
發帖:2466
威望:247 點
金錢:5631 USD
貢獻:0 點
註冊:2017-06-04

感谢作者的分享
TOP Posted: 02-09 07:04 #20樓 引用 | 點評
耗子爱骑猫


級別:騎士 ( 10 )
發帖:3711
威望:374 點
金錢:19890 USD
貢獻:0 點
註冊:2022-01-31

1024
TOP Posted: 02-09 07:12 #21樓 引用 | 點評
andy137025


級別:騎士 ( 10 )
發帖:5913
威望:593 點
金錢:4105738 USD
貢獻:2 點
註冊:2007-01-16

感谢分享
TOP Posted: 02-09 07:26 #22樓 引用 | 點評
动画片大魔王


級別:聖騎士 ( 11 )
發帖:3245
威望:461 點
金錢:23228 USD
貢獻:25266 點
註冊:2021-01-25


感谢分享
TOP Posted: 02-09 07:34 #23樓 引用 | 點評
陈饭饭


級別:俠客 ( 9 )
發帖:1179
威望:118 點
金錢:1179 USD
貢獻:0 點
註冊:2021-10-24

谢谢我大哥码字
TOP Posted: 02-09 08:09 #24樓 引用 | 點評

.:. 草榴社區 -> 技術討論區

快速回帖 頂端
內容
HTML 代碼不可用

使用簽名
Wind Code自動轉換

按 Ctrl+Enter 直接提交