chore: code cleanup

This commit is contained in:
wwqgtxx 2024-09-27 21:33:37 +08:00
parent af5ad3254b
commit a67c379884
6 changed files with 25 additions and 39 deletions

View File

@ -39,6 +39,11 @@ type Proxy struct {
extra *xsync.MapOf[string, *internalProxyState] extra *xsync.MapOf[string, *internalProxyState]
} }
// Adapter implements C.Proxy
func (p *Proxy) Adapter() C.ProxyAdapter {
return p.ProxyAdapter
}
// AliveForTestUrl implements C.Proxy // AliveForTestUrl implements C.Proxy
func (p *Proxy) AliveForTestUrl(url string) bool { func (p *Proxy) AliveForTestUrl(url string) bool {
if state, ok := p.extra.Load(url); ok { if state, ok := p.extra.Load(url); ok {

View File

@ -4,3 +4,7 @@ type SelectAble interface {
Set(string) error Set(string) error
ForceSet(name string) ForceSet(name string)
} }
var _ SelectAble = (*Fallback)(nil)
var _ SelectAble = (*URLTest)(nil)
var _ SelectAble = (*Selector)(nil)

View File

@ -158,6 +158,7 @@ type DelayHistoryStoreType int
type Proxy interface { type Proxy interface {
ProxyAdapter ProxyAdapter
Adapter() ProxyAdapter
AliveForTestUrl(url string) bool AliveForTestUrl(url string) bool
DelayHistory() []DelayHistory DelayHistory() []DelayHistory
ExtraDelayHistories() map[string]ProxyState ExtraDelayHistories() map[string]ProxyState

View File

@ -445,12 +445,12 @@ func patchSelectGroup(proxies map[string]C.Proxy) {
} }
for name, proxy := range proxies { for name, proxy := range proxies {
outbound, ok := proxy.(*adapter.Proxy) outbound, ok := proxy.(C.Proxy)
if !ok { if !ok {
continue continue
} }
selector, ok := outbound.ProxyAdapter.(outboundgroup.SelectAble) selector, ok := outbound.Adapter().(outboundgroup.SelectAble)
if !ok { if !ok {
continue continue
} }

View File

@ -9,7 +9,6 @@ import (
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/render" "github.com/go-chi/render"
"github.com/metacubex/mihomo/adapter"
"github.com/metacubex/mihomo/adapter/outboundgroup" "github.com/metacubex/mihomo/adapter/outboundgroup"
"github.com/metacubex/mihomo/common/utils" "github.com/metacubex/mihomo/common/utils"
"github.com/metacubex/mihomo/component/profile/cachefile" "github.com/metacubex/mihomo/component/profile/cachefile"
@ -32,7 +31,7 @@ func GroupRouter() http.Handler {
func getGroups(w http.ResponseWriter, r *http.Request) { func getGroups(w http.ResponseWriter, r *http.Request) {
var gs []C.Proxy var gs []C.Proxy
for _, p := range tunnel.Proxies() { for _, p := range tunnel.Proxies() {
if _, ok := p.(*adapter.Proxy).ProxyAdapter.(C.Group); ok { if _, ok := p.Adapter().(C.Group); ok {
gs = append(gs, p) gs = append(gs, p)
} }
} }
@ -43,7 +42,7 @@ func getGroups(w http.ResponseWriter, r *http.Request) {
func getGroup(w http.ResponseWriter, r *http.Request) { func getGroup(w http.ResponseWriter, r *http.Request) {
proxy := r.Context().Value(CtxKeyProxy).(C.Proxy) proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
if _, ok := proxy.(*adapter.Proxy).ProxyAdapter.(C.Group); ok { if _, ok := proxy.Adapter().(C.Group); ok {
render.JSON(w, r, proxy) render.JSON(w, r, proxy)
return return
} }
@ -53,25 +52,15 @@ func getGroup(w http.ResponseWriter, r *http.Request) {
func getGroupDelay(w http.ResponseWriter, r *http.Request) { func getGroupDelay(w http.ResponseWriter, r *http.Request) {
proxy := r.Context().Value(CtxKeyProxy).(C.Proxy) proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
group, ok := proxy.(*adapter.Proxy).ProxyAdapter.(C.Group) group, ok := proxy.Adapter().(C.Group)
if !ok { if !ok {
render.Status(r, http.StatusNotFound) render.Status(r, http.StatusNotFound)
render.JSON(w, r, ErrNotFound) render.JSON(w, r, ErrNotFound)
return return
} }
switch proxy.(*adapter.Proxy).Type() { if selectAble, ok := proxy.Adapter().(outboundgroup.SelectAble); ok && proxy.Type() != C.Selector {
case C.URLTest: selectAble.ForceSet("")
if urlTestGroup, ok := proxy.(*adapter.Proxy).ProxyAdapter.(*outboundgroup.URLTest); ok {
urlTestGroup.ForceSet("")
}
case C.Fallback:
if fallbackGroup, ok := proxy.(*adapter.Proxy).ProxyAdapter.(*outboundgroup.Fallback); ok {
fallbackGroup.ForceSet("")
}
}
if proxy.(*adapter.Proxy).Type() != C.Selector {
cachefile.Cache().SetSelected(proxy.Name(), "") cachefile.Cache().SetSelected(proxy.Name(), "")
} }

View File

@ -7,7 +7,6 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/metacubex/mihomo/adapter"
"github.com/metacubex/mihomo/adapter/outboundgroup" "github.com/metacubex/mihomo/adapter/outboundgroup"
"github.com/metacubex/mihomo/common/utils" "github.com/metacubex/mihomo/common/utils"
"github.com/metacubex/mihomo/component/profile/cachefile" "github.com/metacubex/mihomo/component/profile/cachefile"
@ -82,8 +81,8 @@ func updateProxy(w http.ResponseWriter, r *http.Request) {
return return
} }
proxy := r.Context().Value(CtxKeyProxy).(*adapter.Proxy) proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
selector, ok := proxy.ProxyAdapter.(outboundgroup.SelectAble) selector, ok := proxy.Adapter().(outboundgroup.SelectAble)
if !ok { if !ok {
render.Status(r, http.StatusBadRequest) render.Status(r, http.StatusBadRequest)
render.JSON(w, r, newError("Must be a Selector")) render.JSON(w, r, newError("Must be a Selector"))
@ -150,24 +149,12 @@ func getProxyDelay(w http.ResponseWriter, r *http.Request) {
func unfixedProxy(w http.ResponseWriter, r *http.Request) { func unfixedProxy(w http.ResponseWriter, r *http.Request) {
proxy := r.Context().Value(CtxKeyProxy).(C.Proxy) proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
switch proxy.(*adapter.Proxy).Type() { if selectAble, ok := proxy.Adapter().(outboundgroup.SelectAble); ok && proxy.Type() != C.Selector {
case C.URLTest: selectAble.ForceSet("")
if urlTestGroup, ok := proxy.(*adapter.Proxy).ProxyAdapter.(*outboundgroup.URLTest); ok { cachefile.Cache().SetSelected(proxy.Name(), "")
urlTestGroup.ForceSet("") render.NoContent(w, r)
}
case C.Fallback:
if fallbackGroup, ok := proxy.(*adapter.Proxy).ProxyAdapter.(*outboundgroup.Fallback); ok {
fallbackGroup.ForceSet("")
}
default:
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, ErrBadRequest)
return return
} }
render.Status(r, http.StatusBadRequest)
if proxy.(*adapter.Proxy).Type() != C.Selector { render.JSON(w, r, ErrBadRequest)
cachefile.Cache().SetSelected(proxy.Name(), "")
}
render.NoContent(w, r)
} }