updates to admin stats

This commit is contained in:
AJ ONeal 2020-07-22 06:14:14 +00:00
parent c7a1d8d3a2
commit 025fa5da6b
2 changed files with 16 additions and 7 deletions

View File

@ -11,6 +11,7 @@ import (
"os"
"strings"
"sync"
"time"
telebit "git.rootprojects.org/root/telebit"
"git.rootprojects.org/root/telebit/admin"
@ -107,9 +108,9 @@ func apiNotFoundHandler(w http.ResponseWriter, r *http.Request) {
}
type SubscriberStatus struct {
Since *time.Time `json:"since,omitempty"`
Subject string `json:"sub"`
RemoteAddr string `json:"socket"`
Tunnels int `json:"tunnels"`
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,

View File

@ -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