From 5563861590d86446f269bfe7065e08f80f8b54d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 25 Jul 2026 01:33:11 +0800 Subject: [PATCH] daemon: Improve URLTest --- daemon/started_service.go | 38 ++++++++++++++++----------- experimental/libbox/command_client.go | 4 +-- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/daemon/started_service.go b/daemon/started_service.go index 70d7f057b..846625a87 100644 --- a/daemon/started_service.go +++ b/daemon/started_service.go @@ -609,21 +609,17 @@ func (s *StartedService) URLTest(ctx context.Context, request *URLTestRequest) ( } boxService := s.instance s.serviceAccess.RUnlock() - groupTag := request.OutboundTag - abstractOutboundGroup, isLoaded := boxService.outboundManager.Outbound(groupTag) + outboundTag := request.OutboundTag + outbound, isLoaded := boxService.outboundManager.Outbound(outboundTag) if !isLoaded { - return nil, status.Error(codes.NotFound, "outbound group not found: "+groupTag) + return nil, status.Error(codes.NotFound, "outbound not found: "+outboundTag) } - outboundGroup, isOutboundGroup := abstractOutboundGroup.(adapter.OutboundGroup) - if !isOutboundGroup { - return nil, status.Error(codes.InvalidArgument, "outbound is not a group: "+groupTag) - } - urlTest, isURLTest := abstractOutboundGroup.(*group.URLTest) + historyStorage := boxService.urlTestHistoryStorage + urlTest, isURLTest := outbound.(*group.URLTest) + outboundGroup, isOutboundGroup := outbound.(adapter.OutboundGroup) if isURLTest { go urlTest.CheckOutbounds() - } else { - historyStorage := boxService.urlTestHistoryStorage - + } else if isOutboundGroup { outbounds := common.Filter(common.Map(outboundGroup.All(), func(it string) adapter.Outbound { itOutbound, _ := boxService.outboundManager.Outbound(it) return itOutbound @@ -637,13 +633,13 @@ func (s *StartedService) URLTest(ctx context.Context, request *URLTestRequest) ( b, _ := batch.New(boxService.ctx, batch.WithConcurrencyNum[any](10)) for _, detour := range outbounds { outboundToTest := detour - outboundTag := outboundToTest.Tag() - b.Go(outboundTag, func() (any, error) { + itemTag := outboundToTest.Tag() + b.Go(itemTag, func() (any, error) { t, err := urltest.URLTest(boxService.ctx, "", outboundToTest) if err != nil { - historyStorage.DeleteURLTestHistory(outboundTag) + historyStorage.DeleteURLTestHistory(itemTag) } else { - historyStorage.StoreURLTestHistory(outboundTag, &adapter.URLTestHistory{ + historyStorage.StoreURLTestHistory(itemTag, &adapter.URLTestHistory{ Time: time.Now(), Delay: t, }) @@ -651,6 +647,18 @@ func (s *StartedService) URLTest(ctx context.Context, request *URLTestRequest) ( return nil, nil }) } + } else { + go func() { + t, err := urltest.URLTest(boxService.ctx, "", outbound) + if err != nil { + historyStorage.DeleteURLTestHistory(outboundTag) + } else { + historyStorage.StoreURLTestHistory(outboundTag, &adapter.URLTestHistory{ + Time: time.Now(), + Delay: t, + }) + } + }() } return &emptypb.Empty{}, nil } diff --git a/experimental/libbox/command_client.go b/experimental/libbox/command_client.go index df466c870..f15e7219a 100644 --- a/experimental/libbox/command_client.go +++ b/experimental/libbox/command_client.go @@ -550,10 +550,10 @@ func (c *CommandClient) SelectOutbound(groupTag string, outboundTag string) erro return nil } -func (c *CommandClient) URLTest(groupTag string) error { +func (c *CommandClient) URLTest(outboundTag string) error { _, err := callWithResult(c, func(ctx context.Context, client daemon.StartedServiceClient) (*emptypb.Empty, error) { return client.URLTest(ctx, &daemon.URLTestRequest{ - OutboundTag: groupTag, + OutboundTag: outboundTag, }) }) if err != nil {