Refactor: make inbound request contextual

This commit is contained in:
Dreamacro
2021-01-23 14:49:46 +08:00
parent 35925cb3da
commit f4de055aa1
19 changed files with 302 additions and 125 deletions

View File

@ -7,7 +7,7 @@ import (
"strconv"
"time"
T "github.com/Dreamacro/clash/tunnel"
"github.com/Dreamacro/clash/tunnel/statistic"
"github.com/gorilla/websocket"
"github.com/go-chi/chi"
@ -24,7 +24,7 @@ func connectionRouter() http.Handler {
func getConnections(w http.ResponseWriter, r *http.Request) {
if !websocket.IsWebSocketUpgrade(r) {
snapshot := T.DefaultManager.Snapshot()
snapshot := statistic.DefaultManager.Snapshot()
render.JSON(w, r, snapshot)
return
}
@ -50,7 +50,7 @@ func getConnections(w http.ResponseWriter, r *http.Request) {
buf := &bytes.Buffer{}
sendSnapshot := func() error {
buf.Reset()
snapshot := T.DefaultManager.Snapshot()
snapshot := statistic.DefaultManager.Snapshot()
if err := json.NewEncoder(buf).Encode(snapshot); err != nil {
return err
}
@ -73,7 +73,7 @@ func getConnections(w http.ResponseWriter, r *http.Request) {
func closeConnection(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
snapshot := T.DefaultManager.Snapshot()
snapshot := statistic.DefaultManager.Snapshot()
for _, c := range snapshot.Connections {
if id == c.ID() {
c.Close()
@ -84,7 +84,7 @@ func closeConnection(w http.ResponseWriter, r *http.Request) {
}
func closeAllConnections(w http.ResponseWriter, r *http.Request) {
snapshot := T.DefaultManager.Snapshot()
snapshot := statistic.DefaultManager.Snapshot()
for _, c := range snapshot.Connections {
c.Close()
}

View File

@ -9,7 +9,7 @@ import (
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
T "github.com/Dreamacro/clash/tunnel"
"github.com/Dreamacro/clash/tunnel/statistic"
"github.com/go-chi/chi"
"github.com/go-chi/cors"
@ -143,7 +143,7 @@ func traffic(w http.ResponseWriter, r *http.Request) {
tick := time.NewTicker(time.Second)
defer tick.Stop()
t := T.DefaultManager
t := statistic.DefaultManager
buf := &bytes.Buffer{}
var err error
for range tick.C {