mihomo/hub/route/proxies.go

149 lines
3.7 KiB
Go
Raw Permalink Normal View History

2018-11-21 13:47:46 +08:00
package route
import (
"context"
"fmt"
"net/http"
"strconv"
"time"
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/adapter"
"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"
2018-11-21 13:47:46 +08:00
2021-04-03 14:59:03 +08:00
"github.com/go-chi/chi/v5"
2018-11-21 13:47:46 +08:00
"github.com/go-chi/render"
)
2021-11-16 20:08:52 +08:00
var (
SwitchProxiesCallback func(sGroup string, sProxy string)
)
2018-11-21 13:47:46 +08:00
func proxyRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", getProxies)
r.Route("/{name}", func(r chi.Router) {
r.Use(parseProxyName, findProxyByName)
r.Get("/", getProxy)
r.Get("/delay", getProxyDelay)
r.Put("/", updateProxy)
})
return r
}
func parseProxyName(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := getEscapeParam(r, "name")
2018-11-21 13:47:46 +08:00
ctx := context.WithValue(r.Context(), CtxKeyProxyName, name)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func findProxyByName(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := r.Context().Value(CtxKeyProxyName).(string)
proxies := tunnel.ProxiesWithProviders()
2018-11-21 13:47:46 +08:00
proxy, exist := proxies[name]
if !exist {
2018-12-10 11:33:37 +08:00
render.Status(r, http.StatusNotFound)
render.JSON(w, r, ErrNotFound)
2018-11-21 13:47:46 +08:00
return
}
ctx := context.WithValue(r.Context(), CtxKeyProxy, proxy)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func getProxies(w http.ResponseWriter, r *http.Request) {
proxies := tunnel.ProxiesWithProviders()
2019-02-13 23:45:43 +08:00
render.JSON(w, r, render.M{
2018-11-21 13:47:46 +08:00
"proxies": proxies,
})
}
func getProxy(w http.ResponseWriter, r *http.Request) {
proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
2018-12-10 11:33:37 +08:00
render.JSON(w, r, proxy)
2018-11-21 13:47:46 +08:00
}
func updateProxy(w http.ResponseWriter, r *http.Request) {
2023-01-16 15:20:39 +08:00
req := struct {
Name string `json:"name"`
}{}
2018-11-21 13:47:46 +08:00
if err := render.DecodeJSON(r.Body, &req); err != nil {
2018-12-10 11:33:37 +08:00
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, ErrBadRequest)
2018-11-21 13:47:46 +08:00
return
}
2021-06-10 14:05:56 +08:00
proxy := r.Context().Value(CtxKeyProxy).(*adapter.Proxy)
2022-05-23 00:40:27 +08:00
selector, ok := proxy.ProxyAdapter.(outboundgroup.SelectAble)
2018-11-21 13:47:46 +08:00
if !ok {
2018-12-10 11:33:37 +08:00
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, newError("Must be a Selector"))
2018-11-21 13:47:46 +08:00
return
}
if err := selector.Set(req.Name); err != nil {
2018-12-10 11:33:37 +08:00
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, newError(fmt.Sprintf("Selector update error: %s", err.Error())))
2018-11-21 13:47:46 +08:00
return
}
cachefile.Cache().SetSelected(proxy.Name(), req.Name)
2021-11-21 16:57:22 +08:00
if SwitchProxiesCallback != nil {
// refresh tray menu
go SwitchProxiesCallback(proxy.Name(), req.Name)
}
2018-12-10 11:33:37 +08:00
render.NoContent(w, r)
2018-11-21 13:47:46 +08:00
}
func getProxyDelay(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
url := query.Get("url")
timeout, err := strconv.ParseInt(query.Get("timeout"), 10, 16)
if err != nil {
2018-12-10 11:33:37 +08:00
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, ErrBadRequest)
2018-11-21 13:47:46 +08:00
return
}
expectedStatus, err := utils.NewIntRanges[uint16](query.Get("expected"))
if err != nil {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, ErrBadRequest)
return
}
2018-11-21 13:47:46 +08:00
proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(timeout))
2019-07-02 19:18:03 +08:00
defer cancel()
2018-11-21 13:47:46 +08:00
delay, err := proxy.URLTest(ctx, url, expectedStatus)
if ctx.Err() != nil {
2019-09-08 22:33:52 +08:00
render.Status(r, http.StatusGatewayTimeout)
2018-12-10 11:33:37 +08:00
render.JSON(w, r, ErrRequestTimeout)
2019-07-02 19:18:03 +08:00
return
}
if err != nil || delay == 0 {
2019-07-02 19:18:03 +08:00
render.Status(r, http.StatusServiceUnavailable)
if err != nil && delay != 0 {
render.JSON(w, r, err)
} else {
render.JSON(w, r, newError("An error occurred in the delay test"))
}
2019-07-02 19:18:03 +08:00
return
2018-11-21 13:47:46 +08:00
}
2019-07-02 19:18:03 +08:00
render.JSON(w, r, render.M{
"delay": delay,
})
2018-11-21 13:47:46 +08:00
}