mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-19 08:20:05 +08:00
Some checks are pending
Test / test (1.20, macos-15-intel) (push) Waiting to run
Test / test (1.20, macos-latest) (push) Waiting to run
Test / test (1.20, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.20, ubuntu-latest) (push) Waiting to run
Test / test (1.20, windows-latest) (push) Waiting to run
Test / test (1.21, macos-15-intel) (push) Waiting to run
Test / test (1.21, macos-latest) (push) Waiting to run
Test / test (1.21, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.21, ubuntu-latest) (push) Waiting to run
Test / test (1.21, windows-latest) (push) Waiting to run
Test / test (1.22, macos-15-intel) (push) Waiting to run
Test / test (1.22, macos-latest) (push) Waiting to run
Test / test (1.22, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.22, ubuntu-latest) (push) Waiting to run
Test / test (1.22, windows-latest) (push) Waiting to run
Test / test (1.23, macos-15-intel) (push) Waiting to run
Test / test (1.23, macos-latest) (push) Waiting to run
Test / test (1.23, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.23, ubuntu-latest) (push) Waiting to run
Test / test (1.23, windows-latest) (push) Waiting to run
Test / test (1.24, macos-15-intel) (push) Waiting to run
Test / test (1.24, macos-latest) (push) Waiting to run
Test / test (1.24, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.24, ubuntu-latest) (push) Waiting to run
Test / test (1.24, windows-latest) (push) Waiting to run
Test / test (1.25, macos-15-intel) (push) Waiting to run
Test / test (1.25, macos-latest) (push) Waiting to run
Test / test (1.25, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.25, ubuntu-latest) (push) Waiting to run
Test / test (1.25, windows-latest) (push) Waiting to run
Trigger CMFA Update / trigger-CMFA-update (push) Waiting to run
95 lines
2.2 KiB
Go
95 lines
2.2 KiB
Go
package route
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/render"
|
|
|
|
"github.com/metacubex/mihomo/adapter/outboundgroup"
|
|
"github.com/metacubex/mihomo/common/utils"
|
|
"github.com/metacubex/mihomo/component/profile/cachefile"
|
|
C "github.com/metacubex/mihomo/constant"
|
|
"github.com/metacubex/mihomo/tunnel"
|
|
)
|
|
|
|
func groupRouter() http.Handler {
|
|
r := chi.NewRouter()
|
|
r.Get("/", getGroups)
|
|
|
|
r.Route("/{name}", func(r chi.Router) {
|
|
r.Use(parseProxyName, findProxyByName)
|
|
r.Get("/", getGroup)
|
|
r.Get("/delay", getGroupDelay)
|
|
})
|
|
return r
|
|
}
|
|
|
|
func getGroups(w http.ResponseWriter, r *http.Request) {
|
|
var gs []C.Proxy
|
|
for _, p := range tunnel.Proxies() {
|
|
if _, ok := p.Adapter().(outboundgroup.ProxyGroup); ok {
|
|
gs = append(gs, p)
|
|
}
|
|
}
|
|
render.JSON(w, r, render.M{
|
|
"proxies": gs,
|
|
})
|
|
}
|
|
|
|
func getGroup(w http.ResponseWriter, r *http.Request) {
|
|
proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
|
|
if _, ok := proxy.Adapter().(outboundgroup.ProxyGroup); ok {
|
|
render.JSON(w, r, proxy)
|
|
return
|
|
}
|
|
render.Status(r, http.StatusNotFound)
|
|
render.JSON(w, r, ErrNotFound)
|
|
}
|
|
|
|
func getGroupDelay(w http.ResponseWriter, r *http.Request) {
|
|
proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
|
|
group, ok := proxy.Adapter().(outboundgroup.ProxyGroup)
|
|
if !ok {
|
|
render.Status(r, http.StatusNotFound)
|
|
render.JSON(w, r, ErrNotFound)
|
|
return
|
|
}
|
|
|
|
if selectAble, ok := proxy.Adapter().(outboundgroup.SelectAble); ok && proxy.Type() != C.Selector {
|
|
selectAble.ForceSet("")
|
|
cachefile.Cache().SetSelected(proxy.Name(), "")
|
|
}
|
|
|
|
query := r.URL.Query()
|
|
url := query.Get("url")
|
|
timeout, err := strconv.ParseInt(query.Get("timeout"), 10, 32)
|
|
if err != nil {
|
|
render.Status(r, http.StatusBadRequest)
|
|
render.JSON(w, r, ErrBadRequest)
|
|
return
|
|
}
|
|
|
|
expectedStatus, err := utils.NewUnsignedRanges[uint16](query.Get("expected"))
|
|
if err != nil {
|
|
render.Status(r, http.StatusBadRequest)
|
|
render.JSON(w, r, ErrBadRequest)
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(r.Context(), time.Millisecond*time.Duration(timeout))
|
|
defer cancel()
|
|
|
|
dm, err := group.URLTest(ctx, url, expectedStatus)
|
|
if err != nil {
|
|
render.Status(r, http.StatusGatewayTimeout)
|
|
render.JSON(w, r, newError(err.Error()))
|
|
return
|
|
}
|
|
|
|
render.JSON(w, r, dm)
|
|
}
|