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
8d84ecd0
Commit
8d84ecd0
authored
7 years ago
by
Jeromy
Browse files
Options
Download
Email Patches
Plain Diff
functional parameters
parent
d0426805
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
revert-276-update-go-detect-race
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
v5.0.15
v5.0.14
v5.0.13
v5.0.12
v5.0.11
v5.0.10
v5.0.9
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
examples/echo/main.go
+8
-27
examples/echo/main.go
examples/libp2p-host/host.go
+9
-14
examples/libp2p-host/host.go
libp2p.go
+80
-5
libp2p.go
with
97 additions
and
46 deletions
+97
-46
examples/echo/main.go
View file @
8d84ecd0
...
...
@@ -18,12 +18,8 @@ import (
net
"github.com/libp2p/go-libp2p-net"
peer
"github.com/libp2p/go-libp2p-peer"
pstore
"github.com/libp2p/go-libp2p-peerstore"
swarm
"github.com/libp2p/go-libp2p-swarm"
bhost
"github.com/libp2p/go-libp2p/p2p/host/basic"
ma
"github.com/multiformats/go-multiaddr"
gologging
"github.com/whyrusleeping/go-logging"
msmux
"github.com/whyrusleeping/go-smux-multistream"
yamux
"github.com/whyrusleeping/go-smux-yamux"
)
// makeBasicHost creates a LibP2P host with a random peer ID listening on the
...
...
@@ -42,37 +38,21 @@ func makeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, error
// Generate a key pair for this host. We will use it at least
// to obtain a valid host ID.
priv
,
pub
,
err
:=
crypto
.
GenerateKeyPairWithReader
(
crypto
.
RSA
,
2048
,
r
)
priv
,
_
,
err
:=
crypto
.
GenerateKeyPairWithReader
(
crypto
.
RSA
,
2048
,
r
)
if
err
!=
nil
{
return
nil
,
err
}
// Obtain Peer ID from public key
pid
,
err
:=
peer
.
IDFromPublicKey
(
pub
)
if
err
!=
nil
{
return
nil
,
err
}
// Create a multiaddress
addr
,
err
:=
ma
.
NewMultiaddr
(
fmt
.
Sprintf
(
"/ip4/127.0.0.1/tcp/%d"
,
listenPort
))
if
err
!=
nil
{
return
nil
,
err
opts
:=
[]
libp2p
.
Option
{
libp2p
.
ListenAddrStrings
(
fmt
.
Sprintf
(
"/ip4/127.0.0.1/tcp/%d"
,
listenPort
)),
libp2p
.
WithPeerKey
(
priv
),
}
// Create a peerstore
ps
:=
pstore
.
NewPeerstore
()
// If using secio, we add the keys to the peerstore
// for this peer ID.
if
secio
{
ps
.
AddPrivKey
(
pid
,
priv
)
ps
.
AddPubKey
(
pid
,
pub
)
if
!
secio
{
opts
=
append
(
opts
,
libp2p
.
NoSecio
)
}
basicHost
,
err
:=
libp2p
.
NewWithCfg
(
ctx
,
&
libp2p
.
Config
{
ListenAddrs
:
[]
ma
.
Multiaddr
{
addr
},
})
basicHost
,
err
:=
libp2p
.
New
(
context
.
Background
(),
opts
...
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -82,6 +62,7 @@ func makeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, error
// Now we can build a full multiaddress to reach this host
// by encapsulating both addresses:
addr
:=
basicHost
.
Addrs
()[
0
]
fullAddr
:=
addr
.
Encapsulate
(
hostAddr
)
log
.
Printf
(
"I am %s
\n
"
,
fullAddr
)
if
secio
{
...
...
This diff is collapsed.
Click to expand it.
examples/libp2p-host/host.go
View file @
8d84ecd0
...
...
@@ -7,7 +7,6 @@ import (
libp2p
"github.com/libp2p/go-libp2p"
crypto
"github.com/libp2p/go-libp2p-crypto"
ma
"github.com/multiformats/go-multiaddr"
)
func
main
()
{
...
...
@@ -23,27 +22,23 @@ func main() {
fmt
.
Printf
(
"Hello World, my hosts ID is %s
\n
"
,
h
.
ID
())
// If you want more control over the configuration, you can fill out fields
// in the libp2p config, and use `NewWithCfg`
cfg
:=
new
(
libp2p
.
Config
)
// If you want more control over the configuration, you can specify some
// options to the constructor
// Set your own keypair
priv
,
_
,
err
:=
crypto
.
GenerateEd25519Key
(
rand
.
Reader
)
if
err
!=
nil
{
panic
(
err
)
}
cfg
.
PeerKey
=
priv
// Set your own listen address
maddr
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/0.0.0.0/tcp/9000"
)
if
err
!=
nil
{
panic
(
err
)
}
// The config takes an array of addresses, specify as many as you want.
cfg
.
ListenAddrs
=
[]
ma
.
Multiaddr
{
maddr
}
h2
,
err
:=
libp2p
.
New
(
ctx
,
// Use your own created keypair
libp2p
.
WithPeerKey
(
priv
),
h2
,
err
:=
libp2p
.
NewWithCfg
(
ctx
,
cfg
)
// Set your own listen address
// The config takes an array of addresses, specify as many as you want.
libp2p
.
ListenAddrStrings
(
"/ip4/0.0.0.0/tcp/9000"
),
)
if
err
!=
nil
{
panic
(
err
)
}
...
...
This diff is collapsed.
Click to expand it.
libp2p.go
View file @
8d84ecd0
...
...
@@ -32,13 +32,88 @@ type Config struct {
DisableSecio
bool
}
func
New
(
ctx
context
.
Context
)
(
host
.
Host
,
error
)
{
return
NewWithCfg
(
ctx
,
DefaultConfig
())
type
Option
func
(
cfg
*
Config
)
error
func
Transports
(
tpts
[]
transport
.
Transport
)
Option
{
return
func
(
cfg
*
Config
)
error
{
cfg
.
Transports
=
tpts
return
nil
}
}
func
ListenAddrStrings
(
s
...
string
)
Option
{
return
func
(
cfg
*
Config
)
error
{
for
_
,
addrstr
:=
range
s
{
a
,
err
:=
ma
.
NewMultiaddr
(
addrstr
)
if
err
!=
nil
{
return
err
}
cfg
.
ListenAddrs
=
append
(
cfg
.
ListenAddrs
,
a
)
}
return
nil
}
}
func
ListenAddrs
(
addrs
[]
ma
.
Multiaddr
)
Option
{
return
func
(
cfg
*
Config
)
error
{
cfg
.
ListenAddrs
=
append
(
cfg
.
ListenAddrs
,
addrs
...
)
return
nil
}
}
func
NoSecio
(
cfg
*
Config
)
error
{
cfg
.
DisableSecio
=
true
return
nil
}
func
WithMuxer
(
m
mux
.
Transport
)
Option
{
return
func
(
cfg
*
Config
)
error
{
cfg
.
Muxer
=
m
return
nil
}
}
func
WithPeerstore
(
ps
pstore
.
Peerstore
)
Option
{
return
func
(
cfg
*
Config
)
error
{
cfg
.
Peerstore
=
ps
return
nil
}
}
func
WithNetProtector
(
prot
pnet
.
Protector
)
Option
{
return
func
(
cfg
*
Config
)
error
{
cfg
.
Protector
=
prot
return
nil
}
}
func
WithBandwidthReporter
(
rep
metrics
.
Reporter
)
Option
{
return
func
(
cfg
*
Config
)
error
{
cfg
.
Reporter
=
rep
return
nil
}
}
func
New
(
ctx
context
.
Context
,
opts
...
Option
)
(
host
.
Host
,
error
)
{
cfg
:=
DefaultConfig
()
for
_
,
opt
:=
range
opts
{
if
err
:=
opt
(
cfg
);
err
!=
nil
{
return
nil
,
err
}
}
return
newWithCfg
(
ctx
,
cfg
)
}
func
WithPeerKey
(
sk
crypto
.
PrivKey
)
Option
{
return
func
(
cfg
*
Config
)
error
{
cfg
.
PeerKey
=
sk
return
nil
}
}
// Construct instantiates a libp2p host using information from the given
// config. `nil` may be passed to use default options.
func
NewWithCfg
(
ctx
context
.
Context
,
cfg
*
Config
)
(
host
.
Host
,
error
)
{
func
newWithCfg
(
ctx
context
.
Context
,
cfg
*
Config
)
(
host
.
Host
,
error
)
{
if
cfg
==
nil
{
cfg
=
DefaultConfig
()
}
...
...
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