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

修改hosts

打开 http://tool.chinaz.com/dns?type=1&host=www.github.com&ip=

选择一个TTL最小的ip,比如我这里是新加坡的ipimage-20210314084117044

之后打开这个路径C:\Windows\System32\drivers\etc找到hosts,将它复制一份到桌面

image-20210314084400713

通过你的文本编辑器打开这个文件,并输入一行记录13.229.188.59 github.com,同理,如果需要 assets-cdn.github.com[CSS,JS加载慢,添加这个],avatars0.githubusercontent.com[用户头像不能访问,或者访问慢],avatars1.githubusercontent.com[用户头像不能访问,或者访问慢],使用上面的方法找到dns域名,填入即可。

这种方法并不是一劳永逸的,因为nds时刻在变,所以一旦不能访问,还是先找可访问的dns域名吧。

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 的子域名来访问GitHubimage-20210314085631281

image-20210314085649005

但是这种方法我是不大推荐的,因为这样不可以登录,也就是说不可以下载,如果想要下载则需要再新建一个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' } });
}
}

image-20210314090206152

玄学上网

这个东西,懂的都懂,建议开启pac模式,即国内网站走你自己的网络,已经被墙的网站走你节点的网络,但GitHub这种半墙不墙的状态,还是会走你的网络,那就建议您配置pac,并将GitHub的域名添加进去,具体方法因工具不同而异,您可以百度.如果还是不会可以加上我的qq好友,如果你没有玄学上网,也可以添加我的qq好友。

玄学上网需收费,白嫖党勿扰。

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