rkihacker commited on
Commit
326f1b4
·
verified ·
1 Parent(s): aa258f7

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +25 -47
main.py CHANGED
@@ -1,9 +1,8 @@
1
  import requests
2
  import random
3
  import time
4
- import sys
5
  import threading
6
- from flask import Flask, request, jsonify
7
 
8
  PROXY_LIST_URL = "https://proxies.typegpt.net/ips.txt"
9
  proxies_cache = []
@@ -28,70 +27,49 @@ def get_random_proxy(proxies):
28
  return None
29
  return random.choice(proxies)
30
 
31
- def make_request(proxy, target_url):
32
- """Send a GET request through the given proxy to target URL."""
33
- proxies = {"http": proxy, "https": proxy}
34
- try:
35
- print(f"[INFO] Using proxy: {proxy}")
36
- resp = requests.get(target_url, proxies=proxies, timeout=15)
37
- if resp.status_code == 200:
38
- print(f"[SUCCESS] Proxy working! Status: {resp.status_code}")
39
- return True, resp.text[:200]
40
- else:
41
- print(f"[WARN] Proxy responded with status {resp.status_code}")
42
- return False, f"Status {resp.status_code}"
43
- except Exception as e:
44
- print(f"[ERROR] Proxy failed: {proxy} | {e}")
45
- return False, str(e)
46
-
47
- def proxy_loop(target_url):
48
- """Background loop to test proxies continuously."""
49
  global proxies_cache, last_refresh
50
  while True:
51
  if time.time() - last_refresh > 300 or not proxies_cache:
52
  proxies_cache = fetch_proxies()
53
  last_refresh = time.time()
54
  print(f"[INFO] Refreshed {len(proxies_cache)} proxies.")
55
-
56
- proxy = get_random_proxy(proxies_cache)
57
- if proxy:
58
- make_request(proxy, target_url)
59
- time.sleep(10)
60
-
61
- # ---- Flask Endpoints ----
62
 
63
  @app.route("/health")
64
  def health():
65
  return "Healthy", 200
66
 
67
- @app.route("/test")
68
- def test_proxy():
69
- target_url = request.args.get("url")
70
- if not target_url:
71
- return jsonify({"error": "Missing ?url=<target_url>"}), 400
72
 
73
  proxy = get_random_proxy(proxies_cache)
74
  if not proxy:
75
- return jsonify({"error": "No proxies available"}), 500
76
 
77
- ok, msg = make_request(proxy, target_url)
78
- if ok:
79
- return jsonify({"status": "Proxy working!", "proxy": proxy, "response_snippet": msg}), 200
80
- else:
81
- return jsonify({"status": "Proxy failed", "proxy": proxy, "error": msg}), 502
82
-
83
- def main():
84
- if len(sys.argv) < 2:
85
- print("Usage: python main.py <target_url>")
86
- sys.exit(1)
87
 
88
- target_url = sys.argv[1]
 
 
 
 
 
 
89
 
90
- # Start background thread
91
- t = threading.Thread(target=proxy_loop, args=(target_url,), daemon=True)
 
92
  t.start()
93
 
94
- # Start Flask server
95
  app.run(host="0.0.0.0", port=5000)
96
 
97
  if __name__ == "__main__":
 
1
  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 = []
 
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:
35
  proxies_cache = fetch_proxies()
36
  last_refresh = time.time()
37
  print(f"[INFO] Refreshed {len(proxies_cache)} proxies.")
38
+ time.sleep(60)
 
 
 
 
 
 
39
 
40
  @app.route("/health")
41
  def health():
42
  return "Healthy", 200
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__":