Update main.py
Browse files
main.py
CHANGED
|
@@ -2,7 +2,7 @@ import requests
|
|
| 2 |
import random
|
| 3 |
import time
|
| 4 |
import threading
|
| 5 |
-
from flask import Flask, request, Response, jsonify
|
| 6 |
|
| 7 |
PROXY_LIST_URL = "https://proxies.typegpt.net/ips.txt"
|
| 8 |
proxies_cache = []
|
|
@@ -10,6 +10,18 @@ last_refresh = 0
|
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def fetch_proxies():
|
| 14 |
try:
|
| 15 |
resp = requests.get(PROXY_LIST_URL, timeout=10)
|
|
@@ -38,11 +50,9 @@ def refresh_proxies_loop():
|
|
| 38 |
def health():
|
| 39 |
return "Healthy", 200
|
| 40 |
|
| 41 |
-
@app.route("
|
| 42 |
-
def
|
| 43 |
-
|
| 44 |
-
target_url = "http://" + target_url
|
| 45 |
-
|
| 46 |
proxy = get_random_proxy(proxies_cache)
|
| 47 |
if not proxy:
|
| 48 |
return jsonify({"error": "No proxies available"}), 500
|
|
@@ -50,30 +60,40 @@ def proxy_request(target_url):
|
|
| 50 |
proxies = {"http": proxy, "https": proxy}
|
| 51 |
|
| 52 |
try:
|
| 53 |
-
print(f"[INFO] Forwarding
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
method=request.method,
|
| 58 |
url=target_url,
|
| 59 |
-
headers=
|
| 60 |
data=request.get_data(),
|
| 61 |
-
cookies=request.cookies,
|
| 62 |
params=request.args,
|
| 63 |
proxies=proxies,
|
| 64 |
-
|
| 65 |
-
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
headers["X-Proxy-Used"] = proxy
|
| 71 |
|
| 72 |
return Response(
|
| 73 |
-
|
| 74 |
-
status=
|
| 75 |
headers=headers,
|
|
|
|
| 76 |
)
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
return jsonify({"error": "Proxy failed", "proxy": proxy, "details": str(e)}), 502
|
| 79 |
|
|
|
|
| 2 |
import random
|
| 3 |
import time
|
| 4 |
import threading
|
| 5 |
+
from flask import Flask, request, Response, jsonify, stream_with_context
|
| 6 |
|
| 7 |
PROXY_LIST_URL = "https://proxies.typegpt.net/ips.txt"
|
| 8 |
proxies_cache = []
|
|
|
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
|
| 13 |
+
# DeepInfra required headers
|
| 14 |
+
DEEPINFRA_HEADERS = {
|
| 15 |
+
"accept": "text/event-stream",
|
| 16 |
+
"content-type": "application/json",
|
| 17 |
+
"referer": "https://deepinfra.com/",
|
| 18 |
+
"sec-ch-ua": '"Chromium";v="140", "Not=A?Brand";v="24", "Google Chrome";v="140"',
|
| 19 |
+
"sec-ch-ua-mobile": "?0",
|
| 20 |
+
"sec-ch-ua-platform": '"Windows"',
|
| 21 |
+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36",
|
| 22 |
+
"x-deepinfra-source": "web-embed",
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
def fetch_proxies():
|
| 26 |
try:
|
| 27 |
resp = requests.get(PROXY_LIST_URL, timeout=10)
|
|
|
|
| 50 |
def health():
|
| 51 |
return "Healthy", 200
|
| 52 |
|
| 53 |
+
@app.route("/deepinfra", methods=["POST"])
|
| 54 |
+
def proxy_deepinfra():
|
| 55 |
+
target_url = "https://api.deepinfra.com/v1/openai/chat/completions"
|
|
|
|
|
|
|
| 56 |
proxy = get_random_proxy(proxies_cache)
|
| 57 |
if not proxy:
|
| 58 |
return jsonify({"error": "No proxies available"}), 500
|
|
|
|
| 60 |
proxies = {"http": proxy, "https": proxy}
|
| 61 |
|
| 62 |
try:
|
| 63 |
+
print(f"[INFO] Forwarding POST to {target_url} via {proxy}")
|
| 64 |
+
|
| 65 |
+
# Start with required DeepInfra headers, merge client’s headers on top
|
| 66 |
+
forward_headers = dict(DEEPINFRA_HEADERS)
|
| 67 |
+
for k, v in request.headers:
|
| 68 |
+
if k.lower() != "host":
|
| 69 |
+
forward_headers[k] = v
|
| 70 |
|
| 71 |
+
# Stream request
|
| 72 |
+
upstream = requests.post(
|
|
|
|
| 73 |
url=target_url,
|
| 74 |
+
headers=forward_headers,
|
| 75 |
data=request.get_data(),
|
|
|
|
| 76 |
params=request.args,
|
| 77 |
proxies=proxies,
|
| 78 |
+
stream=True,
|
| 79 |
+
timeout=300,
|
| 80 |
)
|
| 81 |
|
| 82 |
+
def generate():
|
| 83 |
+
for chunk in upstream.iter_content(chunk_size=None):
|
| 84 |
+
if chunk:
|
| 85 |
+
yield chunk
|
| 86 |
+
|
| 87 |
+
headers = dict(upstream.headers)
|
| 88 |
headers["X-Proxy-Used"] = proxy
|
| 89 |
|
| 90 |
return Response(
|
| 91 |
+
stream_with_context(generate()),
|
| 92 |
+
status=upstream.status_code,
|
| 93 |
headers=headers,
|
| 94 |
+
content_type=upstream.headers.get("Content-Type", "application/json")
|
| 95 |
)
|
| 96 |
+
|
| 97 |
except Exception as e:
|
| 98 |
return jsonify({"error": "Proxy failed", "proxy": proxy, "details": str(e)}), 502
|
| 99 |
|