Authenticate echo protocol messages
@@ -13,6 +13,7 @@ import (
p2p "github.com/libp2p/go-libp2p/examples/multipro/pb"
protobufCodec "github.com/multiformats/go-multicodec/protobuf"
"gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
+ "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
)
// pattern: /protocol-name/request-or-response-message/version
@@ -45,11 +46,21 @@ func (e EchoProtocol) onEchoRequest(s inet.Stream) {
log.Printf("%s: Received echo request from %s. Message: %s", s.Conn().LocalPeer(), s.Conn().RemotePeer(), data.Message)
+ valid := e.node.authenticateMessage(data, data.MessageData)
+
+ if !valid {
+ log.Fatal("Failed to authenticate message")
+ return
+ } else {
+ log.Print("Authenticated request content was generated by claimed node :-)")
+ }
+
+
log.Printf("%s: Sending echo response to %s. Message id: %s...", s.Conn().LocalPeer(), s.Conn().RemotePeer(), data.MessageData.Id)
- // send response to request send using the message string he provided
+ // send response to the request using the message string he provided
resp := &p2p.EchoResponse{
- MessageData: NewMessageData(e.node.ID().String(), data.MessageData.Id, false),
+ MessageData: NewMessageData(peer.IDB58Encode(e.node.ID()), data.MessageData.Id, false),
Message: data.Message}
// sign the data
@@ -84,6 +95,16 @@ func (e EchoProtocol) onEchoResponse(s inet.Stream) {
return
}
+ // authenticate message content
+ valid := e.node.authenticateMessage(data, data.MessageData)
+
+ if !valid {
+ log.Fatal("Failed to authenticate message")
+ return
+ } else {
+ log.Print("Authenticated response content generated by claimed node :-)")
+ }
+
// locate request data and remove it if found
req, ok := e.requests[data.MessageData.Id]
if ok {
@@ -105,7 +126,7 @@ func (e EchoProtocol) Echo(host host.Host) bool {
// create message data
req := &p2p.EchoRequest{
- MessageData: NewMessageData(e.node.ID().String(), uuid.New().String(), false),
+ MessageData: NewMessageData(peer.IDB58Encode(e.node.ID()), uuid.New().String(), false),
Message: fmt.Sprintf("Echo from %s", e.node.ID())}
signature, err := e.node.signProtoMessage(req)
