Browse Source

Merge pull request #30 from hunshcn/dev

fix: 400 problem
pull/49/head
hunshcn 4 years ago committed by GitHub
parent
commit
e4872c931d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      README.md
  2. 24
      app/main.py

5
README.md

@ -66,7 +66,10 @@ docker run -d --name="gh-proxy-py" \
### 注意
python版本的机器如果无法正常访问github.io会启动报错,请自行修改静态文件url
默认配置下clone走github.com.cnpmjs.org,项目文件会走jsDeliver,如需走服务器,修改配置即可
workers版本默认配置下clone走github.com.cnpmjs.org,项目文件会走jsDeliver,如需走服务器,修改配置即可
python版本默认走服务器(2021.3.27更新)
## Cloudflare Workers计费

24
app/main.py

@ -12,9 +12,9 @@ from urllib3.exceptions import (
DecodeError, ReadTimeoutError, ProtocolError)
# config
# git使用cnpmjs镜像、分支文件使用jsDelivr镜像的开关,0为关闭,默认开启
jsdelivr = 1
cnpmjs = 1
# git使用cnpmjs镜像、分支文件使用jsDelivr镜像的开关,0为关闭,默认关闭
jsdelivr = 0
cnpmjs = 0
size_limit = 1024 * 1024 * 1024 * 999 # 允许的文件大小,默认999GB,相当于无限制了 https://github.com/hunshcn/gh-proxy/issues/8
HOST = '127.0.0.1' # 监听地址,建议监听本地然后由web服务器反代
PORT = 80 # 监听端口
@ -105,25 +105,15 @@ def proxy(u):
if exp2.match(u):
u = u.replace('/blob/', '/raw/', 1)
headers = {}
r_headers = {}
for i in ['Range', 'User-Agent', 'Accept', 'Content-Type', 'Content-Length', 'Content-Encoding']:
if i in request.headers:
r_headers[i] = request.headers.get(i)
r_headers['Accept-Encoding'] = request.headers.get('Accept-Encoding', 'identity')
r_headers = dict(request.headers)
if 'Host' in r_headers:
r_headers.pop('Host')
try:
url = u + request.url.replace(request.base_url, '', 1)
if url.startswith('https:/') and not url.startswith('https://'):
url = 'https://' + url[7:]
r = requests.request(method=request.method, url=url, data=request.data, headers=r_headers, stream=True)
for i in ['Content-Range', 'Content-Type']:
if i in r.headers:
headers[i] = r.headers.get(i)
if r.status_code == 200:
headers = dict(r.headers)
try:
headers.pop('Transfer-Encoding')
except KeyError:
pass
headers = dict(r.headers)
if 'Content-length' in r.headers and int(r.headers['Content-length']) > size_limit:
return redirect(u + request.url.replace(request.base_url, '', 1))

Loading…
Cancel
Save