From b9ce7958181aa28da76c92e1542c9979eb2e7d13 Mon Sep 17 00:00:00 2001 From: hunshnet <337490703@qq.com> Date: Sun, 31 May 2020 15:00:48 +0800 Subject: [PATCH] add size_limit --- README.md | 4 ++++ app/main.py | 9 ++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d00ae5d..7064714 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,10 @@ github release、archive以及项目文件的加速项目,支持clone,有Clo [https://gh.api.99988866.xyz/](https://gh.api.99988866.xyz/) +## python版本和cf worker版本差异 + +- python版本支持进行文件大小限制,超过设定返回原地址 [issue #8](https://github.com/hunshcn/gh-proxy/issues/8) + ## 使用 直接在copy出来的url前加`https://gh.api.99988866.xyz/`即可 diff --git a/app/main.py b/app/main.py index 9681827..0209004 100644 --- a/app/main.py +++ b/app/main.py @@ -8,12 +8,12 @@ from flask import Flask, Response, redirect, request # git使用cnpmjs镜像、分支文件使用jsDelivr镜像的开关,0为关闭,默认开启 jsdelivr = 1 cnpmjs = 1 +size_limit = 1024 * 1024 * 1024 * 999 # 允许的文件大小,默认999GB,相当于无限制了 https://github.com/hunshcn/gh-proxy/issues/8 HOST = '127.0.0.1' # 监听地址,建议监听本地然后由web服务器反代 PORT = 80 # 监听端口 ASSET_URL = 'https://hunshcn.github.io/gh-proxy' # 主页 app = Flask(__name__) -app.debug = True CHUNK_SIZE = 1024 * 10 index_html = requests.get(ASSET_URL, timeout=10).text exp1 = re.compile(r'^(?:https?://)?github\.com/.+?/.+?/(?:releases|archive)/.*$') @@ -60,9 +60,12 @@ def proxy(u): headers = dict(r.headers) try: headers.pop('Transfer-Encoding') - except: + except KeyError: pass + if int(r.headers['Content-length']) > size_limit: + return redirect(u + request.url.replace(request.base_url, '', 1)) + def generate(): for chunk in r.iter_content(chunk_size=CHUNK_SIZE): yield chunk @@ -70,7 +73,7 @@ def proxy(u): return Response(generate(), headers=headers, status=r.status_code) except Exception as e: headers['content-type'] = 'text/html; charset=UTF-8' - return Response('server error' + str(e), status=500, headers=headers) + return Response('server error ' + str(e), status=500, headers=headers) # else: # return Response('Illegal input', status=403, mimetype='text/html; charset=UTF-8')