2019-10-27 21:44:07 +08:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2021-01-23 14:49:46 +08:00
|
|
|
"github.com/Dreamacro/clash/tunnel/statistic"
|
2019-10-27 21:44:07 +08:00
|
|
|
|
2023-04-17 14:08:39 +08:00
|
|
|
"github.com/Dreamacro/protobytes"
|
2021-04-03 14:59:03 +08:00
|
|
|
"github.com/go-chi/chi/v5"
|
2019-10-27 21:44:07 +08:00
|
|
|
"github.com/go-chi/render"
|
2021-04-05 13:00:49 +08:00
|
|
|
"github.com/gorilla/websocket"
|
2019-10-27 21:44:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func connectionRouter() http.Handler {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Get("/", getConnections)
|
|
|
|
r.Delete("/", closeAllConnections)
|
|
|
|
r.Delete("/{id}", closeConnection)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func getConnections(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !websocket.IsWebSocketUpgrade(r) {
|
2021-01-23 14:49:46 +08:00
|
|
|
snapshot := statistic.DefaultManager.Snapshot()
|
2019-10-27 21:44:07 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-17 14:08:39 +08:00
|
|
|
buf := protobytes.BytesWriter{}
|
2019-10-27 21:44:07 +08:00
|
|
|
sendSnapshot := func() error {
|
|
|
|
buf.Reset()
|
2021-01-23 14:49:46 +08:00
|
|
|
snapshot := statistic.DefaultManager.Snapshot()
|
2023-04-17 14:08:39 +08:00
|
|
|
if err := json.NewEncoder(&buf).Encode(snapshot); err != nil {
|
2019-10-27 21:44:07 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn.WriteMessage(websocket.TextMessage, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sendSnapshot(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tick := time.NewTicker(time.Millisecond * time.Duration(interval))
|
2020-07-13 00:25:54 +08:00
|
|
|
defer tick.Stop()
|
2019-10-27 21:44:07 +08:00
|
|
|
for range tick.C {
|
|
|
|
if err := sendSnapshot(); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeConnection(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := chi.URLParam(r, "id")
|
2021-01-23 14:49:46 +08:00
|
|
|
snapshot := statistic.DefaultManager.Snapshot()
|
2019-10-27 21:44:07 +08:00
|
|
|
for _, c := range snapshot.Connections {
|
|
|
|
if id == c.ID() {
|
|
|
|
c.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
render.NoContent(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeAllConnections(w http.ResponseWriter, r *http.Request) {
|
2021-01-23 14:49:46 +08:00
|
|
|
snapshot := statistic.DefaultManager.Snapshot()
|
2019-10-27 21:44:07 +08:00
|
|
|
for _, c := range snapshot.Connections {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
render.NoContent(w, r)
|
|
|
|
}
|