sing-box/experimental/clashapi/proxies.go

235 lines
6.2 KiB
Go
Raw Normal View History

2022-07-19 22:16:49 +08:00
package clashapi
import (
"context"
"net/http"
2022-07-22 13:51:08 +08:00
"sort"
2022-07-21 21:03:41 +08:00
"strconv"
2023-04-13 09:03:08 +08:00
"strings"
2022-07-21 21:03:41 +08:00
"time"
"github.com/sagernet/sing-box/adapter"
2022-07-22 13:51:08 +08:00
"github.com/sagernet/sing-box/common/urltest"
2022-07-21 21:03:41 +08:00
C "github.com/sagernet/sing-box/constant"
2024-11-02 00:39:02 +08:00
"github.com/sagernet/sing-box/protocol/group"
2022-07-21 21:03:41 +08:00
"github.com/sagernet/sing/common"
F "github.com/sagernet/sing/common/format"
"github.com/sagernet/sing/common/json/badjson"
2022-07-30 00:29:22 +08:00
N "github.com/sagernet/sing/common/network"
2022-07-19 22:16:49 +08:00
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)
2022-07-21 21:03:41 +08:00
func proxyRouter(server *Server, router adapter.Router) http.Handler {
2022-07-19 22:16:49 +08:00
r := chi.NewRouter()
r.Get("/", getProxies(server))
2022-07-19 22:16:49 +08:00
r.Route("/{name}", func(r chi.Router) {
r.Use(parseProxyName, findProxyByName(server))
2022-07-21 21:03:41 +08:00
r.Get("/", getProxy(server))
r.Get("/delay", getProxyDelay(server))
2022-07-19 22:16:49 +08:00
r.Put("/", updateProxy)
})
return r
}
func parseProxyName(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := getEscapeParam(r, "name")
ctx := context.WithValue(r.Context(), CtxKeyProxyName, name)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func findProxyByName(server *Server) func(next http.Handler) http.Handler {
2022-07-21 21:03:41 +08:00
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := r.Context().Value(CtxKeyProxyName).(string)
2024-12-06 16:47:30 +08:00
proxy, exist := server.outbound.Outbound(name)
2022-07-21 21:03:41 +08:00
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))
})
}
}
2022-07-19 22:16:49 +08:00
2022-07-21 21:03:41 +08:00
func proxyInfo(server *Server, detour adapter.Outbound) *badjson.JSONObject {
var info badjson.JSONObject
var clashType string
switch detour.Type() {
case C.TypeBlock:
clashType = "Reject"
default:
2023-08-04 17:13:46 +08:00
clashType = C.ProxyDisplayName(detour.Type())
2022-07-21 21:03:41 +08:00
}
info.Put("type", clashType)
info.Put("name", detour.Tag())
2022-07-30 00:29:22 +08:00
info.Put("udp", common.Contains(detour.Network(), N.NetworkUDP))
2022-07-28 16:36:31 +08:00
delayHistory := server.urlTestHistory.LoadURLTestHistory(adapter.OutboundTag(detour))
2022-07-22 13:51:08 +08:00
if delayHistory != nil {
info.Put("history", []*urltest.History{delayHistory})
2022-07-22 09:29:13 +08:00
} else {
2022-07-22 13:51:08 +08:00
info.Put("history", []*urltest.History{})
2022-07-22 09:29:13 +08:00
}
2022-09-15 15:22:08 +08:00
if group, isGroup := detour.(adapter.OutboundGroup); isGroup {
info.Put("now", group.Now())
info.Put("all", group.All())
2022-07-21 21:03:41 +08:00
}
return &info
2022-07-19 22:16:49 +08:00
}
func getProxies(server *Server) func(w http.ResponseWriter, r *http.Request) {
2022-07-21 21:03:41 +08:00
return func(w http.ResponseWriter, r *http.Request) {
var proxyMap badjson.JSONObject
2024-12-06 16:47:30 +08:00
outbounds := common.Filter(server.outbound.Outbounds(), func(detour adapter.Outbound) bool {
2022-07-22 09:29:13 +08:00
return detour.Tag() != ""
})
2024-12-06 16:47:30 +08:00
outbounds = append(outbounds, common.Map(common.Filter(server.endpoint.Endpoints(), func(detour adapter.Endpoint) bool {
return detour.Tag() != ""
}), func(it adapter.Endpoint) adapter.Outbound {
return it
})...)
2022-07-21 21:03:41 +08:00
2022-07-22 09:29:13 +08:00
allProxies := make([]string, 0, len(outbounds))
for _, detour := range outbounds {
switch detour.Type() {
2022-07-28 16:40:06 +08:00
case C.TypeDirect, C.TypeBlock, C.TypeDNS:
2022-07-22 09:29:13 +08:00
continue
}
allProxies = append(allProxies, detour.Tag())
}
2024-12-06 16:47:30 +08:00
defaultTag := server.outbound.Default().Tag()
2022-07-22 09:29:13 +08:00
2023-04-30 16:58:07 +08:00
sort.SliceStable(allProxies, func(i, j int) bool {
2022-07-22 09:29:13 +08:00
return allProxies[i] == defaultTag
2022-07-21 21:03:41 +08:00
})
2022-07-22 09:29:13 +08:00
// fix clash dashboard
2022-07-21 21:03:41 +08:00
proxyMap.Put("GLOBAL", map[string]any{
2022-07-22 09:29:13 +08:00
"type": "Fallback",
2022-07-21 21:03:41 +08:00
"name": "GLOBAL",
"udp": true,
2022-07-22 13:51:08 +08:00
"history": []*urltest.History{},
2022-07-22 09:29:13 +08:00
"all": allProxies,
"now": defaultTag,
2022-07-21 21:03:41 +08:00
})
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)
}
2022-07-19 22:16:49 +08:00
}
2022-07-21 21:03:41 +08:00
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)
}
2022-07-19 22:16:49 +08:00
}
type UpdateProxyRequest struct {
Name string `json:"name"`
}
func updateProxy(w http.ResponseWriter, r *http.Request) {
2022-07-21 21:03:41 +08:00
req := UpdateProxyRequest{}
if err := render.DecodeJSON(r.Body, &req); err != nil {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, ErrBadRequest)
return
}
2022-07-19 22:16:49 +08:00
2022-07-21 21:03:41 +08:00
proxy := r.Context().Value(CtxKeyProxy).(adapter.Outbound)
2024-11-02 00:39:02 +08:00
selector, ok := proxy.(*group.Selector)
2022-07-21 21:03:41 +08:00
if !ok {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, newError("Must be a Selector"))
return
}
2022-07-19 22:16:49 +08:00
2022-07-21 21:03:41 +08:00
if !selector.SelectOutbound(req.Name) {
render.Status(r, http.StatusBadRequest)
2023-11-16 01:10:52 +08:00
render.JSON(w, r, newError("Selector update error: not found"))
2022-07-21 21:03:41 +08:00
return
}
2022-07-19 22:16:49 +08:00
render.NoContent(w, r)
}
2022-07-21 21:03:41 +08:00
func getProxyDelay(server *Server) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
2022-07-19 22:16:49 +08:00
url := query.Get("url")
2023-04-13 09:03:08 +08:00
if strings.HasPrefix(url, "http://") {
url = ""
}
2022-07-19 22:16:49 +08:00
timeout, err := strconv.ParseInt(query.Get("timeout"), 10, 16)
if err != nil {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, ErrBadRequest)
return
}
2022-07-21 21:03:41 +08:00
proxy := r.Context().Value(CtxKeyProxy).(adapter.Outbound)
2022-07-19 22:16:49 +08:00
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(timeout))
defer cancel()
2022-07-22 13:51:08 +08:00
delay, err := urltest.URLTest(ctx, url, proxy)
defer func() {
2024-11-02 00:39:02 +08:00
realTag := group.RealTag(proxy)
2022-07-22 13:51:08 +08:00
if err != nil {
2022-07-28 16:36:31 +08:00
server.urlTestHistory.DeleteURLTestHistory(realTag)
2022-07-22 13:51:08 +08:00
} else {
2022-07-28 16:36:31 +08:00
server.urlTestHistory.StoreURLTestHistory(realTag, &urltest.History{
2022-07-22 13:51:08 +08:00
Time: time.Now(),
Delay: delay,
})
}
}()
2022-07-19 22:16:49 +08:00
if ctx.Err() != nil {
render.Status(r, http.StatusGatewayTimeout)
render.JSON(w, r, ErrRequestTimeout)
return
}
if err != nil || delay == 0 {
render.Status(r, http.StatusServiceUnavailable)
render.JSON(w, r, newError("An error occurred in the delay test"))
return
}
2022-07-21 21:03:41 +08:00
render.JSON(w, r, render.M{
"delay": delay,
})
}
}