2021-11-10 13:36:51 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-02-16 16:17:52 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/protocol"
|
2021-11-10 13:36:51 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2022-01-18 18:17:06 +00:00
|
|
|
"go.uber.org/zap"
|
2021-11-10 13:36:51 +00:00
|
|
|
|
2023-10-24 19:25:04 +00:00
|
|
|
"github.com/waku-org/go-waku/cmd/waku/server"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/node"
|
2021-11-10 13:36:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AdminService struct {
|
|
|
|
node *node.WakuNode
|
2022-05-30 15:55:30 +00:00
|
|
|
log *zap.Logger
|
2021-11-10 13:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GetPeersArgs struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type PeersArgs struct {
|
|
|
|
Peers []string `json:"peers,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PeerReply struct {
|
2023-02-16 16:17:52 +00:00
|
|
|
Multiaddr string `json:"multiaddr,omitempty"`
|
|
|
|
Protocol protocol.ID `json:"protocol,omitempty"`
|
|
|
|
Connected bool `json:"connected,omitempty"`
|
2021-11-10 13:36:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 17:49:10 +00:00
|
|
|
type PeersReply []PeerReply
|
2021-11-10 13:36:51 +00:00
|
|
|
|
|
|
|
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 {
|
2022-05-30 15:55:30 +00:00
|
|
|
a.log.Error("building multiaddr", zap.Error(err))
|
2022-06-14 15:36:34 +00:00
|
|
|
return err
|
2021-11-10 13:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = a.node.DialPeerWithMultiAddress(req.Context(), addr)
|
|
|
|
if err != nil {
|
2022-05-30 15:55:30 +00:00
|
|
|
a.log.Error("dialing peers", zap.Error(err))
|
2022-06-14 15:36:34 +00:00
|
|
|
return err
|
2021-11-10 13:36:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 15:36:34 +00:00
|
|
|
*reply = true
|
2021-11-10 13:36:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AdminService) GetV1Peers(req *http.Request, args *GetPeersArgs, reply *PeersReply) error {
|
|
|
|
peers, err := a.node.Peers()
|
|
|
|
if err != nil {
|
2022-05-30 15:55:30 +00:00
|
|
|
a.log.Error("getting peers", zap.Error(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 {
|
2023-10-24 19:25:04 +00:00
|
|
|
if !server.IsWakuProtocol(proto) {
|
2021-11-17 16:19:42 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-07-26 17:49:10 +00:00
|
|
|
*reply = append(*reply, PeerReply{
|
2021-11-17 16:19:42 +00:00
|
|
|
Multiaddr: addr.String(),
|
|
|
|
Protocol: proto,
|
|
|
|
Connected: peer.Connected,
|
|
|
|
})
|
|
|
|
}
|
2021-11-10 13:36:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|