2023-04-11 16:43:45 +08:00
|
|
|
package clashapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2023-04-13 09:03:08 +08:00
|
|
|
"strings"
|
2023-04-11 16:43:45 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
"github.com/sagernet/sing-box/common/urltest"
|
2024-11-02 00:39:02 +08:00
|
|
|
"github.com/sagernet/sing-box/protocol/group"
|
2023-04-11 16:43:45 +08:00
|
|
|
"github.com/sagernet/sing/common"
|
|
|
|
"github.com/sagernet/sing/common/batch"
|
2023-12-01 20:15:11 +08:00
|
|
|
"github.com/sagernet/sing/common/json/badjson"
|
2023-04-11 16:43:45 +08:00
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
func groupRouter(server *Server) http.Handler {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Get("/", getGroups(server))
|
|
|
|
r.Route("/{name}", func(r chi.Router) {
|
2024-11-09 21:16:11 +08:00
|
|
|
r.Use(parseProxyName, findProxyByName(server))
|
2023-04-11 16:43:45 +08:00
|
|
|
r.Get("/", getGroup(server))
|
|
|
|
r.Get("/delay", getGroupDelay(server))
|
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGroups(server *Server) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-11-09 21:16:11 +08:00
|
|
|
groups := common.Map(common.Filter(server.outboundManager.Outbounds(), func(it adapter.Outbound) bool {
|
2023-04-11 16:43:45 +08:00
|
|
|
_, isGroup := it.(adapter.OutboundGroup)
|
|
|
|
return isGroup
|
|
|
|
}), func(it adapter.Outbound) *badjson.JSONObject {
|
|
|
|
return proxyInfo(server, it)
|
|
|
|
})
|
|
|
|
render.JSON(w, r, render.M{
|
|
|
|
"proxies": groups,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGroup(server *Server) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
proxy := r.Context().Value(CtxKeyProxy).(adapter.Outbound)
|
|
|
|
if _, ok := proxy.(adapter.OutboundGroup); ok {
|
|
|
|
render.JSON(w, r, proxyInfo(server, proxy))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
render.Status(r, http.StatusNotFound)
|
|
|
|
render.JSON(w, r, ErrNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGroupDelay(server *Server) func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
proxy := r.Context().Value(CtxKeyProxy).(adapter.Outbound)
|
2024-11-02 00:39:02 +08:00
|
|
|
outboundGroup, ok := proxy.(adapter.OutboundGroup)
|
2023-04-11 16:43:45 +08:00
|
|
|
if !ok {
|
|
|
|
render.Status(r, http.StatusNotFound)
|
|
|
|
render.JSON(w, r, ErrNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
query := r.URL.Query()
|
|
|
|
url := query.Get("url")
|
2023-04-13 09:03:08 +08:00
|
|
|
if strings.HasPrefix(url, "http://") {
|
|
|
|
url = ""
|
|
|
|
}
|
2023-04-11 16:43:45 +08:00
|
|
|
timeout, err := strconv.ParseInt(query.Get("timeout"), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
render.Status(r, http.StatusBadRequest)
|
|
|
|
render.JSON(w, r, ErrBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(r.Context(), time.Millisecond*time.Duration(timeout))
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var result map[string]uint16
|
2024-11-02 00:39:02 +08:00
|
|
|
if urlTestGroup, isURLTestGroup := outboundGroup.(adapter.URLTestGroup); isURLTestGroup {
|
2023-12-02 17:47:57 +08:00
|
|
|
result, err = urlTestGroup.URLTest(ctx)
|
2023-04-11 16:43:45 +08:00
|
|
|
} else {
|
2024-11-02 00:39:02 +08:00
|
|
|
outbounds := common.FilterNotNil(common.Map(outboundGroup.All(), func(it string) adapter.Outbound {
|
2024-11-09 21:16:11 +08:00
|
|
|
itOutbound, _ := server.outboundManager.Outbound(it)
|
2023-04-11 16:43:45 +08:00
|
|
|
return itOutbound
|
|
|
|
}))
|
|
|
|
b, _ := batch.New(ctx, batch.WithConcurrencyNum[any](10))
|
|
|
|
checked := make(map[string]bool)
|
|
|
|
result = make(map[string]uint16)
|
|
|
|
var resultAccess sync.Mutex
|
|
|
|
for _, detour := range outbounds {
|
|
|
|
tag := detour.Tag()
|
2024-11-02 00:39:02 +08:00
|
|
|
realTag := group.RealTag(detour)
|
2023-04-11 16:43:45 +08:00
|
|
|
if checked[realTag] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
checked[realTag] = true
|
2024-11-09 21:16:11 +08:00
|
|
|
p, loaded := server.outboundManager.Outbound(realTag)
|
2023-04-11 16:43:45 +08:00
|
|
|
if !loaded {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b.Go(realTag, func() (any, error) {
|
|
|
|
t, err := urltest.URLTest(ctx, url, p)
|
|
|
|
if err != nil {
|
|
|
|
server.logger.Debug("outbound ", tag, " unavailable: ", err)
|
|
|
|
server.urlTestHistory.DeleteURLTestHistory(realTag)
|
|
|
|
} else {
|
|
|
|
server.logger.Debug("outbound ", tag, " available: ", t, "ms")
|
|
|
|
server.urlTestHistory.StoreURLTestHistory(realTag, &urltest.History{
|
|
|
|
Time: time.Now(),
|
|
|
|
Delay: t,
|
|
|
|
})
|
|
|
|
resultAccess.Lock()
|
|
|
|
result[tag] = t
|
|
|
|
resultAccess.Unlock()
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
b.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
render.Status(r, http.StatusGatewayTimeout)
|
|
|
|
render.JSON(w, r, newError(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
render.JSON(w, r, result)
|
|
|
|
}
|
|
|
|
}
|