Feature: add basic api for proxy provider

This commit is contained in:
Dreamacro
2019-12-11 17:31:15 +08:00
parent 29cf3ca0ef
commit 95e9ae2d8d
6 changed files with 129 additions and 10 deletions

17
hub/route/common.go Normal file
View File

@ -0,0 +1,17 @@
package route
import (
"net/http"
"net/url"
"github.com/go-chi/chi"
)
// When name is composed of a partial escape string, Golang does not unescape it
func getEscapeParam(r *http.Request, paramName string) string {
param := chi.URLParam(r, paramName)
if newParam, err := url.PathUnescape(param); err == nil {
param = newParam
}
return param
}

View File

@ -1,8 +1,10 @@
package route
var (
CtxKeyProxyName = contextKey("proxy name")
CtxKeyProxy = contextKey("proxy")
CtxKeyProxyName = contextKey("proxy name")
CtxKeyProviderName = contextKey("provider name")
CtxKeyProxy = contextKey("proxy")
CtxKeyProvider = contextKey("provider")
)
type contextKey string

66
hub/route/provider.go Normal file
View File

@ -0,0 +1,66 @@
package route
import (
"context"
"net/http"
"github.com/Dreamacro/clash/adapters/provider"
T "github.com/Dreamacro/clash/tunnel"
"github.com/go-chi/chi"
"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)
r.Get("/healthcheck", doProviderHealthCheck)
})
return r
}
func getProviders(w http.ResponseWriter, r *http.Request) {
providers := T.Instance().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)
}
func doProviderHealthCheck(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 := T.Instance().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))
})
}

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
@ -30,13 +29,9 @@ func proxyRouter() http.Handler {
return r
}
// When name is composed of a partial escape string, Golang does not unescape it
func parseProxyName(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
if newName, err := url.PathUnescape(name); err == nil {
name = newName
}
name := getEscapeParam(r, "name")
ctx := context.WithValue(r.Context(), CtxKeyProxyName, name)
next.ServeHTTP(w, r.WithContext(ctx))
})

View File

@ -68,6 +68,7 @@ func Start(addr string, secret string) {
r.Mount("/proxies", proxyRouter())
r.Mount("/rules", ruleRouter())
r.Mount("/connections", connectionRouter())
r.Mount("/providers/proxies", proxyProviderRouter())
})
if uiPath != "" {