lexicalspace commited on
Commit
e472da6
Β·
verified Β·
1 Parent(s): c5d824d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import threading
4
+ import time
5
+ import random
6
+ from datetime import datetime
7
+
8
+ # ==============================================================================
9
+ # βš™οΈ CONFIGURATION
10
+ # ==============================================================================
11
+ TARGET_COUNT = 10 # We want exactly 10 working proxies
12
+ TIMEOUT_SEC = 5 # Reject if slower than 5 seconds
13
+ CHECK_INTERVAL = 300 # Re-check every 5 minutes (300 seconds)
14
+
15
+ # Shared Memory (The "Live File")
16
+ proxy_storage = {
17
+ "valid_proxies": [],
18
+ "last_updated": "Not yet started"
19
+ }
20
+
21
+ # ==============================================================================
22
+ # πŸ•΅οΈβ€β™‚οΈ PROXY WORKER (Background Thread)
23
+ # ==============================================================================
24
+ def check_proxy(ip):
25
+ """Returns True if proxy connects to YouTube in < 5 seconds."""
26
+ proxies = {"http": f"http://{ip}", "https": f"http://{ip}"}
27
+ try:
28
+ # We test against YouTube specifically because that's our target
29
+ r = requests.get("https://www.youtube.com", proxies=proxies, timeout=TIMEOUT_SEC)
30
+ if r.status_code == 200:
31
+ return True
32
+ except:
33
+ return False
34
+ return False
35
+
36
+ def worker_loop():
37
+ while True:
38
+ print(f"\n[{datetime.now().strftime('%H:%M')}] ♻️ Starting Validation Cycle...")
39
+
40
+ # 1. RE-VALIDATE EXISTING (Keep the good ones)
41
+ current_list = proxy_storage["valid_proxies"]
42
+ still_good = []
43
+ for ip in current_list:
44
+ if check_proxy(ip):
45
+ still_good.append(ip)
46
+ else:
47
+ print(f" ❌ Dropped dead proxy: {ip}")
48
+
49
+ proxy_storage["valid_proxies"] = still_good
50
+
51
+ # 2. FILL THE POOL (If we have less than 10)
52
+ if len(proxy_storage["valid_proxies"]) < TARGET_COUNT:
53
+ needed = TARGET_COUNT - len(proxy_storage["valid_proxies"])
54
+ print(f" πŸ“‰ Pool low. Need {needed} more. Fetching fresh list...")
55
+
56
+ # Fetch huge raw list
57
+ raw_proxies = []
58
+ sources = [
59
+ "https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/http.txt",
60
+ "https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt",
61
+ "https://api.proxyscrape.com/v2/?request=getproxies&protocol=http&timeout=5000&country=all&ssl=all&anonymity=all"
62
+ ]
63
+ for s in sources:
64
+ try:
65
+ r = requests.get(s, timeout=10)
66
+ raw_proxies += r.text.strip().split("\n")
67
+ except: pass
68
+
69
+ # Shuffle to get random ones
70
+ random.shuffle(raw_proxies)
71
+
72
+ # Test until we hit 10
73
+ for ip in raw_proxies:
74
+ ip = ip.strip()
75
+ if not ip or ip in proxy_storage["valid_proxies"]: continue
76
+
77
+ # Stop if we hit our target
78
+ if len(proxy_storage["valid_proxies"]) >= TARGET_COUNT:
79
+ break
80
+
81
+ print(f" Testing {ip}...", end="\r")
82
+ if check_proxy(ip):
83
+ print(f" βœ… FOUND NEW: {ip}")
84
+ proxy_storage["valid_proxies"].append(ip)
85
+
86
+ proxy_storage["last_updated"] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
87
+ print(f" πŸ’€ Sleeping for {CHECK_INTERVAL/60} mins. Current Pool: {len(proxy_storage['valid_proxies'])}")
88
+
89
+ # 3. SLEEP
90
+ time.sleep(CHECK_INTERVAL)
91
+
92
+ # Start Background Thread
93
+ threading.Thread(target=worker_loop, daemon=True).start()
94
+
95
+ # ==============================================================================
96
+ # πŸ”Œ API ENDPOINT
97
+ # ==============================================================================
98
+ def get_proxies_api():
99
+ """Main App calls this to get the list"""
100
+ return proxy_storage["valid_proxies"], f"Updated: {proxy_storage['last_updated']}"
101
+
102
+ with gr.Blocks() as app:
103
+ gr.Markdown("## 🚦 Proxy Engine (Backend)")
104
+ gr.Markdown(f"Maintains a pool of {TARGET_COUNT} fast proxies.")
105
+
106
+ with gr.Row():
107
+ json_out = gr.JSON(label="Live Proxy Pool")
108
+ status_out = gr.Textbox(label="Last Update")
109
+
110
+ # Hidden button to trigger refresh for UI viewers
111
+ refresh_btn = gr.Button("Refresh View")
112
+ refresh_btn.click(get_proxies_api, outputs=[json_out, status_out])
113
+
114
+ # Load immediately on open
115
+ app.load(get_proxies_api, outputs=[json_out, status_out])
116
+
117
+ app.launch()