go-waku/waku/v2/rpc/admin.go

87 lines
2.1 KiB
Go
Raw Normal View History

2021-11-10 13:36:51 +00:00
package rpc
import (
"net/http"
ma "github.com/multiformats/go-multiaddr"
"go.uber.org/zap"
2021-11-10 13:36:51 +00:00
"github.com/status-im/go-waku/waku/v2/node"
2021-11-17 16:19:42 +00:00
"github.com/status-im/go-waku/waku/v2/protocol/filter"
"github.com/status-im/go-waku/waku/v2/protocol/lightpush"
"github.com/status-im/go-waku/waku/v2/protocol/relay"
"github.com/status-im/go-waku/waku/v2/protocol/store"
2021-11-10 13:36:51 +00:00
)
type AdminService struct {
node *node.WakuNode
log *zap.SugaredLogger
2021-11-10 13:36:51 +00:00
}
type GetPeersArgs struct {
}
type PeersArgs struct {
Peers []string `json:"peers,omitempty"`
}
type PeerReply struct {
Multiaddr string `json:"mutliaddr,omitempty"`
Protocol string `json:"protocol,omitempty"`
Connected bool `json:"connected,omitempty"`
}
type PeersReply struct {
Peers []PeerReply `json:"peers,omitempty"`
}
func (a *AdminService) PostV1Peers(req *http.Request, args *PeersArgs, reply *SuccessReply) error {
for _, peer := range args.Peers {
addr, err := ma.NewMultiaddr(peer)
if err != nil {
a.log.Error("Error building multiaddr", err)
2021-11-10 13:36:51 +00:00
reply.Success = false
reply.Error = err.Error()
return nil
}
err = a.node.DialPeerWithMultiAddress(req.Context(), addr)
if err != nil {
a.log.Error("Error dialing peers", err)
2021-11-10 13:36:51 +00:00
reply.Success = false
reply.Error = err.Error()
return nil
}
}
reply.Success = true
return nil
}
2021-11-17 16:19:42 +00:00
func isWakuProtocol(protocol string) bool {
2022-02-23 15:06:47 +00:00
return protocol == string(filter.FilterID_v20beta1) || protocol == string(relay.WakuRelayID_v200) || protocol == string(lightpush.LightPushID_v20beta1) || protocol == string(store.StoreID_v20beta4)
2021-11-17 16:19:42 +00:00
}
2021-11-10 13:36:51 +00:00
func (a *AdminService) GetV1Peers(req *http.Request, args *GetPeersArgs, reply *PeersReply) error {
peers, err := a.node.Peers()
if err != nil {
a.log.Error("Error getting peers", err)
2021-11-10 13:36:51 +00:00
return nil
}
for _, peer := range peers {
2021-11-17 16:19:42 +00:00
for _, addr := range peer.Addrs {
for _, proto := range peer.Protocols {
if !isWakuProtocol(proto) {
continue
}
reply.Peers = append(reply.Peers, PeerReply{
Multiaddr: addr.String(),
Protocol: proto,
Connected: peer.Connected,
})
}
2021-11-10 13:36:51 +00:00
}
}
return nil
}