Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
45c3b5d4
Commit
45c3b5d4
authored
Nov 28, 2017
by
Aviv Eyal
Committed by
Steven Allen
Feb 21, 2018
Browse files
Authenticate echo protocol messages
parent
0b1ed393
Changes
1
Show whitespace changes
Inline
Side-by-side
examples/multipro/echo.go
View file @
45c3b5d4
...
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment