Skip to content

Commit

Permalink
automatically allow through the firewall
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Aug 6, 2024
1 parent 8161c2d commit 457ffc6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/main/resolve/autoRun.ts
Original file line number Diff line number Diff line change
@@ -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 = `
Expand Down Expand Up @@ -45,7 +43,7 @@ const taskXml = `
</Settings>
<Actions Context="Author">
<Exec>
<Command>${exePath}</Command>
<Command>${exePath()}</Command>
</Exec>
</Actions>
</Task>
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions src/main/resolve/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {
appConfigPath,
controledMihomoConfigPath,
dataDir,
exePath,
logDir,
mihomoCorePath,
mihomoTestDir,
mihomoWorkDir,
profileConfigPath,
Expand All @@ -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)) {
Expand Down Expand Up @@ -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)
})
Expand Down
4 changes: 4 additions & 0 deletions src/main/utils/dirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 457ffc6

Please sign in to comment.