fix: use netsh to manage firewall rules for Windows 7 compatibility

This commit is contained in:
Memory
2026-04-26 16:37:39 +08:00
committed by GitHub
parent f494ff7d00
commit 9ea9da7d58

View File

@@ -58,23 +58,22 @@ export async function openUWPTool(): Promise<void> {
export async function setupFirewall(): Promise<void> {
const execPromise = promisify(exec)
const removeCommand = `
$rules = @("mihomo", "mihomo-alpha", "Mihomo Party")
foreach ($rule in $rules) {
if (Get-NetFirewallRule -DisplayName $rule -ErrorAction SilentlyContinue) {
Remove-NetFirewallRule -DisplayName $rule -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') {
await execPromise(removeCommand, { shell: 'powershell' })
await execPromise(createCommand, { shell: 'powershell' })
const rules = [
{ name: 'mihomo', program: mihomoCorePath('mihomo') },
{ name: 'mihomo-alpha', program: mihomoCorePath('mihomo-alpha') },
{ name: 'Mihomo Party', program: exePath() }
]
for (const rule of rules) {
await execPromise(`netsh advfirewall firewall delete rule name="${rule.name}"`, {
shell: 'cmd'
}).catch(() => {})
await execPromise(
`netsh advfirewall firewall add rule name="${rule.name}" dir=in action=allow program="${rule.program}" enable=yes profile=any`,
{ shell: 'cmd' }
)
}
}
}