Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
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
b3d54ab2
Commit
b3d54ab2
authored
7 years ago
by
Aviv Eyal
Committed by
Steven Allen
7 years ago
Browse files
Options
Download
Email Patches
Plain Diff
Sign all messages
parent
0874c503
master
2018-Q4-OKR
feat/protobuf
fix/473
fix/no-custom-field
fix/reset-ping-stream
fix/revert-correct-external-addr
gx/update-nza0mn
jenkinsfile
multistream-ping
punching
v6.0.23
v6.0.22
v6.0.21
v6.0.20
v6.0.19
v6.0.18
v6.0.17
v6.0.16
v6.0.15
v6.0.14
v6.0.13
v6.0.12
v6.0.11
v6.0.10
v6.0.9
v6.0.8
v6.0.7
v6.0.6
v6.0.5
v6.0.4
v6.0.3
v6.0.2
v6.0.1
v6.0.0
v5.0.21
v5.0.20
v5.0.19
v5.0.18
v5.0.17
v5.0.16
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
examples/multipro/echo.go
+26
-6
examples/multipro/echo.go
examples/multipro/main.go
+4
-4
examples/multipro/main.go
examples/multipro/node.go
+13
-1
examples/multipro/node.go
examples/multipro/ping.go
+29
-7
examples/multipro/ping.go
examples/multipro/protocol.go
+2
-1
examples/multipro/protocol.go
with
74 additions
and
19 deletions
+74
-19
examples/multipro/echo.go
View file @
b3d54ab2
...
...
@@ -12,6 +12,7 @@ import (
"github.com/ipfs/go-ipfs/thirdparty/assert"
p2p
"github.com/libp2p/go-libp2p/examples/multipro/pb"
protobufCodec
"github.com/multiformats/go-multicodec/protobuf"
"gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
)
// pattern: /protocol-name/request-or-response-message/version
...
...
@@ -51,13 +52,23 @@ func (e EchoProtocol) onEchoRequest(s inet.Stream) {
MessageData
:
NewMessageData
(
e
.
node
.
ID
()
.
String
(),
data
.
MessageData
.
Id
,
false
),
Message
:
data
.
Message
}
// sign the data
signature
,
err
:=
e
.
node
.
signProtoMessage
(
resp
)
if
err
!=
nil
{
log
.
Fatal
(
"failed to sign response"
)
return
}
// add the signature to the message
resp
.
MessageData
.
Sign
=
string
(
signature
)
s
,
respErr
:=
e
.
node
.
NewStream
(
context
.
Background
(),
s
.
Conn
()
.
RemotePeer
(),
echoResponse
)
if
respErr
!=
nil
{
log
.
Fatal
(
respErr
)
return
}
ok
:=
send
DataObject
(
resp
,
s
)
ok
:=
send
ProtoMessage
(
resp
,
s
)
if
ok
{
log
.
Printf
(
"%s: Echo response to %s sent."
,
s
.
Conn
()
.
LocalPeer
()
.
String
(),
s
.
Conn
()
.
RemotePeer
()
.
String
())
...
...
@@ -89,21 +100,30 @@ func (e EchoProtocol) onEchoResponse(s inet.Stream) {
e
.
done
<-
true
}
func
(
e
EchoProtocol
)
Echo
(
node
*
Node
)
bool
{
log
.
Printf
(
"%s: Sending echo to: %s...."
,
e
.
node
.
ID
(),
node
.
ID
())
func
(
e
EchoProtocol
)
Echo
(
host
host
.
Host
)
bool
{
log
.
Printf
(
"%s: Sending echo to: %s...."
,
e
.
node
.
ID
(),
host
.
ID
())
// create message data
req
:=
&
p2p
.
EchoRequest
{
MessageData
:
NewMessageData
(
e
.
node
.
ID
()
.
String
(),
uuid
.
New
()
.
String
(),
false
),
Message
:
fmt
.
Sprintf
(
"Echo from %s"
,
e
.
node
.
ID
())}
s
,
err
:=
e
.
node
.
NewStream
(
context
.
Background
(),
node
.
ID
(),
echoRequest
)
signature
,
err
:=
e
.
node
.
signProtoMessage
(
req
)
if
err
!=
nil
{
log
.
Fatal
(
"failed to sign message"
)
return
false
}
// add the signature to the message
req
.
MessageData
.
Sign
=
string
(
signature
)
s
,
err
:=
e
.
node
.
NewStream
(
context
.
Background
(),
host
.
ID
(),
echoRequest
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
return
false
}
ok
:=
send
DataObject
(
req
,
s
)
ok
:=
send
ProtoMessage
(
req
,
s
)
if
!
ok
{
return
false
...
...
@@ -111,6 +131,6 @@ func (e EchoProtocol) Echo(node *Node) bool {
// store request so response handler has access to it
e
.
requests
[
req
.
MessageData
.
Id
]
=
req
log
.
Printf
(
"%s: Echo to: %s was sent. Message Id: %s, Message: %s"
,
e
.
node
.
ID
(),
node
.
ID
(),
req
.
MessageData
.
Id
,
req
.
Message
)
log
.
Printf
(
"%s: Echo to: %s was sent. Message Id: %s, Message: %s"
,
e
.
node
.
ID
(),
host
.
ID
(),
req
.
MessageData
.
Id
,
req
.
Message
)
return
true
}
This diff is collapsed.
Click to expand it.
examples/multipro/main.go
View file @
b3d54ab2
...
...
@@ -47,10 +47,10 @@ func main() {
log
.
Printf
(
"This is a conversation between %s and %s
\n
"
,
h1
.
ID
(),
h2
.
ID
())
// send messages using the protocols
h1
.
Ping
(
h2
)
h2
.
Ping
(
h1
)
h1
.
Echo
(
h2
)
h2
.
Echo
(
h1
)
h1
.
Ping
(
h2
.
Host
)
h2
.
Ping
(
h1
.
Host
)
h1
.
Echo
(
h2
.
Host
)
h2
.
Echo
(
h1
.
Host
)
// block until all responses have been processed
for
i
:=
0
;
i
<
4
;
i
++
{
...
...
This diff is collapsed.
Click to expand it.
examples/multipro/node.go
View file @
b3d54ab2
...
...
@@ -3,8 +3,19 @@ package main
import
(
host
"gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
peer
"gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
"github.com/gogo/protobuf/proto"
)
func
(
n
Node
)
signProtoMessage
(
message
proto
.
Message
)
([]
byte
,
error
)
{
// sign the data
data
,
err
:=
proto
.
Marshal
(
message
)
if
err
!=
nil
{
return
nil
,
err
}
return
n
.
signData
(
data
)
}
func
(
n
Node
)
signData
(
data
[]
byte
)
([]
byte
,
error
)
{
key
:=
n
.
Peerstore
()
.
PrivKey
(
n
.
ID
())
res
,
err
:=
key
.
Sign
(
data
)
...
...
@@ -17,11 +28,12 @@ func (n Node) verifyData(data []byte, signature []byte, signerHostId peer.ID) bo
return
res
==
true
&&
err
==
nil
}
// Node type -
implements
one or more p2p protocols
// Node type -
a host with
one or more
implemented
p2p protocols
type
Node
struct
{
host
.
Host
// lib-p2p host
*
PingProtocol
// ping protocol impl
*
EchoProtocol
// echo protocol impl
// add other protocols here...
}
// create a new node with its implemented protocols
...
...
This diff is collapsed.
Click to expand it.
examples/multipro/ping.go
View file @
b3d54ab2
...
...
@@ -11,6 +11,7 @@ import (
uuid
"github.com/google/uuid"
p2p
"github.com/libp2p/go-libp2p/examples/multipro/pb"
protobufCodec
"github.com/multiformats/go-multicodec/protobuf"
"gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
)
// pattern: /protocol-name/request-or-response-message/version
...
...
@@ -45,18 +46,29 @@ func (p PingProtocol) onPingRequest(s inet.Stream) {
log
.
Printf
(
"%s: Received ping request from %s. Message: %s"
,
s
.
Conn
()
.
LocalPeer
(),
s
.
Conn
()
.
RemotePeer
(),
data
.
Message
)
//
s
en
d
response
to sender
//
g
en
erate
response
message
log
.
Printf
(
"%s: Sending ping response to %s. Message id: %s..."
,
s
.
Conn
()
.
LocalPeer
(),
s
.
Conn
()
.
RemotePeer
(),
data
.
MessageData
.
Id
)
resp
:=
&
p2p
.
PingResponse
{
MessageData
:
NewMessageData
(
p
.
node
.
ID
()
.
String
(),
data
.
MessageData
.
Id
,
false
),
Message
:
fmt
.
Sprintf
(
"Ping response from %s"
,
p
.
node
.
ID
())}
// sign the data
signature
,
err
:=
p
.
node
.
signProtoMessage
(
resp
)
if
err
!=
nil
{
log
.
Fatal
(
"failed to sign response"
)
return
}
// add the signature to the message
resp
.
MessageData
.
Sign
=
string
(
signature
)
// send the response
s
,
respErr
:=
p
.
node
.
NewStream
(
context
.
Background
(),
s
.
Conn
()
.
RemotePeer
(),
pingResponse
)
if
respErr
!=
nil
{
log
.
Fatal
(
respErr
)
return
}
ok
:=
send
DataObject
(
resp
,
s
)
ok
:=
send
ProtoMessage
(
resp
,
s
)
if
ok
{
log
.
Printf
(
"%s: Ping response to %s sent."
,
s
.
Conn
()
.
LocalPeer
()
.
String
(),
s
.
Conn
()
.
RemotePeer
()
.
String
())
...
...
@@ -86,20 +98,30 @@ func (p PingProtocol) onPingResponse(s inet.Stream) {
p
.
done
<-
true
}
func
(
p
PingProtocol
)
Ping
(
node
*
Node
)
bool
{
log
.
Printf
(
"%s: Sending ping to: %s...."
,
p
.
node
.
ID
(),
node
.
ID
())
func
(
p
PingProtocol
)
Ping
(
host
host
.
Host
)
bool
{
log
.
Printf
(
"%s: Sending ping to: %s...."
,
p
.
node
.
ID
(),
host
.
ID
())
// create message data
req
:=
&
p2p
.
PingRequest
{
MessageData
:
NewMessageData
(
p
.
node
.
ID
()
.
String
(),
uuid
.
New
()
.
String
(),
false
),
Message
:
fmt
.
Sprintf
(
"Ping from %s"
,
p
.
node
.
ID
())}
s
,
err
:=
p
.
node
.
NewStream
(
context
.
Background
(),
node
.
Host
.
ID
(),
pingRequest
)
// sign the data
signature
,
err
:=
p
.
node
.
signProtoMessage
(
req
)
if
err
!=
nil
{
log
.
Fatal
(
"failed to sign pb data"
)
return
false
}
// add the signature to the message
req
.
MessageData
.
Sign
=
string
(
signature
)
s
,
err
:=
p
.
node
.
NewStream
(
context
.
Background
(),
host
.
ID
(),
pingRequest
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
return
false
}
ok
:=
send
DataObject
(
req
,
s
)
ok
:=
send
ProtoMessage
(
req
,
s
)
if
!
ok
{
return
false
...
...
@@ -107,6 +129,6 @@ func (p PingProtocol) Ping(node *Node) bool {
// store ref request so response handler has access to it
p
.
requests
[
req
.
MessageData
.
Id
]
=
req
log
.
Printf
(
"%s: Ping to: %s was sent. Message Id: %s, Message: %s"
,
p
.
node
.
ID
(),
node
.
ID
(),
req
.
MessageData
.
Id
,
req
.
Message
)
log
.
Printf
(
"%s: Ping to: %s was sent. Message Id: %s, Message: %s"
,
p
.
node
.
ID
(),
host
.
ID
(),
req
.
MessageData
.
Id
,
req
.
Message
)
return
true
}
This diff is collapsed.
Click to expand it.
examples/multipro/protocol.go
View file @
b3d54ab2
...
...
@@ -9,6 +9,7 @@ import (
"time"
//host "gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
//"bytes"
"github.com/gogo/protobuf/proto"
)
// node version
...
...
@@ -17,7 +18,7 @@ const clientVersion = "go-p2p-node/0.0.1"
// helper method - writes a protobuf go data object to a network stream
// data - reference of protobuf go data object to send (not the object itself)
// s - network stream to write the data to
func
send
DataObject
(
data
interface
{}
,
s
inet
.
Stream
)
bool
{
func
send
ProtoMessage
(
data
proto
.
Message
,
s
inet
.
Stream
)
bool
{
writer
:=
bufio
.
NewWriter
(
s
)
enc
:=
protobufCodec
.
Multicodec
(
nil
)
.
Encoder
(
writer
)
err
:=
enc
.
Encode
(
data
)
...
...
This diff is collapsed.
Click to expand it.
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
Menu
Projects
Groups
Snippets
Help