如何使github访问更快?网友:惊呆了!

avatar
avatar
Liues
69
文章
142
评论
2020年3月29日19:38:06如何使github访问更快?网友:惊呆了!已关闭评论 2,382 6574字阅读21分54秒

GitHub是什么

GitHub是一个开源的托管服务,有点像代码的云。它以各种不同的编程语言托管您的源代码项目,并跟踪每次迭代所做的各种更改。该服务可以通过使用git(一种在命令行界面中运行的修订控制系统)来完成此操作。

其他来源类似于GitHub - 包括BitBucket,Microsoft Team Foundation Server等等 - 但如果您希望更多的人看到您的项目,那么社区的庞大规模应该对您很重要。截至2018年,GitHub报告的用户数超过2800万,远远超过竞争对手。

其他差异涉及成本。GitHub仅提供额外费用提供私有存储库,而其他一些服务则免费提供私有存储库。然而,这些通常具有有限的存储和带宽。

为什么访问会慢

废话,人家是国外服务,当然访问慢(我没说)

一、使用代理服务上网

这个方法无论在什么时候都管用,不多赘述,你们都有

二、使用或者搭建Github反代网站

使用Cloudflare Workers进行反代

Cloudflare提供了边缘云计算,请求量免费额度为10W,对于个人来说足够使用,下面贴出代码

// Website you intended to retrieve for users.
const upstream = 'github.com'

// Custom pathname for the upstream website.
const upstream_path = '/'

// Website you intended to retrieve for users using mobile devices.
const upstream_mobile = 'github.com'

// Countries and regions where you wish to suspend your service.
const blocked_region = ['KP', 'SY', 'PK', 'CU']

// IP addresses which you wish to block from using your service.
const blocked_ip_address = ['0.0.0.0', '127.0.0.1']

// Whether to use HTTPS protocol for upstream address.
const https = true

// Replace texts.
const replace_dict = {
    '$upstream': '$custom_domain',
    '//google.com': ''
}

addEventListener('fetch', event => {
    event.respondWith(fetchAndApply(event.request));
})

async function fetchAndApply(request) {

    const region = request.headers.get('cf-ipcountry').toUpperCase();
    const ip_address = request.headers.get('cf-connecting-ip');
    const user_agent = request.headers.get('user-agent');

    let response = null;
    let url = new URL(request.url);
    let url_hostname = url.hostname;

    if (https == true) {
        url.protocol = 'https:';
    } else {
        url.protocol = 'http:';
    }

    if (await device_status(user_agent)) {
        var upstream_domain = upstream;
    } else {
        var upstream_domain = upstream_mobile;
    }

    url.host = upstream_domain;
    if (url.pathname == '/') {
        url.pathname = upstream_path;
    } else {
        url.pathname = upstream_path + url.pathname;
    }

    if (blocked_region.includes(region)) {
        response = new Response('Access denied: WorkersProxy is not available in your region yet.', {
            status: 403
        });
    } else if (blocked_ip_address.includes(ip_address)) {
        response = new Response('Access denied: Your IP address is blocked by WorkersProxy.', {
            status: 403
        });
    } else {
        let method = request.method;
        let request_headers = request.headers;
        let new_request_headers = new Headers(request_headers);

        new_request_headers.set('Host', url.hostname);
        new_request_headers.set('Referer', url.hostname);

        let original_response = await fetch(url.href, {
            method: method,
            headers: new_request_headers
        })

        let original_response_clone = original_response.clone();
        let original_text = null;
        let response_headers = original_response.headers;
        let new_response_headers = new Headers(response_headers);
        let status = original_response.status;

        new_response_headers.set('access-control-allow-origin', '*');
        new_response_headers.set('access-control-allow-credentials', true);
        new_response_headers.delete('content-security-policy');
        new_response_headers.delete('content-security-policy-report-only');
        new_response_headers.delete('clear-site-data');

        const content_type = new_response_headers.get('content-type');
        if (content_type.includes('text/html') && content_type.includes('UTF-8')) {
            original_text = await replace_response_text(original_response_clone, upstream_domain, url_hostname);
        } else {
            original_text = original_response_clone.body
        }

        response = new Response(original_text, {
            status,
            headers: new_response_headers
        })
    }
    return response;
}

