Skip to content
This repository has been archived by the owner on Feb 5, 2025. It is now read-only.

Commit

Permalink
support show traffic on taskbar
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Sep 28, 2024
1 parent 3611e6f commit 45ff6d8
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 17 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### New Features

- Windows 允许添加 `noadmin` 参数以普通权限启动程序
- 支持在 Windows 任务栏显示网速信息

### Bug Fixes

Expand Down
28 changes: 28 additions & 0 deletions scripts/prepare.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,28 @@ const resolveRunner = () =>
file: 'mihomo-party-run.exe',
downloadURL: `https://github.com/mihomo-party-org/mihomo-party-run/releases/download/${arch}/mihomo-party-run.exe`
})

const resolveMonitor = async () => {
const tempDir = path.join(TEMP_DIR, 'TrafficMonitor')
const tempZip = path.join(tempDir, `${arch}.zip`)
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true })
}
await downloadFile(
`https://github.com/mihomo-party-org/mihomo-party-run/releases/download/monitor/${arch}.zip`,
tempZip
)
const zip = new AdmZip(tempZip)
const resDir = path.join(cwd, 'extra', 'files')
const targetPath = path.join(resDir, 'TrafficMonitor')
if (fs.existsSync(targetPath)) {
fs.rmSync(targetPath, { recursive: true })
}
zip.extractAllTo(resDir, true)

console.log(`[INFO]: TrafficMonitor finished`)
}

const resolve7zip = () =>
resolveResource({
file: '7za.exe',
Expand Down Expand Up @@ -355,6 +377,12 @@ const tasks = [
retry: 5,
winOnly: true
},
{
name: 'monitor',
func: resolveMonitor,
retry: 5,
winOnly: true
},
{
name: 'substore',
func: resolveSubstore,
Expand Down
2 changes: 2 additions & 0 deletions src/main/core/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { mainWindow } from '..'
import path from 'path'
import { existsSync } from 'fs'
import { uploadRuntimeConfig } from '../resolve/gistApi'
import { startMonitor } from '../resolve/trafficMonitor'

chokidar.watch(path.join(mihomoCoreDir(), 'meta-update'), {}).on('unlinkDir', async () => {
try {
Expand Down Expand Up @@ -193,6 +194,7 @@ export async function keepCoreAlive(): Promise<void> {

export async function quitWithoutCore(): Promise<void> {
await keepCoreAlive()
await startMonitor(true)
app.exit()
}

Expand Down
27 changes: 27 additions & 0 deletions src/main/resolve/trafficMonitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ChildProcess, spawn } from 'child_process'
import { getAppConfig } from '../config'
import { resourcesFilesDir } from '../utils/dirs'
import path from 'path'

let child: ChildProcess

export async function startMonitor(detached = false): Promise<void> {
if (process.platform !== 'win32') return
await stopMonitor()
const { showTraffic = true } = await getAppConfig()
if (!showTraffic) return
child = spawn(path.join(resourcesFilesDir(), 'TrafficMonitor/TrafficMonitor.exe'), [], {
cwd: path.join(resourcesFilesDir(), 'TrafficMonitor'),
detached: detached,
stdio: detached ? 'ignore' : undefined
})
if (detached) {
child.unref()
}
}

async function stopMonitor(): Promise<void> {
if (child) {
child.kill('SIGINT')
}
}
2 changes: 2 additions & 0 deletions src/main/utils/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from '../config'
import { app } from 'electron'
import { startSSIDCheck } from '../sys/ssid'
import { startMonitor } from '../resolve/trafficMonitor'

async function initDirs(): Promise<void> {
if (!existsSync(dataDir())) {
Expand Down Expand Up @@ -223,6 +224,7 @@ export async function init(): Promise<void> {
await cleanup()
await startPacServer()
await startSubStoreServer()
await startMonitor()
const { sysProxy } = await getAppConfig()
await triggerSysProxy(sysProxy.enable)
await startSSIDCheck()
Expand Down
2 changes: 2 additions & 0 deletions src/main/utils/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import path from 'path'
import v8 from 'v8'
import { getGistUrl } from '../resolve/gistApi'
import { getImageDataURL } from './image'
import { startMonitor } from '../resolve/trafficMonitor'

function ipcErrorWrapper<T>( // eslint-disable-next-line @typescript-eslint/no-explicit-any
fn: (...args: any[]) => Promise<T> // eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -161,6 +162,7 @@ export function registerIpcMainHandlers(): void {
ipcMain.handle('getOverride', (_e, id, ext) => ipcErrorWrapper(getOverride)(id, ext))
ipcMain.handle('setOverride', (_e, id, ext, str) => ipcErrorWrapper(setOverride)(id, ext, str))
ipcMain.handle('restartCore', ipcErrorWrapper(restartCore))
ipcMain.handle('startMonitor', (_e, detached) => ipcErrorWrapper(startMonitor)(detached))
ipcMain.handle('triggerSysProxy', (_e, enable) => ipcErrorWrapper(triggerSysProxy)(enable))
ipcMain.handle('isEncryptionAvailable', isEncryptionAvailable)
ipcMain.handle('encryptString', (_e, str) => encryptString(str))
Expand Down
39 changes: 22 additions & 17 deletions src/renderer/src/components/settings/general-config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
importThemes,
relaunchApp,
resolveThemes,
restartCore,
startMonitor,
writeTheme
} from '@renderer/utils/ipc'
import { useAppConfig } from '@renderer/hooks/use-app-config'
Expand Down Expand Up @@ -176,34 +176,39 @@ const GeneralConfig: React.FC = () => {
</Select>
</SettingItem>
{platform !== 'linux' && (
<SettingItem title="托盘菜单显示节点信息" divider>
<Switch
size="sm"
isSelected={proxyInTray}
onValueChange={async (v) => {
await patchAppConfig({ proxyInTray: v })
}}
/>
</SettingItem>
)}
{platform === 'darwin' && (
<>
<SettingItem title="显示 Dock 图标" divider>
<SettingItem title="托盘菜单显示节点信息" divider>
<Switch
size="sm"
isSelected={useDockIcon}
isSelected={proxyInTray}
onValueChange={async (v) => {
await patchAppConfig({ useDockIcon: v })
await patchAppConfig({ proxyInTray: v })
}}
/>
</SettingItem>
<SettingItem title="显示网速信息" divider>
<SettingItem
title={`${platform === 'win32' ? '任务栏' : '状态栏'}显示网速信息`}
divider
>
<Switch
size="sm"
isSelected={showTraffic}
onValueChange={async (v) => {
await patchAppConfig({ showTraffic: v })
await restartCore()
await startMonitor()
}}
/>
</SettingItem>
</>
)}
{platform === 'darwin' && (
<>
<SettingItem title="显示 Dock 图标" divider>
<Switch
size="sm"
isSelected={useDockIcon}
onValueChange={async (v) => {
await patchAppConfig({ useDockIcon: v })
}}
/>
</SettingItem>
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/src/utils/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ export async function restartCore(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('restartCore'))
}

export async function startMonitor(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('startMonitor'))
}

export async function triggerSysProxy(enable: boolean): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('triggerSysProxy', enable))
}
Expand Down

0 comments on commit 45ff6d8

Please sign in to comment.