Skip to content

Commit

Permalink
feat: 移除cnpmjs github镜像的依赖 hunshcn#57
Browse files Browse the repository at this point in the history
  • Loading branch information
hunshcn committed Feb 15, 2022
1 parent a6dd217 commit 5dbaf35
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ github release、archive以及项目文件的加速项目,支持clone,有Clo

复制 [index.js](https://cdn.jsdelivr.net/hunshcn/gh-proxy@master/index.js) 到左侧代码框,`Save and deploy`。如果正常,右侧应显示首页。

`index.js`默认配置下clone走github.com.cnpmjs.org,项目文件会走jsDeliver,如需走worker,修改Config变量即可
`index.js`默认配置下项目文件会走jsDeliver,如需走worker,修改Config变量即可

`ASSET_URL`是静态资源的url(实际上就是现在显示出来的那个输入框单页面)

Expand Down Expand Up @@ -81,7 +81,7 @@ docker run -d --name="gh-proxy-py" \

python版本的机器如果无法正常访问github.io会启动报错,请自行修改静态文件url

workers版本默认配置下clone走github.com.cnpmjs.org,项目文件会走jsDeliver,如需走服务器,修改配置即可
workers版本默认配置下项目文件会走jsDeliver,_如需走服务器_,修改配置即可

python版本默认走服务器(2021.3.27更新)

Expand Down
8 changes: 2 additions & 6 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
DecodeError, ReadTimeoutError, ProtocolError)

# config
# git使用cnpmjs镜像、分支文件使用jsDelivr镜像的开关,0为关闭,默认关闭
# 分支文件使用jsDelivr镜像的开关,0为关闭,默认关闭
jsdelivr = 0
cnpmjs = 0
size_limit = 1024 * 1024 * 1024 * 999 # 允许的文件大小,默认999GB,相当于无限制了 https://github.com/hunshcn/gh-proxy/issues/8

"""
先生效白名单再匹配黑名单,pass_list匹配到的会直接302到jsdelivr/cnpmjs而忽略设置
先生效白名单再匹配黑名单,pass_list匹配到的会直接302到jsdelivr而忽略设置
生效顺序 白->黑->pass,可以前往https://github.com/hunshcn/gh-proxy/issues/41 查看示例
每个规则一行,可以封禁某个用户的所有仓库,也可以封禁某个用户的特定仓库,下方用黑名单示例,白名单同理
user1 # 封禁user1的所有仓库
Expand Down Expand Up @@ -142,9 +141,6 @@ def handler(u):
if (jsdelivr or pass_by) and exp2.match(u):
u = u.replace('/blob/', '@', 1).replace('github.com', 'cdn.jsdelivr.net/gh', 1)
return redirect(u)
elif (cnpmjs or pass_by) and exp3.match(u):
u = u.replace('github.com', 'github.com.cnpmjs.org', 1) + request.url.replace(request.base_url, '', 1)
return redirect(u)
elif (jsdelivr or pass_by) and exp4.match(u):
u = re.sub(r'(\.com/.*?/.+?)/(.+?/)', r'\1@\2', u, 1)
_u = u.replace('raw.githubusercontent.com', 'cdn.jsdelivr.net/gh', 1)
Expand Down
10 changes: 3 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
const ASSET_URL = 'https://hunshcn.github.io/gh-proxy/'
// 前缀,如果自定义路由为example.com/gh/*,将PREFIX改为 '/gh/',注意,少一个杠都会错!
const PREFIX = '/'
// git使用cnpmjs镜像、分支文件使用jsDelivr镜像的开关,0为关闭,默认开启
// 分支文件使用jsDelivr镜像的开关,0为关闭,默认开启
const Config = {
jsdelivr: 1,
cnpmjs: 1
jsdelivr: 1
}

/** @type {RequestInit} */
Expand Down Expand Up @@ -81,7 +80,7 @@ async function fetchHandler(e) {
}
// cfworker 会把路径中的 `//` 合并成 `/`
path = urlObj.href.substr(urlObj.origin.length + PREFIX.length).replace(/^https?:\/+/, 'https://')
if (path.search(exp1) === 0 || path.search(exp5) === 0 || !Config.cnpmjs && (path.search(exp3) === 0 || path.search(exp4) === 0)) {
if (path.search(exp1) === 0 || path.search(exp5) === 0 || (path.search(exp3) === 0 || path.search(exp4) === 0)) {
return httpHandler(req, path)
} else if (path.search(exp2) === 0) {
if (Config.jsdelivr) {
Expand All @@ -91,9 +90,6 @@ async function fetchHandler(e) {
path = path.replace('/blob/', '/raw/')
return httpHandler(req, path)
}
} else if (path.search(exp3) === 0) {
const newUrl = path.replace(/^(?:https?:\/\/)?github\.com/, 'https://github.com.cnpmjs.org')
return Response.redirect(newUrl, 302)
} else if (path.search(exp4) === 0) {
const newUrl = path.replace(/(?<=com\/.+?\/.+?)\/(.+?\/)/, '@$1').replace(/^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com/, 'https://cdn.jsdelivr.net/gh')
return Response.redirect(newUrl, 302)
Expand Down

0 comments on commit 5dbaf35

Please sign in to comment.