2021-05-16 18:51:09 +00:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"net"
|
2022-09-03 09:50:43 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2021-05-16 18:51:09 +00:00
|
|
|
|
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GetSessionsRequest struct{}
|
|
|
|
|
|
|
|
type GetSessionsResponse struct {
|
2022-09-03 09:50:43 +00:00
|
|
|
Sessions []SessionEntry `json:"sessions"`
|
2021-05-16 18:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SessionEntry struct {
|
2022-09-03 15:55:57 +00:00
|
|
|
IPAddress string `json:"address"`
|
|
|
|
PublicKey string `json:"key"`
|
|
|
|
RXBytes DataUnit `json:"bytes_recvd"`
|
|
|
|
TXBytes DataUnit `json:"bytes_sent"`
|
|
|
|
Uptime float64 `json:"uptime"`
|
2021-05-16 18:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AdminSocket) getSessionsHandler(req *GetSessionsRequest, res *GetSessionsResponse) error {
|
2022-09-03 09:50:43 +00:00
|
|
|
sessions := a.core.GetSessions()
|
|
|
|
res.Sessions = make([]SessionEntry, 0, len(sessions))
|
|
|
|
for _, s := range sessions {
|
2021-05-16 18:51:09 +00:00
|
|
|
addr := address.AddrForKey(s.Key)
|
2022-09-03 09:50:43 +00:00
|
|
|
res.Sessions = append(res.Sessions, SessionEntry{
|
|
|
|
IPAddress: net.IP(addr[:]).String(),
|
2021-05-16 18:51:09 +00:00
|
|
|
PublicKey: hex.EncodeToString(s.Key[:]),
|
2022-09-03 15:55:57 +00:00
|
|
|
RXBytes: DataUnit(s.RXBytes),
|
|
|
|
TXBytes: DataUnit(s.TXBytes),
|
|
|
|
Uptime: s.Uptime.Seconds(),
|
2022-09-03 09:50:43 +00:00
|
|
|
})
|
2021-05-16 18:51:09 +00:00
|
|
|
}
|
2022-09-03 09:50:43 +00:00
|
|
|
sort.SliceStable(res.Sessions, func(i, j int) bool {
|
|
|
|
return strings.Compare(res.Sessions[i].PublicKey, res.Sessions[j].PublicKey) < 0
|
|
|
|
})
|
2021-05-16 18:51:09 +00:00
|
|
|
return nil
|
|
|
|
}
|