gh-proxy/app/main.py

138 lines
5.2 KiB
Python
Raw Normal View History

2020-04-09 14:26:51 +00:00
# -*- coding: utf-8 -*-
import re
import requests
from flask import Flask, Response, redirect, request
2020-11-06 04:50:39 +00:00
from requests.exceptions import (
ChunkedEncodingError,
ContentDecodingError, ConnectionError, StreamConsumedError)
from requests.utils import (
2021-03-29 07:04:05 +00:00
stream_decode_response_unicode, iter_slices, CaseInsensitiveDict)
2020-11-06 04:50:39 +00:00
from urllib3.exceptions import (
DecodeError, ReadTimeoutError, ProtocolError)
2020-04-09 14:26:51 +00:00
# config
2021-03-27 12:08:23 +00:00
# git使用cnpmjs镜像、分支文件使用jsDelivr镜像的开关0为关闭默认关闭
jsdelivr = 0
cnpmjs = 0
2020-05-31 07:00:48 +00:00
size_limit = 1024 * 1024 * 1024 * 999 # 允许的文件大小默认999GB相当于无限制了 https://github.com/hunshcn/gh-proxy/issues/8
2020-04-09 14:26:51 +00:00
HOST = '127.0.0.1' # 监听地址建议监听本地然后由web服务器反代
PORT = 80 # 监听端口
ASSET_URL = 'https://hunshcn.github.io/gh-proxy' # 主页
app = Flask(__name__)
CHUNK_SIZE = 1024 * 10
index_html = requests.get(ASSET_URL, timeout=10).text
icon_r = requests.get(ASSET_URL + '/favicon.ico', timeout=10).content
exp1 = re.compile(r'^(?:https?://)?github\.com/.+?/.+?/(?:releases|archive)/.*$')
exp2 = re.compile(r'^(?:https?://)?github\.com/.+?/.+?/(?:blob)/.*$')
2020-06-10 12:09:21 +00:00
exp3 = re.compile(r'^(?:https?://)?github\.com/.+?/.+?/(?:info|git-).*$')
exp4 = re.compile(r'^(?:https?://)?raw\.githubusercontent\.com/.+?/.+?/.+?/.+$')
2021-03-16 07:54:52 +00:00
exp5 = re.compile(r'^(?:https?://)?gist\.(?:githubusercontent|github)\.com/.+?/.+?/.+$')
2020-04-09 14:26:51 +00:00
2021-03-29 07:04:05 +00:00
requests.sessions.default_headers = lambda: CaseInsensitiveDict()
2020-04-09 14:26:51 +00:00
@app.route('/')
def index():
if 'q' in request.args:
return redirect('/' + request.args.get('q'))
return index_html
@app.route('/favicon.ico')
def icon():
return Response(icon_r, content_type='image/vnd.microsoft.icon')
2020-11-06 04:50:39 +00:00
def iter_content(self, chunk_size=1, decode_unicode=False):
"""rewrite requests function, set decode_content with False"""
def generate():
# Special case for urllib3.
if hasattr(self.raw, 'stream'):
try:
for chunk in self.raw.stream(chunk_size, decode_content=False):
yield chunk
except ProtocolError as e:
raise ChunkedEncodingError(e)
except DecodeError as e:
raise ContentDecodingError(e)
except ReadTimeoutError as e:
raise ConnectionError(e)
else:
# Standard file-like object.
while True:
chunk = self.raw.read(chunk_size)
if not chunk:
break
yield chunk
self._content_consumed = True
if self._content_consumed and isinstance(self._content, bool):
raise StreamConsumedError()
elif chunk_size is not None and not isinstance(chunk_size, int):
raise TypeError("chunk_size must be an int, it is instead a %s." % type(chunk_size))
# simulate reading small chunks of the content
reused_chunks = iter_slices(self._content, chunk_size)
stream_chunks = generate()
chunks = reused_chunks if self._content_consumed else stream_chunks
if decode_unicode:
chunks = stream_decode_response_unicode(chunks, self)
return chunks
2020-05-09 08:00:22 +00:00
@app.route('/<path:u>', methods=['GET', 'POST'])
2020-04-09 14:26:51 +00:00
def proxy(u):
u = u if u.startswith('http') else 'https://' + u
2021-05-30 04:54:27 +00:00
if u.rfind('://', 3, 9) == -1:
u = u.replace('s:/', 's://', 1) # uwsgi会将//传递为/
2021-03-16 07:40:54 +00:00
if not any([i.match(u) for i in [exp1, exp2, exp3, exp4, exp5]]):
return Response('Invalid input.', status=403)
2020-04-09 14:26:51 +00:00
if jsdelivr and exp2.match(u):
u = u.replace('/blob/', '@', 1).replace('github.com', 'cdn.jsdelivr.net/gh', 1)
2020-04-09 14:26:51 +00:00
return redirect(u)
elif cnpmjs and exp3.match(u):
u = u.replace('github.com', 'github.com.cnpmjs.org', 1) + request.url.replace(request.base_url, '', 1)
2020-04-09 14:26:51 +00:00
return redirect(u)
elif jsdelivr and exp4.match(u):
2020-08-19 10:13:42 +00:00
u = re.sub(r'(\.com/.*?/.+?)/(.+?/)', r'\1@\2', u, 1)
u = u.replace('raw.githubusercontent.com', 'cdn.jsdelivr.net/gh', 1)
return redirect(u)
2020-04-09 14:26:51 +00:00
else:
if exp2.match(u):
u = u.replace('/blob/', '/raw/', 1)
2020-04-09 14:26:51 +00:00
headers = {}
2021-03-27 13:34:16 +00:00
r_headers = dict(request.headers)
if 'Host' in r_headers:
r_headers.pop('Host')
2020-04-09 14:26:51 +00:00
try:
2020-08-20 10:37:41 +00:00
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)
2021-03-27 12:08:23 +00:00
headers = dict(r.headers)
2020-04-09 14:26:51 +00:00
2020-08-19 10:13:42 +00:00
if 'Content-length' in r.headers and int(r.headers['Content-length']) > size_limit:
2020-05-31 07:00:48 +00:00
return redirect(u + request.url.replace(request.base_url, '', 1))
2020-04-09 14:26:51 +00:00
def generate():
2020-11-06 04:50:39 +00:00
for chunk in iter_content(r, chunk_size=CHUNK_SIZE):
2020-04-09 14:26:51 +00:00
yield chunk
return Response(generate(), headers=headers, status=r.status_code)
except Exception as e:
headers['content-type'] = 'text/html; charset=UTF-8'
2020-05-31 07:00:48 +00:00
return Response('server error ' + str(e), status=500, headers=headers)
2020-04-09 14:26:51 +00:00
# else:
# return Response('Illegal input', status=403, mimetype='text/html; charset=UTF-8')
if __name__ == '__main__':
app.run(host=HOST, port=PORT)