@@ -440,26 +440,35 @@ func (s *StartedService) SubscribeGroups(empty *emptypb.Empty, server grpc.Serve
440440 return err
441441 }
442442 defer s .urlTestObserver .UnSubscribe (subscription )
443+ statusSubscription , statusDone , err := s .serviceStatusObserver .Subscribe ()
444+ if err != nil {
445+ return err
446+ }
447+ defer s .serviceStatusObserver .UnSubscribe (statusSubscription )
443448 for {
444449 s .serviceAccess .RLock ()
445- if s .serviceStatus .Status != ServiceStatus_STARTED {
446- s .serviceAccess .RUnlock ()
447- return os .ErrInvalid
450+ var groups * Groups
451+ if s .serviceStatus .Status == ServiceStatus_STARTED {
452+ groups = s .readGroups ()
453+ } else {
454+ groups = & Groups {}
448455 }
449- groups := s .readGroups ()
450456 s .serviceAccess .RUnlock ()
451457 err = server .Send (groups )
452458 if err != nil {
453459 return err
454460 }
455461 select {
456462 case <- subscription :
463+ case <- statusSubscription :
457464 case <- s .ctx .Done ():
458465 return s .ctx .Err ()
459466 case <- server .Context ().Done ():
460467 return server .Context ().Err ()
461468 case <- done :
462469 return nil
470+ case <- statusDone :
471+ return nil
463472 }
464473 }
465474}
@@ -537,31 +546,40 @@ func (s *StartedService) SubscribeClashMode(empty *emptypb.Empty, server grpc.Se
537546 return err
538547 }
539548 defer s .clashModeObserver .UnSubscribe (subscription )
549+ statusSubscription , statusDone , err := s .serviceStatusObserver .Subscribe ()
550+ if err != nil {
551+ return err
552+ }
553+ defer s .serviceStatusObserver .UnSubscribe (statusSubscription )
540554 for {
541555 s .serviceAccess .RLock ()
542- if s .serviceStatus .Status != ServiceStatus_STARTED {
543- s .serviceAccess .RUnlock ()
544- return os .ErrInvalid
545- }
546- clashServer := s .instance .clashServer
547- if clashServer == nil {
548- s .serviceAccess .RUnlock ()
549- return status .Error (codes .NotFound , "clash mode not available" )
556+ var message * ClashMode
557+ if s .serviceStatus .Status == ServiceStatus_STARTED {
558+ clashServer := s .instance .clashServer
559+ if clashServer == nil {
560+ s .serviceAccess .RUnlock ()
561+ return status .Error (codes .NotFound , "clash mode not available" )
562+ }
563+ message = & ClashMode {Mode : clashServer .Mode ()}
564+ } else {
565+ message = & ClashMode {}
550566 }
551- message := & ClashMode {Mode : clashServer .Mode ()}
552567 s .serviceAccess .RUnlock ()
553568 err = server .Send (message )
554569 if err != nil {
555570 return err
556571 }
557572 select {
558573 case <- subscription :
574+ case <- statusSubscription :
559575 case <- s .ctx .Done ():
560576 return s .ctx .Err ()
561577 case <- server .Context ().Done ():
562578 return server .Context ().Err ()
563579 case <- done :
564580 return nil
581+ case <- statusDone :
582+ return nil
565583 }
566584 }
567585}
@@ -1050,50 +1068,57 @@ func (s *StartedService) SubscribeOutbounds(_ *emptypb.Empty, server grpc.Server
10501068 return err
10511069 }
10521070 defer s .urlTestObserver .UnSubscribe (subscription )
1071+ statusSubscription , statusDone , err := s .serviceStatusObserver .Subscribe ()
1072+ if err != nil {
1073+ return err
1074+ }
1075+ defer s .serviceStatusObserver .UnSubscribe (statusSubscription )
10531076 for {
10541077 s .serviceAccess .RLock ()
1055- if s .serviceStatus .Status != ServiceStatus_STARTED {
1056- s .serviceAccess .RUnlock ()
1057- return os .ErrInvalid
1058- }
10591078 boxService := s .instance
1079+ started := s .serviceStatus .Status == ServiceStatus_STARTED
10601080 s .serviceAccess .RUnlock ()
1061- historyStorage := boxService .urlTestHistoryStorage
10621081 var list OutboundList
1063- for _ , ob := range boxService .outboundManager .Outbounds () {
1064- item := & GroupItem {
1065- Tag : ob .Tag (),
1066- Type : ob .Type (),
1067- }
1068- if history := historyStorage .LoadURLTestHistory (adapter .OutboundTag (ob )); history != nil {
1069- item .UrlTestTime = history .Time .Unix ()
1070- item .UrlTestDelay = int32 (history .Delay )
1071- }
1072- list .Outbounds = append (list .Outbounds , item )
1073- }
1074- for _ , ep := range boxService .endpointManager .Endpoints () {
1075- item := & GroupItem {
1076- Tag : ep .Tag (),
1077- Type : ep .Type (),
1082+ if started {
1083+ historyStorage := boxService .urlTestHistoryStorage
1084+ for _ , ob := range boxService .outboundManager .Outbounds () {
1085+ item := & GroupItem {
1086+ Tag : ob .Tag (),
1087+ Type : ob .Type (),
1088+ }
1089+ if history := historyStorage .LoadURLTestHistory (adapter .OutboundTag (ob )); history != nil {
1090+ item .UrlTestTime = history .Time .Unix ()
1091+ item .UrlTestDelay = int32 (history .Delay )
1092+ }
1093+ list .Outbounds = append (list .Outbounds , item )
10781094 }
1079- if history := historyStorage .LoadURLTestHistory (adapter .OutboundTag (ep )); history != nil {
1080- item .UrlTestTime = history .Time .Unix ()
1081- item .UrlTestDelay = int32 (history .Delay )
1095+ for _ , ep := range boxService .endpointManager .Endpoints () {
1096+ item := & GroupItem {
1097+ Tag : ep .Tag (),
1098+ Type : ep .Type (),
1099+ }
1100+ if history := historyStorage .LoadURLTestHistory (adapter .OutboundTag (ep )); history != nil {
1101+ item .UrlTestTime = history .Time .Unix ()
1102+ item .UrlTestDelay = int32 (history .Delay )
1103+ }
1104+ list .Outbounds = append (list .Outbounds , item )
10821105 }
1083- list .Outbounds = append (list .Outbounds , item )
10841106 }
10851107 err = server .Send (& list )
10861108 if err != nil {
10871109 return err
10881110 }
10891111 select {
10901112 case <- subscription :
1113+ case <- statusSubscription :
10911114 case <- s .ctx .Done ():
10921115 return s .ctx .Err ()
10931116 case <- server .Context ().Done ():
10941117 return server .Context ().Err ()
10951118 case <- done :
10961119 return nil
1120+ case <- statusDone :
1121+ return nil
10971122 }
10981123 }
10991124}
0 commit comments