2
2
appConfigPath ,
3
3
controledMihomoConfigPath ,
4
4
dataDir ,
5
+ exePath ,
5
6
logDir ,
7
+ mihomoCorePath ,
6
8
mihomoTestDir ,
7
9
mihomoWorkDir ,
8
10
profileConfigPath ,
@@ -23,6 +25,7 @@ import { startPacServer } from './server'
23
25
import { triggerSysProxy } from './sysproxy'
24
26
import { getAppConfig } from '../config'
25
27
import { app } from 'electron'
28
+ import { execSync } from 'child_process'
26
29
27
30
function initDirs ( ) : void {
28
31
if ( ! fs . existsSync ( dataDir ) ) {
@@ -82,11 +85,38 @@ function initDeeplink(): void {
82
85
}
83
86
}
84
87
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
+
85
114
export function init ( ) : void {
86
115
initDirs ( )
87
116
initConfig ( )
88
117
initFiles ( )
89
118
initDeeplink ( )
119
+ initFirewall ( )
90
120
startPacServer ( ) . then ( ( ) => {
91
121
triggerSysProxy ( getAppConfig ( ) . sysProxy . enable )
92
122
} )
0 commit comments