clean up some debug API output

This commit is contained in:
Arceliar 2023-10-28 06:36:01 -05:00
parent d17ac39789
commit 82c54f87ea

View File

@ -1,12 +1,16 @@
package admin package admin
import ( import (
"crypto/ed25519"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"net"
"sync" "sync"
"time" "time"
"github.com/Arceliar/ironwood/network" "github.com/Arceliar/ironwood/network"
"github.com/yggdrasil-network/yggdrasil-go/src/address"
) )
func (c *AdminSocket) _applyOption(opt SetupOption) { func (c *AdminSocket) _applyOption(opt SetupOption) {
@ -32,9 +36,10 @@ func (l LogLookups) isSetupOption() {}
func (a *AdminSocket) logLookups() { func (a *AdminSocket) logLookups() {
type resi struct { type resi struct {
Key string `json:"key"` Address string `json:"addr"`
Path []uint64 `json:"path"` Key string `json:"key"`
Time int64 `json:"time"` Path []uint64 `json:"path"`
Time int64 `json:"time"`
} }
type res struct { type res struct {
Infos []resi `json:"infos"` Infos []resi `json:"infos"`
@ -43,12 +48,14 @@ func (a *AdminSocket) logLookups() {
path []uint64 path []uint64
time time.Time time time.Time
} }
infos := make(map[string]info) type edk [ed25519.PublicKeySize]byte
infos := make(map[edk]info)
var m sync.Mutex var m sync.Mutex
a.core.PacketConn.PacketConn.Debug.SetDebugLookupLogger(func(l network.DebugLookupInfo) { a.core.PacketConn.PacketConn.Debug.SetDebugLookupLogger(func(l network.DebugLookupInfo) {
key := hex.EncodeToString(l.Key[:]) var k edk
copy(k[:], l.Key[:])
m.Lock() m.Lock()
infos[key] = info{path: l.Path, time: time.Now()} infos[k] = info{path: l.Path, time: time.Now()}
m.Unlock() m.Unlock()
}) })
_ = a.AddHandler( _ = a.AddHandler(
@ -61,7 +68,9 @@ func (a *AdminSocket) logLookups() {
// TODO? automatic cleanup, so we don't need to call lookups periodically to prevent leaks // TODO? automatic cleanup, so we don't need to call lookups periodically to prevent leaks
delete(infos, k) delete(infos, k)
} }
rs = append(rs, resi{Key: k, Path: v.path, Time: v.time.Unix()}) a := address.AddrForKey(ed25519.PublicKey(k[:]))
addr := net.IP(a[:]).String()
rs = append(rs, resi{Address: addr, Key: hex.EncodeToString(k[:]), Path: v.path, Time: v.time.Unix()})
} }
m.Unlock() m.Unlock()
return &res{Infos: rs}, nil return &res{Infos: rs}, nil