automatically allow through the firewall

This commit is contained in:
pompurin404 2024-08-06 22:09:20 +08:00
parent d6825fd7a9
commit 2130cacdb6
No known key found for this signature in database
3 changed files with 38 additions and 6 deletions

View File

@ -1,10 +1,8 @@
import { exec } from 'child_process' import { exec } from 'child_process'
import { exePath } from '../utils/dirs'
import { app } from 'electron' import { app } from 'electron'
import fs from 'fs' import fs from 'fs'
// 获取应用的可执行文件路径
const exePath = app.getPath('exe')
const appName = 'mihomo-party' const appName = 'mihomo-party'
const taskXml = ` const taskXml = `
@ -45,7 +43,7 @@ const taskXml = `
</Settings> </Settings>
<Actions Context="Author"> <Actions Context="Author">
<Exec> <Exec>
<Command>${exePath}</Command> <Command>${exePath()}</Command>
</Exec> </Exec>
</Actions> </Actions>
</Task> </Task>
@ -80,14 +78,14 @@ export function enableAutoRun(): void {
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
app.setLoginItemSettings({ app.setLoginItemSettings({
openAtLogin: true, openAtLogin: true,
path: exePath path: exePath()
}) })
} }
if (process.platform === 'linux') { if (process.platform === 'linux') {
let desktop = ` let desktop = `
[Desktop Entry] [Desktop Entry]
Name=mihomo-party Name=mihomo-party
Exec=${exePath} %U Exec=${exePath()} %U
Terminal=false Terminal=false
Type=Application Type=Application
Icon=mihomo-party Icon=mihomo-party

View File

@ -2,7 +2,9 @@ import {
appConfigPath, appConfigPath,
controledMihomoConfigPath, controledMihomoConfigPath,
dataDir, dataDir,
exePath,
logDir, logDir,
mihomoCorePath,
mihomoTestDir, mihomoTestDir,
mihomoWorkDir, mihomoWorkDir,
profileConfigPath, profileConfigPath,
@ -23,6 +25,7 @@ import { startPacServer } from './server'
import { triggerSysProxy } from './sysproxy' import { triggerSysProxy } from './sysproxy'
import { getAppConfig } from '../config' import { getAppConfig } from '../config'
import { app } from 'electron' import { app } from 'electron'
import { execSync } from 'child_process'
function initDirs(): void { function initDirs(): void {
if (!fs.existsSync(dataDir)) { 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 { export function init(): void {
initDirs() initDirs()
initConfig() initConfig()
initFiles() initFiles()
initDeeplink() initDeeplink()
initFirewall()
startPacServer().then(() => { startPacServer().then(() => {
triggerSysProxy(getAppConfig().sysProxy.enable) triggerSysProxy(getAppConfig().sysProxy.enable)
}) })

View File

@ -4,6 +4,10 @@ import path from 'path'
export const dataDir = app.getPath('userData') export const dataDir = app.getPath('userData')
export function exePath(): string {
return app.getPath('exe')
}
export function resourcesDir(): string { export function resourcesDir(): string {
if (is.dev) { if (is.dev) {
return path.join(__dirname, '../../resources') return path.join(__dirname, '../../resources')