如何搭建Github代理加速

前言

最近在网上浏览时,突然发现有很多人想要去Github上进行学习,但是苦于Github在国内的访问速度实在太慢,连进去都很费劲,有的时候还直接进不去的东西,更别说下载里面的优质开源项目以及安装包了,为了方便大家快乐的访问Github,我便想着看能不能做一个教程,来帮助浏览GitHub。在寻找了大量的资料后,最终突然有了想法。
是否可以用cloudflare去部署一个worker,然后用这个worker去帮我们访问GitHub,实际上是这个worker去代理我们的流量,从而使我们可以在Github上下载文件和访问了,并且速度肯定是比官方的快的。
下面我们开始配置。

准备条件

  • 一个Cloudflare账号,可以去官网自己注册,这里就不多做赘述了
  • 一台电脑
  • 一双可以活动的手
  • 一个域名

开始配置

首先打开cloudflare,在边栏点击workers和pages,点击创建,然后点击创建worker
alt text

进入后将名字改为github,然后再点击创建,由于我这里创建过了所以会显示不可用
alt text

创建完成后,点击编辑代码,将原先的内容全部删除
alt text

在其中填入下列代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
function logError(request, message) {
console.error(
`${message}, clientIp: ${request.headers.get(
"cf-connecting-ip"
)}, user-agent: ${request.headers.get("user-agent")}, url: ${request.url}`
);
}

function createNewRequest(request, url, proxyHostname, originHostname) {
const newRequestHeaders = new Headers(request.headers);
for (const [key, value] of newRequestHeaders) {
if (value.includes(originHostname)) {
newRequestHeaders.set(
key,
value.replace(
new RegExp(`(?<!\\.)\\b${originHostname}\\b`, "g"),
proxyHostname
)
);
}
}
return new Request(url.toString(), {
method: request.method,
headers: newRequestHeaders,
body: request.body,
});
}

function setResponseHeaders(
originalResponse,
proxyHostname,
originHostname,
DEBUG
) {
const newResponseHeaders = new Headers(originalResponse.headers);
for (const [key, value] of newResponseHeaders) {
if (value.includes(proxyHostname)) {
newResponseHeaders.set(
key,
value.replace(
new RegExp(`(?<!\\.)\\b${proxyHostname}\\b`, "g"),
originHostname
)
);
}
}
if (DEBUG) {
newResponseHeaders.delete("content-security-policy");
}
return newResponseHeaders;
}

/**
* 替换内容
* @param originalResponse 响应
* @param proxyHostname 代理地址 hostname
* @param pathnameRegex 代理地址路径匹配的正则表达式
* @param originHostname 替换的字符串
* @returns {Promise<*>}
*/
async function replaceResponseText(
originalResponse,
proxyHostname,
pathnameRegex,
originHostname
) {
let text = await originalResponse.text();
if (pathnameRegex) {
pathnameRegex = pathnameRegex.replace(/^\^/, "");
return text.replace(
new RegExp(`((?<!\\.)\\b${proxyHostname}\\b)(${pathnameRegex})`, "g"),
`${originHostname}$2`
);
} else {
return text.replace(
new RegExp(`(?<!\\.)\\b${proxyHostname}\\b`, "g"),
originHostname
);
}
}

async function nginx() {
return `<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>`;
}