async function replace_response_text(response, upstream_domain, host_name) {
    let text = await response.text()

    var i, j;
    for (i in replace_dict) {
        j = replace_dict[i]
        if (i == '$upstream') {
            i = upstream_domain
        } else if (i == '$custom_domain') {
            i = host_name
        }

        if (j == '$upstream') {
            j = upstream_domain
        } else if (j == '$custom_domain') {
            j = host_name
        }

        let re = new RegExp(i, 'g')
        text = text.replace(re, j);
    }
    return text;
}


async function device_status(user_agent_info) {
    var agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < agents.length; v++) {
        if (user_agent_info.indexOf(agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}

项目地址:https://github.com/Liuescn/CloudFlare-Workers

直链:https://raw.githubusercontent.com/xs314/CloudFlare-Workers/master/index.js

优点:能加快下载速度

缺点:无法使用cookie,无法进行登录操作

三、使用Nginx进行反代

设置upstream(上游),ip地址可以通过dig github.com获取,或者比较懒的,直接server github.com:443,它会自动解析。

upstream github {
    server 192.30.253.112:443;
    server 192.30.253.113:443;
    keepalive 16;#设置连接池加快访问速度。
}

配置https,也可以直接使用http,将listen 443 ssl http2 reuseport;替换成listen 80;就可以。

server
{
    listen 443 ssl http2 reuseport;
    ssl_certificate ssl/p.ghostcir.com.pem;
    ssl_certificate_key ssl/p.ghostcir.com.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_session_timeout      1d;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache        shared:SSL:50m;
    ssl_session_tickets      on;
    ssl_stapling             on;
    server_name p.ghostcir.com; #绑定的域名

屏蔽搜索引擎

    if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot") #防止搜索引擎收录
    {
        return 403;
    }

配置反向代理

    location / {
        proxy_set_header Accept-Encoding ""; #不使用压缩,如gzip
        proxy_set_header Connection "";
        proxy_http_version 1.1; #使用http1.1长连接
        proxy_connect_timeout    10s; #设置连接超时
        proxy_read_timeout       10s; #设置读取超时
        proxy_set_header Host github.com;
        proxy_hide_header Strict-Transport-Security; #隐藏协议头,避免因为反向代理开启hsts
        proxy_pass https://github;
    }
}

最后贴一下完整规则

upstream github {
    server 192.30.253.112:443;
    server 192.30.253.113:443;
}

server
{
    listen 443 ssl http2 reuseport;
    ssl_certificate ssl/p.ghostcir.com.pem;
    ssl_certificate_key ssl/p.ghostcir.com.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_session_timeout      1d;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache        shared:SSL:50m;
    ssl_session_tickets      on;
    ssl_stapling             on;
    server_name p.ghostcir.com; #绑定的域名

    if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot") #防止搜索引擎收录
    {
        return 403;
    }

    location / {
        proxy_set_header Accept-Encoding "";
        proxy_set_header Connection "";
        proxy_http_version 1.1;
        proxy_connect_timeout    10s;
        proxy_read_timeout       10s;
        proxy_set_header Host github.com;
        proxy_hide_header Strict-Transport-Security; #隐藏协议头,避免因为反向代理开启hsts
        proxy_pass https://github;
    }
}

四、使用火狐DoH+ESNI

https://www.zhihu.com/question/382718053/answer/1107698817

实测有效,跟我大喊:Firefox!

(不过此法不能解决下载慢

写在文末

这里提供一些Demo:

--Github介绍来源:https://www.php.cn/tool/git/413611.html

--nginx反代来源:https://ghostcir.com/s/128.html

--其他方法来源:https://blog.shiyunhong.com/3408.html

继续阅读
weinxin
滑稽社
流星博客建立的官方交流群,包含技术交流|随意闲聊|好友扩列|热心管理在一体的技术群
avatar