Skip to content

Commit 457ffc6

Browse files
author
pompurin404
committed
automatically allow through the firewall
1 parent 8161c2d commit 457ffc6

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/main/resolve/autoRun.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { exec } from 'child_process'
2+
import { exePath } from '../utils/dirs'
23
import { app } from 'electron'
34
import fs from 'fs'
45

5-
// 获取应用的可执行文件路径
6-
const exePath = app.getPath('exe')
7-
86
const appName = 'mihomo-party'
97

108
const taskXml = `
@@ -45,7 +43,7 @@ const taskXml = `
4543
</Settings>
4644
<Actions Context="Author">
4745
<Exec>
48-
<Command>${exePath}</Command>
46+
<Command>${exePath()}</Command>
4947
</Exec>
5048
</Actions>
5149
</Task>
@@ -80,14 +78,14 @@ export function enableAutoRun(): void {
8078
if (process.platform === 'darwin') {
8179
app.setLoginItemSettings({
8280
openAtLogin: true,
83-
path: exePath
81+
path: exePath()
8482
})
8583
}
8684
if (process.platform === 'linux') {
8785
let desktop = `
8886
[Desktop Entry]
8987
Name=mihomo-party
90-
Exec=${exePath} %U
88+
Exec=${exePath()} %U
9189
Terminal=false
9290
Type=Application
9391
Icon=mihomo-party

src/main/resolve/init.ts

+30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import {
22
appConfigPath,
33
controledMihomoConfigPath,
44
dataDir,
5+
exePath,
56
logDir,
7+
mihomoCorePath,
68
mihomoTestDir,
79
mihomoWorkDir,
810
profileConfigPath,
@@ -23,6 +25,7 @@ import { startPacServer } from './server'
2325
import { triggerSysProxy } from './sysproxy'
2426
import { getAppConfig } from '../config'
2527
import { app } from 'electron'
28+
import { execSync } from 'child_process'
2629

2730
function initDirs(): void {
2831
if (!fs.existsSync(dataDir)) {
@@ -82,11 +85,38 @@ function initDeeplink(): void {
8285
}
8386
}
8487

88+
function initFirewall(): void {
89+
const removeCommand = `
90+
Remove-NetFirewallRule -DisplayName "mihomo" -ErrorAction SilentlyContinue
91+
Remove-NetFirewallRule -DisplayName "mihomo-alpha" -ErrorAction SilentlyContinue
92+
Remove-NetFirewallRule -DisplayName "Mihomo Party" -ErrorAction SilentlyContinue
93+
`
94+
const createCommand = `
95+
New-NetFirewallRule -DisplayName "mihomo" -Direction Inbound -Action Allow -Program "${mihomoCorePath('mihomo')}" -Enabled True -Profile Any -ErrorAction SilentlyContinue
96+
New-NetFirewallRule -DisplayName "mihomo-alpha" -Direction Inbound -Action Allow -Program "${mihomoCorePath('mihomo-alpha')}" -Enabled True -Profile Any -ErrorAction SilentlyContinue
97+
New-NetFirewallRule -DisplayName "Mihomo Party" -Direction Inbound -Action Allow -Program "${exePath()}" -Enabled True -Profile Any -ErrorAction SilentlyContinue
98+
`
99+
100+
if (process.platform === 'win32') {
101+
try {
102+
execSync(removeCommand, { shell: 'powershell' })
103+
} catch {
104+
console.log('Remove-NetFirewallRule Failed')
105+
}
106+
try {
107+
execSync(createCommand, { shell: 'powershell' })
108+
} catch {
109+
console.log('New-NetFirewallRule Failed')
110+
}
111+
}
112+
}
113+
85114
export function init(): void {
86115
initDirs()
87116
initConfig()
88117
initFiles()
89118
initDeeplink()
119+
initFirewall()
90120
startPacServer().then(() => {
91121
triggerSysProxy(getAppConfig().sysProxy.enable)
92122
})

src/main/utils/dirs.ts

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import path from 'path'
44

55
export const dataDir = app.getPath('userData')
66

7+
export function exePath(): string {
8+
return app.getPath('exe')
9+
}
10+
711
export function resourcesDir(): string {
812
if (is.dev) {
913
return path.join(__dirname, '../../resources')

0 commit comments

Comments
 (0)