support copy env

This commit is contained in:
pompurin404 2024-08-19 19:16:46 +08:00
parent 469e9d2818
commit dcb3efbfb8
No known key found for this signature in database
6 changed files with 75 additions and 3 deletions

View File

@ -1,3 +1,7 @@
### New Features
- 支持一键复制环境变量
### Bug Fixes
- 修复托盘菜单在Linux下崩溃的问题

View File

@ -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<Menu> => {
}
]
},
{
id: 'copyenv',
label: '复制环境变量',
type: 'normal',
click: copyEnv
},
{ type: 'separator' },
{
id: 'restart',
@ -250,3 +256,30 @@ async function updateTrayMenu(): Promise<void> {
tray?.setContextMenu(menu)
}
}
export async function copyEnv(): Promise<void> {
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
}
}
}

View File

@ -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<T>( // eslint-disable-next-line @typescript-eslint/no-explicit-any
fn: (...args: any[]) => Promise<T> // 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)
})

View File

@ -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 = () => {
}}
/>
</SettingItem>
<SettingItem
title="复制环境变量类型"
actions={
<Button isIconOnly size="sm" className="ml-2" variant="light" onPress={copyEnv}>
<BiCopy className="text-lg" />
</Button>
}
divider
>
<Select
className="w-[150px]"
size="sm"
selectedKeys={new Set([envType])}
onSelectionChange={async (v) => {
try {
await patchAppConfig({ envType: v.currentKey as 'bash' | 'cmd' | 'powershell' })
} catch (e) {
alert(e)
}
}}
>
<SelectItem key="bash">Bash</SelectItem>
<SelectItem key="cmd">CMD</SelectItem>
<SelectItem key="powershell">PowerShell</SelectItem>
</Select>
</SettingItem>
{platform !== 'linux' && (
<SettingItem title="托盘菜单显示节点信息" divider>
<Switch
@ -206,7 +235,6 @@ const Settings: React.FC = () => {
/>
</SettingItem>
)}
{platform === 'darwin' && (
<>
<SettingItem title="显示Dock图标" divider>

View File

@ -283,6 +283,10 @@ export async function quitApp(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('quitApp'))
}
export async function copyEnv(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('copyEnv'))
}
async function alert<T>(msg: T): Promise<void> {
const msgStr = typeof msg === 'string' ? msg : JSON.stringify(msg)
return await window.electron.ipcRenderer.invoke('alert', msgStr)

View File

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