-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
146 lines (125 loc) · 5.69 KB
/
proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import requests
import time
import os
import random
# 定义文件路径
ip_file = "cfip/ip.txt" # 输入的 IP 文件
proxy_file = "cfip/proxy.txt" # 输出的 proxy IP 文件
# 定义 SOCKS 代理
proxies = {
'http': 'socks5://eytan:[email protected]:15004',
'https': 'socks5://eytan:[email protected]:15004'
}
# 定义一组常见的 User-Agent
USER_AGENTS = [
# Chrome on Windows
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
# Chrome on macOS
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
# Firefox on Windows
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0",
# Safari on macOS
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15",
# Edge on Windows
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0",
]
def get_random_user_agent():
"""随机返回一个 User-Agent"""
return random.choice(USER_AGENTS)
def get_user_agent_by_tls(tls_version):
"""
根据 TLS 版本选择合适的 User-Agent
:param tls_version: TLS 版本,例如 "TLSv1.3"
:return: 匹配的 User-Agent
"""
if tls_version == "TLSv1.3":
# TLS 1.3 是现代浏览器的标配,选择较新的 UA
return random.choice([
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
])
else:
# 对于旧版 TLS,选择较旧的 UA
return random.choice([
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
])
def test_socks_proxy():
"""测试 SOCKS 代理是否正常,并打印代理 IP"""
try:
# 使用代理访问外部网站(例如 httpbin.org/ip)
response = requests.get('https://httpbin.org/ip', proxies=proxies, timeout=(5, 10))
if response.status_code == 200:
data = response.json()
print(f"SOCKS 代理正常工作,当前代理 IP: {data['origin']}")
return True
else:
print(f"SOCKS 代理请求失败,状态码: {response.status_code}")
return False
except requests.exceptions.RequestException as e:
print(f"SOCKS 代理请求出错: {e}")
return False
def get_proxyip_status(ip_address, retries=3):
"""获取 IP 的 proxyip 状态"""
api_url = f"http://proxyip.edtunnel.best/api?ip={ip_address}&host=speed.cloudflare.com&port=443&tls=true"
for attempt in range(retries):
try:
# 动态设置 User-Agent
headers = {
'User-Agent': get_random_user_agent(),
'Accept': 'application/json',
'Accept-Language': 'en-US,en;q=0.9',
}
# 发送请求
response = requests.get(api_url, headers=headers, proxies=proxies, timeout=(5, 10))
response.raise_for_status() # 如果状态码不是 200,抛出异常
# 解析响应数据
data = response.json()
proxyip = data.get('proxyip', False)
return proxyip
except requests.exceptions.HTTPError as e:
print(f"HTTP 错误,IP {ip_address}: {e}")
if response.status_code == 500:
print("服务器内部错误,请检查请求参数或联系 API 提供方。")
print(f"响应内容: {response.text}") # 打印响应内容
except requests.exceptions.RequestException as e:
print(f"请求出错,IP {ip_address}: {e}")
# 如果请求失败,等待一段时间后重试
if attempt < retries - 1:
time.sleep(5) # 每次重试前等待 5 秒
return False
def filter_proxy_ips():
"""从 ip.txt 中读取 IP,筛选 proxyip 为 true 的 IP 并写入 proxy.txt"""
try:
# 检查文件路径是否存在
if not os.path.exists(ip_file):
print(f"文件 {ip_file} 不存在")
return
# 读取 ip.txt 文件
with open(ip_file, mode="r", encoding="utf-8") as file:
ip_lines = file.readlines()
# 遍历每一行
for line in ip_lines:
# 去除换行符并分割 IP 和注释部分
line = line.strip()
if "#" in line:
ip_address, comment = line.split("#", 1)
else:
ip_address, comment = line, ""
# 获取 proxyip 状态
proxyip = get_proxyip_status(ip_address)
# 如果 proxyip 为 true,写入 proxy.txt
if proxyip:
try:
with open(proxy_file, mode="a", encoding="utf-8") as proxy_output:
proxy_output.write(f"{ip_address}#{comment}\n")
print(f"IP {ip_address} 已写入 proxy.txt")
except Exception as e:
print(f"写入文件时出错: {e}")
# 降低请求频率,每次请求后等待 2 秒
time.sleep(2)
except Exception as e:
print(f"处理文件时出错: {e}")
# 调用函数
test_socks_proxy()
filter_proxy_ips()