In ordet to limit the access to a relay host, I try to use the libp2p.PrivateNetwork() to build a private network. But I get the error message “failed to setup private network protector: expected 32 byte PSK” while running peerA. What is the correct way to use this function. Thanks
My relay host code :
key := make([]byte, 32)
_, err = rand.Read(key)
if err != nil {
panic(err)
}
// print for the peerA
fmt.Println(hex.EncodeToString(key))
host, err := libp2p.New(
context.Background(),
libp2p.Identity(priv),
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", listenPort)),
libp2p.EnableRelay(circuit.OptHop),
libp2p.ForceReachabilityPrivate(),
libp2p.PrivateNetwork(key),
)
if err != nil {
log.Printf("Failed to create relay-libp2p server: %s", err)
return
}
my peer A code:
// the key is equal to the value of relay code's random key
key := "d6a3ab80d31ab42650da9173c764380ab7e1421b4041329ff3e1a3cbe0860f6b"
relayID := "QmbSUTgoPDgRqP5S1Zz2fJJhtg8MFiQna3XAQTQRk9nDSG"
host, err := libp2p.New(context.Background(), libp2p.EnableRelay(),
libp2p.PrivateNetwork([]byte(key)))
if err != nil {
log.Printf("Failed to create h1: %s", err)
return
}
relayAddr := "/ip4/192.168.0.10/tcp/10001/p2p/" + relayID
relayAddrInfo, err := utils.Addr2info(relayAddr)
if err != nil {
log.Println("err: ", err)
return
}
if err := host.Connect(context.Background(), *relayAddrInfo); err != nil {
log.Printf("Failed to connect peerA and relay, err: %v", err)
return
}
It seems that I need to use the pnet.DecodeV1PSK(). And it works now.
relay code
certBytes, err := ioutil.ReadFile("./host-acl-with-privatenetwork/server.key")
if err != nil {
log.Println("unable to read client.pem")
return
}
block, _ := pem.Decode(certBytes)
priv, err := crypto.UnmarshalRsaPrivateKey(block.Bytes)
if err != nil {
panic(err)
}
listenPort := 10001
key := make([]byte, 32)
_, err = rand.Read(key)
if err != nil {
panic(err)
}
s := ""
s += fmt.Sprintln("/key/swarm/psk/1.0.0/")
s += fmt.Sprintln("/base16/")
s += fmt.Sprintf("%s", hex.EncodeToString(key))
fmt.Println(s)
psk, err := pnet.DecodeV1PSK(bytes.NewBuffer([]byte(s)))
if err != nil {
panic(err)
}
host, err := libp2p.New(
context.Background(),
libp2p.Identity(priv),
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", listenPort)),
libp2p.EnableRelay(circuit.OptHop),
libp2p.ForceReachabilityPrivate(),
libp2p.PrivateNetwork(psk),
)
if err != nil {
log.Printf("Failed to create relay-libp2p server: %s", err)
return
}
utils.PrintHostAddr("RELAY : ", host)
log.Println("ID: ", host.ID())
select {}
peerA code
key := "f8ee61d0330c9684a85ff34ba78b5433f7c5ae561c577538aef1c51b73009bab"
s := ""
s += fmt.Sprintln("/key/swarm/psk/1.0.0/")
s += fmt.Sprintln("/base16/")
s += fmt.Sprintf("%s", key)
psk, err := pnet.DecodeV1PSK(bytes.NewBuffer([]byte(s)))
if err != nil {
panic(err)
}
relayID := "QmbSUTgoPDgRqP5S1Zz2fJJhtg8MFiQna3XAQTQRk9nDSG"
host, err := libp2p.New(context.Background(), libp2p.EnableRelay(),
libp2p.PrivateNetwork(psk))
if err != nil {
log.Printf("Failed to create h1: %s", err)
return
}
relayAddr := "/ip4/192.168.0.10/tcp/10001/p2p/" + relayID
relayAddrInfo, err := utils.Addr2info(relayAddr)
if err != nil {
log.Println("err: ", err)
return
}
if err := host.Connect(context.Background(), *relayAddrInfo); err != nil {
log.Printf("Failed to connect peerA and relay, err: %v", err)
return
}
log.Println("success to connect to relay")
}
Hi @khalid-davis,
Thanks for this info.
On the peerA code, where did you get the key from? Is it the public key from the server.key?
Thanks