From 025fa5da6b76ef78f28f43e6977c930f7cb1b8c7 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 22 Jul 2020 06:14:14 +0000 Subject: [PATCH] updates to admin stats --- cmd/telebit/admin.go | 21 ++++++++++++++------- table/table.go | 2 ++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/cmd/telebit/admin.go b/cmd/telebit/admin.go index a79ef23..8a3f29f 100644 --- a/cmd/telebit/admin.go +++ b/cmd/telebit/admin.go @@ -11,6 +11,7 @@ import ( "os" "strings" "sync" + "time" telebit "git.rootprojects.org/root/telebit" "git.rootprojects.org/root/telebit/admin" @@ -107,10 +108,10 @@ func apiNotFoundHandler(w http.ResponseWriter, r *http.Request) { } type SubscriberStatus struct { - Subject string `json:"sub"` - RemoteAddr string `json:"socket"` - Tunnels int `json:"tunnels"` - Clients int `json:"clients"` + Since *time.Time `json:"since,omitempty"` + Subject string `json:"sub"` + Sockets []string `json:"sockets"` + Clients int `json:"clients"` // TODO bytes read } @@ -118,16 +119,20 @@ func getSubscribers(w http.ResponseWriter, r *http.Request) { statuses := []*SubscriberStatus{} table.Servers.Range(func(key, value interface{}) bool { status := &SubscriberStatus{ + Since: nil, Subject: "", - //RemoteAddr: k.(string), - Tunnels: 0, + Sockets: []string{}, Clients: 0, } //subject := key.(string) srvMap := value.(*sync.Map) srvMap.Range(func(k, v interface{}) bool { - status.Tunnels++ + status.Sockets = append(status.Sockets, k.(string)) srv := v.(*table.SubscriberConn) + if nil == status.Since || srv.Since.Sub(*status.Since) < 0 { + copied := srv.Since.Truncate(time.Second) + status.Since = &copied + } status.Subject = srv.Grants.Subject srv.Clients.Range(func(k, v interface{}) bool { status.Clients++ @@ -206,7 +211,9 @@ func upgradeWebsocket(w http.ResponseWriter, r *http.Request) { // The local address of the server (port to which it connected) is not very meaningful. // Rather the client's local address (the specific relay server) would be more useful. ctxEncoder, cancelEncoder := context.WithCancel(context.Background()) + now := time.Now() server := &table.SubscriberConn{ + Since: &now, RemoteAddr: r.RemoteAddr, WSConn: conn, WSTun: wsTun, diff --git a/table/table.go b/table/table.go index 4606efa..03aa47e 100644 --- a/table/table.go +++ b/table/table.go @@ -5,6 +5,7 @@ import ( "net" "os" "sync" + "time" "io" "strconv" @@ -104,6 +105,7 @@ func Remove(subject string) bool { // SubscriberConn represents a tunneled server, its grants, and its clients type SubscriberConn struct { + Since *time.Time RemoteAddr string WSConn *websocket.Conn WSTun net.Conn // *telebit.WebsocketTunnel