-
Notifications
You must be signed in to change notification settings - Fork 0
/
getnodeinfo.go
69 lines (54 loc) · 1.8 KB
/
getnodeinfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Package api provides API for chestnut.
package api
import (
"fmt"
"net/http"
"strings"
"github.com/labstack/echo/v4"
"github.com/lixvyang/chestnut/nodectx"
"github.com/lixvyang/chestnut/p2p"
p2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
)
type NodeInfo struct {
Node_publickey string `json:"node_publickey"`
Node_status string `json:"node_status"`
Node_version string `json:"node_version"`
User_id string `json:"user_id"`
}
func updateNodeStatus(nodenetworkname string) {
peersprotocol := nodectx.GetNodeCtx().PeersProtocol()
networknamewithprefix := fmt.Sprintf("%s/%s", p2p.ProtocolPrefix, nodenetworkname)
for protocol, peerlist := range *peersprotocol {
if strings.HasPrefix(protocol, networknamewithprefix) {
if len(peerlist) > 0 {
nodectx.GetNodeCtx().UpdateOnlineStatus(nodectx.NODE_ONLINE)
return
}
}
}
if nodectx.GetNodeCtx().Status != nodectx.NODE_OFFLINE {
nodectx.GetNodeCtx().UpdateOnlineStatus(nodectx.NODE_OFFLINE)
}
}
func (h *Handler) GetNodeInfo(c echo.Context) (err error) {
// nodeopt *options.NodeOptions
output := make(map[string]interface{})
output[NODE_VERSION] = nodectx.GetNodeCtx().Version + " - " + h.GitCommit
output[NODETYPE] = "peer"
updateNodeStatus(h.Node.NetworkName)
if nodectx.GetNodeCtx().Status == nodectx.NODE_ONLINE {
output[NODE_STATUS] = "NODE_ONLINE"
} else {
output[NODE_STATUS] = "NODE_OFFLINE"
}
pubkeybytes, err := p2pcrypto.MarshalPublicKey(nodectx.GetNodeCtx().PublickKey)
if err != nil {
output[ERROR_INFO] = err.Error()
return c.JSON(http.StatusBadRequest, output)
}
output[NODE_PUBKEY] = p2pcrypto.ConfigEncodeKey(pubkeybytes)
output[NODE_ID] = nodectx.GetNodeCtx().PeerId.Pretty()
peers := nodectx.GetNodeCtx().PeersProtocol()
output[PEERS] = *peers
return c.JSON(http.StatusOK, output)
}