feat: add /s5/p2p/peers endpoint

This commit is contained in:
Derrick Hammer 2024-03-11 17:24:21 -04:00
parent db5716f3a3
commit 90d78d310e
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 41 additions and 4 deletions

View File

@ -40,6 +40,7 @@ func (h *HTTPServiceDefault) GetHttpRouter(inject map[string]jape.Handler) *http
"GET /s5/version": h.versionHandler,
"GET /s5/p2p": h.p2pHandler,
"GET /s5/p2p/nodes": h.p2pNodesHandler,
"GET /s5/p2p/peers": h.p2pPeersHandler,
}
for k, v := range inject {
@ -115,10 +116,6 @@ func (h *HTTPServiceDefault) p2pHandler(ctx jape.Context) {
}
}
if clientIP != nil {
peer.SetIP(&net.IPAddr{IP: clientIP})
}
if blockConnection(peer.GetIP()) {
err := peer.End()
if err != nil {
@ -127,6 +124,10 @@ func (h *HTTPServiceDefault) p2pHandler(ctx jape.Context) {
return
}
if clientIP != nil {
peer.SetIP(&net.IPAddr{IP: clientIP})
}
h.Services().P2P().ConnectionTracker().Add(1)
go func() {
@ -160,3 +161,39 @@ func (h *HTTPServiceDefault) p2pNodesHandler(ctx jape.Context) {
Nodes: nodeList,
})
}
func (h *HTTPServiceDefault) p2pPeersHandler(ctx jape.Context) {
peers := h.Services().P2P().Peers().Values()
peerList := make([]P2PNodeResponse, 0)
for _, p := range peers {
peer, ok := p.(s5net.Peer)
if !ok {
continue
}
id, err := peer.Id().ToString()
if err != nil {
h.Logger().Error("error getting peer id", zap.Error(err))
continue
}
if len(peer.ConnectionURIs()) == 0 {
continue
}
uris := make([]string, len(peer.ConnectionURIs()))
for i, uri := range peer.ConnectionURIs() {
uris[i] = uri.String()
}
peerList = append(peerList, P2PNodeResponse{
Id: id,
Uris: uris,
})
}
ctx.Encode(P2PNodesResponse{
Nodes: peerList,
})
}