Add selector outbound
This commit is contained in:
@@ -2,20 +2,33 @@ package clashapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/common/badjson"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/outbound"
|
||||
"github.com/sagernet/sing/common"
|
||||
F "github.com/sagernet/sing/common/format"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/render"
|
||||
)
|
||||
|
||||
func proxyRouter() http.Handler {
|
||||
func proxyRouter(server *Server, router adapter.Router) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Get("/", getProxies)
|
||||
r.Get("/", getProxies(server, router))
|
||||
|
||||
r.Route("/{name}", func(r chi.Router) {
|
||||
r.Use(parseProxyName, findProxyByName)
|
||||
r.Get("/", getProxy)
|
||||
r.Get("/delay", getProxyDelay)
|
||||
r.Use(parseProxyName, findProxyByName(router))
|
||||
r.Get("/", getProxy(server))
|
||||
r.Get("/delay", getProxyDelay(server))
|
||||
r.Put("/", updateProxy)
|
||||
})
|
||||
return r
|
||||
@@ -29,33 +42,123 @@ func parseProxyName(next http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func findProxyByName(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
/*name := r.Context().Value(CtxKeyProxyName).(string)
|
||||
proxies := tunnel.Proxies()
|
||||
proxy, exist := proxies[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))
|
||||
})
|
||||
func findProxyByName(router adapter.Router) func(next http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
name := r.Context().Value(CtxKeyProxyName).(string)
|
||||
proxy, exist := router.Outbound(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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func getProxies(w http.ResponseWriter, r *http.Request) {
|
||||
// proxies := tunnel.Proxies()
|
||||
render.JSON(w, r, render.M{
|
||||
"proxies": []string{},
|
||||
})
|
||||
func proxyInfo(server *Server, detour adapter.Outbound) *badjson.JSONObject {
|
||||
var info badjson.JSONObject
|
||||
var clashType string
|
||||
var isSelector bool
|
||||
switch detour.Type() {
|
||||
case C.TypeDirect:
|
||||
clashType = "Direct"
|
||||
case C.TypeBlock:
|
||||
clashType = "Reject"
|
||||
case C.TypeSocks:
|
||||
clashType = "Socks"
|
||||
case C.TypeHTTP:
|
||||
clashType = "Http"
|
||||
case C.TypeShadowsocks:
|
||||
clashType = "Shadowsocks"
|
||||
case C.TypeVMess:
|
||||
clashType = "Vmess"
|
||||
case C.TypeSelector:
|
||||
clashType = "Selector"
|
||||
isSelector = true
|
||||
default:
|
||||
clashType = "Unknown"
|
||||
}
|
||||
info.Put("type", clashType)
|
||||
info.Put("name", detour.Tag())
|
||||
info.Put("udp", common.Contains(detour.Network(), C.NetworkUDP))
|
||||
|
||||
delayHistory, loaded := server.delayHistory[detour.Tag()]
|
||||
if loaded {
|
||||
info.Put("history", []*DelayHistory{delayHistory, delayHistory})
|
||||
} else {
|
||||
info.Put("history", []*DelayHistory{{Time: time.Now()}, {Time: time.Now()}})
|
||||
}
|
||||
|
||||
if isSelector {
|
||||
selector := detour.(*outbound.Selector)
|
||||
info.Put("now", selector.Now())
|
||||
info.Put("all", selector.All())
|
||||
}
|
||||
return &info
|
||||
}
|
||||
|
||||
func getProxy(w http.ResponseWriter, r *http.Request) {
|
||||
/* proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
|
||||
render.JSON(w, r, proxy)*/
|
||||
render.Status(r, http.StatusServiceUnavailable)
|
||||
func getProxies(server *Server, router adapter.Router) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var proxyMap badjson.JSONObject
|
||||
|
||||
// fix clash dashboard
|
||||
proxyMap.Put("DIRECT", map[string]any{
|
||||
"type": "Direct",
|
||||
"name": "DIRECT",
|
||||
"udp": true,
|
||||
"history": []*DelayHistory{},
|
||||
})
|
||||
proxyMap.Put("GLOBAL", map[string]any{
|
||||
"type": "Selector",
|
||||
"name": "GLOBAL",
|
||||
"udp": true,
|
||||
"history": []*DelayHistory{},
|
||||
"all": []string{},
|
||||
"now": "",
|
||||
})
|
||||
proxyMap.Put("REJECT", map[string]any{
|
||||
"type": "Reject",
|
||||
"name": "REJECT",
|
||||
"udp": true,
|
||||
"history": []*DelayHistory{},
|
||||
})
|
||||
|
||||
outbounds := router.Outbounds()
|
||||
for i, detour := range outbounds {
|
||||
var tag string
|
||||
if detour.Tag() == "" {
|
||||
tag = F.ToString(i)
|
||||
} else {
|
||||
tag = detour.Tag()
|
||||
}
|
||||
proxyMap.Put(tag, proxyInfo(server, detour))
|
||||
}
|
||||
var responseMap badjson.JSONObject
|
||||
responseMap.Put("proxies", &proxyMap)
|
||||
response, err := responseMap.MarshalJSON()
|
||||
if err != nil {
|
||||
render.Status(r, http.StatusInternalServerError)
|
||||
render.JSON(w, r, newError(err.Error()))
|
||||
return
|
||||
}
|
||||
w.Write(response)
|
||||
}
|
||||
}
|
||||
|
||||
func getProxy(server *Server) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
proxy := r.Context().Value(CtxKeyProxy).(adapter.Outbound)
|
||||
response, err := proxyInfo(server, proxy).MarshalJSON()
|
||||
if err != nil {
|
||||
render.Status(r, http.StatusInternalServerError)
|
||||
render.JSON(w, r, newError(err.Error()))
|
||||
return
|
||||
}
|
||||
w.Write(response)
|
||||
}
|
||||
}
|
||||
|
||||
type UpdateProxyRequest struct {
|
||||
@@ -63,33 +166,33 @@ type UpdateProxyRequest struct {
|
||||
}
|
||||
|
||||
func updateProxy(w http.ResponseWriter, r *http.Request) {
|
||||
/* req := UpdateProxyRequest{}
|
||||
if err := render.DecodeJSON(r.Body, &req); err != nil {
|
||||
render.Status(r, http.StatusBadRequest)
|
||||
render.JSON(w, r, ErrBadRequest)
|
||||
return
|
||||
}
|
||||
req := UpdateProxyRequest{}
|
||||
if err := render.DecodeJSON(r.Body, &req); err != nil {
|
||||
render.Status(r, http.StatusBadRequest)
|
||||
render.JSON(w, r, ErrBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
proxy := r.Context().Value(CtxKeyProxy).(*adapter.Proxy)
|
||||
selector, ok := proxy.ProxyAdapter.(*outboundgroup.Selector)
|
||||
if !ok {
|
||||
render.Status(r, http.StatusBadRequest)
|
||||
render.JSON(w, r, newError("Must be a Selector"))
|
||||
return
|
||||
}
|
||||
proxy := r.Context().Value(CtxKeyProxy).(adapter.Outbound)
|
||||
selector, ok := proxy.(*outbound.Selector)
|
||||
if !ok {
|
||||
render.Status(r, http.StatusBadRequest)
|
||||
render.JSON(w, r, newError("Must be a Selector"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := selector.Set(req.Name); err != nil {
|
||||
render.Status(r, http.StatusBadRequest)
|
||||
render.JSON(w, r, newError(fmt.Sprintf("Selector update error: %s", err.Error())))
|
||||
return
|
||||
}
|
||||
if !selector.SelectOutbound(req.Name) {
|
||||
render.Status(r, http.StatusBadRequest)
|
||||
render.JSON(w, r, newError(fmt.Sprintf("Selector update error: not found")))
|
||||
return
|
||||
}
|
||||
|
||||
cachefile.Cache().SetSelected(proxy.Name(), req.Name)*/
|
||||
render.NoContent(w, r)
|
||||
}
|
||||
|
||||
func getProxyDelay(w http.ResponseWriter, r *http.Request) {
|
||||
/* query := r.URL.Query()
|
||||
func getProxyDelay(server *Server) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(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 {
|
||||
@@ -98,12 +201,11 @@ func getProxyDelay(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
proxy := r.Context().Value(CtxKeyProxy).(C.Proxy)
|
||||
|
||||
proxy := r.Context().Value(CtxKeyProxy).(adapter.Outbound)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(timeout))
|
||||
defer cancel()
|
||||
|
||||
delay, err := proxy.URLTest(ctx, url)
|
||||
delay, err := URLTest(ctx, url, proxy)
|
||||
if ctx.Err() != nil {
|
||||
render.Status(r, http.StatusGatewayTimeout)
|
||||
render.JSON(w, r, ErrRequestTimeout)
|
||||
@@ -115,8 +217,71 @@ func getProxyDelay(w http.ResponseWriter, r *http.Request) {
|
||||
render.JSON(w, r, newError("An error occurred in the delay test"))
|
||||
return
|
||||
}
|
||||
*/
|
||||
render.JSON(w, r, render.M{
|
||||
"delay": 114514,
|
||||
})
|
||||
|
||||
server.delayHistory[proxy.Tag()] = &DelayHistory{
|
||||
Time: time.Now(),
|
||||
Delay: delay,
|
||||
}
|
||||
|
||||
render.JSON(w, r, render.M{
|
||||
"delay": delay,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func URLTest(ctx context.Context, link string, detour adapter.Outbound) (t uint16, err error) {
|
||||
linkURL, err := url.Parse(link)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
hostname := linkURL.Hostname()
|
||||
port := linkURL.Port()
|
||||
if port == "" {
|
||||
switch linkURL.Scheme {
|
||||
case "http":
|
||||
port = "80"
|
||||
case "https":
|
||||
port = "443"
|
||||
}
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
instance, err := detour.DialContext(ctx, "tcp", M.ParseSocksaddrHostPortStr(hostname, port))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer instance.Close()
|
||||
|
||||
req, err := http.NewRequest(http.MethodHead, link, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
transport := &http.Transport{
|
||||
Dial: func(string, string) (net.Conn, error) {
|
||||
return instance, nil
|
||||
},
|
||||
// from http.DefaultTransport
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
|
||||
client := http.Client{
|
||||
Transport: transport,
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
}
|
||||
defer client.CloseIdleConnections()
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
resp.Body.Close()
|
||||
t = uint16(time.Since(start) / time.Millisecond)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user