Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mybee
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
mybee
Commits
afe42f9b
Commit
afe42f9b
authored
Jan 22, 2020
by
Janos Guljas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add bootnode cli option and adjust pingpong api
parent
c29012e8
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
36 additions
and
32 deletions
+36
-32
README.md
README.md
+6
-8
start.go
cmd/bee/cmd/start.go
+5
-2
go.mod
go.mod
+1
-0
go.sum
go.sum
+2
-0
pingpong.go
pkg/api/pingpong.go
+3
-19
router.go
pkg/api/router.go
+3
-2
libp2p.go
pkg/p2p/libp2p/libp2p.go
+16
-1
No files found.
README.md
View file @
afe42f9b
...
...
@@ -15,26 +15,24 @@ Docker image `janos/bee`.
## Usage (experimental api)
Execute the command
s in two terminals to start
`node 1`
and
`node 2
`
:
Execute the command
terminals to start
`node 1
`
:
```
sh
bee start
--api-addr
:8501
--p2p-addr
:30401
```
Use one of the multiaddresses as bootnode for
`node 2`
in order to connect them:
```
sh
bee start
--api-addr
:8502
--p2p-addr
:30402
bee start
--api-addr
:8502
--p2p-addr
:30402
--bootnode
/ip4/127.0.0.1/tcp/30401/p2p/QmT4TNB4cKYanUjdYodw1Cns8cuVaRVo24hHNYcT7JjkTB
```
Copy one of the multiaddresses from one running instance.
Make an HTTP request to
`localhost:{PORT1}/pingpong/{MULTIADDRESS2}`
like:
Use the last part of
`node 1`
multiaddress to ping it using
`node 2`
by making an HTTP request to
`localhost:{PORT2}/pingpong/{ID1}`
like:
```
sh
curl localhost:850
1/pingpong/ip4/127.0.0.1/tcp/60304/p2p/Qmdao2FbfSK8ZcFxuUVmVDPUJifgRmbofNWH21WQESZm7x
curl localhost:850
2/pingpong/QmT4TNB4cKYanUjdYodw1Cns8cuVaRVo24hHNYcT7JjkTB
```
Ping pong messages should be exchanged from
`node 1`
(listening on
`PORT1`
) to
`node 2`
(with multiaddress
`MULTIADDRESS2`
).
## Structure
-
cmd/bee - a simple application integrating p2p and pingpong service
...
...
cmd/bee/cmd/start.go
View file @
afe42f9b
...
...
@@ -20,6 +20,7 @@ func (c *command) initStartCmd() (err error) {
optionNameP2PAddr
=
"p2p-addr"
optionNameP2PDisableWS
=
"p2p-disable-ws"
optionNameP2PDisableQUIC
=
"p2p-disable-quic"
optionNameBootnodes
=
"bootnode"
)
cmd
:=
&
cobra
.
Command
{
...
...
@@ -40,6 +41,7 @@ func (c *command) initStartCmd() (err error) {
Addr
:
c
.
config
.
GetString
(
optionNameP2PAddr
),
DisableWS
:
c
.
config
.
GetBool
(
optionNameP2PDisableWS
),
DisableQUIC
:
c
.
config
.
GetBool
(
optionNameP2PDisableQUIC
),
Bootnodes
:
c
.
config
.
GetStringSlice
(
optionNameBootnodes
),
// Routing: func(h host.Host) (r routing.PeerRouting, err error) {
// idht, err = dht.New(ctx, h)
// return idht, err
...
...
@@ -84,8 +86,9 @@ func (c *command) initStartCmd() (err error) {
cmd
.
Flags
()
.
String
(
optionNameAPIAddr
,
":8500"
,
"HTTP API listen address"
)
cmd
.
Flags
()
.
String
(
optionNameP2PAddr
,
":30399"
,
"P2P listen address"
)
cmd
.
Flags
()
.
Bool
(
optionNameP2PDisableWS
,
false
,
"Disable P2P WebSocket protocol"
)
cmd
.
Flags
()
.
Bool
(
optionNameP2PDisableQUIC
,
false
,
"Disable P2P QUIC protocol"
)
cmd
.
Flags
()
.
Bool
(
optionNameP2PDisableWS
,
false
,
"disable P2P WebSocket protocol"
)
cmd
.
Flags
()
.
Bool
(
optionNameP2PDisableQUIC
,
false
,
"disable P2P QUIC protocol"
)
cmd
.
Flags
()
.
StringSlice
(
optionNameBootnodes
,
nil
,
"initial nodes to connect to"
)
if
err
:=
c
.
config
.
BindPFlags
(
cmd
.
Flags
());
err
!=
nil
{
return
err
...
...
go.mod
View file @
afe42f9b
...
...
@@ -5,6 +5,7 @@ go 1.13
require (
github.com/gogo/protobuf v1.3.1
github.com/gorilla/handlers v1.4.2
github.com/gorilla/mux v1.7.3
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/ipfs/go-log v1.0.1 // indirect
github.com/libp2p/go-libp2p v0.5.0
...
...
go.sum
View file @
afe42f9b
...
...
@@ -99,6 +99,8 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGa
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/handlers v1.4.2 h1:0QniY0USkHQ1RGCLfKxeNHK9bkDHGRYGNDFBCS+YARg=
github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
...
...
pkg/api/pingpong.go
View file @
afe42f9b
...
...
@@ -3,34 +3,18 @@ package api
import
(
"fmt"
"net/http"
"strings"
"github.com/
multiformats/go-multiaddr
"
"github.com/
gorilla/mux
"
)
func
(
s
*
server
)
pingpongHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
target
:=
strings
.
TrimPrefix
(
r
.
URL
.
Path
,
"/pingpong"
)
addr
,
err
:=
multiaddr
.
NewMultiaddr
(
target
)
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusBadRequest
)
fmt
.
Fprintln
(
w
,
"invalid address"
,
target
,
err
)
return
}
peerID
:=
mux
.
Vars
(
r
)[
"peer-id"
]
ctx
:=
r
.
Context
()
peerID
,
err
:=
s
.
P2P
.
Connect
(
ctx
,
addr
)
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
fmt
.
Fprintln
(
w
,
"connect error"
,
addr
,
err
)
return
}
rtt
,
err
:=
s
.
Pingpong
.
Ping
(
ctx
,
peerID
,
"hey"
,
"there"
,
","
,
"how are"
,
"you"
,
"?"
)
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
fmt
.
Fprintln
(
w
,
"ping error"
,
addr
,
err
)
fmt
.
Fprintln
(
w
,
"ping error"
,
peerID
,
err
)
return
}
...
...
pkg/api/router.go
View file @
afe42f9b
...
...
@@ -5,17 +5,18 @@ import (
"net/http"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"resenje.org/web"
)
func
(
s
*
server
)
setupRouting
()
{
baseRouter
:=
http
.
NewServeMux
()
baseRouter
:=
mux
.
NewRouter
()
baseRouter
.
HandleFunc
(
"/robots.txt"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
fmt
.
Fprintln
(
w
,
"User-agent: *
\n
Disallow: /"
)
})
baseRouter
.
HandleFunc
(
"/pingpong/"
,
s
.
pingpongHandler
)
baseRouter
.
HandleFunc
(
"/pingpong/
{peer-id}
"
,
s
.
pingpongHandler
)
s
.
Handler
=
web
.
ChainHandlers
(
handlers
.
CompressHandler
,
...
...
pkg/p2p/libp2p/libp2p.go
View file @
afe42f9b
...
...
@@ -33,6 +33,7 @@ type Options struct {
Addr
string
DisableWS
bool
DisableQUIC
bool
Bootnodes
[]
string
// PrivKey []byte
// Routing func(host.Host) (routing.PeerRouting, error)
}
...
...
@@ -128,7 +129,21 @@ func New(ctx context.Context, o Options) (*Service, error) {
return
nil
,
fmt
.
Errorf
(
"autonat: %w"
,
err
)
}
return
&
Service
{
host
:
h
},
nil
s
:=
&
Service
{
host
:
h
}
// TODO: be more resilient on connection errors and connect in parallel
for
_
,
a
:=
range
o
.
Bootnodes
{
addr
,
err
:=
ma
.
NewMultiaddr
(
a
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"bootnode %s: %w"
,
a
,
err
)
}
if
_
,
err
:=
s
.
Connect
(
ctx
,
addr
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"connect to bootnode %s: %w"
,
a
,
err
)
}
}
return
s
,
nil
}
func
(
s
*
Service
)
AddProtocol
(
p
p2p
.
ProtocolSpec
)
(
err
error
)
{
...
...
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