connections.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package clashapi
  2. import (
  3. "bytes"
  4. "net/http"
  5. "strconv"
  6. "time"
  7. "github.com/sagernet/sing-box/adapter"
  8. "github.com/sagernet/sing-box/experimental/clashapi/trafficontrol"
  9. "github.com/sagernet/sing/common/json"
  10. "github.com/sagernet/ws"
  11. "github.com/sagernet/ws/wsutil"
  12. "github.com/go-chi/chi/v5"
  13. "github.com/go-chi/render"
  14. )
  15. func connectionRouter(router adapter.Router, trafficManager *trafficontrol.Manager) http.Handler {
  16. r := chi.NewRouter()
  17. r.Get("/", getConnections(trafficManager))
  18. r.Delete("/", closeAllConnections(router, trafficManager))
  19. r.Delete("/{id}", closeConnection(trafficManager))
  20. return r
  21. }
  22. func getConnections(trafficManager *trafficontrol.Manager) func(w http.ResponseWriter, r *http.Request) {
  23. return func(w http.ResponseWriter, r *http.Request) {
  24. if r.Header.Get("Upgrade") != "websocket" {
  25. snapshot := trafficManager.Snapshot()
  26. render.JSON(w, r, snapshot)
  27. return
  28. }
  29. conn, _, _, err := ws.UpgradeHTTP(r, w)
  30. if err != nil {
  31. return
  32. }
  33. intervalStr := r.URL.Query().Get("interval")
  34. interval := 1000
  35. if intervalStr != "" {
  36. t, err := strconv.Atoi(intervalStr)
  37. if err != nil {
  38. render.Status(r, http.StatusBadRequest)
  39. render.JSON(w, r, ErrBadRequest)
  40. return
  41. }
  42. interval = t
  43. }
  44. buf := &bytes.Buffer{}
  45. sendSnapshot := func() error {
  46. buf.Reset()
  47. snapshot := trafficManager.Snapshot()
  48. if err := json.NewEncoder(buf).Encode(snapshot); err != nil {
  49. return err
  50. }
  51. return wsutil.WriteServerText(conn, buf.Bytes())
  52. }
  53. if err = sendSnapshot(); err != nil {
  54. return
  55. }
  56. tick := time.NewTicker(time.Millisecond * time.Duration(interval))
  57. defer tick.Stop()
  58. for range tick.C {
  59. if err = sendSnapshot(); err != nil {
  60. break
  61. }
  62. }
  63. }
  64. }
  65. func closeConnection(trafficManager *trafficontrol.Manager) func(w http.ResponseWriter, r *http.Request) {
  66. return func(w http.ResponseWriter, r *http.Request) {
  67. id := chi.URLParam(r, "id")
  68. snapshot := trafficManager.Snapshot()
  69. for _, c := range snapshot.Connections {
  70. if id == c.ID() {
  71. c.Close()
  72. break
  73. }
  74. }
  75. render.NoContent(w, r)
  76. }
  77. }
  78. func closeAllConnections(router adapter.Router, trafficManager *trafficontrol.Manager) func(w http.ResponseWriter, r *http.Request) {
  79. return func(w http.ResponseWriter, r *http.Request) {
  80. snapshot := trafficManager.Snapshot()
  81. for _, c := range snapshot.Connections {
  82. c.Close()
  83. }
  84. router.ResetNetwork()
  85. render.NoContent(w, r)
  86. }
  87. }