1
- import { app , dialog , ipcMain , safeStorage } from 'electron'
1
+ import { app , ipcMain , safeStorage } from 'electron'
2
2
import {
3
3
mihomoChangeProxy ,
4
4
mihomoCloseAllConnections ,
@@ -19,7 +19,7 @@ import {
19
19
stopMihomoConnections ,
20
20
stopMihomoLogs
21
21
} from '../core/mihomoApi'
22
- import { checkAutoRun , disableAutoRun , enableAutoRun } from '../resolve /autoRun'
22
+ import { checkAutoRun , disableAutoRun , enableAutoRun } from '../sys /autoRun'
23
23
import {
24
24
getAppConfig ,
25
25
patchAppConfig ,
@@ -45,14 +45,11 @@ import {
45
45
updateOverrideItem
46
46
} from '../config'
47
47
import { isEncryptionAvailable , manualGrantCorePermition , restartCore } from '../core/manager'
48
- import { triggerSysProxy } from '../resolve /sysproxy'
48
+ import { triggerSysProxy } from '../sys /sysproxy'
49
49
import { checkUpdate } from '../resolve/autoUpdater'
50
- import { exePath , mihomoCorePath , mihomoWorkConfigPath , resourcesDir } from './dirs'
51
- import { exec , execFile } from 'child_process'
52
- import yaml from 'yaml'
53
- import path from 'path'
54
- import { promisify } from 'util'
55
- import { readFile } from 'fs/promises'
50
+ import { getFilePath , openUWPTool , readTextFile , setupFirewall } from '../sys/misc'
51
+ import { getRuntimeConfig , getRuntimeConfigStr } from '../core/factory'
52
+ import { isPortable , setPortable } from './dirs'
56
53
57
54
function ipcErrorWrapper < T > ( // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
55
fn : ( ...args : any [ ] ) => Promise < T > // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -138,50 +135,7 @@ export function registerIpcMainHandlers(): void {
138
135
ipcMain . handle ( 'platform' , ( ) => process . platform )
139
136
ipcMain . handle ( 'openUWPTool' , ipcErrorWrapper ( openUWPTool ) )
140
137
ipcMain . handle ( 'setupFirewall' , ipcErrorWrapper ( setupFirewall ) )
138
+ ipcMain . handle ( 'setPortable' , ( _e , portable ) => ipcErrorWrapper ( setPortable ) ( portable ) )
139
+ ipcMain . handle ( 'isPortable' , isPortable )
141
140
ipcMain . handle ( 'quitApp' , ( ) => app . quit ( ) )
142
141
}
143
-
144
- function getFilePath ( ext : string [ ] ) : string [ ] | undefined {
145
- return dialog . showOpenDialogSync ( {
146
- title : '选择订阅文件' ,
147
- filters : [ { name : `${ ext } file` , extensions : ext } ] ,
148
- properties : [ 'openFile' ]
149
- } )
150
- }
151
-
152
- async function readTextFile ( filePath : string ) : Promise < string > {
153
- return await readFile ( filePath , 'utf8' )
154
- }
155
-
156
- async function getRuntimeConfigStr ( ) : Promise < string > {
157
- return readFile ( mihomoWorkConfigPath ( ) , 'utf8' )
158
- }
159
-
160
- async function getRuntimeConfig ( ) : Promise < IMihomoConfig > {
161
- return yaml . parse ( await getRuntimeConfigStr ( ) )
162
- }
163
-
164
- async function openUWPTool ( ) : Promise < void > {
165
- const execFilePromise = promisify ( execFile )
166
- const uwpToolPath = path . join ( resourcesDir ( ) , 'files' , 'enableLoopback.exe' )
167
- await execFilePromise ( uwpToolPath )
168
- }
169
-
170
- async function setupFirewall ( ) : Promise < void > {
171
- const execPromise = promisify ( exec )
172
- const removeCommand = `
173
- Remove-NetFirewallRule -DisplayName "mihomo" -ErrorAction SilentlyContinue
174
- Remove-NetFirewallRule -DisplayName "mihomo-alpha" -ErrorAction SilentlyContinue
175
- Remove-NetFirewallRule -DisplayName "Mihomo Party" -ErrorAction SilentlyContinue
176
- `
177
- const createCommand = `
178
- New-NetFirewallRule -DisplayName "mihomo" -Direction Inbound -Action Allow -Program "${ mihomoCorePath ( 'mihomo' ) } " -Enabled True -Profile Any -ErrorAction SilentlyContinue
179
- New-NetFirewallRule -DisplayName "mihomo-alpha" -Direction Inbound -Action Allow -Program "${ mihomoCorePath ( 'mihomo-alpha' ) } " -Enabled True -Profile Any -ErrorAction SilentlyContinue
180
- New-NetFirewallRule -DisplayName "Mihomo Party" -Direction Inbound -Action Allow -Program "${ exePath ( ) } " -Enabled True -Profile Any -ErrorAction SilentlyContinue
181
- `
182
-
183
- if ( process . platform === 'win32' ) {
184
- await execPromise ( removeCommand , { shell : 'powershell' } )
185
- await execPromise ( createCommand , { shell : 'powershell' } )
186
- }
187
- }
0 commit comments