From 2130cacdb637678c691e72f2224b32a4d7b6ed27 Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Tue, 6 Aug 2024 22:09:20 +0800 Subject: [PATCH] automatically allow through the firewall --- src/main/resolve/autoRun.ts | 10 ++++------ src/main/resolve/init.ts | 30 ++++++++++++++++++++++++++++++ src/main/utils/dirs.ts | 4 ++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/main/resolve/autoRun.ts b/src/main/resolve/autoRun.ts index e8d03ba..56e09d2 100644 --- a/src/main/resolve/autoRun.ts +++ b/src/main/resolve/autoRun.ts @@ -1,10 +1,8 @@ import { exec } from 'child_process' +import { exePath } from '../utils/dirs' import { app } from 'electron' import fs from 'fs' -// 获取应用的可执行文件路径 -const exePath = app.getPath('exe') - const appName = 'mihomo-party' const taskXml = ` @@ -45,7 +43,7 @@ const taskXml = ` - ${exePath} + ${exePath()} @@ -80,14 +78,14 @@ export function enableAutoRun(): void { if (process.platform === 'darwin') { app.setLoginItemSettings({ openAtLogin: true, - path: exePath + path: exePath() }) } if (process.platform === 'linux') { let desktop = ` [Desktop Entry] Name=mihomo-party -Exec=${exePath} %U +Exec=${exePath()} %U Terminal=false Type=Application Icon=mihomo-party diff --git a/src/main/resolve/init.ts b/src/main/resolve/init.ts index ee15ddb..c4064e6 100644 --- a/src/main/resolve/init.ts +++ b/src/main/resolve/init.ts @@ -2,7 +2,9 @@ import { appConfigPath, controledMihomoConfigPath, dataDir, + exePath, logDir, + mihomoCorePath, mihomoTestDir, mihomoWorkDir, profileConfigPath, @@ -23,6 +25,7 @@ import { startPacServer } from './server' import { triggerSysProxy } from './sysproxy' import { getAppConfig } from '../config' import { app } from 'electron' +import { execSync } from 'child_process' function initDirs(): void { if (!fs.existsSync(dataDir)) { @@ -82,11 +85,38 @@ function initDeeplink(): void { } } +function initFirewall(): void { + const removeCommand = ` + Remove-NetFirewallRule -DisplayName "mihomo" -ErrorAction SilentlyContinue + Remove-NetFirewallRule -DisplayName "mihomo-alpha" -ErrorAction SilentlyContinue + Remove-NetFirewallRule -DisplayName "Mihomo Party" -ErrorAction SilentlyContinue + ` + const createCommand = ` + New-NetFirewallRule -DisplayName "mihomo" -Direction Inbound -Action Allow -Program "${mihomoCorePath('mihomo')}" -Enabled True -Profile Any -ErrorAction SilentlyContinue + New-NetFirewallRule -DisplayName "mihomo-alpha" -Direction Inbound -Action Allow -Program "${mihomoCorePath('mihomo-alpha')}" -Enabled True -Profile Any -ErrorAction SilentlyContinue + New-NetFirewallRule -DisplayName "Mihomo Party" -Direction Inbound -Action Allow -Program "${exePath()}" -Enabled True -Profile Any -ErrorAction SilentlyContinue + ` + + if (process.platform === 'win32') { + try { + execSync(removeCommand, { shell: 'powershell' }) + } catch { + console.log('Remove-NetFirewallRule Failed') + } + try { + execSync(createCommand, { shell: 'powershell' }) + } catch { + console.log('New-NetFirewallRule Failed') + } + } +} + export function init(): void { initDirs() initConfig() initFiles() initDeeplink() + initFirewall() startPacServer().then(() => { triggerSysProxy(getAppConfig().sysProxy.enable) }) diff --git a/src/main/utils/dirs.ts b/src/main/utils/dirs.ts index 9a870fa..b7a60e4 100644 --- a/src/main/utils/dirs.ts +++ b/src/main/utils/dirs.ts @@ -4,6 +4,10 @@ import path from 'path' export const dataDir = app.getPath('userData') +export function exePath(): string { + return app.getPath('exe') +} + export function resourcesDir(): string { if (is.dev) { return path.join(__dirname, '../../resources')