From 9df8392c65dd5470e131ae68d4fddf15f9ac348e Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Wed, 3 Dec 2025 11:08:16 +0800 Subject: [PATCH] chore: clean up internal interface definitions --- adapter/outboundgroup/fallback.go | 8 ++++ adapter/outboundgroup/loadbalance.go | 12 ++++++ adapter/outboundgroup/patch_android.go | 52 -------------------------- adapter/outboundgroup/selector.go | 8 ++++ adapter/outboundgroup/urltest.go | 8 ++++ adapter/outboundgroup/util.go | 24 ++++++++++++ constant/adapters.go | 5 --- hub/route/groups.go | 6 +-- 8 files changed, 63 insertions(+), 60 deletions(-) delete mode 100644 adapter/outboundgroup/patch_android.go diff --git a/adapter/outboundgroup/fallback.go b/adapter/outboundgroup/fallback.go index 3772107b..0174a7b9 100644 --- a/adapter/outboundgroup/fallback.go +++ b/adapter/outboundgroup/fallback.go @@ -150,6 +150,14 @@ func (f *Fallback) ForceSet(name string) { f.selected = name } +func (f *Fallback) Providers() []P.ProxyProvider { + return f.providers +} + +func (f *Fallback) Proxies() []C.Proxy { + return f.GetProxies(false) +} + func NewFallback(option *GroupCommonOption, providers []P.ProxyProvider) *Fallback { return &Fallback{ GroupBase: NewGroupBase(GroupBaseOption{ diff --git a/adapter/outboundgroup/loadbalance.go b/adapter/outboundgroup/loadbalance.go index dff9b5ed..19ee38c7 100644 --- a/adapter/outboundgroup/loadbalance.go +++ b/adapter/outboundgroup/loadbalance.go @@ -239,6 +239,18 @@ func (lb *LoadBalance) MarshalJSON() ([]byte, error) { }) } +func (lb *LoadBalance) Providers() []P.ProxyProvider { + return lb.providers +} + +func (lb *LoadBalance) Proxies() []C.Proxy { + return lb.GetProxies(false) +} + +func (lb *LoadBalance) Now() string { + return "" +} + func NewLoadBalance(option *GroupCommonOption, providers []P.ProxyProvider, strategy string) (lb *LoadBalance, err error) { var strategyFn strategyFn switch strategy { diff --git a/adapter/outboundgroup/patch_android.go b/adapter/outboundgroup/patch_android.go deleted file mode 100644 index f0c254c2..00000000 --- a/adapter/outboundgroup/patch_android.go +++ /dev/null @@ -1,52 +0,0 @@ -//go:build android && cmfa - -package outboundgroup - -import ( - C "github.com/metacubex/mihomo/constant" - P "github.com/metacubex/mihomo/constant/provider" -) - -type ProxyGroup interface { - C.ProxyAdapter - - Providers() []P.ProxyProvider - Proxies() []C.Proxy - Now() string -} - -func (f *Fallback) Providers() []P.ProxyProvider { - return f.providers -} - -func (lb *LoadBalance) Providers() []P.ProxyProvider { - return lb.providers -} - -func (f *Fallback) Proxies() []C.Proxy { - return f.GetProxies(false) -} - -func (lb *LoadBalance) Proxies() []C.Proxy { - return lb.GetProxies(false) -} - -func (lb *LoadBalance) Now() string { - return "" -} - -func (s *Selector) Providers() []P.ProxyProvider { - return s.providers -} - -func (s *Selector) Proxies() []C.Proxy { - return s.GetProxies(false) -} - -func (u *URLTest) Providers() []P.ProxyProvider { - return u.providers -} - -func (u *URLTest) Proxies() []C.Proxy { - return u.GetProxies(false) -} diff --git a/adapter/outboundgroup/selector.go b/adapter/outboundgroup/selector.go index f8975df7..7bc138fd 100644 --- a/adapter/outboundgroup/selector.go +++ b/adapter/outboundgroup/selector.go @@ -108,6 +108,14 @@ func (s *Selector) selectedProxy(touch bool) C.Proxy { return proxies[0] } +func (s *Selector) Providers() []P.ProxyProvider { + return s.providers +} + +func (s *Selector) Proxies() []C.Proxy { + return s.GetProxies(false) +} + func NewSelector(option *GroupCommonOption, providers []P.ProxyProvider) *Selector { return &Selector{ GroupBase: NewGroupBase(GroupBaseOption{ diff --git a/adapter/outboundgroup/urltest.go b/adapter/outboundgroup/urltest.go index 2adb3b81..49ea12aa 100644 --- a/adapter/outboundgroup/urltest.go +++ b/adapter/outboundgroup/urltest.go @@ -185,6 +185,14 @@ func (u *URLTest) MarshalJSON() ([]byte, error) { }) } +func (u *URLTest) Providers() []P.ProxyProvider { + return u.providers +} + +func (u *URLTest) Proxies() []C.Proxy { + return u.GetProxies(false) +} + func (u *URLTest) URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (map[string]uint16, error) { return u.GroupBase.URLTest(ctx, u.testUrl, expectedStatus) } diff --git a/adapter/outboundgroup/util.go b/adapter/outboundgroup/util.go index 66b2510c..d35ea66f 100644 --- a/adapter/outboundgroup/util.go +++ b/adapter/outboundgroup/util.go @@ -1,5 +1,29 @@ package outboundgroup +import ( + "context" + + "github.com/metacubex/mihomo/common/utils" + C "github.com/metacubex/mihomo/constant" + P "github.com/metacubex/mihomo/constant/provider" +) + +type ProxyGroup interface { + C.ProxyAdapter + + Providers() []P.ProxyProvider + Proxies() []C.Proxy + Now() string + Touch() + + URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (mp map[string]uint16, err error) +} + +var _ ProxyGroup = (*Fallback)(nil) +var _ ProxyGroup = (*LoadBalance)(nil) +var _ ProxyGroup = (*URLTest)(nil) +var _ ProxyGroup = (*Selector)(nil) + type SelectAble interface { Set(string) error ForceSet(name string) diff --git a/constant/adapters.go b/constant/adapters.go index c1ac3723..0b9098fd 100644 --- a/constant/adapters.go +++ b/constant/adapters.go @@ -139,11 +139,6 @@ type ProxyAdapter interface { Close() error } -type Group interface { - URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (mp map[string]uint16, err error) - Touch() -} - type DelayHistory struct { Time time.Time `json:"time"` Delay uint16 `json:"delay"` diff --git a/hub/route/groups.go b/hub/route/groups.go index 873a94df..05e8fe49 100644 --- a/hub/route/groups.go +++ b/hub/route/groups.go @@ -31,7 +31,7 @@ func groupRouter() http.Handler { func getGroups(w http.ResponseWriter, r *http.Request) { var gs []C.Proxy for _, p := range tunnel.Proxies() { - if _, ok := p.Adapter().(C.Group); ok { + if _, ok := p.Adapter().(outboundgroup.ProxyGroup); ok { gs = append(gs, p) } } @@ -42,7 +42,7 @@ func getGroups(w http.ResponseWriter, r *http.Request) { func getGroup(w http.ResponseWriter, r *http.Request) { proxy := r.Context().Value(CtxKeyProxy).(C.Proxy) - if _, ok := proxy.Adapter().(C.Group); ok { + if _, ok := proxy.Adapter().(outboundgroup.ProxyGroup); ok { render.JSON(w, r, proxy) return } @@ -52,7 +52,7 @@ func getGroup(w http.ResponseWriter, r *http.Request) { func getGroupDelay(w http.ResponseWriter, r *http.Request) { proxy := r.Context().Value(CtxKeyProxy).(C.Proxy) - group, ok := proxy.Adapter().(C.Group) + group, ok := proxy.Adapter().(outboundgroup.ProxyGroup) if !ok { render.Status(r, http.StatusNotFound) render.JSON(w, r, ErrNotFound)