mirror of
https://github.com/hyb-oyqq/FRAISEMOE-Addons-Installer-NEXT.git
synced 2026-04-05 17:16:31 +00:00
feat(core): 初步添加IPv6支持和优化功能
- 在CloudflareOptimizer中添加IPv6优化功能,支持同时进行IPv4和IPv6的优选。 - 更新UIManager,增加IPv6支持选项,并实现用户切换功能。 - 在下载线程中检查IPv6支持状态,并根据设置决定是否禁用IPv6。 - 在IP优化器中实现获取最优IPv6地址的功能,优化测速参数设置。 - 改进用户提示信息,确保用户了解IPv6优选的时间和效果。
This commit is contained in:
@@ -21,11 +21,13 @@ class CloudflareOptimizer:
|
||||
self.main_window = main_window
|
||||
self.hosts_manager = hosts_manager
|
||||
self.optimized_ip = None
|
||||
self.optimized_ipv6 = None
|
||||
self.optimization_done = False # 标记是否已执行过优选
|
||||
self.countdown_finished = False # 标记倒计时是否结束
|
||||
self.optimizing_msg_box = None
|
||||
self.optimization_cancelled = False
|
||||
self.ip_optimizer_thread = None
|
||||
self.ipv6_optimizer_thread = None
|
||||
|
||||
def is_optimization_done(self):
|
||||
"""检查是否已完成优化
|
||||
@@ -50,6 +52,14 @@ class CloudflareOptimizer:
|
||||
str: 优选的IP地址,如果未优选则为None
|
||||
"""
|
||||
return self.optimized_ip
|
||||
|
||||
def get_optimized_ipv6(self):
|
||||
"""获取优选的IPv6地址
|
||||
|
||||
Returns:
|
||||
str: 优选的IPv6地址,如果未优选则为None
|
||||
"""
|
||||
return self.optimized_ipv6
|
||||
|
||||
def start_ip_optimization(self, url):
|
||||
"""开始IP优化过程
|
||||
@@ -61,10 +71,55 @@ class CloudflareOptimizer:
|
||||
self.optimization_cancelled = False
|
||||
self.countdown_finished = False
|
||||
|
||||
# 检查是否启用了IPv6
|
||||
use_ipv6 = False
|
||||
if hasattr(self.main_window, 'config'):
|
||||
use_ipv6 = self.main_window.config.get("ipv6_enabled", False)
|
||||
|
||||
# 如果启用了IPv6,显示警告消息
|
||||
if use_ipv6:
|
||||
ipv6_warning = QtWidgets.QMessageBox(self.main_window)
|
||||
ipv6_warning.setWindowTitle(f"IPv6优选警告 - {self.main_window.APP_NAME}")
|
||||
ipv6_warning.setText("\nIPv6优选比IPv4耗时更长且感知不强(预计耗时10分钟以上),不建议使用。\n\n确定要同时执行IPv6优选吗?\n")
|
||||
ipv6_warning.setIcon(QtWidgets.QMessageBox.Icon.Warning)
|
||||
|
||||
# 设置图标
|
||||
icon_path = resource_path(os.path.join("IMG", "ICO", "icon.png"))
|
||||
if os.path.exists(icon_path):
|
||||
pixmap = QPixmap(icon_path)
|
||||
if not pixmap.isNull():
|
||||
ipv6_warning.setWindowIcon(QIcon(pixmap))
|
||||
|
||||
yes_button = ipv6_warning.addButton("是", QtWidgets.QMessageBox.ButtonRole.YesRole)
|
||||
no_button = ipv6_warning.addButton("否,仅使用IPv4", QtWidgets.QMessageBox.ButtonRole.NoRole)
|
||||
cancel_button = ipv6_warning.addButton("取消优选", QtWidgets.QMessageBox.ButtonRole.RejectRole)
|
||||
|
||||
ipv6_warning.setDefaultButton(no_button)
|
||||
ipv6_warning.exec()
|
||||
|
||||
if ipv6_warning.clickedButton() == cancel_button:
|
||||
# 用户取消了优选
|
||||
self.optimization_cancelled = True
|
||||
return
|
||||
|
||||
# 根据用户选择调整IPv6设置
|
||||
if ipv6_warning.clickedButton() == no_button:
|
||||
use_ipv6 = False
|
||||
# 临时覆盖配置(不保存到文件)
|
||||
if hasattr(self.main_window, 'config'):
|
||||
self.main_window.config["ipv6_enabled"] = False
|
||||
|
||||
# 准备提示信息
|
||||
optimization_msg = "\n正在优选Cloudflare IP,请稍候...\n\n"
|
||||
if use_ipv6:
|
||||
optimization_msg += "已启用IPv6支持,同时进行IPv4和IPv6优选。\n这可能需要10分钟以上,请耐心等待喵~\n"
|
||||
else:
|
||||
optimization_msg += "这可能需要5-10分钟,请耐心等待喵~\n"
|
||||
|
||||
# 使用Cloudflare图标创建消息框
|
||||
self.optimizing_msg_box = msgbox_frame(
|
||||
f"通知 - {self.main_window.APP_NAME}",
|
||||
"\n正在优选Cloudflare IP,请稍候...\n\n这可能需要5-10分钟,请耐心等待喵~"
|
||||
optimization_msg
|
||||
)
|
||||
# 设置Cloudflare图标
|
||||
cf_icon_path = resource_path("IMG/ICO/cloudflare_logo_icon.ico")
|
||||
@@ -82,7 +137,16 @@ class CloudflareOptimizer:
|
||||
|
||||
# 创建并启动优化线程
|
||||
self.ip_optimizer_thread = IpOptimizerThread(url)
|
||||
self.ip_optimizer_thread.finished.connect(self.on_optimization_finished)
|
||||
self.ip_optimizer_thread.finished.connect(self.on_ipv4_optimization_finished)
|
||||
|
||||
# 如果启用IPv6,同时启动IPv6优化线程
|
||||
if use_ipv6:
|
||||
print("IPv6已启用,将同时优选IPv6地址")
|
||||
self.ipv6_optimizer_thread = IpOptimizerThread(url, use_ipv6=True)
|
||||
self.ipv6_optimizer_thread.finished.connect(self.on_ipv6_optimization_finished)
|
||||
self.ipv6_optimizer_thread.start()
|
||||
|
||||
# 启动IPv4优化线程
|
||||
self.ip_optimizer_thread.start()
|
||||
|
||||
# 显示消息框(非模态,不阻塞)
|
||||
@@ -102,6 +166,9 @@ class CloudflareOptimizer:
|
||||
if hasattr(self, 'ip_optimizer_thread') and self.ip_optimizer_thread and self.ip_optimizer_thread.isRunning():
|
||||
self.ip_optimizer_thread.stop()
|
||||
|
||||
if hasattr(self, 'ipv6_optimizer_thread') and self.ipv6_optimizer_thread and self.ipv6_optimizer_thread.isRunning():
|
||||
self.ipv6_optimizer_thread.stop()
|
||||
|
||||
# 恢复主窗口状态
|
||||
self.main_window.setEnabled(True)
|
||||
self.main_window.ui.start_install_text.setText("开始安装")
|
||||
@@ -112,9 +179,9 @@ class CloudflareOptimizer:
|
||||
f"已取消 - {self.main_window.APP_NAME}",
|
||||
"\n已取消IP优选和安装过程。\n"
|
||||
)
|
||||
|
||||
def on_optimization_finished(self, ip):
|
||||
"""IP优化完成后的处理
|
||||
|
||||
def on_ipv4_optimization_finished(self, ip):
|
||||
"""IPv4优化完成后的处理
|
||||
|
||||
Args:
|
||||
ip: 优选的IP地址,如果失败则为空字符串
|
||||
@@ -124,6 +191,14 @@ class CloudflareOptimizer:
|
||||
return
|
||||
|
||||
self.optimized_ip = ip
|
||||
print(f"IPv4优选完成,结果: {ip if ip else '未找到合适的IP'}")
|
||||
|
||||
# 检查是否还有IPv6优化正在运行
|
||||
if hasattr(self, 'ipv6_optimizer_thread') and self.ipv6_optimizer_thread and self.ipv6_optimizer_thread.isRunning():
|
||||
print("等待IPv6优选完成...")
|
||||
return
|
||||
|
||||
# 所有优选都已完成,继续处理
|
||||
self.optimization_done = True
|
||||
self.countdown_finished = False # 确保倒计时标志重置
|
||||
|
||||
@@ -132,15 +207,70 @@ class CloudflareOptimizer:
|
||||
if self.optimizing_msg_box.isVisible():
|
||||
self.optimizing_msg_box.accept()
|
||||
self.optimizing_msg_box = None
|
||||
|
||||
# 显示优选结果
|
||||
if not ip:
|
||||
# 临时启用窗口以显示对话框
|
||||
self.main_window.setEnabled(True)
|
||||
|
||||
# 处理优选结果
|
||||
self._process_optimization_results()
|
||||
|
||||
def on_ipv6_optimization_finished(self, ipv6):
|
||||
"""IPv6优化完成后的处理
|
||||
|
||||
Args:
|
||||
ipv6: 优选的IPv6地址,如果失败则为空字符串
|
||||
"""
|
||||
# 如果已经取消,则不继续处理
|
||||
if hasattr(self, 'optimization_cancelled') and self.optimization_cancelled:
|
||||
return
|
||||
|
||||
self.optimized_ipv6 = ipv6
|
||||
print(f"IPv6优选完成,结果: {ipv6 if ipv6 else '未找到合适的IPv6'}")
|
||||
|
||||
# 检查IPv4优化是否已完成
|
||||
if hasattr(self, 'ip_optimizer_thread') and self.ip_optimizer_thread and self.ip_optimizer_thread.isRunning():
|
||||
print("等待IPv4优选完成...")
|
||||
return
|
||||
|
||||
# 所有优选都已完成,继续处理
|
||||
self.optimization_done = True
|
||||
self.countdown_finished = False # 确保倒计时标志重置
|
||||
|
||||
# 关闭提示框
|
||||
if hasattr(self, 'optimizing_msg_box') and self.optimizing_msg_box:
|
||||
if self.optimizing_msg_box.isVisible():
|
||||
self.optimizing_msg_box.accept()
|
||||
self.optimizing_msg_box = None
|
||||
|
||||
# 处理优选结果
|
||||
self._process_optimization_results()
|
||||
|
||||
def _process_optimization_results(self):
|
||||
"""处理优选的IP结果,显示相应提示"""
|
||||
use_ipv6 = False
|
||||
if hasattr(self.main_window, 'config'):
|
||||
use_ipv6 = self.main_window.config.get("ipv6_enabled", False)
|
||||
|
||||
# 判断优选结果
|
||||
ipv4_success = bool(self.optimized_ip)
|
||||
ipv6_success = bool(self.optimized_ipv6) if use_ipv6 else False
|
||||
|
||||
# 临时启用窗口以显示对话框
|
||||
self.main_window.setEnabled(True)
|
||||
|
||||
hostname = urlparse(self.main_window.current_url).hostname if hasattr(self.main_window, 'current_url') else None
|
||||
|
||||
if not ipv4_success and (not use_ipv6 or not ipv6_success):
|
||||
# 两种IP都没有优选成功
|
||||
msg_box = QtWidgets.QMessageBox(self.main_window)
|
||||
msg_box.setWindowTitle(f"优选失败 - {self.main_window.APP_NAME}")
|
||||
msg_box.setText("\n未能找到合适的Cloudflare IP,将使用默认网络进行下载。\n\n10秒后自动继续...")
|
||||
|
||||
fail_message = "\n未能找到合适的Cloudflare "
|
||||
if use_ipv6:
|
||||
fail_message += "IPv4和IPv6地址"
|
||||
else:
|
||||
fail_message += "IP地址"
|
||||
|
||||
fail_message += ",将使用默认网络进行下载。\n\n10秒后自动继续..."
|
||||
|
||||
msg_box.setText(fail_message)
|
||||
msg_box.setIcon(QtWidgets.QMessageBox.Icon.Warning)
|
||||
ok_button = msg_box.addButton("确定 (10)", QtWidgets.QMessageBox.ButtonRole.AcceptRole)
|
||||
cancel_button = msg_box.addButton("取消安装", QtWidgets.QMessageBox.ButtonRole.RejectRole)
|
||||
@@ -180,20 +310,32 @@ class CloudflareOptimizer:
|
||||
self.countdown_finished = True
|
||||
return True
|
||||
else:
|
||||
# 应用优选IP到hosts文件
|
||||
hostname = urlparse(self.main_window.current_url).hostname if hasattr(self.main_window, 'current_url') else None
|
||||
|
||||
# 至少有一种IP优选成功
|
||||
success_message = ""
|
||||
if ipv4_success:
|
||||
success_message += f"IPv4: {self.optimized_ip}\n"
|
||||
|
||||
if ipv6_success:
|
||||
success_message += f"IPv6: {self.optimized_ipv6}\n"
|
||||
|
||||
if hostname:
|
||||
# 先清理可能存在的旧记录
|
||||
self.hosts_manager.clean_hostname_entries(hostname)
|
||||
|
||||
# 临时启用窗口以显示对话框
|
||||
self.main_window.setEnabled(True)
|
||||
success = False
|
||||
|
||||
if self.hosts_manager.apply_ip(hostname, ip):
|
||||
# 应用优选IP到hosts文件
|
||||
if ipv4_success:
|
||||
success = self.hosts_manager.apply_ip(hostname, self.optimized_ip) or success
|
||||
|
||||
# 如果启用IPv6并且找到了IPv6地址,也应用到hosts
|
||||
if ipv6_success:
|
||||
success = self.hosts_manager.apply_ip(hostname, self.optimized_ipv6) or success
|
||||
|
||||
if success:
|
||||
msg_box = QtWidgets.QMessageBox(self.main_window)
|
||||
msg_box.setWindowTitle(f"成功 - {self.main_window.APP_NAME}")
|
||||
msg_box.setText(f"\n已将优选IP ({ip}) 应用到hosts文件。\n\n10秒后自动继续...")
|
||||
msg_box.setText(f"\n已将优选IP应用到hosts文件:\n{success_message}\n10秒后自动继续...")
|
||||
msg_box.setIcon(QtWidgets.QMessageBox.Icon.Information)
|
||||
ok_button = msg_box.addButton("确定 (10)", QtWidgets.QMessageBox.ButtonRole.AcceptRole)
|
||||
cancel_button = msg_box.addButton("取消安装", QtWidgets.QMessageBox.ButtonRole.RejectRole)
|
||||
@@ -248,7 +390,12 @@ class CloudflareOptimizer:
|
||||
if hasattr(self, 'ip_optimizer_thread') and self.ip_optimizer_thread and self.ip_optimizer_thread.isRunning():
|
||||
self.ip_optimizer_thread.stop()
|
||||
self.ip_optimizer_thread.wait()
|
||||
if hasattr(self, 'optimizing_msg_box') and self.optimizing_msg_box:
|
||||
if self.optimizing_msg_box.isVisible():
|
||||
self.optimizing_msg_box.accept()
|
||||
self.optimizing_msg_box = None
|
||||
|
||||
if hasattr(self, 'ipv6_optimizer_thread') and self.ipv6_optimizer_thread and self.ipv6_optimizer_thread.isRunning():
|
||||
self.ipv6_optimizer_thread.stop()
|
||||
self.ipv6_optimizer_thread.wait()
|
||||
|
||||
if hasattr(self, 'optimizing_msg_box') and self.optimizing_msg_box:
|
||||
if self.optimizing_msg_box.isVisible():
|
||||
self.optimizing_msg_box.accept()
|
||||
self.optimizing_msg_box = None
|
||||
@@ -279,6 +279,21 @@ class UIManager:
|
||||
self.hosts_submenu.setFont(menu_font)
|
||||
self.hosts_submenu.setStyleSheet(menu_style)
|
||||
|
||||
# 添加IPv6支持选项
|
||||
self.ipv6_action = QAction("启用IPv6支持", self.main_window, checkable=True)
|
||||
self.ipv6_action.setFont(menu_font)
|
||||
|
||||
# 检查配置中是否已启用IPv6
|
||||
config = getattr(self.main_window, 'config', {})
|
||||
ipv6_enabled = False
|
||||
if isinstance(config, dict):
|
||||
ipv6_enabled = config.get("ipv6_enabled", False)
|
||||
|
||||
self.ipv6_action.setChecked(ipv6_enabled)
|
||||
|
||||
# 连接IPv6支持切换事件
|
||||
self.ipv6_action.triggered.connect(self.toggle_ipv6_support)
|
||||
|
||||
# 添加hosts子选项
|
||||
self.restore_hosts_action = QAction("还原软件备份的hosts文件", self.main_window)
|
||||
self.restore_hosts_action.setFont(menu_font)
|
||||
@@ -350,6 +365,7 @@ class UIManager:
|
||||
# 添加Debug子菜单到开发者选项菜单
|
||||
self.dev_menu.addMenu(self.debug_submenu)
|
||||
self.dev_menu.addMenu(self.hosts_submenu) # 添加hosts文件选项子菜单
|
||||
self.dev_menu.addAction(self.ipv6_action) # 添加IPv6支持选项
|
||||
|
||||
def show_menu(self, menu, button):
|
||||
"""显示菜单
|
||||
@@ -539,6 +555,53 @@ class UIManager:
|
||||
)
|
||||
msg_box.exec()
|
||||
|
||||
def toggle_ipv6_support(self, enabled):
|
||||
"""切换IPv6支持
|
||||
|
||||
Args:
|
||||
enabled: 是否启用IPv6支持
|
||||
"""
|
||||
print(f"Toggle IPv6 support: {enabled}")
|
||||
|
||||
# 如果用户尝试启用IPv6,检查系统是否支持IPv6
|
||||
if enabled:
|
||||
# 检查是否可以访问IPv6
|
||||
import socket
|
||||
ipv6_supported = False
|
||||
|
||||
try:
|
||||
# 尝试创建一个IPv6套接字
|
||||
socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
||||
ipv6_supported = True
|
||||
except Exception:
|
||||
ipv6_supported = False
|
||||
|
||||
if not ipv6_supported:
|
||||
msg_box = msgbox_frame(
|
||||
f"错误 - {APP_NAME}",
|
||||
"\n找不到IPv6地址,您的系统可能不支持IPv6。\n",
|
||||
QMessageBox.StandardButton.Ok,
|
||||
)
|
||||
msg_box.exec()
|
||||
|
||||
# 恢复复选框状态
|
||||
self.ipv6_action.setChecked(False)
|
||||
return
|
||||
|
||||
# 保存设置到配置
|
||||
if hasattr(self.main_window, 'config'):
|
||||
self.main_window.config["ipv6_enabled"] = enabled
|
||||
self.main_window.save_config(self.main_window.config)
|
||||
|
||||
# 显示设置已保存的消息
|
||||
status = "启用" if enabled else "禁用"
|
||||
msg_box = msgbox_frame(
|
||||
f"IPv6设置 - {APP_NAME}",
|
||||
f"\nIPv6支持已{status}。新的设置将在下一次下载时生效。\n",
|
||||
QMessageBox.StandardButton.Ok,
|
||||
)
|
||||
msg_box.exec()
|
||||
|
||||
def clean_hosts_entries(self):
|
||||
"""手动删除软件添加的hosts条目"""
|
||||
if hasattr(self.main_window, 'download_manager') and hasattr(self.main_window.download_manager, 'hosts_manager'):
|
||||
|
||||
Reference in New Issue
Block a user