85 lines
3.0 KiB
Python
85 lines
3.0 KiB
Python
from PyQt5.QtWidgets import (
|
|
QDialog, QVBoxLayout, QHBoxLayout, QLabel,
|
|
QLineEdit, QPushButton, QMessageBox
|
|
)
|
|
from PyQt5.QtCore import Qt
|
|
from urllib.parse import urlparse
|
|
|
|
class SetupDialog(QDialog):
|
|
def __init__(self, app_context, parent=None):
|
|
super().__init__(parent)
|
|
self.app_context = app_context
|
|
self.setWindowTitle("设置代理地址")
|
|
self.setMinimumWidth(500)
|
|
|
|
layout = QVBoxLayout()
|
|
|
|
# 说明标签
|
|
desc_label = QLabel("请输入您的VLESS代理地址 (格式: vless://...)")
|
|
desc_label.setWordWrap(True)
|
|
layout.addWidget(desc_label)
|
|
|
|
# 代理地址输入
|
|
self.url_input = QLineEdit()
|
|
self.url_input.setPlaceholderText("vless://uuid@server:port?type=tcp&security=reality&...")
|
|
layout.addWidget(self.url_input)
|
|
|
|
# 按钮布局
|
|
btn_layout = QHBoxLayout()
|
|
self.test_btn = QPushButton("测试连接")
|
|
self.test_btn.clicked.connect(self.test_connection)
|
|
self.save_btn = QPushButton("保存并继续")
|
|
self.save_btn.clicked.connect(self.save_and_continue)
|
|
self.cancel_btn = QPushButton("取消")
|
|
self.cancel_btn.clicked.connect(self.reject)
|
|
|
|
btn_layout.addWidget(self.test_btn)
|
|
btn_layout.addStretch()
|
|
btn_layout.addWidget(self.save_btn)
|
|
btn_layout.addWidget(self.cancel_btn)
|
|
|
|
layout.addLayout(btn_layout)
|
|
self.setLayout(layout)
|
|
|
|
def test_connection(self):
|
|
"""测试代理连接"""
|
|
url = self.url_input.text().strip()
|
|
if not url:
|
|
QMessageBox.warning(self, "输入错误", "请输入代理地址")
|
|
return
|
|
|
|
if not url.startswith('vless://'):
|
|
QMessageBox.warning(self, "格式错误", "代理地址必须以 vless:// 开头")
|
|
return
|
|
|
|
try:
|
|
parsed = urlparse(url)
|
|
if not parsed.hostname or not parsed.port:
|
|
raise ValueError("无效的服务器地址或端口")
|
|
|
|
QMessageBox.information(self, "测试成功",
|
|
f"代理地址解析成功:\n服务器: {parsed.hostname}\n端口: {parsed.port}")
|
|
except Exception as e:
|
|
QMessageBox.warning(self, "测试失败", f"代理地址格式错误: {str(e)}")
|
|
|
|
def save_and_continue(self):
|
|
"""保存配置并继续"""
|
|
url = self.url_input.text().strip()
|
|
if not url:
|
|
QMessageBox.warning(self, "输入错误", "请输入代理地址")
|
|
return
|
|
|
|
if not url.startswith('vless://'):
|
|
QMessageBox.warning(self, "格式错误", "代理地址必须以 vless:// 开头")
|
|
return
|
|
|
|
# 保存到配置
|
|
config = self.app_context.config
|
|
config['proxy_url'] = url
|
|
|
|
# 保存配置文件
|
|
if not save_config(config):
|
|
QMessageBox.critical(self, "保存失败", "无法保存配置文件")
|
|
return
|
|
|
|
self.accept() |