feat: implement comprehensive error diagnostics and structured error codes for VPN connectivity, API fetching, and proxy initialization.

This commit is contained in:
baoweise-bot
2026-05-30 14:02:39 +08:00
parent 531beca616
commit 7b9248c4df
4 changed files with 638 additions and 440 deletions
+13 -2
View File
@@ -51,7 +51,11 @@ def resolve_dns_over_tun0(host: str, dns_server: str = "8.8.8.8", timeout: float
sock.settimeout(timeout)
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, b"tun0")
except OSError:
except OSError as e:
if "operation not permitted" in str(e).lower() or e.errno == 1:
print("[DNS 绑定失败] [错误代码 3006] DNS 解析绑定 tun0 权限不足,请确保程序以 root 权限运行!", flush=True)
elif "no such device" in str(e).lower() or e.errno == 19:
print("[DNS 绑定失败] [错误代码 3004] DNS 解析绑定 tun0 失败,网卡设备不存在,请检查 VPN 连接!", flush=True)
return None
sock.sendto(packet, (dns_server, 53))
resp, _ = sock.recvfrom(2048)
@@ -131,6 +135,10 @@ def create_connection(address: tuple[str, int], timeout: float = 20) -> socket.s
return sock
except OSError as e:
err = e
if "operation not permitted" in str(e).lower() or e.errno == 1:
err = OSError(f"[错误代码 3006] [ERR_PROXY_BIND_TUN_PERM_DENIED] 绑定虚拟网卡 tun0 失败,权限不足!必须以 root 权限运行,或者进程缺少 CAP_NET_RAW 权限。")
elif "no such device" in str(e).lower() or e.errno == 19:
err = OSError(f"[错误代码 3004] [ERR_ROUTE_DEV_NOT_FOUND] 绑定虚拟网卡 tun0 失败,找不到设备!这通常是因为 OpenVPN 核心未能成功连接或已被异常终止。")
if sock is not None:
sock.close()
if err is not None:
@@ -255,7 +263,10 @@ def start_proxy_server(host: str, port: int) -> None:
server.listen(256)
print(f"HTTP/SOCKS5 proxy listening on {host}:{port}", flush=True)
except Exception as e:
print(f"[ERROR] Failed to start HTTP/SOCKS5 proxy on {host}:{port}: {e}", flush=True)
import vpn_utils
diag = vpn_utils.diagnose_local_obstructions(port)
diag_msg = diag[1] if diag else str(e)
print(f"[ERROR] Failed to start HTTP/SOCKS5 proxy on {host}:{port}: {diag_msg}", flush=True)
return
while True: