sing-box/experimental/clashapi/connections.go

98 lines
2.2 KiB
Go
Raw Normal View History

2022-07-19 22:16:49 +08:00
package clashapi
import (
"bytes"
"encoding/json"
"net/http"
"strconv"
"time"
2022-07-22 09:29:13 +08:00
"github.com/sagernet/sing-box/experimental/clashapi/trafficontrol"
2022-07-19 22:16:49 +08:00
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/gorilla/websocket"
)
2022-07-22 09:29:13 +08:00
func connectionRouter(trafficManager *trafficontrol.Manager) http.Handler {
2022-07-19 22:16:49 +08:00
r := chi.NewRouter()
r.Get("/", getConnections(trafficManager))
r.Delete("/", closeAllConnections(trafficManager))
r.Delete("/{id}", closeConnection(trafficManager))
return r
}
2022-07-22 09:29:13 +08:00
func getConnections(trafficManager *trafficontrol.Manager) func(w http.ResponseWriter, r *http.Request) {
2022-07-19 22:16:49 +08:00
return func(w http.ResponseWriter, r *http.Request) {
if !websocket.IsWebSocketUpgrade(r) {
snapshot := trafficManager.Snapshot()
render.JSON(w, r, snapshot)
return
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
intervalStr := r.URL.Query().Get("interval")
interval := 1000
if intervalStr != "" {
t, err := strconv.Atoi(intervalStr)
if err != nil {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, ErrBadRequest)
return
}
interval = t
}
buf := &bytes.Buffer{}
sendSnapshot := func() error {
buf.Reset()
snapshot := trafficManager.Snapshot()
if err := json.NewEncoder(buf).Encode(snapshot); err != nil {
return err
}
return conn.WriteMessage(websocket.TextMessage, buf.Bytes())
}
if err = sendSnapshot(); err != nil {
return
}
tick := time.NewTicker(time.Millisecond * time.Duration(interval))
defer tick.Stop()
for range tick.C {
if err = sendSnapshot(); err != nil {
break
}
}
}
}
2022-07-22 09:29:13 +08:00
func closeConnection(trafficManager *trafficontrol.Manager) func(w http.ResponseWriter, r *http.Request) {
2022-07-19 22:16:49 +08:00
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
snapshot := trafficManager.Snapshot()
for _, c := range snapshot.Connections {
if id == c.ID() {
c.Close()
break
}
}
render.NoContent(w, r)
}
}
2022-07-22 09:29:13 +08:00
func closeAllConnections(trafficManager *trafficontrol.Manager) func(w http.ResponseWriter, r *http.Request) {
2022-07-19 22:16:49 +08:00
return func(w http.ResponseWriter, r *http.Request) {
snapshot := trafficManager.Snapshot()
for _, c := range snapshot.Connections {
c.Close()
}
render.NoContent(w, r)
}
}