feat: add provider proxies api
This commit is contained in:
parent
0207a7ac96
commit
34f62a0919
1 changed files with 36 additions and 2 deletions
|
@ -4,22 +4,35 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/constant/provider"
|
"github.com/Dreamacro/clash/constant/provider"
|
||||||
"github.com/Dreamacro/clash/tunnel"
|
"github.com/Dreamacro/clash/tunnel"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/go-chi/render"
|
"github.com/go-chi/render"
|
||||||
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func proxyProviderRouter() http.Handler {
|
func proxyProviderRouter() http.Handler {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Get("/", getProviders)
|
r.Get("/", getProviders)
|
||||||
|
|
||||||
r.Route("/{name}", func(r chi.Router) {
|
r.Route("/{providerName}", func(r chi.Router) {
|
||||||
r.Use(parseProviderName, findProviderByName)
|
r.Use(parseProviderName, findProviderByName)
|
||||||
r.Get("/", getProvider)
|
r.Get("/", getProvider)
|
||||||
r.Put("/", updateProvider)
|
r.Put("/", updateProvider)
|
||||||
r.Get("/healthcheck", healthCheckProvider)
|
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
|
return r
|
||||||
}
|
}
|
||||||
|
@ -54,7 +67,7 @@ func healthCheckProvider(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func parseProviderName(next http.Handler) http.Handler {
|
func parseProviderName(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
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)
|
ctx := context.WithValue(r.Context(), CtxKeyProviderName, name)
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
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 {
|
func ruleProviderRouter() http.Handler {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Get("/", getRuleProviders)
|
r.Get("/", getRuleProviders)
|
||||||
|
|
Loading…
Reference in a new issue