Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import re
|
2 |
import logging
|
|
|
3 |
from flask import Flask, request, Response
|
4 |
import requests
|
5 |
from urllib.parse import urlparse, unquote
|
@@ -8,9 +9,15 @@ from urllib.parse import urlparse, unquote
|
|
8 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
9 |
app = Flask(__name__)
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# --- Whitelisted URL Patterns for GitHub ---
|
12 |
-
# This list defines all URL patterns that are permitted to be proxied.
|
13 |
-
# It's a security measure to prevent the proxy from being used to access unintended domains.
|
14 |
ALLOWED_PATTERNS = [
|
15 |
# Repositories: releases, archives, blobs, raw content
|
16 |
re.compile(r'^https://github\.com/[^/]+/[^/]+/(?:releases|archive)/.*$', re.IGNORECASE),
|
@@ -33,35 +40,34 @@ ALLOWED_PATTERNS = [
|
|
33 |
re.compile(r'^https://github\.com/[^/]+/[^/]+/?$', re.IGNORECASE),
|
34 |
]
|
35 |
|
36 |
-
# --- Custom Index Page ---
|
37 |
-
INDEX_PAGE_HTML = """
|
38 |
<!DOCTYPE html>
|
39 |
<html lang="en">
|
40 |
<head>
|
41 |
<meta charset="UTF-8">
|
42 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
43 |
-
<title>GitHub Proxy</title>
|
44 |
<style>
|
45 |
-
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; line-height: 1.6; color: #333; max-width: 800px; margin: 40px auto; padding: 20px; }
|
46 |
-
.container { border: 1px solid #ddd; border-radius: 8px; padding: 20px 40px; background-color: #f9f9f9; }
|
47 |
-
.warning { color: #856404; background-color: #fff3cd; border: 1px solid #ffeeba; padding: 15px; border-radius: 4px; margin-bottom: 20px; }
|
48 |
-
h1, h2 { border-bottom: 1px solid #eaecef; padding-bottom: 0.3em; }
|
49 |
-
code { background-color: #eef; padding: 2px 4px; border-radius: 3px; }
|
50 |
</style>
|
51 |
</head>
|
52 |
<body>
|
53 |
<div class="container">
|
54 |
-
<h1>GitHub Reverse Proxy</h1>
|
55 |
<div class="warning">
|
56 |
-
<strong>
|
57 |
-
All content served from this page is provided directly by GitHub.
|
58 |
</div>
|
59 |
<h2>How to Use</h2>
|
60 |
-
<p>To access GitHub content,
|
61 |
<p>For example, to clone a repository:</p>
|
62 |
-
<code>git clone {YOUR_PROXY_URL}/https://github.com/owner/repo.git</code>
|
63 |
<p>Or to view a repository page:</p>
|
64 |
-
<code>{YOUR_PROXY_URL}/https://github.com/owner/repo</code>
|
65 |
</div>
|
66 |
</body>
|
67 |
</html>
|
@@ -74,24 +80,31 @@ def is_url_allowed(url):
|
|
74 |
return True
|
75 |
return False
|
76 |
|
77 |
-
# --- Core Proxy Logic ---
|
78 |
@app.route('/', defaults={'path': ''}, methods=['GET', 'POST', 'PUT', 'DELETE'])
|
79 |
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
|
80 |
def proxy(path):
|
81 |
"""
|
82 |
-
|
83 |
-
|
84 |
"""
|
85 |
-
#
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
-
#
|
91 |
-
#
|
92 |
-
if
|
93 |
return INDEX_PAGE_HTML, 200
|
94 |
|
|
|
|
|
95 |
# Prepend 'https://' if the scheme is missing.
|
96 |
if not target_path.startswith(('http://', 'https://')):
|
97 |
target_url = 'https://' + target_path
|
@@ -100,7 +113,7 @@ def proxy(path):
|
|
100 |
|
101 |
# Security check: Ensure the URL is in the whitelist.
|
102 |
if not is_url_allowed(target_url):
|
103 |
-
logging.warning(f"URL Denied!
|
104 |
return "<h1>403 Forbidden</h1><p>Request blocked by proxy security policy.</p>", 403
|
105 |
|
106 |
try:
|
@@ -116,24 +129,19 @@ def proxy(path):
|
|
116 |
headers['Host'] = target_host
|
117 |
|
118 |
try:
|
119 |
-
# Stream the request to handle large files efficiently.
|
120 |
resp = requests.request(
|
121 |
method=request.method,
|
122 |
url=target_url,
|
123 |
headers=headers,
|
124 |
data=request.get_data(),
|
125 |
cookies=request.cookies,
|
126 |
-
allow_redirects=False,
|
127 |
stream=True,
|
128 |
-
timeout=30
|
129 |
)
|
130 |
-
|
131 |
-
# Exclude headers that can interfere with streaming.
|
132 |
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
133 |
response_headers = [(name, value) for (name, value) in resp.raw.headers.items()
|
134 |
if name.lower() not in excluded_headers]
|
135 |
-
|
136 |
-
# Stream the response back to the original client.
|
137 |
return Response(resp.iter_content(chunk_size=8192), resp.status_code, response_headers)
|
138 |
|
139 |
except requests.exceptions.RequestException as e:
|
|
|
1 |
import re
|
2 |
import logging
|
3 |
+
import os
|
4 |
from flask import Flask, request, Response
|
5 |
import requests
|
6 |
from urllib.parse import urlparse, unquote
|
|
|
9 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
10 |
app = Flask(__name__)
|
11 |
|
12 |
+
# --- Authentication Configuration ---
|
13 |
+
# Read the secret key from an environment variable.
|
14 |
+
# The service will not start if this key is not set for security reasons.
|
15 |
+
SECRET_KEY = os.environ.get('PROXY_SECRET_KEY')
|
16 |
+
if not SECRET_KEY:
|
17 |
+
logging.critical("FATAL: Environment variable PROXY_SECRET_KEY is not set. Service cannot start.")
|
18 |
+
exit("Error: The PROXY_SECRET_KEY environment variable must be set.")
|
19 |
+
|
20 |
# --- Whitelisted URL Patterns for GitHub ---
|
|
|
|
|
21 |
ALLOWED_PATTERNS = [
|
22 |
# Repositories: releases, archives, blobs, raw content
|
23 |
re.compile(r'^https://github\.com/[^/]+/[^/]+/(?:releases|archive)/.*$', re.IGNORECASE),
|
|
|
40 |
re.compile(r'^https://github\.com/[^/]+/[^/]+/?$', re.IGNORECASE),
|
41 |
]
|
42 |
|
43 |
+
# --- Custom Index Page (Updated with Authentication Info) ---
|
44 |
+
INDEX_PAGE_HTML = f"""
|
45 |
<!DOCTYPE html>
|
46 |
<html lang="en">
|
47 |
<head>
|
48 |
<meta charset="UTF-8">
|
49 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
50 |
+
<title>Private GitHub Proxy</title>
|
51 |
<style>
|
52 |
+
body {{ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; line-height: 1.6; color: #333; max-width: 800px; margin: 40px auto; padding: 20px; }}
|
53 |
+
.container {{ border: 1px solid #ddd; border-radius: 8px; padding: 20px 40px; background-color: #f9f9f9; }}
|
54 |
+
.warning {{ color: #856404; background-color: #fff3cd; border: 1px solid #ffeeba; padding: 15px; border-radius: 4px; margin-bottom: 20px; }}
|
55 |
+
h1, h2 {{ border-bottom: 1px solid #eaecef; padding-bottom: 0.3em; }}
|
56 |
+
code {{ background-color: #eef; padding: 2px 4px; border-radius: 3px; font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace; }}
|
57 |
</style>
|
58 |
</head>
|
59 |
<body>
|
60 |
<div class="container">
|
61 |
+
<h1>Private GitHub Reverse Proxy</h1>
|
62 |
<div class="warning">
|
63 |
+
<strong>Authentication Required:</strong> This is a private proxy. You must include your secret key in the URL to access content.
|
|
|
64 |
</div>
|
65 |
<h2>How to Use</h2>
|
66 |
+
<p>To access GitHub content, prepend your secret key to the GitHub URL.</p>
|
67 |
<p>For example, to clone a repository:</p>
|
68 |
+
<code>git clone {{YOUR_PROXY_URL}}/{SECRET_KEY}/https://github.com/owner/repo.git</code>
|
69 |
<p>Or to view a repository page:</p>
|
70 |
+
<code>{{YOUR_PROXY_URL}}/{SECRET_KEY}/https://github.com/owner/repo</code>
|
71 |
</div>
|
72 |
</body>
|
73 |
</html>
|
|
|
80 |
return True
|
81 |
return False
|
82 |
|
83 |
+
# --- Core Proxy Logic (Updated with Authentication) ---
|
84 |
@app.route('/', defaults={'path': ''}, methods=['GET', 'POST', 'PUT', 'DELETE'])
|
85 |
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
|
86 |
def proxy(path):
|
87 |
"""
|
88 |
+
Authenticates the request via a secret key in the path, then proxies
|
89 |
+
the request to GitHub after validating it against a whitelist.
|
90 |
"""
|
91 |
+
# Split the path to separate the secret key from the target URL.
|
92 |
+
# e.g., "mysecretkey/https://github.com/user/repo" -> ["mysecretkey", "https://..."]
|
93 |
+
path_parts = path.split('/', 1)
|
94 |
+
|
95 |
+
# --- Authentication Check ---
|
96 |
+
# The path must contain a key. If not, or if the key is wrong, deny access.
|
97 |
+
if len(path_parts) < 1 or path_parts[0] != SECRET_KEY:
|
98 |
+
logging.warning(f"Authentication failed for request from {request.remote_addr}. Path: '{path}'")
|
99 |
+
return "<h1>401 Unauthorized</h1><p>A valid secret key is required in the URL path.</p>", 401
|
100 |
|
101 |
+
# If the key is correct but there is no target URL, show the index page.
|
102 |
+
# This happens when accessing /<secret_key>/
|
103 |
+
if len(path_parts) == 1 or not path_parts[1]:
|
104 |
return INDEX_PAGE_HTML, 200
|
105 |
|
106 |
+
target_path = unquote(path_parts[1])
|
107 |
+
|
108 |
# Prepend 'https://' if the scheme is missing.
|
109 |
if not target_path.startswith(('http://', 'https://')):
|
110 |
target_url = 'https://' + target_path
|
|
|
113 |
|
114 |
# Security check: Ensure the URL is in the whitelist.
|
115 |
if not is_url_allowed(target_url):
|
116 |
+
logging.warning(f"URL Denied! Key was correct, but pattern not matched: {target_url}")
|
117 |
return "<h1>403 Forbidden</h1><p>Request blocked by proxy security policy.</p>", 403
|
118 |
|
119 |
try:
|
|
|
129 |
headers['Host'] = target_host
|
130 |
|
131 |
try:
|
|
|
132 |
resp = requests.request(
|
133 |
method=request.method,
|
134 |
url=target_url,
|
135 |
headers=headers,
|
136 |
data=request.get_data(),
|
137 |
cookies=request.cookies,
|
138 |
+
allow_redirects=False,
|
139 |
stream=True,
|
140 |
+
timeout=30
|
141 |
)
|
|
|
|
|
142 |
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
143 |
response_headers = [(name, value) for (name, value) in resp.raw.headers.items()
|
144 |
if name.lower() not in excluded_headers]
|
|
|
|
|
145 |
return Response(resp.iter_content(chunk_size=8192), resp.status_code, response_headers)
|
146 |
|
147 |
except requests.exceptions.RequestException as e:
|