2019-12-11 17:31:15 +08:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2023-04-22 19:16:51 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-07-04 20:32:59 +08:00
|
|
|
"github.com/Dreamacro/clash/constant/provider"
|
2020-02-15 21:42:46 +08:00
|
|
|
"github.com/Dreamacro/clash/tunnel"
|
2019-12-11 17:31:15 +08:00
|
|
|
|
2021-04-03 14:59:03 +08:00
|
|
|
"github.com/go-chi/chi/v5"
|
2019-12-11 17:31:15 +08:00
|
|
|
"github.com/go-chi/render"
|
2023-04-22 19:16:51 +08:00
|
|
|
"github.com/samber/lo"
|
2019-12-11 17:31:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func proxyProviderRouter() http.Handler {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Get("/", getProviders)
|
|
|
|
|
2023-04-22 19:16:51 +08:00
|
|
|
r.Route("/{providerName}", func(r chi.Router) {
|
2019-12-11 17:31:15 +08:00
|
|
|
r.Use(parseProviderName, findProviderByName)
|
|
|
|
r.Get("/", getProvider)
|
2019-12-13 00:29:24 +08:00
|
|
|
r.Put("/", updateProvider)
|
|
|
|
r.Get("/healthcheck", healthCheckProvider)
|
2023-04-22 19:16:51 +08:00
|
|
|
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)
|
2019-12-11 17:31:15 +08:00
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProviders(w http.ResponseWriter, r *http.Request) {
|
2020-02-15 21:42:46 +08:00
|
|
|
providers := tunnel.Providers()
|
2019-12-11 17:31:15 +08:00
|
|
|
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) {
|
2019-12-11 17:31:15 +08:00
|
|
|
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) {
|
2023-04-22 19:16:51 +08:00
|
|
|
name := getEscapeParam(r, "providerName")
|
2019-12-11 17:31:15 +08:00
|
|
|
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)
|
2020-02-15 21:42:46 +08:00
|
|
|
providers := tunnel.Providers()
|
2019-12-11 17:31:15 +08:00
|
|
|
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))
|
|
|
|
})
|
|
|
|
}
|
2023-04-22 19:16:51 +08:00
|
|
|
|
|
|
|
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))
|
|
|
|
})
|
|
|
|
}
|