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
bbbc040b
Commit
bbbc040b
authored
7 years ago
by
Jeromy
Browse files
Options
Download
Email Patches
Plain Diff
incorporate lgierths opinions
parent
8d84ecd0
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
+2
-2
examples/echo/main.go
examples/libp2p-host/host.go
+1
-1
examples/libp2p-host/host.go
libp2p.go
+72
-32
libp2p.go
with
75 additions
and
35 deletions
+75
-35
examples/echo/main.go
View file @
bbbc040b
...
...
@@ -45,11 +45,11 @@ func makeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, error
opts
:=
[]
libp2p
.
Option
{
libp2p
.
ListenAddrStrings
(
fmt
.
Sprintf
(
"/ip4/127.0.0.1/tcp/%d"
,
listenPort
)),
libp2p
.
WithPeerKe
y
(
priv
),
libp2p
.
Identit
y
(
priv
),
}
if
!
secio
{
opts
=
append
(
opts
,
libp2p
.
No
Secio
)
opts
=
append
(
opts
,
libp2p
.
No
Encryption
()
)
}
basicHost
,
err
:=
libp2p
.
New
(
context
.
Background
(),
opts
...
)
...
...
This diff is collapsed.
Click to expand it.
examples/libp2p-host/host.go
View file @
bbbc040b
...
...
@@ -33,7 +33,7 @@ func main() {
h2
,
err
:=
libp2p
.
New
(
ctx
,
// Use your own created keypair
libp2p
.
WithPeerKe
y
(
priv
),
libp2p
.
Identit
y
(
priv
),
// Set your own listen address
// The config takes an array of addresses, specify as many as you want.
...
...
This diff is collapsed.
Click to expand it.
libp2p.go
View file @
bbbc040b
...
...
@@ -3,6 +3,7 @@ package libp2p
import
(
"context"
"crypto/rand"
"fmt"
crypto
"github.com/libp2p/go-libp2p-crypto"
host
"github.com/libp2p/go-libp2p-host"
...
...
@@ -34,9 +35,9 @@ type Config struct {
type
Option
func
(
cfg
*
Config
)
error
func
Transports
(
tpts
[]
transport
.
Transport
)
Option
{
func
Transports
(
tpts
...
transport
.
Transport
)
Option
{
return
func
(
cfg
*
Config
)
error
{
cfg
.
Transports
=
tpts
cfg
.
Transports
=
append
(
cfg
.
Transports
,
tpts
...
)
return
nil
}
}
...
...
@@ -54,70 +55,110 @@ func ListenAddrStrings(s ...string) Option {
}
}
func
ListenAddrs
(
addrs
[]
ma
.
Multiaddr
)
Option
{
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
type
transportEncOpt
int
const
(
EncPlaintext
=
transportEncOpt
(
0
)
EncSecio
=
transportEncOpt
(
1
)
)
func
TransportEncryption
(
tenc
...
transportEncOpt
)
Option
{
return
func
(
cfg
*
Config
)
error
{
if
len
(
tenc
)
!=
1
{
return
fmt
.
Errorf
(
"can only specify a single transport encryption option right now"
)
}
// TODO: actually make this pluggable, otherwise tls will get tricky
switch
tenc
[
0
]
{
case
EncPlaintext
:
cfg
.
DisableSecio
=
true
case
EncSecio
:
// noop
default
:
return
fmt
.
Errorf
(
"unrecognized transport encryption option: %d"
,
tenc
[
0
])
}
return
nil
}
}
func
WithMuxer
(
m
mux
.
Transport
)
Option
{
func
NoEncryption
()
Option
{
return
TransportEncryption
(
EncPlaintext
)
}
func
Muxer
(
m
mux
.
Transport
)
Option
{
return
func
(
cfg
*
Config
)
error
{
if
cfg
.
Muxer
!=
nil
{
return
fmt
.
Errorf
(
"cannot specify multiple muxer options"
)
}
cfg
.
Muxer
=
m
return
nil
}
}
func
With
Peerstore
(
ps
pstore
.
Peerstore
)
Option
{
func
Peerstore
(
ps
pstore
.
Peerstore
)
Option
{
return
func
(
cfg
*
Config
)
error
{
if
cfg
.
Peerstore
!=
nil
{
return
fmt
.
Errorf
(
"cannot specify multiple peerstore options"
)
}
cfg
.
Peerstore
=
ps
return
nil
}
}
func
WithNetProtect
or
(
prot
pnet
.
Protector
)
Option
{
func
PrivateNetw
or
k
(
prot
pnet
.
Protector
)
Option
{
return
func
(
cfg
*
Config
)
error
{
if
cfg
.
Protector
!=
nil
{
return
fmt
.
Errorf
(
"cannot specify multiple private network options"
)
}
cfg
.
Protector
=
prot
return
nil
}
}
func
With
BandwidthReporter
(
rep
metrics
.
Reporter
)
Option
{
func
BandwidthReporter
(
rep
metrics
.
Reporter
)
Option
{
return
func
(
cfg
*
Config
)
error
{
if
cfg
.
Reporter
!=
nil
{
return
fmt
.
Errorf
(
"cannot specify multiple bandwidth reporter options"
)
}
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
func
Identity
(
sk
crypto
.
PrivKey
)
Option
{
return
func
(
cfg
*
Config
)
error
{
if
cfg
.
PeerKey
!=
nil
{
return
fmt
.
Errorf
(
"cannot specify multiple identities"
)
}
}
return
newWithCfg
(
ctx
,
cfg
)
}
func
WithPeerKey
(
sk
crypto
.
PrivKey
)
Option
{
return
func
(
cfg
*
Config
)
error
{
cfg
.
PeerKey
=
sk
return
nil
}
}
func
newWithCfg
(
ctx
context
.
Context
,
cfg
*
Config
)
(
host
.
Host
,
error
)
{
if
cfg
==
nil
{
cfg
=
DefaultConfig
()
func
New
(
ctx
context
.
Context
,
opts
...
Option
)
(
host
.
Host
,
error
)
{
var
cfg
Config
for
_
,
opt
:=
range
opts
{
if
err
:=
opt
(
&
cfg
);
err
!=
nil
{
return
nil
,
err
}
}
return
newWithCfg
(
ctx
,
&
cfg
)
}
func
newWithCfg
(
ctx
context
.
Context
,
cfg
*
Config
)
(
host
.
Host
,
error
)
{
// If no key was given, generate a random 2048 bit RSA key
if
cfg
.
PeerKey
==
nil
{
priv
,
_
,
err
:=
crypto
.
GenerateKeyPairWithReader
(
crypto
.
RSA
,
2048
,
rand
.
Reader
)
...
...
@@ -166,16 +207,15 @@ func DefaultMuxer() mux.Transport {
return
tpt
}
func
Default
Config
()
*
Config
{
func
Default
s
(
cfg
*
Config
)
error
{
// Create a multiaddress that listens on a random port on all interfaces
addr
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/0.0.0.0/tcp/0"
)
if
err
!=
nil
{
panic
(
err
)
return
err
}
return
&
Config
{
ListenAddrs
:
[]
ma
.
Multiaddr
{
addr
},
Peerstore
:
pstore
.
NewPeerstore
(),
Muxer
:
DefaultMuxer
(),
}
cfg
.
ListenAddrs
=
[]
ma
.
Multiaddr
{
addr
}
cfg
.
Peerstore
=
pstore
.
NewPeerstore
()
cfg
.
Muxer
=
DefaultMuxer
()
return
nil
}
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