WhereAmI/diagnose.py
2026-06-15 00:49:26 +08:00

125 lines
3.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""诊断脚本 - 检查代理检测失败原因"""
import sys
import socket
import requests
from loguru import logger
from core.models import ProxyInfo, ProxyProtocol
def test_single_proxy():
"""测试单个代理的详细检测过程"""
logger.info("=" * 60)
logger.info("代理诊断工具")
logger.info("=" * 60)
# 创建一个测试代理使用local.json中的第一个
proxy = ProxyInfo(
ip_address="171.6.75.111",
port=8080,
username="no need",
password="no need",
protocol=ProxyProtocol.SOCKS4
)
logger.info(f"\n测试代理: {proxy.get_address()} ({proxy.protocol.value})\n")
# 测试1: TCP连接
logger.info("[测试1] TCP连接测试...")
try:
start_time = __import__('time').time()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
result = sock.connect_ex((proxy.ip_address, proxy.port))
elapsed = (__import__('time').time() - start_time) * 1000
sock.close()
if result == 0:
logger.info(f"✓ TCP连接成功延迟: {elapsed:.0f}ms")
else:
logger.error(f"✗ TCP连接失败错误码: {result}")
return False
except Exception as e:
logger.error(f"✗ TCP连接异常: {str(e)}")
return False
# 测试2: SOCKS握手
logger.info("\n[测试2] SOCKS握手测试...")
try:
import socks
proxy_type = socks.SOCKS4 if proxy.protocol.value == 'socks4' else socks.SOCKS5
test_sock = socks.socksocket()
test_sock.set_proxy(
proxy_type,
proxy.ip_address,
proxy.port,
username=None,
password=None
)
test_sock.settimeout(5)
test_sock.connect(('www.google.com', 80))
test_sock.close()
logger.info("✓ SOCKS握手成功")
except Exception as e:
logger.error(f"✗ SOCKS握手失败: {str(e)}")
return False
# 测试3: Google连通性
logger.info("\n[测试3] Google连通性测试...")
try:
proxies_dict = {}
protocol = proxy.protocol.value
if protocol in ['socks4', 'socks5']:
proxies_dict['http'] = f"socks5://{proxy.get_address()}"
proxies_dict['https'] = f"socks5://{proxy.get_address()}"
response = requests.get(
'https://www.google.com/',
proxies=proxies_dict,
timeout=10,
verify=False
)
logger.info(f"✓ Google连通性测试成功状态码: {response.status_code}")
except Exception as e:
logger.error(f"✗ Google连通性测试失败: {str(e)}")
return False
logger.info("\n" + "=" * 60)
logger.info("✓ 所有测试通过!该代理可用")
logger.info("=" * 60)
return True
def check_network():
"""检查本地网络环境"""
logger.info("\n[网络检查] 本地网络环境...")
# 测试直连Google
try:
response = requests.get('https://www.google.com/', timeout=5)
logger.info(f"✓ 直连Google成功状态码: {response.status_code}")
logger.info(" 提示: 您的网络可以直接访问Google可能不需要代理")
except Exception as e:
logger.info(f"✗ 直连Google失败: {str(e)}")
logger.info(" 提示: 您的网络无法直接访问Google需要代理")
if __name__ == '__main__':
# 设置日志
logger.remove()
logger.add(sys.stderr, level='INFO')
# 检查网络
check_network()
# 测试代理
test_single_proxy()