display traffic info in tray

This commit is contained in:
pompurin404 2024-08-15 08:57:45 +08:00
parent 0394548f91
commit 18718eb697
No known key found for this signature in database
8 changed files with 103 additions and 26 deletions

View File

@ -19,3 +19,39 @@
| 蓝色 | ![](./images/dark-blue.png) | ![](./images/gray-blue.png) | ![](./images/light-blue.png) |
| 粉色 | ![](./images/dark-pink.png) | ![](./images/gray-pink.png) | ![](./images/light-pink.png) |
| 绿色 | ![](./images/dark-green.png) | ![](./images/gray-green.png) | ![](./images/light-green.png) |
### 特性
- [x] 开箱即用无需服务模式的Tun
- [x] 多种配色主题可选UI焕然一新
- [x] 支持大部分Mihomo常用配置修改
- [x] 内置稳定版和预览版Mihomo内核
- [x] 一键更新geo数据库
### 安装
在 [Releases](https://github.com/pompurin404/mihomo-party/releases/latest) 页面下载对应平台的安装包,安装后即可使用。
#### Windows
下载exe安装包双击安装
或者下载便携版压缩包解压后运行mihomo-party.exe文件
#### MacOS
下载dmg包打开后将mihomo-party图标拖入Applications文件夹安装
#### Linux
##### Debian/Ubuntu
下载deb包使用apt安装
##### Fedora
下载rpm包使用dnf安装
##### Arch/Manjaro
通过 [AUR](https://aur.archlinux.org/packages?O=0&K=mihomo-party) 安装

View File

@ -278,12 +278,12 @@ const resolveFont = async () => {
const tasks = [
{
name: 'verge-mihomo-alpha',
name: 'mihomo-alpha',
func: () => getLatestAlphaVersion().then(() => resolveSidecar(MihomoAlpha())),
retry: 5
},
{
name: 'verge-mihomo',
name: 'mihomo',
func: () => getLatestReleaseVersion().then(() => resolveSidecar(mihomo())),
retry: 5
},

View File

@ -2,6 +2,8 @@ import axios, { AxiosInstance } from 'axios'
import { getAppConfig, getControledMihomoConfig } from '../config'
import { mainWindow } from '..'
import WebSocket from 'ws'
import { tray } from './tray'
import { calcTraffic } from '../utils/calc'
let axiosIns: AxiosInstance = null!
let mihomoTrafficWs: WebSocket | null = null
@ -151,8 +153,21 @@ const mihomoTraffic = async (): Promise<void> => {
mihomoTrafficWs.onmessage = (e): void => {
const data = e.data as string
const json = JSON.parse(data) as IMihomoTrafficInfo
tray?.setTitle(
'↑' +
`${calcTraffic(json.up)}/s`.padStart(16) +
'\n↓' +
`${calcTraffic(json.down)}/s`.padStart(16)
)
tray?.setToolTip(
'↑' +
`${calcTraffic(json.up)}/s`.padStart(16) +
'\n↓' +
`${calcTraffic(json.down)}/s`.padStart(16)
)
trafficRetry = 10
mainWindow?.webContents.send('mihomoTraffic', JSON.parse(data) as IMihomoTrafficInfo)
mainWindow?.webContents.send('mihomoTraffic', json)
}
mihomoTrafficWs.onclose = (): void => {
@ -308,10 +323,9 @@ const mihomoConnections = async (): Promise<void> => {
export const pauseWebsockets = () => {
const recoverList: (() => void)[] = []
if (mihomoTrafficWs) {
stopMihomoTraffic()
recoverList.push(startMihomoTraffic)
}
// Traffic 始终开启
stopMihomoTraffic()
recoverList.push(startMihomoTraffic)
if (mihomoMemoryWs) {
stopMihomoMemory()
recoverList.push(startMihomoMemory)

View File

@ -13,7 +13,7 @@ import { app, ipcMain, Menu, nativeImage, shell, Tray } from 'electron'
import { dataDir, logDir, mihomoCoreDir, mihomoWorkDir } from '../utils/dirs'
import { triggerSysProxy } from '../resolve/sysproxy'
let tray: Tray | null = null
export let tray: Tray | null = null
const buildContextMenu = async (): Promise<Menu> => {
const { mode, tun } = await getControledMihomoConfig()
@ -159,20 +159,33 @@ export async function createTray(): Promise<void> {
ipcMain.on('appConfigUpdated', async () => {
await updateTrayMenu()
})
tray?.setToolTip('Mihomo Party')
tray?.setContextMenu(menu)
tray?.setIgnoreDoubleClickEvents(true)
tray?.setToolTip('Mihomo Party')
tray?.addListener('click', () => {
if (mainWindow?.isVisible()) {
mainWindow?.close()
} else {
showMainWindow()
}
})
if (process.platform === 'darwin') {
app.dock.setMenu(menu)
tray?.addListener('right-click', () => {
if (mainWindow?.isVisible()) {
mainWindow?.close()
} else {
showMainWindow()
}
})
} else {
tray?.addListener('click', () => {
if (mainWindow?.isVisible()) {
mainWindow?.close()
} else {
showMainWindow()
}
})
}
}
async function updateTrayMenu(): Promise<void> {
const menu = await buildContextMenu()
if (process.platform === 'darwin') {
app.dock.setMenu(menu) // 更新dock菜单
}
tray?.setContextMenu(menu) // 更新菜单
}

View File

@ -9,12 +9,7 @@ import { createTray } from './core/tray'
import { init } from './resolve/init'
import { addProfileItem, getAppConfig } from './config'
import { join } from 'path'
import {
startMihomoMemory,
startMihomoTraffic,
stopMihomoMemory,
stopMihomoTraffic
} from './core/mihomoApi'
import { startMihomoMemory, stopMihomoMemory } from './core/mihomoApi'
export let mainWindow: BrowserWindow | null = null
export let destroyTimer: NodeJS.Timeout | null = null
@ -159,12 +154,10 @@ export function createWindow(show = false): void {
})
mainWindow.on('show', () => {
startMihomoTraffic()
startMihomoMemory()
})
mainWindow.on('close', (event) => {
stopMihomoTraffic()
stopMihomoMemory()
event.preventDefault()
mainWindow?.hide()

View File

@ -29,6 +29,7 @@ import { getAppConfig } from '../config'
import { app } from 'electron'
import { startCore } from '../core/manager'
import { initProfileUpdater } from '../core/profileUpdater'
import { startMihomoTraffic } from '../core/mihomoApi'
async function initDirs(): Promise<void> {
if (!existsSync(dataDir)) {
@ -109,6 +110,7 @@ export async function init(): Promise<void> {
const { sysProxy } = await getAppConfig()
await triggerSysProxy(sysProxy.enable)
startCore().then(() => {
startMihomoTraffic()
setTimeout(async () => {
await initProfileUpdater()
}, 60000)

19
src/main/utils/calc.ts Normal file
View File

@ -0,0 +1,19 @@
export function calcTraffic(byte: number): string {
if (byte < 1024) return `${byte} B`
byte /= 1024
if (byte < 1024) return `${byte.toFixed(2)} KB`
byte /= 1024
if (byte < 1024) return `${byte.toFixed(2)} MB`
byte /= 1024
if (byte < 1024) return `${byte.toFixed(2)} GB`
byte /= 1024
if (byte < 1024) return `${byte.toFixed(2)} TB`
byte /= 1024
if (byte < 1024) return `${byte.toFixed(2)} PB`
byte /= 1024
if (byte < 1024) return `${byte.toFixed(2)} EB`
byte /= 1024
if (byte < 1024) return `${byte.toFixed(2)} ZB`
byte /= 1024
return `${byte.toFixed(2)} YB`
}

View File

@ -17,7 +17,7 @@ const OutboundModeSwitcher: React.FC = () => {
await mihomoCloseAllConnections()
}
}
if (!mode) return null
return (
<Tabs
fullWidth