1010import os
1111import subprocess
1212import threading
13- import urllib .request
1413import ipaddress
15- import socket
1614from pathlib import Path
1715
18- # 尝试导入代理相关库(用于 SOCKS5 代理)
19- HAS_SOCKS = False
20- HAS_REQUESTS = False
21-
22- try :
23- import socks
24- HAS_SOCKS = True
25- except ImportError :
26- pass
27-
28- try :
29- import requests
30- HAS_REQUESTS = True
31- except ImportError :
32- pass
33-
3416# Windows 特殊处理
3517if sys .platform == 'win32' :
3618 # 隐藏控制台窗口
8264 print ("安装命令: pip3 install PyQt5" )
8365 sys .exit (1 )
8466
85- APP_VERSION = "1.4 "
67+ APP_VERSION = "1.3 "
8668APP_TITLE = f"ECH Workers 客户端 v{ APP_VERSION } "
8769
88- # 中国IP列表URL
89- CHINA_IP_LIST_URL = "https://raw.githubusercontent.com/mayaxcn/china-ip-list/master/ chn_ip.txt"
70+ # 中国IP列表文件名(离线版本,放在程序目录)
71+ CHINA_IP_LIST_FILE = "chn_ip.txt"
9072
9173def get_app_dir ():
9274 """获取程序所在目录(支持打包后的可执行文件)"""
@@ -560,29 +542,21 @@ def quit_application(self):
560542
561543 QApplication .quit ()
562544
563- def load_china_ip_list_async (self , silent = False , use_proxy = False , proxy_addr = None ):
564- """异步加载中国IP列表(静默模式:失败时不显示错误 )
545+ def load_china_ip_list_async (self , silent = False ):
546+ """异步加载中国IP列表(从离线文件读取 )
565547
566548 Args:
567549 silent: 是否静默模式(失败时不显示错误)
568- use_proxy: 是否使用代理下载
569- proxy_addr: 代理地址,格式为 "127.0.0.1:1080"
570550 """
571551 def load_in_thread ():
572552 try :
573553 if not silent :
574- if use_proxy :
575- self .append_log (f"[系统] 正在通过代理 { proxy_addr } 下载中国IP列表...\n " )
576- else :
577- self .append_log ("[系统] 正在加载中国IP列表...\n " )
578- ranges = self ._load_china_ip_list (use_proxy = use_proxy , proxy_addr = proxy_addr )
554+ self .append_log ("[系统] 正在加载中国IP列表(离线版本)...\n " )
555+ ranges = self ._load_china_ip_list ()
579556 if ranges :
580557 self .china_ip_ranges = ranges
581558 if not silent :
582- if use_proxy :
583- self .append_log (f"[系统] 已通过代理下载中国IP列表,共 { len (ranges )} 个IP段\n " )
584- else :
585- self .append_log (f"[系统] 已加载中国IP列表,共 { len (ranges )} 个IP段\n " )
559+ self .append_log (f"[系统] 已加载中国IP列表,共 { len (ranges )} 个IP段\n " )
586560 # 失败时不显示错误(静默模式)
587561 except Exception as e :
588562 # 静默模式:不显示错误
@@ -592,13 +566,8 @@ def load_in_thread():
592566 thread = threading .Thread (target = load_in_thread , daemon = True )
593567 thread .start ()
594568
595- def _load_china_ip_list (self , use_proxy = False , proxy_addr = None ):
596- """下载并解析中国IP列表(缓存永久有效)
597-
598- Args:
599- use_proxy: 是否使用代理下载
600- proxy_addr: 代理地址,格式为 "127.0.0.1:1080" 或 "host:port"
601- """
569+ def _load_china_ip_list (self ):
570+ """从程序目录读取并解析中国IP列表(离线版本)"""
602571 try :
603572 # 尝试从缓存读取(永久有效,不检查过期时间)
604573 cache_file = self .config_manager .config_dir / "china_ip_list.json"
@@ -612,57 +581,17 @@ def _load_china_ip_list(self, use_proxy=False, proxy_addr=None):
612581 except :
613582 pass
614583
615- # 下载IP列表
616- if use_proxy and proxy_addr :
617- # 解析代理地址
618- if ':' in proxy_addr :
619- proxy_host , proxy_port = proxy_addr .rsplit (':' , 1 )
620- proxy_port = int (proxy_port )
621- else :
622- proxy_host = proxy_addr
623- proxy_port = 1080
624-
625- # 尝试使用代理下载
626- if HAS_REQUESTS :
627- # 使用 requests 库(支持 SOCKS5)
628- try :
629- proxies = {
630- 'http' : f'socks5://{ proxy_host } :{ proxy_port } ' ,
631- 'https' : f'socks5://{ proxy_host } :{ proxy_port } '
632- }
633- response = requests .get (CHINA_IP_LIST_URL , proxies = proxies , timeout = 30 )
634- response .raise_for_status ()
635- content = response .text
636- except Exception :
637- # 代理下载失败,尝试直接下载
638- with urllib .request .urlopen (CHINA_IP_LIST_URL , timeout = 10 ) as response :
639- content = response .read ().decode ('utf-8' )
640- elif HAS_SOCKS :
641- # 使用 socks 库
642- try :
643- # 创建 SOCKS5 代理 socket
644- socks .set_default_proxy (socks .SOCKS5 , proxy_host , proxy_port )
645- socket .socket = socks .socksocket
646-
647- # 下载
648- with urllib .request .urlopen (CHINA_IP_LIST_URL , timeout = 30 ) as response :
649- content = response .read ().decode ('utf-8' )
650-
651- # 恢复原始 socket
652- socket .socket = socket ._socket
653- except Exception :
654- # 代理下载失败,尝试直接下载
655- socket .socket = socket ._socket # 确保恢复
656- with urllib .request .urlopen (CHINA_IP_LIST_URL , timeout = 10 ) as response :
657- content = response .read ().decode ('utf-8' )
658- else :
659- # 没有代理库,直接下载
660- with urllib .request .urlopen (CHINA_IP_LIST_URL , timeout = 10 ) as response :
661- content = response .read ().decode ('utf-8' )
662- else :
663- # 直接下载(不使用代理)
664- with urllib .request .urlopen (CHINA_IP_LIST_URL , timeout = 10 ) as response :
665- content = response .read ().decode ('utf-8' )
584+ # 从程序目录读取IP列表文件(离线版本)
585+ app_dir = get_app_dir ()
586+ ip_list_file = app_dir / CHINA_IP_LIST_FILE
587+
588+ if not ip_list_file .exists ():
589+ # 如果文件不存在,返回 None(静默失败)
590+ return None
591+
592+ # 读取文件内容
593+ with open (ip_list_file , 'r' , encoding = 'utf-8' ) as f :
594+ content = f .read ()
666595
667596 # 解析IP范围
668597 ranges = []
@@ -1046,33 +975,9 @@ def start_process(self):
1046975 self .server_combo .setEnabled (False )
1047976 self .append_log (f"[系统] 已启动服务器: { server ['name' ]} \n " )
1048977
1049- # 代理启动后,通过代理下载中国IP列表
1050- def download_ip_list_after_proxy_start ():
1051- import time
1052- # 等待代理启动(最多等待5秒)
1053- for _ in range (10 ):
1054- if self .process_thread and self .process_thread .is_running :
1055- time .sleep (0.5 ) # 再等待0.5秒确保代理已就绪
1056- break
1057- time .sleep (0.5 )
1058-
1059- # 如果代理已启动,通过代理下载
1060- if self .process_thread and self .process_thread .is_running :
1061- listen_addr = server .get ('listen' , '127.0.0.1:1080' )
1062- if self .china_ip_ranges is None :
1063- self .append_log (f"[系统] 代理已启动,正在通过代理 { listen_addr } 下载中国IP列表...\n " )
1064- self .load_china_ip_list_async (silent = False , use_proxy = True , proxy_addr = listen_addr )
1065- else :
1066- # 如果已有缓存,尝试更新(通过代理)
1067- self .append_log (f"[系统] 正在通过代理 { listen_addr } 更新中国IP列表...\n " )
1068- self .load_china_ip_list_async (silent = True , use_proxy = True , proxy_addr = listen_addr )
1069- else :
1070- # 代理未启动,直接下载
1071- if self .china_ip_ranges is None :
1072- self .append_log ("[系统] 正在下载中国IP列表...\n " )
1073- self .load_china_ip_list_async (silent = False , use_proxy = False )
1074-
1075- threading .Thread (target = download_ip_list_after_proxy_start , daemon = True ).start ()
978+ # 如果中国IP列表未加载,尝试加载(从离线文件)
979+ if self .china_ip_ranges is None :
980+ self .load_china_ip_list_async (silent = True )
1076981
1077982 def stop_process (self ):
1078983 """停止进程"""
0 commit comments