MB-IDK commited on
Commit
c349d9d
·
verified ·
1 Parent(s): 1185883

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import json
3
+ import requests
4
+ import time
5
+ from threading import Thread, Lock
6
+ from http.server import BaseHTTPRequestHandler, HTTPServer
7
+ import socketserver
8
+ import urllib.request
9
+
10
+ # --- CONFIG ---
11
+ PROXY_JSON_URL = "https://raw.githubusercontent.com/mishableskineetudiant-stack/proxylistfiltered/main/proxies_elite.json"
12
+ REFRESH_INTERVAL = 300 # 5 minutes
13
+ PORT = 3129
14
+
15
+ # Pool de proxies (thread-safe)
16
+ proxy_pool = []
17
+ pool_lock = Lock()
18
+
19
+ def fetch_proxies():
20
+ try:
21
+ resp = requests.get(PROXY_JSON_URL, timeout=10)
22
+ data = resp.json()
23
+ cleaned = []
24
+ for p in data:
25
+ # Format "ip:port"
26
+ cleaned.append(f"http://{p}")
27
+ return cleaned
28
+ except Exception as e:
29
+ print(f"[Fetcher] Error fetching proxies: {e}")
30
+ return []
31
+
32
+ def refresh_loop():
33
+ global proxy_pool
34
+ while True:
35
+ print("[Refresher] Fetching new proxy list...")
36
+ new_list = fetch_proxies()
37
+ if new_list:
38
+ with pool_lock:
39
+ proxy_pool = new_list
40
+ print(f"[Refresher] Loaded {len(new_list)} proxies")
41
+ else:
42
+ print("[Refresher] No update (empty list)")
43
+ time.sleep(REFRESH_INTERVAL)
44
+
45
+ class ProxyHandler(BaseHTTPRequestHandler):
46
+ def do_GET(self):
47
+ self.forward_request()
48
+
49
+ def do_POST(self):
50
+ self.forward_request()
51
+
52
+ def forward_request(self):
53
+ # Choisir proxy round-robin
54
+ with pool_lock:
55
+ if not proxy_pool:
56
+ self.send_error(503, "No proxies available")
57
+ return
58
+ proxy = proxy_pool[int(time.time()) % len(proxy_pool)]
59
+
60
+ try:
61
+ req = urllib.request.Request(self.path)
62
+ opener = urllib.request.build_opener(urllib.request.ProxyHandler({"http": proxy, "https": proxy}))
63
+ resp = opener.open(req, timeout=10)
64
+ data = resp.read()
65
+
66
+ self.send_response(resp.getcode())
67
+ for key, val in resp.getheaders():
68
+ self.send_header(key, val)
69
+ self.end_headers()
70
+ self.wfile.write(data)
71
+
72
+ except Exception as e:
73
+ self.send_error(502, f"Bad Gateway: {e}")
74
+
75
+ def run_server():
76
+ with HTTPServer(("", PORT), ProxyHandler) as httpd:
77
+ print(f"[Proxy Server] Running on port {PORT}")
78
+ httpd.serve_forever()
79
+
80
+ if __name__ == "__main__":
81
+ # Start refresh thread
82
+ t = Thread(target=refresh_loop, daemon=True)
83
+ t.start()
84
+
85
+ # Run proxy HTTP server
86
+ run_server()
87
+