This commit is contained in:
liyp 2023-12-23 22:45:32 +08:00
commit a9bcd96046
6 changed files with 176 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.venv

BIN
img/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

117
main/main.py Normal file
View file

@ -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_())

41
main/test.py Normal file
View file

@ -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_()

15
main/test1.py Normal file
View file

@ -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())

2
requirements.txt Normal file
View file

@ -0,0 +1,2 @@
PyQt5
requests