66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
from pathlib import Path
|
|
from typing import List
|
|
from tweety.types import Proxy, PROXY_TYPE_SOCKS5, PROXY_TYPE_HTTP
|
|
|
|
|
|
def read_proxy_file(file_path: str) -> List[Proxy]:
|
|
path = Path(file_path)
|
|
if not path.exists():
|
|
raise FileNotFoundError(f"Proxy file not found: {file_path}")
|
|
|
|
proxies = []
|
|
lines = path.read_text(encoding='utf-8').strip().split('\n')
|
|
|
|
for line in lines:
|
|
line = line.strip()
|
|
if not line or line.startswith('#'):
|
|
continue
|
|
|
|
try:
|
|
if '://' in line:
|
|
protocol, rest = line.split('://', 1)
|
|
else:
|
|
protocol = 'socks5'
|
|
rest = line
|
|
|
|
username = None
|
|
password = None
|
|
|
|
if '@' in rest:
|
|
auth, host_port = rest.rsplit('@', 1)
|
|
if ':' in auth:
|
|
username, password = auth.split(':', 1)
|
|
else:
|
|
host_port = rest
|
|
|
|
if ':' in host_port:
|
|
host, port = host_port.rsplit(':', 1)
|
|
port = int(port)
|
|
else:
|
|
continue
|
|
|
|
protocol = protocol.lower()
|
|
if protocol in ['socks5', 'socks', 'socks5h']:
|
|
proxy_type = PROXY_TYPE_SOCKS5
|
|
elif protocol in ['http', 'https']:
|
|
proxy_type = PROXY_TYPE_HTTP
|
|
else:
|
|
continue
|
|
|
|
proxy = Proxy(
|
|
host=host,
|
|
port=port,
|
|
proxy_type=proxy_type,
|
|
username=username,
|
|
password=password
|
|
)
|
|
|
|
proxies.append(proxy)
|
|
|
|
except Exception:
|
|
continue
|
|
|
|
if proxies:
|
|
print(f"✓ Loaded {len(proxies)} proxies from file")
|
|
|
|
return proxies
|