commit a9bcd9604678456c2ae70e31512cf66faee1cfc3 Author: liyp Date: Sat Dec 23 22:45:32 2023 +0800 备份 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b694934 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.venv \ No newline at end of file diff --git a/img/icon.png b/img/icon.png new file mode 100644 index 0000000..c624b94 Binary files /dev/null and b/img/icon.png differ diff --git a/main/main.py b/main/main.py new file mode 100644 index 0000000..49526d7 --- /dev/null +++ b/main/main.py @@ -0,0 +1,117 @@ +import sys +import requests +from PyQt5.QtWidgets import QApplication, QMainWindow, QSystemTrayIcon, QMenu, QAction,QActionGroup +from PyQt5.QtGui import QIcon + + +class TrayIconExample(QMainWindow): + def __init__(self): + super(TrayIconExample, self).__init__() + + self.init_ui() + + def init_ui(self): + self.setWindowTitle('Clash Tray') + + # 创建系统托盘图标 + tray_icon = QSystemTrayIcon(QIcon('img/icon.png'), self) + tray_icon.setToolTip('Clash Tray') + + # 创建右键菜单 + context_menu = QMenu(self) + proxy_mode = QMenu('代理模式', self) + exit_action = QAction('退出', self) + self.tun_mode = QAction('TUN模式', self) + self.tun_mode.setCheckable(True) + + # 创建一个 QActionGroup + proxy_mode_group = QActionGroup(self) + proxy_mode_group.setExclusive(True) # 设置为单选模式 + + self.direct_mode=QAction('直连模式',self) + self.rule_mode=QAction('规则模式',self) + self.global_mode=QAction('全局模式',self) + self.direct_mode.setCheckable(True) + self.rule_mode.setCheckable(True) + self.global_mode.setCheckable(True) + + proxy_mode_group.addAction(self.direct_mode) + proxy_mode_group.addAction(self.rule_mode) + proxy_mode_group.addAction(self.global_mode) + + exit_action.triggered.connect(self.exit_application) + context_menu.addAction(self.tun_mode) + context_menu.addMenu(proxy_mode) + + context_menu.addAction(exit_action) + proxy_mode.addActions([self.direct_mode,self.rule_mode,self.global_mode]) + self.direct_mode.triggered.connect(lambda:self.set_proxy_mode(0)) + self.rule_mode.triggered.connect(lambda:self.set_proxy_mode(1)) + self.global_mode.triggered.connect(lambda:self.set_proxy_mode(2)) + + + # 将右键菜单设置给托盘图标 + tray_icon.setContextMenu(context_menu) + + # 显示托盘图标 + tray_icon.show() + self.get_config() + + self.setGeometry(100, 100, 800, 600) + + def get_connect(self,url=None,port=None): + url='http://127.0.0.1' + port=str(9090) + return url,port + + def get_config(self): + url,port=self.get_connect(self) + response=requests.get(url=url+':'+port+'/configs') + # print(response.json['mode']) + if response.status_code==200: + res=response.json() + mode=res['mode'] + if mode=='direct': + self.direct_mode.setChecked(True) + elif mode=='rule': + self.rule_mode.setChecked(True) + else: + self.global_mode.setChecked(True) + tun=res['tun']['enable'] + print('tun:',tun) + if tun: + self.tun_mode.setChecked(True) + else: + self.tun_mode.setChecked(False) + def set_proxy_mode(self,type): + mode=None + if type==0: + mode='direct' + # self.rule_mode.setChecked(True) + elif type==1: + mode='rule' + # self.rule_mode.setChecked(True) + elif type==2: + mode='global' + # self.global_mode.setChecked(True) + url,port=self.get_connect() + data = {"mode": mode} + response=requests.patch(url=url+':'+port+'/configs?force=true',json=data) + if response.status_code==204: + self.get_config() + + + + + + + + + def exit_application(self): + QApplication.quit() + + +if __name__ == '__main__': + app = QApplication(sys.argv) + window = TrayIconExample() + sys.exit(app.exec_()) diff --git a/main/test.py b/main/test.py new file mode 100644 index 0000000..0c0fb37 --- /dev/null +++ b/main/test.py @@ -0,0 +1,41 @@ +from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QAction, QActionGroup + +class MenuExample(QMainWindow): + def __init__(self): + super(MenuExample, self).__init__() + + self.init_ui() + + def init_ui(self): + self.setWindowTitle('Menu Example') + + main_menu = self.menuBar() + + file_menu = main_menu.addMenu('File') + + # 创建一个 QActionGroup + action_group = QActionGroup(self) + action_group.setExclusive(True) # 设置为单选模式 + + # 创建两个菜单项,添加到 QActionGroup 中 + action1 = QAction('Option 1', self) + action1.setCheckable(True) + action1.setChecked(True) + action1.setIconText("• Option 1") + action_group.addAction(action1) + file_menu.addAction(action1) + + action2 = QAction('Option 2', self) + action2.setCheckable(True) + action2.setChecked(False) + action2.setIconText("• Option 2") + action_group.addAction(action2) + file_menu.addAction(action2) + + self.setGeometry(100, 100, 800, 600) + +if __name__ == '__main__': + app = QApplication([]) + window = MenuExample() + window.show() + app.exec_() diff --git a/main/test1.py b/main/test1.py new file mode 100644 index 0000000..1e09b1c --- /dev/null +++ b/main/test1.py @@ -0,0 +1,15 @@ +import requests + +url = "http://127.0.0.1:9090/" +data = {"mode": "rule"} + +response=requests.get(url=url+'configs') +print(response.status_code) +print(response.json()) +response1=requests.patch(url=url+'configs?force=true',json=data) +# global +# response = requests.patch(url+'configs', json=data) +# direct +print(response1.status_code) +print(response1.text) # 如果服务器返回 JSON 数据的话 +print(response.json()) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c811f4c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +PyQt5 +requests \ No newline at end of file