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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
import re
|
| 5 |
+
import requests
|
| 6 |
+
import asyncio
|
| 7 |
+
from fastapi import FastAPI, WebSocket
|
| 8 |
+
from fastapi.responses import HTMLResponse, Response
|
| 9 |
+
from threading import Lock
|
| 10 |
+
import uvicorn
|
| 11 |
+
from faker import Faker
|
| 12 |
+
from bs4 import BeautifulSoup
|
| 13 |
+
|
| 14 |
+
app = FastAPI()
|
| 15 |
+
faker = Faker()
|
| 16 |
+
valid_proxies = {}
|
| 17 |
+
invalid_proxies = {}
|
| 18 |
+
proxies_lock = Lock()
|
| 19 |
+
|
| 20 |
+
def create_headers():
|
| 21 |
+
user_agent = faker.user_agent()
|
| 22 |
+
random_ip = faker.ipv4()
|
| 23 |
+
return {
|
| 24 |
+
"User-Agent": user_agent,
|
| 25 |
+
"X-Forwarded-For": random_ip,
|
| 26 |
+
"Client-IP": random_ip,
|
| 27 |
+
"X-Real-IP": random_ip
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
def is_valid_proxy(proxy: str) -> bool:
|
| 31 |
+
ip_port_pattern = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{2,5}$')
|
| 32 |
+
if ip_port_pattern.match(proxy) is None:
|
| 33 |
+
return False
|
| 34 |
+
ip, port = proxy.split(':')
|
| 35 |
+
if any(char in ip for char in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') or \
|
| 36 |
+
any(char in port for char in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'):
|
| 37 |
+
return False
|
| 38 |
+
return True
|
| 39 |
+
|
| 40 |
+
async def verify_proxy(proxy: str):
|
| 41 |
+
if not is_valid_proxy(proxy):
|
| 42 |
+
with proxies_lock:
|
| 43 |
+
invalid_proxies[proxy] = True
|
| 44 |
+
return proxy, False
|
| 45 |
+
|
| 46 |
+
test_urls = [
|
| 47 |
+
"https://google.com",
|
| 48 |
+
"https://www.youtube.com/",
|
| 49 |
+
"http://httpbin.org/ip",
|
| 50 |
+
"https://instagram.com"
|
| 51 |
+
]
|
| 52 |
+
proxy_dict = {
|
| 53 |
+
"http": f"http://{proxy}",
|
| 54 |
+
"https": f"http://{proxy}"
|
| 55 |
+
}
|
| 56 |
+
headers = create_headers()
|
| 57 |
+
try:
|
| 58 |
+
for url in test_urls:
|
| 59 |
+
try:
|
| 60 |
+
response = await asyncio.to_thread(requests.get, url, proxies=proxy_dict, headers=headers, timeout=10)
|
| 61 |
+
response.raise_for_status()
|
| 62 |
+
except (requests.RequestException, ValueError):
|
| 63 |
+
with proxies_lock:
|
| 64 |
+
invalid_proxies[proxy] = True
|
| 65 |
+
return proxy, False
|
| 66 |
+
with proxies_lock:
|
| 67 |
+
valid_proxies[proxy] = True
|
| 68 |
+
return proxy, True
|
| 69 |
+
except Exception:
|
| 70 |
+
with proxies_lock:
|
| 71 |
+
valid_proxies.pop(proxy, None)
|
| 72 |
+
invalid_proxies[proxy] = True
|
| 73 |
+
return proxy, False
|
| 74 |
+
|
| 75 |
+
async def verify_proxies_in_background():
|
| 76 |
+
while True:
|
| 77 |
+
with proxies_lock:
|
| 78 |
+
proxies_to_verify = list(valid_proxies.keys())
|
| 79 |
+
if proxies_to_verify:
|
| 80 |
+
tasks = [verify_proxy(proxy) for proxy in proxies_to_verify]
|
| 81 |
+
results = await asyncio.gather(*tasks)
|
| 82 |
+
for proxy, is_valid in results:
|
| 83 |
+
if not is_valid:
|
| 84 |
+
with proxies_lock:
|
| 85 |
+
valid_proxies.pop(proxy, None)
|
| 86 |
+
invalid_proxies[proxy] = True
|
| 87 |
+
await asyncio.sleep(0)
|
| 88 |
+
|
| 89 |
+
async def fetch_proxies_from_sources():
|
| 90 |
+
proxy_urls = [
|
| 91 |
+
os.getenv("PROXY_API_URL", "http://pubproxy.com/api/proxy?format=txt&level=anonymous,elite&type=http,socks4,socks5&last_check=60&speed=25&limit=1&post=true&user_agent=true&cookies=true&referer=true"),
|
| 92 |
+
'https://raw.githubusercontent.com/clarketm/proxy-list/master/proxy-list-raw.txt',
|
| 93 |
+
'https://raw.githubusercontent.com/TheSpeedX/PROXY-List/master/http.txt',
|
| 94 |
+
'https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/socks4.txt',
|
| 95 |
+
'https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/socks5.txt',
|
| 96 |
+
'https://raw.githubusercontent.com/ShiftyTR/Proxy-List/master/proxy.txt',
|
| 97 |
+
'https://raw.githubusercontent.com/sunny9577/proxy-scraper/master/proxies.txt',
|
| 98 |
+
"https://storage.googleapis.com/river-treat-249913.appspot.com/p.txt",
|
| 99 |
+
"https://storage.googleapis.com/river-treat-249913.appspot.com/proxy.txt",
|
| 100 |
+
"https://storage.googleapis.com/river-treat-249913.appspot.com/ultimate.txt",
|
| 101 |
+
'https://raw.githubusercontent.com/proxylist/proxylist/master/proxy.txt',
|
| 102 |
+
'https://raw.githubusercontent.com/scrapfly/proxy-list/main/proxies.txt',
|
| 103 |
+
'https://raw.githubusercontent.com/roosterkid/openproxylist/main/HTTP.txt',
|
| 104 |
+
'https://raw.githubusercontent.com/roosterkid/openproxylist/main/SOCKS4.txt',
|
| 105 |
+
'https://raw.githubusercontent.com/roosterkid/openproxylist/main/SOCKS5.txt',
|
| 106 |
+
'https://raw.githubusercontent.com/roosterkid/openproxylist/main/HTTPS.txt',
|
| 107 |
+
'https://raw.githubusercontent.com/roosterkid/openproxylist/main/ALL.txt',
|
| 108 |
+
'https://raw.githubusercontent.com/proxylist/proxylist/master/https.txt',
|
| 109 |
+
'https://raw.githubusercontent.com/proxylist/proxylist/master/socks4.txt',
|
| 110 |
+
'https://raw.githubusercontent.com/proxylist/proxylist/master/socks5.txt',
|
| 111 |
+
'https://raw.githubusercontent.com/proxylist/proxylist/master/http.txt',
|
| 112 |
+
'https://raw.githubusercontent.com/proxylist/proxylist/master/all.txt',
|
| 113 |
+
'https://raw.githubusercontent.com/jetlore/proxies/master/proxy-list.txt',
|
| 114 |
+
'https://raw.githubusercontent.com/hookzof/proxy-list/main/proxy.txt',
|
| 115 |
+
'https://raw.githubusercontent.com/zzlol123/proxy-list/main/proxies.txt',
|
| 116 |
+
'https://raw.githubusercontent.com/sqSfg/Proxy-List/master/http.txt',
|
| 117 |
+
'https://raw.githubusercontent.com/sqSfg/Proxy-List/master/https.txt',
|
| 118 |
+
'https://raw.githubusercontent.com/sqSfg/Proxy-List/master/socks4.txt',
|
| 119 |
+
'https://raw.githubusercontent.com/sqSfg/Proxy-List/master/socks5.txt',
|
| 120 |
+
'https://raw.githubusercontent.com/sqSfg/Proxy-List/master/all.txt',
|
| 121 |
+
'https://www.proxy-list.download/api/v1/get?type=https',
|
| 122 |
+
'https://www.proxy-list.download/api/v1/get?type=http',
|
| 123 |
+
'https://www.proxy-list.download/api/v1/get?type=socks4',
|
| 124 |
+
'https://www.proxy-list.download/api/v1/get?type=socks5',
|
| 125 |
+
'https://www.proxy-list.download/api/v1/get?type=all',
|
| 126 |
+
'https://www.sslproxies.org/',
|
| 127 |
+
'https://www.us-proxy.org/',
|
| 128 |
+
'https://free-proxy-list.net/',
|
| 129 |
+
'https://www.proxy-list.download/',
|
| 130 |
+
'https://www.proxy-list.org/eng/proxylist.txt',
|
| 131 |
+
"https://api.proxyscrape.com/v2/?request=displayproxies&protocol=http&timeout=10000&country=all&ssl=all&anonymity=all"
|
| 132 |
+
]
|
| 133 |
+
|
| 134 |
+
for i in range(2, 52):
|
| 135 |
+
proxy_urls.append(f'https://free-proxy-list.net/uk-proxy.html?page={i}')
|
| 136 |
+
|
| 137 |
+
proxies = set()
|
| 138 |
+
|
| 139 |
+
for url in proxy_urls:
|
| 140 |
+
response_text = fetch_response(url)
|
| 141 |
+
if response_text:
|
| 142 |
+
if response_text.startswith('{') and response_text.endswith('}'):
|
| 143 |
+
try:
|
| 144 |
+
data = json.loads(response_text)
|
| 145 |
+
if isinstance(data, dict) and 'data' in data:
|
| 146 |
+
new_proxies = {f"{proxy_data['ip']}:{proxy_data['port']}" for proxy_data in data['data'] if 'ip' in proxy_data and 'port' in proxy_data}
|
| 147 |
+
proxies.update(new_proxies)
|
| 148 |
+
except ValueError:
|
| 149 |
+
pass
|
| 150 |
+
else:
|
| 151 |
+
if 'free-proxy-list.net' in url:
|
| 152 |
+
soup = BeautifulSoup(response_text, 'html.parser')
|
| 153 |
+
table = soup.find('table', {'id': 'proxylisttable'})
|
| 154 |
+
if table: # Verifica si se encontr贸 la tabla
|
| 155 |
+
for row in table.find_all('tr')[1:]:
|
| 156 |
+
try:
|
| 157 |
+
columns = row.find_all('td')
|
| 158 |
+
ip = columns[0].text.strip()
|
| 159 |
+
port = columns[1].text.strip()
|
| 160 |
+
proxy = f"{ip}:{port}"
|
| 161 |
+
proxies.add(proxy)
|
| 162 |
+
except:
|
| 163 |
+
pass
|
| 164 |
+
else:
|
| 165 |
+
lines = response_text.splitlines()
|
| 166 |
+
new_proxies = {line.strip() for line in lines if line.strip()}
|
| 167 |
+
proxies.update(new_proxies)
|
| 168 |
+
|
| 169 |
+
with proxies_lock:
|
| 170 |
+
for proxy in proxies:
|
| 171 |
+
if is_valid_proxy(proxy):
|
| 172 |
+
valid_proxies[proxy] = True
|
| 173 |
+
else:
|
| 174 |
+
invalid_proxies[proxy] = True
|
| 175 |
+
return valid_proxies, invalid_proxies
|
| 176 |
+
|
| 177 |
+
@app.on_event("startup")
|
| 178 |
+
async def on_startup():
|
| 179 |
+
global valid_proxies, invalid_proxies
|
| 180 |
+
valid_proxies, invalid_proxies = await fetch_proxies_from_sources()
|
| 181 |
+
|
| 182 |
+
def fetch_response(url):
|
| 183 |
+
headers = create_headers()
|
| 184 |
+
try:
|
| 185 |
+
response = requests.get(url, headers=headers)
|
| 186 |
+
response.raise_for_status()
|
| 187 |
+
return response.text
|
| 188 |
+
except requests.RequestException:
|
| 189 |
+
return None
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
@app.get("/")
|
| 193 |
+
async def root():
|
| 194 |
+
html_content = '''
|
| 195 |
+
<html>
|
| 196 |
+
<body>
|
| 197 |
+
<h1>Proxy List</h1>
|
| 198 |
+
<p><a href="/valid">Valid Proxies</a></p>
|
| 199 |
+
<p><a href="/invalid">Invalid Proxies</a></p>
|
| 200 |
+
</body>
|
| 201 |
+
</html>
|
| 202 |
+
'''
|
| 203 |
+
return HTMLResponse(content=html_content, status_code=200)
|
| 204 |
+
|
| 205 |
+
@app.get("/valid")
|
| 206 |
+
async def valid_proxies_endpoint():
|
| 207 |
+
with proxies_lock:
|
| 208 |
+
valid_proxies_list = list(valid_proxies.keys())
|
| 209 |
+
if len(valid_proxies_list) > 10:
|
| 210 |
+
valid_proxies_list = random.sample(valid_proxies_list, 10)
|
| 211 |
+
response_content = "\n".join(valid_proxies_list)
|
| 212 |
+
return Response(content=response_content, media_type='text/plain')
|
| 213 |
+
|
| 214 |
+
@app.get("/invalid")
|
| 215 |
+
async def invalid_proxies_endpoint():
|
| 216 |
+
with proxies_lock:
|
| 217 |
+
invalid_proxies_list = list(invalid_proxies.keys())
|
| 218 |
+
if len(invalid_proxies_list) > 10:
|
| 219 |
+
invalid_proxies_list = random.sample(invalid_proxies_list, 10)
|
| 220 |
+
response_content = "\n".join(invalid_proxies_list)
|
| 221 |
+
return Response(content=response_content, media_type='text/plain')
|
| 222 |
+
|
| 223 |
+
@app.websocket("/ws")
|
| 224 |
+
async def websocket_endpoint(websocket: WebSocket):
|
| 225 |
+
await websocket.accept()
|
| 226 |
+
while True:
|
| 227 |
+
with proxies_lock:
|
| 228 |
+
proxies_list = list(valid_proxies.keys())
|
| 229 |
+
await websocket.send_text("\n".join(proxies_list))
|
| 230 |
+
await asyncio.sleep(0)
|
| 231 |
+
|
| 232 |
+
if __name__ == "__main__":
|
| 233 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|