export default {
async fetch(request, env, ctx) {
try {
const {
PROXY_HOSTNAME,
PROXY_PROTOCOL = "https",
PATHNAME_REGEX,
UA_WHITELIST_REGEX,
UA_BLACKLIST_REGEX,
URL302,
IP_WHITELIST_REGEX,
IP_BLACKLIST_REGEX,
REGION_WHITELIST_REGEX,
REGION_BLACKLIST_REGEX,
DEBUG = false,
} = env;
const url = new URL(request.url);
const originHostname = url.hostname;
if (
!PROXY_HOSTNAME ||
(PATHNAME_REGEX && !new RegExp(PATHNAME_REGEX).test(url.pathname)) ||
(UA_WHITELIST_REGEX &&
!new RegExp(UA_WHITELIST_REGEX).test(
request.headers.get("user-agent").toLowerCase()
)) ||
(UA_BLACKLIST_REGEX &&
new RegExp(UA_BLACKLIST_REGEX).test(
request.headers.get("user-agent").toLowerCase()
)) ||
(IP_WHITELIST_REGEX &&
!new RegExp(IP_WHITELIST_REGEX).test(
request.headers.get("cf-connecting-ip")
)) ||
(IP_BLACKLIST_REGEX &&
new RegExp(IP_BLACKLIST_REGEX).test(
request.headers.get("cf-connecting-ip")
)) ||
(REGION_WHITELIST_REGEX &&
!new RegExp(REGION_WHITELIST_REGEX).test(
request.headers.get("cf-ipcountry")
)) ||
(REGION_BLACKLIST_REGEX &&
new RegExp(REGION_BLACKLIST_REGEX).test(
request.headers.get("cf-ipcountry")
))
) {
logError(request, "Invalid");
return URL302
? Response.redirect(URL302, 302)
: new Response(await nginx(), {
headers: {
"Content-Type": "text/html; charset=utf-8",
},
});
}
url.host = PROXY_HOSTNAME;
url.protocol = PROXY_PROTOCOL;
const newRequest = createNewRequest(
request,
url,
PROXY_HOSTNAME,
originHostname
);
const originalResponse = await fetch(newRequest);
const newResponseHeaders = setResponseHeaders(
originalResponse,
PROXY_HOSTNAME,
originHostname,
DEBUG
);
const contentType = newResponseHeaders.get("content-type") || "";
let body;
if (contentType.includes("text/")) {
body = await replaceResponseText(
originalResponse,
PROXY_HOSTNAME,
PATHNAME_REGEX,
originHostname
);
} else {
body = originalResponse.body;
}
return new Response(body, {
status: originalResponse.status,
headers: newResponseHeaders,
});
} catch (error) {
logError(request, `Fetch error: ${error.message}`);
return new Response("Internal Server Error", { status: 500 });
}
},
};

输入后点击部署,保存后回到worker主界面,点击设置,在域和路由中点击添加,然后添加自定义域,这里输入自己的域名,可以添加子域名,例如我的就是github.twindy.top,添加完成点击添加域
alt text

接着在变量与机密中点击添加环境变量

变量名称 变量值 备注
PROXY_HOSTNAME github.com 代理地址 hostname
PATHNAME_REGEX ^/Tomorini/Tomorini.github.io 代理地址路径正则表达式,这里的Tomorini需要改成你所访问的Github用户名称,Tomorini.github.io则改成他的GitHub仓库名,如果不添加的话就是代理他账户下的所有仓库

添加完变量名后点击部署即可
alt text

添加完这两个变量后,就可以开始使用了

使用方法

想要去访问Github仓库以及下载文件,只需在网址栏中输入你的代理域名,比如我的就是github.twindy.top,接着在代理域名后面输入该变量名PATHNAME_REGEX的变量值,即/Tomorini/Tomorini.github.io,所以完整的地址就是 github.twindy.top/Tomorini/Tomorini.github.io,接着回车后就可以访问这个GitHub仓库了
alt text
这里可以看到我输入这串网址后成功进入了GitHub,并且速度比直接访问快了很多

其他问题

  • 如果想要访问其他仓库的话,只需把PATHNAME_REGEX的变量值更改成需要访问的GitHub用户名和仓库名。注意开头要加^
  • 不要把变量值改成代理GitHub全站!!!否则请求过多的话,会被cloudflare认为是异常流量导致账号被封

总结

总的来说,这种方法对于访问GitHub有奇效,关键是白嫖的,所以比较适合爱折腾的人去试试,有任何问题可以留言!

友情链接

参考B站视频教程

相关github仓库

非常感谢!