mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 22:25:07 +03:00
Admin socket and yggdrasilctl
refactoring (#939)
This commit is contained in:
parent
4f2abece81
commit
c6fe81b5d2
11 changed files with 401 additions and 470 deletions
|
@ -3,6 +3,8 @@ package admin
|
|||
import (
|
||||
"encoding/hex"
|
||||
"net"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||
)
|
||||
|
@ -10,21 +12,26 @@ import (
|
|||
type GetSessionsRequest struct{}
|
||||
|
||||
type GetSessionsResponse struct {
|
||||
Sessions map[string]SessionEntry `json:"sessions"`
|
||||
Sessions []SessionEntry `json:"sessions"`
|
||||
}
|
||||
|
||||
type SessionEntry struct {
|
||||
IPAddress string `json:"address"`
|
||||
PublicKey string `json:"key"`
|
||||
}
|
||||
|
||||
func (a *AdminSocket) getSessionsHandler(req *GetSessionsRequest, res *GetSessionsResponse) error {
|
||||
res.Sessions = map[string]SessionEntry{}
|
||||
for _, s := range a.core.GetSessions() {
|
||||
sessions := a.core.GetSessions()
|
||||
res.Sessions = make([]SessionEntry, 0, len(sessions))
|
||||
for _, s := range sessions {
|
||||
addr := address.AddrForKey(s.Key)
|
||||
so := net.IP(addr[:]).String()
|
||||
res.Sessions[so] = SessionEntry{
|
||||
res.Sessions = append(res.Sessions, SessionEntry{
|
||||
IPAddress: net.IP(addr[:]).String(),
|
||||
PublicKey: hex.EncodeToString(s.Key[:]),
|
||||
}
|
||||
})
|
||||
}
|
||||
sort.SliceStable(res.Sessions, func(i, j int) bool {
|
||||
return strings.Compare(res.Sessions[i].PublicKey, res.Sessions[j].PublicKey) < 0
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue