From 5dbaf35b144bca0d902b73c09488073b5c61bd22 Mon Sep 17 00:00:00 2001 From: hunsh Date: Tue, 15 Feb 2022 23:11:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A7=BB=E9=99=A4cnpmjs=20github?= =?UTF-8?q?=E9=95=9C=E5=83=8F=E7=9A=84=E4=BE=9D=E8=B5=96=20#57?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- app/main.py | 8 ++------ index.js | 10 +++------- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index e2b5117..5b96ccb 100644 --- a/README.md +++ b/README.md @@ -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(实际上就是现在显示出来的那个输入框单页面) @@ -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更新) diff --git a/app/main.py b/app/main.py index 808e030..e8a419d 100644 --- a/app/main.py +++ b/app/main.py @@ -12,13 +12,12 @@ from urllib3.exceptions import ( 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的所有仓库 @@ -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) diff --git a/index.js b/index.js index 29a7212..5f5db09 100644 --- a/index.js +++ b/index.js @@ -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} */ @@ -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) { @@ -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)