From 005c14f9a5bae2dd1124cb3542ad78a0b626ae86 Mon Sep 17 00:00:00 2001 From: juligasa <11684004+juligasa@users.noreply.github.com> Date: Mon, 19 Sep 2022 11:45:05 +0200 Subject: [PATCH] adding ping utility Former-commit-id: 1867cbb091f12a2aa8874e0eb5e5a3bfeb6005b1 --- backend/BUILD.plz | 10 ++++ backend/cmd/pingp2p/main.go | 98 +++++++++++++++++++++++++++++++++++++ dev | 4 ++ 3 files changed, 112 insertions(+) create mode 100644 backend/cmd/pingp2p/main.go diff --git a/backend/BUILD.plz b/backend/BUILD.plz index 4b435f8bcf..5a42f15fc4 100644 --- a/backend/BUILD.plz +++ b/backend/BUILD.plz @@ -22,3 +22,13 @@ go_binary( "//third_party:sqlite", ], ) + +go_binary( + name = "pingp2p", + srcs = glob(["./cmd/pingp2p/*.go"]), + out = "pingp2p-" + target_platform_triple(), + cgo = True, + gomod = "//:gomod", + package = "./cmd/pingp2p", + visibility = ["PUBLIC"], +) diff --git a/backend/cmd/pingp2p/main.go b/backend/cmd/pingp2p/main.go new file mode 100644 index 0000000000..055606f3c4 --- /dev/null +++ b/backend/cmd/pingp2p/main.go @@ -0,0 +1,98 @@ +package main + +import ( + "context" + "flag" + "fmt" + "os" + "os/signal" + "syscall" + + "github.com/libp2p/go-libp2p" + peerstore "github.com/libp2p/go-libp2p-core/peer" + "github.com/libp2p/go-libp2p/p2p/protocol/ping" + multiaddr "github.com/multiformats/go-multiaddr" +) + +func main() { + flag.Usage = func() { + fmt.Fprintf(os.Stderr, "Pings a provided p2p node address to check visibility\n") + flag.PrintDefaults() + } + listen := flag.Bool("listen", false, "Launch a node and then listen for incoming pings until signal is received") + flag.Parse() + + // start a libp2p node that listens on a random local TCP port, + // but without running the built-in ping protocol + node, err := libp2p.New( + libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"), + libp2p.Ping(false), + ) + if err != nil { + fmt.Printf("%s", err.Error()) + os.Exit(1) + } + + // configure our own ping protocol + pingService := &ping.PingService{Host: node} + node.SetStreamHandler(ping.ID, pingService.PingHandler) + + // print the node's PeerInfo in multiaddr format + peerInfo := peerstore.AddrInfo{ + ID: node.ID(), + Addrs: node.Addrs(), + } + addrs, err := peerstore.AddrInfoToP2pAddrs(&peerInfo) + if err != nil { + fmt.Printf("%s", err.Error()) + os.Exit(1) + } + + // if a remote peer has been passed on the command line, connect to it + // and send it 5 ping messages, otherwise wait for a signal to stop + if flag.NArg() == 1 { + if *listen { + fmt.Println("listen flag not allowed with positional arguments. ignoring listen") + } + fmt.Println("libp2p node address:", addrs[0]) + addr, err := multiaddr.NewMultiaddr(flag.Arg(0)) + if err != nil { + fmt.Printf("%s", err.Error()) + os.Exit(1) + } + peer, err := peerstore.AddrInfoFromP2pAddr(addr) + if err != nil { + fmt.Printf("%s", err.Error()) + os.Exit(1) + } + if err := node.Connect(context.Background(), *peer); err != nil { + fmt.Printf("%s", err.Error()) + os.Exit(1) + } + fmt.Println("sending 5 ping messages to", addr) + ch := pingService.Ping(context.Background(), peer.ID) + for i := 0; i < 5; i++ { + res := <-ch + fmt.Println("pinged", addr, "in", res.RTT) + } + } else if *listen { + // wait for a SIGINT or SIGTERM signal + fmt.Println("libp2p node address:", addrs[0]) + ch := make(chan os.Signal, 1) + signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) + <-ch + fmt.Println("Received signal, shutting down...") + } else if flag.NArg() == 0 { + flag.Usage() + os.Exit(1) + } else { + fmt.Println("Some arguments were misspelled, please check the doc") + os.Exit(1) + } + + // shut the node down + if err := node.Close(); err != nil { + fmt.Printf("%s", err.Error()) + os.Exit(1) + } +} diff --git a/dev b/dev index 9f99f851b0..61300c75d1 100755 --- a/dev +++ b/dev @@ -81,6 +81,10 @@ def main(): run("plz build //frontend:app //backend:mintterd") run("cargo tauri build --debug") + @cmd(cmds, "ping-p2p", "Execute ping utility to check visibility.") + def ping_p2p(args): + return run("plz run //backend:pingp2p", args=args) + @cmd(cmds, "run-backend", "Build and run mintterd binary for the current platform.") def run_backend(args): return run("plz run //backend:mintterd", args=args)