mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 11:42:43 +08:00
feat: add provider proxies api
This commit is contained in:
parent
0207a7ac96
commit
34f62a0919
|
@ -4,22 +4,35 @@ import (
|
|||
"context"
|
||||
"net/http"
|
||||
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/constant/provider"
|
||||
"github.com/Dreamacro/clash/tunnel"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/render"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func proxyProviderRouter() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Get("/", getProviders)
|
||||
|
||||
r.Route("/{name}", func(r chi.Router) {
|
||||
r.Route("/{providerName}", func(r chi.Router) {
|
||||
r.Use(parseProviderName, findProviderByName)
|
||||
r.Get("/", getProvider)
|
||||
r.Put("/", updateProvider)
|
||||
r.Get("/healthcheck", healthCheckProvider)
|
||||
r.Mount("/", proxyProviderProxyRouter())
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
||||
func proxyProviderProxyRouter() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Route("/{name}", func(r chi.Router) {
|
||||
r.Use(parseProxyName, findProviderProxyByName)
|
||||
r.Get("/", getProxy)
|
||||
r.Get("/healthcheck", getProxyDelay)
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
@ -54,7 +67,7 @@ func healthCheckProvider(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
func parseProviderName(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
name := getEscapeParam(r, "name")
|
||||
name := getEscapeParam(r, "providerName")
|
||||
ctx := context.WithValue(r.Context(), CtxKeyProviderName, name)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
|
@ -76,6 +89,27 @@ func findProviderByName(next http.Handler) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func findProviderProxyByName(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
name = r.Context().Value(CtxKeyProxyName).(string)
|
||||
pd = r.Context().Value(CtxKeyProvider).(provider.ProxyProvider)
|
||||
)
|
||||
proxy, exist := lo.Find(pd.Proxies(), func(proxy C.Proxy) bool {
|
||||
return proxy.Name() == name
|
||||
})
|
||||
|
||||
if !exist {
|
||||
render.Status(r, http.StatusNotFound)
|
||||
render.JSON(w, r, ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.WithValue(r.Context(), CtxKeyProxy, proxy)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func ruleProviderRouter() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Get("/", getRuleProviders)
|
||||
|
|
Loading…
Reference in New Issue
Block a user