diff --git a/changelog.md b/changelog.md index 6442c0c..e886c08 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,7 @@ +### New Features + +- 支持一键复制环境变量 + ### Bug Fixes - 修复托盘菜单在Linux下崩溃的问题 diff --git a/src/main/resolve/tray.ts b/src/main/resolve/tray.ts index 87bfe83..938afa5 100644 --- a/src/main/resolve/tray.ts +++ b/src/main/resolve/tray.ts @@ -14,7 +14,7 @@ import { patchMihomoConfig } from '../core/mihomoApi' import { mainWindow, showMainWindow } from '..' -import { app, ipcMain, Menu, nativeImage, shell, Tray } from 'electron' +import { app, clipboard, ipcMain, Menu, nativeImage, shell, Tray } from 'electron' import { dataDir, logDir, mihomoCoreDir, mihomoWorkDir } from '../utils/dirs' import { triggerSysProxy } from '../sys/sysproxy' @@ -170,6 +170,12 @@ const buildContextMenu = async (): Promise => { } ] }, + { + id: 'copyenv', + label: '复制环境变量', + type: 'normal', + click: copyEnv + }, { type: 'separator' }, { id: 'restart', @@ -250,3 +256,30 @@ async function updateTrayMenu(): Promise { tray?.setContextMenu(menu) } } + +export async function copyEnv(): Promise { + const defaultType = process.platform === 'win32' ? 'powershell' : 'bash' + const { 'mixed-port': mixedPort = 7890 } = await getControledMihomoConfig() + const { envType = defaultType, sysProxy } = await getAppConfig() + const { host } = sysProxy + switch (envType) { + case 'bash': { + clipboard.writeText( + `export https_proxy=http://${host || '127.0.0.1'}:${mixedPort} http_proxy=http://${host || '127.0.0.1'}:${mixedPort} all_proxy=http://${host || '127.0.0.1'}:${mixedPort}` + ) + break + } + case 'cmd': { + clipboard.writeText( + `set http_proxy=http://${host || '127.0.0.1'}:${mixedPort}\r\nset https_proxy=http://${host || '127.0.0.1'}:${mixedPort}` + ) + break + } + case 'powershell': { + clipboard.writeText( + `$env:HTTP_PROXY="http://${host || '127.0.0.1'}:${mixedPort}"; $env:HTTPS_PROXY="http://${host || '127.0.0.1'}:${mixedPort}"` + ) + break + } + } +} diff --git a/src/main/utils/ipc.ts b/src/main/utils/ipc.ts index 0fc6870..f73604d 100644 --- a/src/main/utils/ipc.ts +++ b/src/main/utils/ipc.ts @@ -53,6 +53,7 @@ import { getRuntimeConfig, getRuntimeConfigStr } from '../core/factory' import { isPortable, setPortable } from './dirs' import { listWebdavBackups, webdavBackup, webdavRestore } from '../resolve/backup' import { getInterfaces } from '../sys/interface' +import { copyEnv } from '../resolve/tray' function ipcErrorWrapper( // eslint-disable-next-line @typescript-eslint/no-explicit-any fn: (...args: any[]) => Promise // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -148,6 +149,7 @@ export function registerIpcMainHandlers(): void { ipcMain.handle('webdavBackup', ipcErrorWrapper(webdavBackup)) ipcMain.handle('webdavRestore', (_e, filename) => ipcErrorWrapper(webdavRestore)(filename)) ipcMain.handle('listWebdavBackups', ipcErrorWrapper(listWebdavBackups)) + ipcMain.handle('copyEnv', ipcErrorWrapper(copyEnv)) ipcMain.handle('alert', (_e, msg) => { dialog.showErrorBox('Mihomo Party', msg) }) diff --git a/src/renderer/src/pages/settings.tsx b/src/renderer/src/pages/settings.tsx index f96a35f..45ed460 100644 --- a/src/renderer/src/pages/settings.tsx +++ b/src/renderer/src/pages/settings.tsx @@ -15,8 +15,10 @@ import { setPortable, restartCore, webdavBackup, - listWebdavBackups + listWebdavBackups, + copyEnv } from '@renderer/utils/ipc' +import { BiCopy } from 'react-icons/bi' import { CgWebsite } from 'react-icons/cg' import { IoLogoGithub } from 'react-icons/io5' import { platform, version } from '@renderer/utils/init' @@ -38,6 +40,7 @@ const Settings: React.FC = () => { useDockIcon = true, showTraffic = true, proxyInTray = true, + envType = platform === 'win32' ? 'powershell' : 'bash', delayTestUrl, delayTestTimeout, autoCheckUpdate, @@ -195,6 +198,32 @@ const Settings: React.FC = () => { }} /> + + + + } + divider + > + + {platform !== 'linux' && ( { /> )} - {platform === 'darwin' && ( <> diff --git a/src/renderer/src/utils/ipc.ts b/src/renderer/src/utils/ipc.ts index e8abc7c..e0237be 100644 --- a/src/renderer/src/utils/ipc.ts +++ b/src/renderer/src/utils/ipc.ts @@ -283,6 +283,10 @@ export async function quitApp(): Promise { return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('quitApp')) } +export async function copyEnv(): Promise { + return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('copyEnv')) +} + async function alert(msg: T): Promise { const msgStr = typeof msg === 'string' ? msg : JSON.stringify(msg) return await window.electron.ipcRenderer.invoke('alert', msgStr) diff --git a/src/shared/types.d.ts b/src/shared/types.d.ts index 98215df..a9199a6 100644 --- a/src/shared/types.d.ts +++ b/src/shared/types.d.ts @@ -214,6 +214,7 @@ interface IAppConfig { core: 'mihomo' | 'mihomo-alpha' proxyDisplayMode: 'simple' | 'full' proxyDisplayOrder: 'default' | 'delay' | 'name' + envType: 'bash' | 'cmd' | 'powershell' proxyInTray: boolean siderOrder: string[] appTheme: AppTheme