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

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +8 -11
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
6
 
7
  PROXY_LIST_URL = "https://proxies.typegpt.net/ips.txt"
8
  proxies_cache = []
@@ -11,7 +11,6 @@ last_refresh = 0
11
  app = Flask(__name__)
12
 
13
  def fetch_proxies():
14
- """Fetch proxy list from remote URL."""
15
  try:
16
  resp = requests.get(PROXY_LIST_URL, timeout=10)
17
  resp.raise_for_status()
@@ -22,13 +21,11 @@ def fetch_proxies():
22
  return []
23
 
24
  def get_random_proxy(proxies):
25
- """Pick a random proxy from the list."""
26
  if not proxies:
27
  return None
28
  return random.choice(proxies)
29
 
30
  def refresh_proxies_loop():
31
- """Background thread: refresh proxy list every 5 minutes."""
32
  global proxies_cache, last_refresh
33
  while True:
34
  if time.time() - last_refresh > 300 or not proxies_cache:
@@ -43,33 +40,33 @@ def health():
43
 
44
  @app.route("/<path:target_url>")
45
  def proxy_request(target_url):
46
- """Catch-all route: forward request to target_url using random proxy."""
47
  if not target_url.startswith("http"):
48
  target_url = "http://" + target_url # allow shorthand
49
 
50
  proxy = get_random_proxy(proxies_cache)
51
  if not proxy:
52
- return {"error": "No proxies available"}, 500
53
 
54
  proxies = {"http": proxy, "https": proxy}
55
  try:
56
  print(f"[INFO] Forwarding to {target_url} via {proxy}")
57
  resp = requests.get(target_url, proxies=proxies, timeout=15)
58
 
 
 
 
 
59
  return Response(
60
  resp.content,
61
  status=resp.status_code,
62
- headers=dict(resp.headers),
63
  )
64
  except Exception as e:
65
- return {"error": f"Proxy failed: {proxy}", "details": str(e)}, 502
66
 
67
  def main():
68
- # Start proxy refresher thread
69
  t = threading.Thread(target=refresh_proxies_loop, daemon=True)
70
  t.start()
71
-
72
- # Run Flask server
73
  app.run(host="0.0.0.0", port=5000)
74
 
75
  if __name__ == "__main__":
 
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 = []
 
11
  app = Flask(__name__)
12
 
13
  def fetch_proxies():
 
14
  try:
15
  resp = requests.get(PROXY_LIST_URL, timeout=10)
16
  resp.raise_for_status()
 
21
  return []
22
 
23
  def get_random_proxy(proxies):
 
24
  if not proxies:
25
  return None
26
  return random.choice(proxies)
27
 
28
  def refresh_proxies_loop():
 
29
  global proxies_cache, last_refresh
30
  while True:
31
  if time.time() - last_refresh > 300 or not proxies_cache:
 
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
+
59
  return Response(
60
  resp.content,
61
  status=resp.status_code,
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)
69
  t.start()
 
 
70
  app.run(host="0.0.0.0", port=5000)
71
 
72
  if __name__ == "__main__":