2018-11-21 05:47:46 +00:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
A "github.com/Dreamacro/clash/adapters/outbound"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
T "github.com/Dreamacro/clash/tunnel"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
func proxyRouter() http.Handler {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Get("/", getProxies)
|
|
|
|
|
|
|
|
r.Route("/{name}", func(r chi.Router) {
|
|
|
|
r.Use(parseProxyName, findProxyByName)
|
|
|
|
r.Get("/", getProxy)
|
|
|
|
r.Get("/delay", getProxyDelay)
|
|
|
|
r.Put("/", updateProxy)
|
|
|
|
})
|
|
|
|
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(), CtxKeyProxyName, name)
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func findProxyByName(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
name := r.Context().Value(CtxKeyProxyName).(string)
|
|
|
|
proxies := T.Instance().Proxies()
|
|
|
|
proxy, exist := proxies[name]
|
|
|
|
if !exist {
|
2018-12-10 03:33:37 +00:00
|
|
|
render.Status(r, http.StatusNotFound)
|
|
|
|
render.JSON(w, r, ErrNotFound)
|
2018-11-21 05:47:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.WithValue(r.Context(), CtxKeyProxy, proxy)
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProxies(w http.ResponseWriter, r *http.Request) {
|
|
|
|
proxies := T.Instance().Proxies()
|
2019-02-13 15:45:43 +00:00
|
|
|
render.JSON(w, r, render.M{
|
2018-11-21 05:47:46 +00:00
|
|
|
"proxies": proxies,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProxy(w http.ResponseWriter, r *http.Request) {
|
|
|
|
proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
|
2018-12-10 03:33:37 +00:00
|
|
|
render.JSON(w, r, proxy)
|
2018-11-21 05:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateProxyRequest struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateProxy(w http.ResponseWriter, r *http.Request) {
|
|
|
|
req := UpdateProxyRequest{}
|
|
|
|
if err := render.DecodeJSON(r.Body, &req); err != nil {
|
2018-12-10 03:33:37 +00:00
|
|
|
render.Status(r, http.StatusBadRequest)
|
|
|
|
render.JSON(w, r, ErrBadRequest)
|
2018-11-21 05:47:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-15 16:43:16 +00:00
|
|
|
proxy := r.Context().Value(CtxKeyProxy).(*A.Proxy)
|
|
|
|
selector, ok := proxy.ProxyAdapter.(*A.Selector)
|
2018-11-21 05:47:46 +00:00
|
|
|
if !ok {
|
2018-12-10 03:33:37 +00:00
|
|
|
render.Status(r, http.StatusBadRequest)
|
2019-03-15 16:43:16 +00:00
|
|
|
render.JSON(w, r, newError("Must be a Selector"))
|
2018-11-21 05:47:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := selector.Set(req.Name); err != nil {
|
2018-12-10 03:33:37 +00:00
|
|
|
render.Status(r, http.StatusBadRequest)
|
|
|
|
render.JSON(w, r, newError(fmt.Sprintf("Selector update error: %s", err.Error())))
|
2018-11-21 05:47:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-10 03:33:37 +00:00
|
|
|
render.NoContent(w, r)
|
2018-11-21 05:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getProxyDelay(w http.ResponseWriter, r *http.Request) {
|
|
|
|
query := r.URL.Query()
|
|
|
|
url := query.Get("url")
|
|
|
|
timeout, err := strconv.ParseInt(query.Get("timeout"), 10, 16)
|
|
|
|
if err != nil {
|
2018-12-10 03:33:37 +00:00
|
|
|
render.Status(r, http.StatusBadRequest)
|
|
|
|
render.JSON(w, r, ErrBadRequest)
|
2018-11-21 05:47:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
|
|
|
|
|
2019-03-17 06:52:39 +00:00
|
|
|
sigCh := make(chan uint16)
|
2018-11-21 05:47:46 +00:00
|
|
|
go func() {
|
2019-03-15 16:43:16 +00:00
|
|
|
t, err := proxy.URLTest(url)
|
2018-11-21 05:47:46 +00:00
|
|
|
if err != nil {
|
|
|
|
sigCh <- 0
|
|
|
|
}
|
|
|
|
sigCh <- t
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Millisecond * time.Duration(timeout)):
|
2018-12-10 03:33:37 +00:00
|
|
|
render.Status(r, http.StatusRequestTimeout)
|
|
|
|
render.JSON(w, r, ErrRequestTimeout)
|
2018-11-21 05:47:46 +00:00
|
|
|
case t := <-sigCh:
|
|
|
|
if t == 0 {
|
2018-12-10 03:33:37 +00:00
|
|
|
render.Status(r, http.StatusServiceUnavailable)
|
|
|
|
render.JSON(w, r, newError("An error occurred in the delay test"))
|
2018-11-21 05:47:46 +00:00
|
|
|
} else {
|
2019-02-13 15:45:43 +00:00
|
|
|
render.JSON(w, r, render.M{
|
2018-11-21 05:47:46 +00:00
|
|
|
"delay": t,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|