Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connectivity probe script #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions inspector/connectivity-probe/connectivity_probe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package main

import (
"context"
"encoding/json"
"fmt"
libp2p "github.com/libp2p/go-libp2p"
libp2p_peer "github.com/libp2p/go-libp2p-core/peer"
ping "github.com/libp2p/go-libp2p/p2p/protocol/ping"
"github.com/multiformats/go-multiaddr"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)

type Diagnostics struct {
ConnectedPeers []ConnectedPeer `json:"connected_peers"`
}

type ConnectedPeer struct {
EthereumAddress string `json:"ethereum_address"`
NetworkID string `json:"network_id"`
Multiaddresses []string `json:"multiaddresses"`
}

// Usage: `go run connectivity_probe.go http://<host>:<port>/diagnostics`
func main() {
if len(os.Args) != 2 || len(os.Args[1]) == 0 {
panic("please pass a valid diagnostics endpoint")
}

diagnosticsEndpoint := os.Args[1]

diagnosticsData, err := getDiagnosticsData(diagnosticsEndpoint)
if err != nil {
panic(err)
}

fmt.Printf(
"checking %v peers fetched from diagnostics data\n\n",
len(diagnosticsData.ConnectedPeers),
)

ctx := context.Background()

host, err := libp2p.New(ctx)
if err != nil {
panic(err)
}

pingService := ping.NewPingService(host)

for _, peer := range diagnosticsData.ConnectedPeers {
err = sendPing(pingService, &peer)

fmt.Printf(
"peer: %v | available: %v\n",
peer.NetworkID,
isAvailable(err),
)
}

if err := host.Close(); err != nil {
panic(err)
}
}

func getDiagnosticsData(endpoint string) (*Diagnostics, error) {
diagnosticsResponse, err := http.Get(endpoint)
if err != nil {
return nil, err
}

diagnosticsResponseBody, err := ioutil.ReadAll(diagnosticsResponse.Body)
if err != nil {
return nil, err
}

var diagnosticsData Diagnostics
err = json.Unmarshal(diagnosticsResponseBody, &diagnosticsData)
if err != nil {
return nil, err
}

return &diagnosticsData, nil
}

func sendPing(pingService *ping.PingService, peer *ConnectedPeer) error {
timeout := 5 * time.Second

ctx, cancelCtx := context.WithTimeout(context.Background(), timeout)
defer cancelCtx()

peerID, err := libp2p_peer.IDB58Decode(peer.NetworkID)
if err != nil {
return err
}

pingService.Host.Peerstore().AddAddrs(
peerID,
parseMultiaddresses(peer.Multiaddresses),
2*timeout,
)

return (<-pingService.Ping(ctx, peerID)).Error
}

func parseMultiaddresses(addresses []string) []multiaddr.Multiaddr {
multiaddresses := make([]multiaddr.Multiaddr, 0)

for _, address := range addresses {
multiaddress, err := multiaddr.NewMultiaddr(address)
if err != nil {
fmt.Printf(
"could not parse address string [%v]: [%v]",
address,
err,
)
continue
}
multiaddresses = append(multiaddresses, multiaddress)
}

return multiaddresses
}

func isAvailable(pingError error) bool {
// no error - ping was successful and remote peer supports ping protocol
if pingError == nil {
return true
}

// `protocol is not supported` error - one of peer's
// addresses is publicly available but remote peer doesn't
// support ping protocol
if strings.Contains(pingError.Error(), "protocol not supported") {
return true
}

// all other cases - peer is PROBABLY not publicly available
return false
}
9 changes: 9 additions & 0 deletions inspector/connectivity-probe/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module connectivity-probe

go 1.13

require (
github.com/libp2p/go-libp2p v0.10.3
github.com/libp2p/go-libp2p-core v0.6.1
github.com/multiformats/go-multiaddr v0.2.2
)
Loading