mihomo/hub/route/provider.go

78 lines
2 KiB
Go
Raw Normal View History

package route
import (
"context"
"net/http"
"github.com/Dreamacro/clash/adapters/provider"
"github.com/Dreamacro/clash/tunnel"
2021-04-03 14:59:03 +08:00
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)
func proxyProviderRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", getProviders)
r.Route("/{name}", func(r chi.Router) {
r.Use(parseProviderName, findProviderByName)
r.Get("/", getProvider)
2019-12-13 00:29:24 +08:00
r.Put("/", updateProvider)
r.Get("/healthcheck", healthCheckProvider)
})
return r
}
func getProviders(w http.ResponseWriter, r *http.Request) {
providers := tunnel.Providers()
render.JSON(w, r, render.M{
"providers": providers,
})
}
func getProvider(w http.ResponseWriter, r *http.Request) {
provider := r.Context().Value(CtxKeyProvider).(provider.ProxyProvider)
render.JSON(w, r, provider)
}
2019-12-13 00:29:24 +08:00
func updateProvider(w http.ResponseWriter, r *http.Request) {
provider := r.Context().Value(CtxKeyProvider).(provider.ProxyProvider)
if err := provider.Update(); err != nil {
render.Status(r, http.StatusServiceUnavailable)
render.JSON(w, r, newError(err.Error()))
return
}
render.NoContent(w, r)
}
func healthCheckProvider(w http.ResponseWriter, r *http.Request) {
provider := r.Context().Value(CtxKeyProvider).(provider.ProxyProvider)
provider.HealthCheck()
render.NoContent(w, r)
}
func parseProviderName(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := getEscapeParam(r, "name")
ctx := context.WithValue(r.Context(), CtxKeyProviderName, name)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func findProviderByName(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := r.Context().Value(CtxKeyProviderName).(string)
providers := tunnel.Providers()
provider, exist := providers[name]
if !exist {
render.Status(r, http.StatusNotFound)
render.JSON(w, r, ErrNotFound)
return
}
ctx := context.WithValue(r.Context(), CtxKeyProvider, provider)
next.ServeHTTP(w, r.WithContext(ctx))
})
}