add size_limit

pull/30/head
hunshnet 2020-05-31 15:00:48 +08:00
parent 9963eab51c
commit b9ce795818
2 changed files with 10 additions and 3 deletions

View File

@ -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/`即可

View File

@ -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')