From 8d09e68e806f235f841d7730ec43f9b711c038a9 Mon Sep 17 00:00:00 2001 From: Arceliar Date: Sun, 16 May 2021 16:16:58 -0500 Subject: [PATCH] admin socket getpaths --- src/admin/admin.go | 11 +++++++++++ src/admin/getpaths.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/admin/getpaths.go diff --git a/src/admin/admin.go b/src/admin/admin.go index 8ad8d907..c82d705d 100644 --- a/src/admin/admin.go +++ b/src/admin/admin.go @@ -117,6 +117,17 @@ func (a *AdminSocket) SetupAdminHandlers(na *AdminSocket) { } return res, nil }) + _ = a.AddHandler("getPaths", []string{}, func(in json.RawMessage) (interface{}, error) { + req := &GetPathsRequest{} + res := &GetPathsResponse{} + if err := json.Unmarshal(in, &req); err != nil { + return nil, err + } + if err := a.getPathsHandler(req, res); err != nil { + return nil, err + } + return res, nil + }) _ = a.AddHandler("getSessions", []string{}, func(in json.RawMessage) (interface{}, error) { req := &GetSessionsRequest{} res := &GetSessionsResponse{} diff --git a/src/admin/getpaths.go b/src/admin/getpaths.go new file mode 100644 index 00000000..c8e97d01 --- /dev/null +++ b/src/admin/getpaths.go @@ -0,0 +1,33 @@ +package admin + +import ( + "encoding/hex" + "net" + + "github.com/yggdrasil-network/yggdrasil-go/src/address" +) + +type GetPathsRequest struct { +} + +type GetPathsResponse struct { + Paths map[string]PathEntry `json:"paths"` +} + +type PathEntry struct { + PublicKey string `json:"key"` + Path []uint64 `json:"path"` +} + +func (a *AdminSocket) getPathsHandler(req *GetPathsRequest, res *GetPathsResponse) error { + res.Paths = map[string]PathEntry{} + for _, p := range a.core.GetPaths() { + addr := address.AddrForKey(p.Key) + so := net.IP(addr[:]).String() + res.Paths[so] = PathEntry{ + PublicKey: hex.EncodeToString(p.Key), + Path: p.Path, + } + } + return nil +}