Skip to content

Commit d904e69

Browse files
authored
Create main.go
1 parent 6052964 commit d904e69

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

cmd/relaytest/main.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

0 commit comments

Comments
 (0)