@@ -2,8 +2,8 @@ import { defineStore } from 'pinia'
22import { computed , ref , watch } from 'vue'
33
44import { getConfigs , setConfigs , getProxies , getProviders , Api } from '@/api/kernel'
5- import { ProcessInfo , KillProcess , ExecBackground } from '@/bridge'
6- import { CoreStopOutputKeyword , CoreWorkingDirectory } from '@/constant'
5+ import { ProcessInfo , KillProcess , ExecBackground , ReadFile , WriteFile , RemoveFile } from '@/bridge'
6+ import { CorePidFilePath , CoreStopOutputKeyword , CoreWorkingDirectory } from '@/constant'
77import { Branch } from '@/enums/app'
88import {
99 useAppSettingsStore ,
@@ -14,7 +14,6 @@ import {
1414} from '@/stores'
1515import {
1616 generateConfigFile ,
17- ignoredError ,
1817 updateTrayMenus ,
1918 getKernelFileName ,
2019 WebSockets ,
@@ -203,75 +202,95 @@ export const useKernelApiStore = defineStore('kernelApi', () => {
203202 }
204203
205204 /* Bridge API */
205+ const corePid = ref ( - 1 )
206+ const running = ref ( false )
206207 const loading = ref ( false )
207- const statusLoading = ref ( true )
208+ const coreStateLoading = ref ( true )
208209 let isCoreStartedByThisInstance = false
209- let doneCoreStopped : ( value : unknown ) => void
210+ let coreStoppedResolver : ( value : unknown ) => void
210211 let coreStoppedPromise : Promise < unknown >
211- let doneFirstCoreUpdate : ( value : unknown ) => void
212- const firstCoreUpdatePromise = new Promise ( ( r ) => ( doneFirstCoreUpdate = r ) )
213212
214- const isKernelRunning = async ( pid : number ) => {
215- return pid && ( await ProcessInfo ( pid ) ) . startsWith ( 'mihomo' )
216- }
217-
218- const updateKernelState = async ( ) => {
219- appSettingsStore . app . kernel . running = ! ! ( await ignoredError (
220- isKernelRunning ,
221- appSettingsStore . app . kernel . pid ,
222- ) )
213+ const updateCoreState = async ( ) => {
214+ corePid . value = Number ( await ReadFile ( CorePidFilePath ) . catch ( ( ) => - 1 ) )
215+ const processName = corePid . value === - 1 ? '' : await ProcessInfo ( corePid . value ) . catch ( ( ) => '' )
216+ running . value = processName . startsWith ( 'mihomo' )
223217
224- if ( ! appSettingsStore . app . kernel . running ) {
225- appSettingsStore . app . kernel . pid = 0
226- }
218+ coreStateLoading . value = false
227219
228- statusLoading . value = false
229-
230- if ( appSettingsStore . app . kernel . running ) {
220+ if ( running . value ) {
221+ initCoreWebsockets ( )
222+ longLivedWS . setup ?. ( )
231223 await Promise . all ( [ refreshConfig ( ) , refreshProviderProxies ( ) ] )
232224 await envStore . updateSystemProxyStatus ( )
233225 } else if ( appSettingsStore . app . autoStartKernel ) {
234- await startKernel ( )
226+ await startCore ( )
235227 }
228+ }
236229
237- doneFirstCoreUpdate ( null )
230+ const runCoreProcess = ( coreFilePath : string , args : string [ ] , env : Recordable ) => {
231+ return new Promise < number > ( ( resolve ) => {
232+ const pid = ExecBackground (
233+ coreFilePath ,
234+ args ,
235+ ( out ) => {
236+ logsStore . recordKernelLog ( out )
237+ if ( out . toLowerCase ( ) . includes ( CoreStopOutputKeyword ) ) {
238+ resolve ( pid )
239+ }
240+ } ,
241+ ( ) => {
242+ onCoreStopped ( )
243+ resolve ( 0 )
244+ } ,
245+ { StopOutputKeyword : CoreStopOutputKeyword , Env : env } ,
246+ )
247+ } )
238248 }
239249
240250 const onCoreStarted = async ( pid : number ) => {
241- loading . value = false
242- appSettingsStore . app . kernel . pid = pid
243- appSettingsStore . app . kernel . running = true
251+ await WriteFile ( CorePidFilePath , String ( pid ) )
244252
253+ corePid . value = pid
254+ loading . value = false
255+ running . value = true
245256 isCoreStartedByThisInstance = true
246- coreStoppedPromise = new Promise ( ( r ) => ( doneCoreStopped = r ) )
257+ coreStoppedPromise = new Promise ( ( r ) => ( coreStoppedResolver = r ) )
258+
247259 await Promise . all ( [ refreshConfig ( ) , refreshProviderProxies ( ) ] )
248260
249261 if ( appSettingsStore . app . autoSetSystemProxy ) {
250262 await envStore . setSystemProxy ( ) . catch ( ( err ) => message . error ( err ) )
251263 }
252264 await pluginsStore . onCoreStartedTrigger ( )
265+
266+ initCoreWebsockets ( )
267+ longLivedWS . setup ?.( )
253268 }
254269
255270 const onCoreStopped = async ( ) => {
271+ await RemoveFile ( CorePidFilePath )
272+
273+ corePid . value = - 1
256274 loading . value = false
257- appSettingsStore . app . kernel . pid = 0
258- appSettingsStore . app . kernel . running = false
275+ running . value = false
259276
260277 if ( appSettingsStore . app . autoSetSystemProxy ) {
261278 await envStore . clearSystemProxy ( )
262279 }
263280 await pluginsStore . onCoreStoppedTrigger ( )
264281
265- isCoreStartedByThisInstance && doneCoreStopped ( null )
282+ isCoreStartedByThisInstance && coreStoppedResolver ( null )
283+
284+ destroyCoreWebsockets ( )
266285 }
267286
268- const startKernel = async ( ) => {
287+ const startCore = async ( ) => {
288+ if ( running . value ) throw 'The core is already running'
289+
269290 const { profile : profileID , branch } = appSettingsStore . app . kernel
270291 const profile = profilesStore . getProfileById ( profileID )
271292 if ( ! profile ) throw 'Choose a profile first'
272293
273- await stopKernel ( )
274-
275294 const isAlpha = branch === Branch . Alpha
276295 const fileName = getKernelFileName ( isAlpha )
277296 const kernelFilePath = CoreWorkingDirectory + '/' + fileName
@@ -282,43 +301,32 @@ export const useKernelApiStore = defineStore('kernelApi', () => {
282301 await generateConfigFile ( profile , ( config ) =>
283302 pluginsStore . onBeforeCoreStartTrigger ( config , profile ) ,
284303 )
285- const pid = await ExecBackground (
304+ const pid = await runCoreProcess (
286305 kernelFilePath ,
287306 getKernelRuntimeArgs ( isAlpha ) ,
288- ( out ) => {
289- logsStore . recordKernelLog ( out )
290- if ( out . toLowerCase ( ) . includes ( CoreStopOutputKeyword ) ) {
291- onCoreStarted ( pid )
292- }
293- } ,
294- onCoreStopped ,
295- {
296- StopOutputKeyword : CoreStopOutputKeyword ,
297- Env : getKernelRuntimeEnv ( isAlpha ) ,
298- } ,
307+ getKernelRuntimeEnv ( isAlpha ) ,
299308 )
309+ pid && ( await onCoreStarted ( pid ) )
300310 } catch ( error ) {
301311 loading . value = false
302312 throw error
303313 }
304314 }
305315
306- const stopKernel = async ( ) => {
307- const { pid } = appSettingsStore . app . kernel
308- const running = await ignoredError ( isKernelRunning , pid )
309- if ( running ) {
310- await pluginsStore . onBeforeCoreStopTrigger ( )
311- await KillProcess ( pid )
312- await ( isCoreStartedByThisInstance ? coreStoppedPromise : onCoreStopped ( ) )
313- }
316+ const stopCore = async ( ) => {
317+ if ( ! running . value ) throw 'The core is not running'
318+
319+ await pluginsStore . onBeforeCoreStopTrigger ( )
320+ await KillProcess ( corePid . value )
321+ await ( isCoreStartedByThisInstance ? coreStoppedPromise : onCoreStopped ( ) )
314322
315323 logsStore . clearKernelLog ( )
316324 }
317325
318- const restartKernel = async ( cleanupTask ?: ( ) => Promise < any > ) => {
319- await stopKernel ( )
326+ const restartCore = async ( cleanupTask ?: ( ) => Promise < any > ) => {
327+ await stopCore ( )
320328 await cleanupTask ?.( )
321- await startKernel ( )
329+ await startCore ( )
322330 }
323331
324332 const getProxyPort = ( ) :
@@ -364,28 +372,17 @@ export const useKernelApiStore = defineStore('kernelApi', () => {
364372 return source . concat ( [ proxySignature , unAvailable , sortByDelay ] ) . join ( '' )
365373 } )
366374
367- watch ( watchSources , updateTrayMenus )
368-
369- watch (
370- ( ) => appSettingsStore . app . kernel . running ,
371- async ( v ) => {
372- await firstCoreUpdatePromise
373- if ( v ) {
374- initCoreWebsockets ( )
375- longLivedWS . setup ?.( )
376- } else {
377- destroyCoreWebsockets ( )
378- }
379- } ,
380- )
375+ watch ( [ watchSources , running ] , updateTrayMenus )
381376
382377 return {
383- startKernel,
384- stopKernel,
385- restartKernel,
386- updateKernelState,
378+ startCore,
379+ stopCore,
380+ restartCore,
381+ updateCoreState,
382+ pid : corePid ,
383+ running,
387384 loading,
388- statusLoading ,
385+ coreStateLoading ,
389386 config,
390387 proxies,
391388 providers,
@@ -398,5 +395,19 @@ export const useKernelApiStore = defineStore('kernelApi', () => {
398395 onMemory : createCoreWSHandlerRegister ( websocketHandlers . memory ) ,
399396 onTraffic : createCoreWSHandlerRegister ( websocketHandlers . traffic ) ,
400397 onConnections : createCoreWSHandlerRegister ( websocketHandlers . connections ) ,
398+
399+ // Deprecated
400+ startKernel : ( ) => {
401+ console . warn ( '[Deprecated] "startKernel" is deprecated. Please use "startCore" instead.' )
402+ startCore ( )
403+ } ,
404+ stopKernel : ( ) => {
405+ console . warn ( '[Deprecated] "stopKernel" is deprecated. Please use "stopCore" instead.' )
406+ stopCore ( )
407+ } ,
408+ restartKernel : ( ...args : any [ ] ) => {
409+ console . warn ( '[Deprecated] "restartKernel" is deprecated. Please use "restartCore" instead.' )
410+ restartCore ( ...args )
411+ } ,
401412 }
402413} )
0 commit comments