11import { defineStore } from 'pinia'
22import { computed , ref , watch } from 'vue'
33
4- import { getConfigs , setConfigs , getProxies , getProviders , Api } from '@/api/kernel'
4+ import { getConfigs , setConfigs , getProxies , Api } from '@/api/kernel'
55import { ProcessInfo , KillProcess , ExecBackground , ReadFile , WriteFile , RemoveFile } from '@/bridge'
66import { CorePidFilePath , CoreStopOutputKeyword , CoreWorkingDirectory } from '@/constant'
77import { Branch } from '@/enums/app'
8+ import { RuleType } from '@/enums/kernel'
89import {
910 useAppSettingsStore ,
1011 useProfilesStore ,
1112 useLogsStore ,
1213 useEnvStore ,
1314 usePluginsStore ,
15+ useSubscribesStore ,
16+ useRulesetsStore ,
1417} from '@/stores'
1518import {
1619 generateConfigFile ,
@@ -21,6 +24,7 @@ import {
2124 message ,
2225 getKernelRuntimeArgs ,
2326 getKernelRuntimeEnv ,
27+ eventBus ,
2428} from '@/utils'
2529
2630import type {
@@ -39,6 +43,8 @@ export const useKernelApiStore = defineStore('kernelApi', () => {
3943 const logsStore = useLogsStore ( )
4044 const pluginsStore = usePluginsStore ( )
4145 const profilesStore = useProfilesStore ( )
46+ const subscribesStore = useSubscribesStore ( )
47+ const rulesetsStore = useRulesetsStore ( )
4248 const appSettingsStore = useAppSettingsStore ( )
4349
4450 /** RESTful API */
@@ -51,18 +57,12 @@ export const useKernelApiStore = defineStore('kernelApi', () => {
5157 mode : '' ,
5258 tun : {
5359 enable : false ,
54- stack : 'gVisor ' ,
60+ stack : '' ,
5561 device : '' ,
5662 } ,
5763 } )
5864
5965 const proxies = ref < Record < string , CoreApiProxy > > ( { } )
60- const providers = ref < {
61- [ key : string ] : {
62- name : string
63- proxies : CoreApiProxy [ ]
64- }
65- } > ( { } )
6666
6767 const refreshConfig = async ( ) => {
6868 config . value = await getConfigs ( )
@@ -74,8 +74,7 @@ export const useKernelApiStore = defineStore('kernelApi', () => {
7474 }
7575
7676 const refreshProviderProxies = async ( ) => {
77- const [ { providers : a } , { proxies : b } ] = await Promise . all ( [ getProviders ( ) , getProxies ( ) ] )
78- providers . value = a
77+ const { proxies : b } = await getProxies ( )
7978 proxies . value = b
8079 }
8180
@@ -207,6 +206,7 @@ export const useKernelApiStore = defineStore('kernelApi', () => {
207206 const starting = ref ( false )
208207 const stopping = ref ( false )
209208 const restarting = ref ( false )
209+ const needRestart = ref ( false )
210210 const coreStateLoading = ref ( true )
211211 let isCoreStartedByThisInstance = false
212212 let { promise : coreStoppedPromise , resolve : coreStoppedResolver } = Promise . withResolvers ( )
@@ -255,6 +255,7 @@ export const useKernelApiStore = defineStore('kernelApi', () => {
255255
256256 corePid . value = pid
257257 running . value = true
258+ needRestart . value = false
258259 isCoreStartedByThisInstance = true
259260 coreStoppedPromise = new Promise ( ( r ) => ( coreStoppedResolver = r ) )
260261
@@ -361,6 +362,67 @@ export const useKernelApiStore = defineStore('kernelApi', () => {
361362 return undefined
362363 }
363364
365+ eventBus . on ( 'profileChange' , ( { id } ) => {
366+ if ( running . value && id === appSettingsStore . app . kernel . profile ) {
367+ needRestart . value = true
368+ }
369+ } )
370+
371+ eventBus . on ( 'subscriptionChange' , ( { id } ) => {
372+ if ( running . value && profilesStore . currentProfile ) {
373+ const inUse = profilesStore . currentProfile . proxyGroupsConfig . some (
374+ ( group ) => group . use . includes ( id ) || group . proxies . some ( ( proxy ) => proxy . type === id ) ,
375+ )
376+ if ( inUse ) {
377+ needRestart . value = true
378+ }
379+ }
380+ } )
381+
382+ eventBus . on ( 'subscriptionsChange' , ( ) => {
383+ if ( running . value && profilesStore . currentProfile ) {
384+ const enabledSubs = subscribesStore . subscribes . flatMap ( ( v ) => ( v . disabled ? [ ] : v . id ) )
385+ const inUse = profilesStore . currentProfile . proxyGroupsConfig . some (
386+ ( group ) =>
387+ group . use . some ( ( sub ) => enabledSubs . includes ( sub ) ) ||
388+ group . proxies . some ( ( proxy ) => enabledSubs . includes ( proxy . type ) ) ,
389+ )
390+ if ( inUse ) {
391+ needRestart . value = true
392+ }
393+ }
394+ } )
395+
396+ const collectRulesetIDs = ( ) => {
397+ if ( ! profilesStore . currentProfile ) return [ ]
398+ const l1 = Object . keys ( profilesStore . currentProfile . dnsConfig [ 'nameserver-policy' ] ) . flatMap (
399+ ( v ) => ( v . startsWith ( 'rule-set:' ) ? v . slice ( 'rule-set:' . length ) : [ ] ) ,
400+ )
401+ const l2 = profilesStore . currentProfile . rulesConfig . flatMap ( ( v ) =>
402+ v . type === RuleType . RuleSet ? v . payload : [ ] ,
403+ )
404+ return [ ...l1 , ...l2 ]
405+ }
406+
407+ eventBus . on ( 'rulesetChange' , ( { id } ) => {
408+ if ( running . value && profilesStore . currentProfile ) {
409+ const inUse = collectRulesetIDs ( ) . includes ( id )
410+ if ( inUse ) {
411+ needRestart . value = true
412+ }
413+ }
414+ } )
415+
416+ eventBus . on ( 'rulesetsChange' , ( ) => {
417+ if ( running . value && profilesStore . currentProfile ) {
418+ const enabledRulesets = rulesetsStore . rulesets . flatMap ( ( v ) => ( v . disabled ? [ ] : v . id ) )
419+ const inUse = collectRulesetIDs ( ) . some ( ( v ) => enabledRulesets . includes ( v ) )
420+ if ( inUse ) {
421+ needRestart . value = true
422+ }
423+ }
424+ } )
425+
364426 const watchSources = computed ( ( ) => {
365427 const source = [ config . value . mode , config . value . tun . enable ]
366428 if ( ! appSettingsStore . app . addGroupToMenu ) return source . join ( '' )
@@ -387,10 +449,10 @@ export const useKernelApiStore = defineStore('kernelApi', () => {
387449 starting,
388450 stopping,
389451 restarting,
452+ needRestart,
390453 coreStateLoading,
391454 config,
392455 proxies,
393- providers,
394456 refreshConfig,
395457 updateConfig,
396458 refreshProviderProxies,
0 commit comments