mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-02-23 00:43:15 +08:00
Fix: unescape proxy name in /proixes
This commit is contained in:
parent
990bba4a05
commit
e12d46f619
@ -1,8 +1,10 @@
|
|||||||
package hub
|
package hub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -16,12 +18,24 @@ import (
|
|||||||
func proxyRouter() http.Handler {
|
func proxyRouter() http.Handler {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Get("/", getProxies)
|
r.Get("/", getProxies)
|
||||||
r.Get("/{name}", getProxy)
|
r.With(parseProxyName).Get("/{name}", getProxy)
|
||||||
r.Get("/{name}/delay", getProxyDelay)
|
r.With(parseProxyName).Get("/{name}/delay", getProxyDelay)
|
||||||
r.Put("/{name}", updateProxy)
|
r.With(parseProxyName).Put("/{name}", updateProxy)
|
||||||
return r
|
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
|
||||||
|
}
|
||||||
|
ctx := context.WithValue(r.Context(), contextKey("proxy name"), name)
|
||||||
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
type SampleProxy struct {
|
type SampleProxy struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
}
|
}
|
||||||
@ -83,7 +97,7 @@ func getProxies(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getProxy(w http.ResponseWriter, r *http.Request) {
|
func getProxy(w http.ResponseWriter, r *http.Request) {
|
||||||
name := chi.URLParam(r, "name")
|
name := r.Context().Value(contextKey("proxy name")).(string)
|
||||||
proxies := cfg.Proxies()
|
proxies := cfg.Proxies()
|
||||||
proxy, exist := proxies[name]
|
proxy, exist := proxies[name]
|
||||||
if !exist {
|
if !exist {
|
||||||
@ -110,7 +124,7 @@ func updateProxy(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
name := chi.URLParam(r, "name")
|
name := r.Context().Value(contextKey("proxy name")).(string)
|
||||||
proxies := cfg.Proxies()
|
proxies := cfg.Proxies()
|
||||||
proxy, exist := proxies[name]
|
proxy, exist := proxies[name]
|
||||||
if !exist {
|
if !exist {
|
||||||
@ -162,7 +176,7 @@ func getProxyDelay(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
name := chi.URLParam(r, "name")
|
name := r.Context().Value(contextKey("proxy name")).(string)
|
||||||
proxies := cfg.Proxies()
|
proxies := cfg.Proxies()
|
||||||
proxy, exist := proxies[name]
|
proxy, exist := proxies[name]
|
||||||
if !exist {
|
if !exist {
|
||||||
|
@ -100,6 +100,12 @@ func authentication(next http.Handler) http.Handler {
|
|||||||
return http.HandlerFunc(fn)
|
return http.HandlerFunc(fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type contextKey string
|
||||||
|
|
||||||
|
func (c contextKey) String() string {
|
||||||
|
return "clash context key " + string(c)
|
||||||
|
}
|
||||||
|
|
||||||
func traffic(w http.ResponseWriter, r *http.Request) {
|
func traffic(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user