rkihacker commited on
Commit
14f06cc
·
verified ·
1 Parent(s): 4de386a

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +20 -7
main.py CHANGED
@@ -34,25 +34,38 @@ def refresh_proxies_loop():
34
  print(f"[INFO] Refreshed {len(proxies_cache)} proxies.")
35
  time.sleep(60)
36
 
37
- @app.route("/health")
38
  def health():
39
  return "Healthy", 200
40
 
41
- @app.route("/<path:target_url>")
42
  def proxy_request(target_url):
43
  if not target_url.startswith("http"):
44
- target_url = "http://" + target_url # allow shorthand
45
 
46
  proxy = get_random_proxy(proxies_cache)
47
  if not proxy:
48
  return jsonify({"error": "No proxies available"}), 500
49
 
50
  proxies = {"http": proxy, "https": proxy}
 
51
  try:
52
- print(f"[INFO] Forwarding to {target_url} via {proxy}")
53
- resp = requests.get(target_url, proxies=proxies, timeout=15)
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- # Add "X-Proxy-Used" header so user knows which IP was used
56
  headers = dict(resp.headers)
57
  headers["X-Proxy-Used"] = proxy
58
 
@@ -62,7 +75,7 @@ def proxy_request(target_url):
62
  headers=headers,
63
  )
64
  except Exception as e:
65
- return jsonify({"error": f"Proxy failed", "proxy": proxy, "details": str(e)}), 502
66
 
67
  def main():
68
  t = threading.Thread(target=refresh_proxies_loop, daemon=True)
 
34
  print(f"[INFO] Refreshed {len(proxies_cache)} proxies.")
35
  time.sleep(60)
36
 
37
+ @app.route("/health", methods=["GET"])
38
  def health():
39
  return "Healthy", 200
40
 
41
+ @app.route("/<path:target_url>", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
42
  def proxy_request(target_url):
43
  if not target_url.startswith("http"):
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
49
 
50
  proxies = {"http": proxy, "https": proxy}
51
+
52
  try:
53
+ print(f"[INFO] Forwarding {request.method} to {target_url} via {proxy}")
54
+
55
+ # Forward original request (method, headers, body, params)
56
+ resp = requests.request(
57
+ method=request.method,
58
+ url=target_url,
59
+ headers={k: v for k, v in request.headers if k.lower() != "host"},
60
+ data=request.get_data(),
61
+ cookies=request.cookies,
62
+ params=request.args,
63
+ proxies=proxies,
64
+ timeout=30,
65
+ allow_redirects=False
66
+ )
67
 
68
+ # Copy headers + add X-Proxy-Used
69
  headers = dict(resp.headers)
70
  headers["X-Proxy-Used"] = proxy
71
 
 
75
  headers=headers,
76
  )
77
  except Exception as e:
78
+ return jsonify({"error": "Proxy failed", "proxy": proxy, "details": str(e)}), 502
79
 
80
  def main():
81
  t = threading.Thread(target=refresh_proxies_loop, daemon=True)