Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import threading
|
| 4 |
+
import time
|
| 5 |
+
import random
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 8 |
+
|
| 9 |
+
# ==============================================================================
|
| 10 |
+
# βοΈ CONFIGURATION (SAFE MODE)
|
| 11 |
+
# ==============================================================================
|
| 12 |
+
TARGET_COUNT = 200
|
| 13 |
+
TIMEOUT_SEC = 5
|
| 14 |
+
CHECK_INTERVAL = 30
|
| 15 |
+
MAX_THREADS = 40 # β οΈ REDUCED to 40. Start small. If it works, try 80.
|
| 16 |
+
|
| 17 |
+
# Shared Memory
|
| 18 |
+
proxy_storage = {
|
| 19 |
+
"valid_proxies": [],
|
| 20 |
+
"last_updated": "Waiting for startup..."
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
# ==============================================================================
|
| 24 |
+
# π΅οΈββοΈ PROXY WORKER
|
| 25 |
+
# ==============================================================================
|
| 26 |
+
def check_proxy(ip):
|
| 27 |
+
"""Returns IP if valid and FAST."""
|
| 28 |
+
proxies = {"http": f"http://{ip}", "https": f"http://{ip}"}
|
| 29 |
+
try:
|
| 30 |
+
r = requests.get("https://www.youtube.com", proxies=proxies, timeout=TIMEOUT_SEC)
|
| 31 |
+
if r.status_code == 200:
|
| 32 |
+
return ip
|
| 33 |
+
except:
|
| 34 |
+
pass
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
+
def worker_loop():
|
| 38 |
+
# π CRITICAL FIX: Sleep 15s to let Gradio start first!
|
| 39 |
+
print("β³ Worker waiting 15s for Gradio to boot...")
|
| 40 |
+
time.sleep(15)
|
| 41 |
+
|
| 42 |
+
while True:
|
| 43 |
+
print(f"\n[{datetime.now().strftime('%H:%M:%S')}] β’οΈ Starting Scan...")
|
| 44 |
+
|
| 45 |
+
# 1. FETCH SOURCES (Fixed Syntax Errors)
|
| 46 |
+
raw_proxies = []
|
| 47 |
+
sources = [
|
| 48 |
+
"https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/http.txt",
|
| 49 |
+
"https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt",
|
| 50 |
+
"https://api.proxyscrape.com/v2/?request=getproxies&protocol=http&timeout=5000&country=all&ssl=all&anonymity=all",
|
| 51 |
+
"https://raw.githubusercontent.com/shiftytr/proxy-list/master/proxy.txt",
|
| 52 |
+
"https://raw.githubusercontent.com/hookzof/socks5_list/master/proxy.txt",
|
| 53 |
+
"https://raw.githubusercontent.com/clarketm/proxy-list/master/proxy-list-raw.txt",
|
| 54 |
+
"https://raw.githubusercontent.com/sunny9577/proxy-scraper/master/proxies.txt",
|
| 55 |
+
"https://raw.githubusercontent.com/jetkai/proxy-list/main/online-proxies/txt/proxies-http.txt"
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
print(" π₯ Downloading proxy lists...")
|
| 59 |
+
for s in sources:
|
| 60 |
+
try:
|
| 61 |
+
r = requests.get(s, timeout=5)
|
| 62 |
+
if r.status_code == 200:
|
| 63 |
+
raw_proxies += r.text.strip().split("\n")
|
| 64 |
+
except: pass
|
| 65 |
+
|
| 66 |
+
raw_proxies = list(set(raw_proxies))
|
| 67 |
+
random.shuffle(raw_proxies)
|
| 68 |
+
|
| 69 |
+
# 2. VALIDATE
|
| 70 |
+
check_list = proxy_storage["valid_proxies"] + raw_proxies
|
| 71 |
+
check_list = check_list[:2000] # Hard limit to save RAM
|
| 72 |
+
|
| 73 |
+
new_valid_pool = []
|
| 74 |
+
|
| 75 |
+
print(f" π Scanning {len(check_list)} proxies with {MAX_THREADS} threads...")
|
| 76 |
+
|
| 77 |
+
with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
|
| 78 |
+
future_to_ip = {executor.submit(check_proxy, ip): ip for ip in check_list}
|
| 79 |
+
|
| 80 |
+
for future in as_completed(future_to_ip):
|
| 81 |
+
result = future.result()
|
| 82 |
+
if result:
|
| 83 |
+
if result not in new_valid_pool:
|
| 84 |
+
new_valid_pool.append(result)
|
| 85 |
+
|
| 86 |
+
if len(new_valid_pool) >= TARGET_COUNT:
|
| 87 |
+
executor.shutdown(wait=False, cancel_futures=True)
|
| 88 |
+
break
|
| 89 |
+
|
| 90 |
+
proxy_storage["valid_proxies"] = new_valid_pool
|
| 91 |
+
proxy_storage["last_updated"] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 92 |
+
|
| 93 |
+
print(f" β
Update Complete. Found {len(new_valid_pool)} proxies.")
|
| 94 |
+
time.sleep(CHECK_INTERVAL)
|
| 95 |
+
|
| 96 |
+
# Start Background Thread
|
| 97 |
+
threading.Thread(target=worker_loop, daemon=True).start()
|
| 98 |
+
|
| 99 |
+
# ==============================================================================
|
| 100 |
+
# π API ENDPOINT
|
| 101 |
+
# ==============================================================================
|
| 102 |
+
def get_proxies_api():
|
| 103 |
+
return proxy_storage["valid_proxies"], f"Updated: {proxy_storage['last_updated']}"
|
| 104 |
+
|
| 105 |
+
with gr.Blocks() as app:
|
| 106 |
+
gr.Markdown("## π¦ Proxy Engine (Safe Mode)")
|
| 107 |
+
with gr.Row():
|
| 108 |
+
json_out = gr.JSON(label="Live Proxy Pool")
|
| 109 |
+
status_out = gr.Textbox(label="Last Update")
|
| 110 |
+
|
| 111 |
+
refresh_btn = gr.Button("Refresh View")
|
| 112 |
+
refresh_btn.click(get_proxies_api, outputs=[json_out, status_out], api_name="get_proxies")
|
| 113 |
+
app.load(get_proxies_api, outputs=[json_out, status_out])
|
| 114 |
+
|
| 115 |
+
# Explicitly set server settings to ensure HF can find the app
|
| 116 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|