rkihacker commited on
Commit
2d93720
·
verified ·
1 Parent(s): 683a8ce

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +98 -0
main.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = []
10
+ last_refresh = 0
11
+
12
+ app = Flask(__name__)
13
+
14
+ def fetch_proxies():
15
+ """Fetch proxy list from remote URL."""
16
+ try:
17
+ resp = requests.get(PROXY_LIST_URL, timeout=10)
18
+ resp.raise_for_status()
19
+ proxies = [line.strip() for line in resp.text.splitlines() if line.strip()]
20
+ return proxies
21
+ except Exception as e:
22
+ print(f"[ERROR] Failed to fetch proxies: {e}")
23
+ return []
24
+
25
+ def get_random_proxy(proxies):
26
+ """Pick a random proxy from the list."""
27
+ if not 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__":
98
+ main()