Feature: add provider proxies API (#2668)

This commit is contained in:
yaling888 2023-04-22 19:16:51 +08:00 committed by GitHub
parent 63770b328f
commit c5fe5235f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 2 deletions

View File

@ -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))
})
@ -75,3 +88,24 @@ func findProviderByName(next http.Handler) http.Handler {
next.ServeHTTP(w, r.WithContext(ctx))
})
}
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))
})
}