Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Sep 27, 2024
1 parent af5ad32 commit a67c379
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 39 deletions.
5 changes: 5 additions & 0 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ type Proxy struct {
extra *xsync.MapOf[string, *internalProxyState]
}

// Adapter implements C.Proxy
func (p *Proxy) Adapter() C.ProxyAdapter {
return p.ProxyAdapter
}

// AliveForTestUrl implements C.Proxy
func (p *Proxy) AliveForTestUrl(url string) bool {
if state, ok := p.extra.Load(url); ok {
Expand Down
4 changes: 4 additions & 0 deletions adapter/outboundgroup/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ type SelectAble interface {
Set(string) error
ForceSet(name string)
}

var _ SelectAble = (*Fallback)(nil)
var _ SelectAble = (*URLTest)(nil)
var _ SelectAble = (*Selector)(nil)
1 change: 1 addition & 0 deletions constant/adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ type DelayHistoryStoreType int

type Proxy interface {
ProxyAdapter
Adapter() ProxyAdapter
AliveForTestUrl(url string) bool
DelayHistory() []DelayHistory
ExtraDelayHistories() map[string]ProxyState
Expand Down
4 changes: 2 additions & 2 deletions hub/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,12 @@ func patchSelectGroup(proxies map[string]C.Proxy) {
}

for name, proxy := range proxies {
outbound, ok := proxy.(*adapter.Proxy)
outbound, ok := proxy.(C.Proxy)
if !ok {
continue
}

selector, ok := outbound.ProxyAdapter.(outboundgroup.SelectAble)
selector, ok := outbound.Adapter().(outboundgroup.SelectAble)
if !ok {
continue
}
Expand Down
21 changes: 5 additions & 16 deletions hub/route/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"

"github.com/metacubex/mihomo/adapter"
"github.com/metacubex/mihomo/adapter/outboundgroup"
"github.com/metacubex/mihomo/common/utils"
"github.com/metacubex/mihomo/component/profile/cachefile"
Expand All @@ -32,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.Proxy).ProxyAdapter.(C.Group); ok {
if _, ok := p.Adapter().(C.Group); ok {
gs = append(gs, p)
}
}
Expand All @@ -43,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.Proxy).ProxyAdapter.(C.Group); ok {
if _, ok := proxy.Adapter().(C.Group); ok {
render.JSON(w, r, proxy)
return
}
Expand All @@ -53,25 +52,15 @@ 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.Proxy).ProxyAdapter.(C.Group)
group, ok := proxy.Adapter().(C.Group)
if !ok {
render.Status(r, http.StatusNotFound)
render.JSON(w, r, ErrNotFound)
return
}

switch proxy.(*adapter.Proxy).Type() {
case C.URLTest:
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 {
if selectAble, ok := proxy.Adapter().(outboundgroup.SelectAble); ok && proxy.Type() != C.Selector {
selectAble.ForceSet("")
cachefile.Cache().SetSelected(proxy.Name(), "")
}

Expand Down
29 changes: 8 additions & 21 deletions hub/route/proxies.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strconv"
"time"

"github.com/metacubex/mihomo/adapter"
"github.com/metacubex/mihomo/adapter/outboundgroup"
"github.com/metacubex/mihomo/common/utils"
"github.com/metacubex/mihomo/component/profile/cachefile"
Expand Down Expand Up @@ -82,8 +81,8 @@ func updateProxy(w http.ResponseWriter, r *http.Request) {
return
}

proxy := r.Context().Value(CtxKeyProxy).(*adapter.Proxy)
selector, ok := proxy.ProxyAdapter.(outboundgroup.SelectAble)
proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
selector, ok := proxy.Adapter().(outboundgroup.SelectAble)
if !ok {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, newError("Must be a Selector"))
Expand Down Expand Up @@ -150,24 +149,12 @@ func getProxyDelay(w http.ResponseWriter, r *http.Request) {

func unfixedProxy(w http.ResponseWriter, r *http.Request) {
proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
switch proxy.(*adapter.Proxy).Type() {
case C.URLTest:
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("")
}
default:
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, ErrBadRequest)
return
}

if proxy.(*adapter.Proxy).Type() != C.Selector {
if selectAble, ok := proxy.Adapter().(outboundgroup.SelectAble); ok && proxy.Type() != C.Selector {
selectAble.ForceSet("")
cachefile.Cache().SetSelected(proxy.Name(), "")
render.NoContent(w, r)
return
}

render.NoContent(w, r)
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, ErrBadRequest)
}

0 comments on commit a67c379

Please sign in to comment.