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
bbbc040b
Commit
bbbc040b
authored
Dec 15, 2017
by
Jeromy
Browse files
incorporate lgierths opinions
parent
8d84ecd0
Changes
3
Show whitespace changes
Inline
Side-by-side
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
...
)
...
...
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.
...
...
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
{
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
NoEncryption
()
Option
{
return
TransportEncryption
(
EncPlaintext
)
}
func
With
Muxer
(
m
mux
.
Transport
)
Option
{
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
}
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