Hello. I’m experimenting with libp2p and have come across an issue that I can’t solve. I am running a host that connects to bootstrap peers. The following snippet from the terminal shows the list of bootstrap peers from the library “github.com/libp2p/go-libp2p-kad-dht” :
{QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN: [/dnsaddr/bootstrap.libp2p.io]}
[/dnsaddr/bootstrap.libp2p.io]}
{QmQCU2EcMqAqQPR2i9bChDtGNJNJchTbq5TbXJJJ16u19uLTa: [/dnsaddr/bootstrap.libp2p.io]}
[/dnsaddr/bootstrap.libp2p.io]}
{QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb: [/dnsaddr/bootstrap.libp2p.io]}
[/dnsaddr/bootstrap.libp2p.io]}
{QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt: [/dnsaddr/bootstrap.libp2p.io]}
[/dnsaddr/bootstrap.libp2p.io]}
{QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ: [/ip4/104.131.131.82/tcp/4001]}
After initializing the host (it is behind NAT, Upnp is off on the router) I make an http server as shown in the following code snippet:
...
bootstrapPeers := d.DefaultBootstrapPeers
fmt.Println(len(bootstrapPeers))
for _, addrInfo := range bootstrapPeers {
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
peerinfo, _ := peer.AddrInfoFromP2pAddr(addrInfo)
if err := h.Connect(timeoutCtx, *peerinfo); err != nil {
fmt.Println("Failed to connect to bootstrap peer:", err)
} else {
fmt.Println(peerinfo)
}
fmt.Println(peerinfo.Addrs)
}
listener, _ := gostream.Listen(h, p2phttp.DefaultP2PProtocol)
defer listener.Close()
go func() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hi1!"))
})
server := &http.Server{}
server.Serve(listener)
}()
for {
time.Sleep(10)
}
So I also make a client that connects to the bootstrap piram theme and sends a GET request to the server id. The following code snippet demonstrates this:
bootstrapPeers := d.DefaultBootstrapPeers
for _, addrInfo := range bootstrapPeers {
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
peerinfo, _ := peer.AddrInfoFromP2pAddr(addrInfo)
if err := h.Connect(timeoutCtx, *peerinfo); err != nil {
fmt.Println("Failed to connect to bootstrap peer:", err)
} else {
fmt.Println(peerinfo)
}
fmt.Println(peerinfo.Addrs)
}
tr := &http.Transport{}
tr.RegisterProtocol("libp2p", p2phttp.NewTransport(h))
client := &http.Client{Transport: tr}
res, err := client.Get("libp2p://QmaZLjJ4LhihSC6wvmFPZiadZdKPi57YxJiX4BhVux6gFk/hello")
if err != nil {
fmt.Println(err.Error())
return
}
defer res.Body.Close()
text, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(text))
I expect the client to find the host by its public key and establish a connection using bootstrap peers as an intermediary but instead I get the error message Get
“libp2p://QmaZLjJ4LhihSC6wvmFPZiadZdZdKPi57YxJiX4BhVux6gFk/hello”: routing: not found
Is it possible to use these bootstrap peers as an intermediary to establish a connection to a server behind NAT?