Commit 45c3b5d4 authored by Aviv Eyal's avatar Aviv Eyal Committed by Steven Allen
Browse files

Authenticate echo protocol messages

parent 0b1ed393
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
p2p "github.com/libp2p/go-libp2p/examples/multipro/pb" p2p "github.com/libp2p/go-libp2p/examples/multipro/pb"
protobufCodec "github.com/multiformats/go-multicodec/protobuf" protobufCodec "github.com/multiformats/go-multicodec/protobuf"
"gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host" "gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
"gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
) )
// pattern: /protocol-name/request-or-response-message/version // pattern: /protocol-name/request-or-response-message/version
...@@ -45,11 +46,21 @@ func (e EchoProtocol) onEchoRequest(s inet.Stream) { ...@@ -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) 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) 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{ 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} Message: data.Message}
// sign the data // sign the data
...@@ -84,6 +95,16 @@ func (e EchoProtocol) onEchoResponse(s inet.Stream) { ...@@ -84,6 +95,16 @@ func (e EchoProtocol) onEchoResponse(s inet.Stream) {
return 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 // locate request data and remove it if found
req, ok := e.requests[data.MessageData.Id] req, ok := e.requests[data.MessageData.Id]
if ok { if ok {
...@@ -105,7 +126,7 @@ func (e EchoProtocol) Echo(host host.Host) bool { ...@@ -105,7 +126,7 @@ func (e EchoProtocol) Echo(host host.Host) bool {
// create message data // create message data
req := &p2p.EchoRequest{ 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())} Message: fmt.Sprintf("Echo from %s", e.node.ID())}
signature, err := e.node.signProtoMessage(req) signature, err := e.node.signProtoMessage(req)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment