forked from hunshcn/gh-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
155 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
FROM tiangolo/uwsgi-nginx:python3.7 | ||
|
||
LABEL maintainer="Sebastian Ramirez <[email protected]>" | ||
|
||
RUN pip install flask requests | ||
|
||
COPY ./app /app | ||
WORKDIR /app | ||
|
||
# Make /app/* available to be imported by Python globally to better support several use cases like Alembic migrations. | ||
ENV PYTHONPATH=/app | ||
|
||
# Move the base entrypoint to reuse it | ||
RUN mv /entrypoint.sh /uwsgi-nginx-entrypoint.sh | ||
# Copy the entrypoint that will generate Nginx additional configs | ||
COPY entrypoint.sh /entrypoint.sh | ||
RUN chmod +x /entrypoint.sh | ||
|
||
ENTRYPOINT ["/entrypoint.sh"] | ||
|
||
# Run the start script provided by the parent image tiangolo/uwsgi-nginx. | ||
# It will check for an /app/prestart.sh script (e.g. for migrations) | ||
# And then will start Supervisor, which in turn will start Nginx and uWSGI | ||
|
||
EXPOSE 80 | ||
|
||
CMD ["/start.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# -*- coding: utf-8 -*- | ||
import re | ||
|
||
import requests | ||
from flask import Flask, Response, redirect, request | ||
|
||
# config | ||
# git使用cnpmjs镜像、分支文件使用jsDelivr镜像的开关,0为关闭,默认开启 | ||
jsdelivr = 1 | ||
cnpmjs = 1 | ||
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)\/.*$') | ||
exp2 = re.compile(r'^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob)\/.*$') | ||
exp3 = re.compile(r'^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-upload-pack).*$') | ||
|
||
|
||
@app.route('/') | ||
def index(): | ||
if 'q' in request.args: | ||
return redirect('/' + request.args.get('q')) | ||
return index_html | ||
|
||
|
||
@app.route('/<path:u>') | ||
def proxy(u): | ||
u = u if u.startswith('http') else 'https://' + u | ||
u = u.replace(':/g', ':https://g', 1) # uwsgi会将//传递为/ | ||
if jsdelivr and exp2.match(u): | ||
u = u.replace('/blob/', '@').replace('github.com', 'cdn.jsdelivr.net/gh') | ||
return redirect(u) | ||
elif cnpmjs and exp3.match(u): | ||
u = u.replace('github.com', 'github.com.cnpmjs.org') + request.url.replace(request.base_url, '') | ||
return redirect(u) | ||
# elif exp1.match(u): | ||
else: | ||
headers = {} | ||
r_headers = {} | ||
for i in ['Range', 'User-Agent']: | ||
if i in request.headers: | ||
r_headers[i] = request.headers.get(i) | ||
try: | ||
r = requests.get(u, headers=r_headers, stream=True) | ||
for i in ['Content-Type']: | ||
if i in r.headers: | ||
headers[i] = r.headers.get(i) | ||
if r.status_code == 200: | ||
headers = dict(r.headers) | ||
|
||
def generate(): | ||
for chunk in r.iter_content(chunk_size=CHUNK_SIZE): | ||
yield chunk | ||
|
||
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) | ||
# else: | ||
# return Response('Illegal input', status=403, mimetype='text/html; charset=UTF-8') | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(host=HOST, port=PORT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[uwsgi] | ||
module = main | ||
callable = app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#! /usr/bin/env bash | ||
set -e | ||
|
||
/uwsgi-nginx-entrypoint.sh | ||
|
||
# Get the listen port for Nginx, default to 80 | ||
USE_LISTEN_PORT=${LISTEN_PORT:-80} | ||
|
||
if [ -f /app/nginx.conf ]; then | ||
cp /app/nginx.conf /etc/nginx/nginx.conf | ||
else | ||
content_server='server {\n' | ||
content_server=$content_server" listen ${USE_LISTEN_PORT};\n" | ||
content_server=$content_server' location / {\n' | ||
content_server=$content_server' try_files $uri @app;\n' | ||
content_server=$content_server' }\n' | ||
content_server=$content_server' location @app {\n' | ||
content_server=$content_server' include uwsgi_params;\n' | ||
content_server=$content_server' uwsgi_pass unix:https:///tmp/uwsgi.sock;\n' | ||
content_server=$content_server' }\n' | ||
content_server=$content_server'}\n' | ||
# Save generated server /etc/nginx/conf.d/nginx.conf | ||
printf "$content_server" > /etc/nginx/conf.d/nginx.conf | ||
fi | ||
|
||
exec "$@" |