Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,116 +3,93 @@ import requests
|
|
| 3 |
import threading
|
| 4 |
import time
|
| 5 |
import random
|
| 6 |
-
from datetime import datetime
|
| 7 |
|
| 8 |
# ==============================================================================
|
| 9 |
# βοΈ CONFIGURATION
|
| 10 |
# ==============================================================================
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
# Shared Memory (The "Live File")
|
| 16 |
proxy_storage = {
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
}
|
| 20 |
|
| 21 |
# ==============================================================================
|
| 22 |
-
# π΅οΈββοΈ
|
| 23 |
# ==============================================================================
|
| 24 |
def check_proxy(ip):
|
| 25 |
-
"""
|
| 26 |
proxies = {"http": f"http://{ip}", "https": f"http://{ip}"}
|
| 27 |
try:
|
| 28 |
-
# We
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
except:
|
| 33 |
return False
|
| 34 |
return False
|
| 35 |
|
| 36 |
-
def
|
| 37 |
while True:
|
| 38 |
-
print(f"
|
| 39 |
|
| 40 |
-
# 1.
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
for ip in current_list:
|
| 44 |
if check_proxy(ip):
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
proxy_storage[
|
| 50 |
-
|
| 51 |
-
|
| 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
|
| 64 |
try:
|
| 65 |
-
r = requests.get(
|
| 66 |
-
|
| 67 |
except: pass
|
| 68 |
|
| 69 |
-
# Shuffle
|
| 70 |
-
random.shuffle(
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
for ip in raw_proxies:
|
| 74 |
-
ip = ip.strip()
|
| 75 |
-
if not ip or ip in proxy_storage["valid_proxies"]: continue
|
| 76 |
|
| 77 |
-
|
| 78 |
-
if
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 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 |
-
|
| 93 |
-
|
| 94 |
|
|
|
|
| 95 |
|
| 96 |
# ==============================================================================
|
| 97 |
-
# π API ENDPOINT
|
| 98 |
# ==============================================================================
|
| 99 |
-
def
|
| 100 |
-
|
| 101 |
-
return proxy_storage["valid_proxies"], f"Updated: {proxy_storage['last_updated']}"
|
| 102 |
|
| 103 |
with gr.Blocks() as app:
|
| 104 |
-
gr.Markdown("##
|
| 105 |
-
|
| 106 |
with gr.Row():
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
refresh_btn = gr.Button("Refresh View")
|
| 111 |
-
|
| 112 |
-
# --- THIS IS THE FIX: We added api_name="get_proxies" ---
|
| 113 |
-
refresh_btn.click(get_proxies_api, outputs=[json_out, status_out], api_name="get_proxies")
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
|
|
|
| 117 |
|
| 118 |
app.launch()
|
|
|
|
| 3 |
import threading
|
| 4 |
import time
|
| 5 |
import random
|
|
|
|
| 6 |
|
| 7 |
# ==============================================================================
|
| 8 |
# βοΈ CONFIGURATION
|
| 9 |
# ==============================================================================
|
| 10 |
+
TARGET_POOL_SIZE = 15 # Keep 15 good proxies ready
|
| 11 |
+
TIMEOUT_STRICT = 5 # Reject anything slower than 5 seconds
|
| 12 |
+
CHECK_URL = "https://m.youtube.com" # We check against YouTube Mobile (faster)
|
| 13 |
|
|
|
|
| 14 |
proxy_storage = {
|
| 15 |
+
"pool": [],
|
| 16 |
+
"last_update": "Initializing..."
|
| 17 |
}
|
| 18 |
|
| 19 |
# ==============================================================================
|
| 20 |
+
# π΅οΈββοΈ REFINERY WORKER
|
| 21 |
# ==============================================================================
|
| 22 |
def check_proxy(ip):
|
| 23 |
+
"""Strictly checks if proxy can load YouTube."""
|
| 24 |
proxies = {"http": f"http://{ip}", "https": f"http://{ip}"}
|
| 25 |
try:
|
| 26 |
+
# We use a Session for speed
|
| 27 |
+
with requests.Session() as s:
|
| 28 |
+
s.proxies.update(proxies)
|
| 29 |
+
# HEAD request is faster than GET
|
| 30 |
+
r = s.head(CHECK_URL, timeout=TIMEOUT_STRICT)
|
| 31 |
+
if r.status_code == 200:
|
| 32 |
+
return True
|
| 33 |
except:
|
| 34 |
return False
|
| 35 |
return False
|
| 36 |
|
| 37 |
+
def refinery_loop():
|
| 38 |
while True:
|
| 39 |
+
print(f"β»οΈ Refining Pool... Current: {len(proxy_storage['pool'])}")
|
| 40 |
|
| 41 |
+
# 1. Clean existing pool (Re-check)
|
| 42 |
+
valid_proxies = []
|
| 43 |
+
for ip in proxy_storage['pool']:
|
|
|
|
| 44 |
if check_proxy(ip):
|
| 45 |
+
valid_proxies.append(ip)
|
| 46 |
+
proxy_storage['pool'] = valid_proxies
|
| 47 |
+
|
| 48 |
+
# 2. Refill if needed
|
| 49 |
+
if len(proxy_storage['pool']) < TARGET_POOL_SIZE:
|
| 50 |
+
print(" π Low fuel. Fetching fresh proxies...")
|
| 51 |
+
raw_list = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
sources = [
|
| 53 |
"https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/http.txt",
|
| 54 |
"https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt",
|
| 55 |
"https://api.proxyscrape.com/v2/?request=getproxies&protocol=http&timeout=5000&country=all&ssl=all&anonymity=all"
|
| 56 |
]
|
| 57 |
+
for url in sources:
|
| 58 |
try:
|
| 59 |
+
r = requests.get(url, timeout=10)
|
| 60 |
+
raw_list += r.text.strip().split("\n")
|
| 61 |
except: pass
|
| 62 |
|
| 63 |
+
# Shuffle and Test
|
| 64 |
+
random.shuffle(raw_list)
|
| 65 |
+
for ip in raw_list:
|
| 66 |
+
if len(proxy_storage['pool']) >= TARGET_POOL_SIZE: break
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
ip = ip.strip()
|
| 69 |
+
if ip and ip not in proxy_storage['pool']:
|
| 70 |
+
if check_proxy(ip):
|
| 71 |
+
print(f" β
Verified: {ip}")
|
| 72 |
+
proxy_storage['pool'].append(ip)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
proxy_storage['last_update'] = time.strftime("%H:%M:%S")
|
| 75 |
+
time.sleep(120) # Run every 2 minutes for freshness
|
| 76 |
|
| 77 |
+
threading.Thread(target=refinery_loop, daemon=True).start()
|
| 78 |
|
| 79 |
# ==============================================================================
|
| 80 |
+
# π API ENDPOINT
|
| 81 |
# ==============================================================================
|
| 82 |
+
def serve_proxies():
|
| 83 |
+
return proxy_storage['pool'], f"Last Refined: {proxy_storage['last_update']}"
|
|
|
|
| 84 |
|
| 85 |
with gr.Blocks() as app:
|
| 86 |
+
gr.Markdown("## π Proxy Refinery")
|
|
|
|
| 87 |
with gr.Row():
|
| 88 |
+
out_json = gr.JSON(label="Active High-Speed Pool")
|
| 89 |
+
out_status = gr.Textbox(label="Status")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
btn = gr.Button("Refresh")
|
| 92 |
+
btn.click(serve_proxies, outputs=[out_json, out_status], api_name="get_proxies")
|
| 93 |
+
app.load(serve_proxies, outputs=[out_json, out_status])
|
| 94 |
|
| 95 |
app.launch()
|