Diag session routing (#8317)

Co-authored-by: Mark Holt <mark@distributed.vision>
This commit is contained in:
Dmytro 2023-09-28 15:02:02 +02:00 committed by GitHub
parent 669503d38f
commit 2cbd446e88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 0 deletions

4
.gitignore vendored
View File

@ -90,3 +90,7 @@ caplin-recordings
jwt.hex
.tool-versions
*__debug_bin*
yarn.lock
node_modules

58
diagnostics/peers.go Normal file
View File

@ -0,0 +1,58 @@
package diagnostics
import (
"context"
"encoding/json"
"net/http"
"github.com/ledgerwatch/erigon/p2p"
"github.com/ledgerwatch/erigon/turbo/node"
"github.com/urfave/cli/v2"
)
func SetupPeersAccess(ctx *cli.Context, metricsMux *http.ServeMux, node *node.ErigonNode) {
metricsMux.HandleFunc("/peers", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
writePeers(w, ctx, node)
})
}
func writePeers(w http.ResponseWriter, ctx *cli.Context, node *node.ErigonNode) {
reply, err := node.Backend().Peers(context.Background())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
peers := make([]*p2p.PeerInfo, 0, len(reply.Peers))
for _, rpcPeer := range reply.Peers {
peer := p2p.PeerInfo{
ENR: rpcPeer.Enr,
Enode: rpcPeer.Enode,
ID: rpcPeer.Id,
Name: rpcPeer.Name,
Caps: rpcPeer.Caps,
Network: struct {
LocalAddress string `json:"localAddress"`
RemoteAddress string `json:"remoteAddress"`
Inbound bool `json:"inbound"`
Trusted bool `json:"trusted"`
Static bool `json:"static"`
}{
LocalAddress: rpcPeer.ConnLocalAddr,
RemoteAddress: rpcPeer.ConnRemoteAddr,
Inbound: rpcPeer.ConnIsInbound,
Trusted: rpcPeer.ConnIsTrusted,
Static: rpcPeer.ConnIsStatic,
},
Protocols: nil,
}
peers = append(peers, &peer)
}
json.NewEncoder(w).Encode(peers)
}

View File

@ -25,4 +25,6 @@ func Setup(ctx *cli.Context, metricsMux *http.ServeMux, node *node.ErigonNode) {
SetupBlockBodyDownload(debugMux)
SetupHeaderDownloadStats(debugMux)
SetupNodeInfoAccess(debugMux, node)
SetupPeersAccess(ctx, debugMux, node)
}