github.com/yggdrasil-network/yggdrasil-go@v0.5.6/src/admin/gettree.go (about)

     1  package admin
     2  
     3  import (
     4  	"encoding/hex"
     5  	"net"
     6  	"sort"
     7  	"strings"
     8  
     9  	"github.com/yggdrasil-network/yggdrasil-go/src/address"
    10  )
    11  
    12  type GetTreeRequest struct{}
    13  
    14  type GetTreeResponse struct {
    15  	Tree []TreeEntry `json:"tree"`
    16  }
    17  
    18  type TreeEntry struct {
    19  	IPAddress string `json:"address"`
    20  	PublicKey string `json:"key"`
    21  	Parent    string `json:"parent"`
    22  	Sequence  uint64 `json:"sequence"`
    23  	//Port      uint64 `json:"port"`
    24  	//Rest      uint64 `json:"rest"`
    25  }
    26  
    27  func (a *AdminSocket) getTreeHandler(req *GetTreeRequest, res *GetTreeResponse) error {
    28  	tree := a.core.GetTree()
    29  	res.Tree = make([]TreeEntry, 0, len(tree))
    30  	for _, d := range tree {
    31  		addr := address.AddrForKey(d.Key)
    32  		res.Tree = append(res.Tree, TreeEntry{
    33  			IPAddress: net.IP(addr[:]).String(),
    34  			PublicKey: hex.EncodeToString(d.Key[:]),
    35  			Parent:    hex.EncodeToString(d.Parent[:]),
    36  			Sequence:  d.Sequence,
    37  			//Port:      d.Port,
    38  			//Rest:      d.Rest,
    39  		})
    40  	}
    41  	sort.SliceStable(res.Tree, func(i, j int) bool {
    42  		return strings.Compare(res.Tree[i].PublicKey, res.Tree[j].PublicKey) < 0
    43  	})
    44  	return nil
    45  }