File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "context"
5+ "fmt"
6+ "log"
7+
8+ libp2p "github.com/libp2p/go-libp2p"
9+ "github.com/libp2p/go-libp2p/core/peer"
10+ ma "github.com/multiformats/go-multiaddr"
11+ )
12+
13+ func main () {
14+ // Define the multiaddress to connect to
15+ targetAddr := "/dns/relay.dev.fx.land/tcp/4001/p2p/12D3KooWDRrBaAfPwsGJivBoUw5fE7ZpDiyfUjqgiURq2DEcL835/p2p-circuit/p2p/12D3KooWPtXiYKheTJyrQeNyPwjjPjCPi6QFdAK6qHN9c2U7Qhfe"
16+
17+ // Parse the multiaddress
18+ maddr , err := ma .NewMultiaddr (targetAddr )
19+ if err != nil {
20+ log .Fatalf ("Invalid multiaddress: %v" , err )
21+ }
22+
23+ // Extract the peer ID and address info from the multiaddress
24+ info , err := peer .AddrInfoFromP2pAddr (maddr )
25+ if err != nil {
26+ log .Fatalf ("Failed to extract peer info: %v" , err )
27+ }
28+
29+ // Create a new libp2p host
30+ host , err := libp2p .New ()
31+ if err != nil {
32+ log .Fatalf ("Failed to create libp2p host: %v" , err )
33+ }
34+ defer host .Close ()
35+
36+ fmt .Printf ("Libp2p host created. ID: %s\n " , host .ID ().String ())
37+
38+ // Connect to the target peer
39+ fmt .Printf ("Attempting to connect to peer: %s\n " , info .ID .String ())
40+ if err := host .Connect (context .Background (), * info ); err != nil {
41+ log .Fatalf ("Failed to connect to peer: %v" , err )
42+ }
43+
44+ fmt .Println ("Successfully connected to the peer!" )
45+ }
You can’t perform that action at this time.
0 commit comments