Skip to content

Commit

Permalink
Merge pull request #20 from teknomunk/master
Browse files Browse the repository at this point in the history
Add configuration file option for LibP2P listen addresses
  • Loading branch information
nasdf authored Feb 26, 2021
2 parents f79355f + 10bee26 commit abe236f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
9 changes: 2 additions & 7 deletions p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ import (
"github.com/libp2p/go-libp2p-tls"
)

// ListenAddresses is a list of addresses to listen on.
var ListenAddresses = []string{
"/ip4/0.0.0.0/tcp/9000",
}

const (
// LowWater is the minimum amount of connections to keep.
LowWater = 100
Expand All @@ -32,13 +27,13 @@ const (
)

// NewHost returns a new libp2p host and router.
func NewHost(ctx context.Context, priv crypto.PrivKey) (host.Host, routing.Routing, error) {
func NewHost(ctx context.Context, priv crypto.PrivKey, listen_addresses []string) (host.Host, routing.Routing, error) {
var router routing.Routing
var err error

host, err := libp2p.New(ctx,
libp2p.Identity(priv),
libp2p.ListenAddrStrings(ListenAddresses...),
libp2p.ListenAddrStrings(listen_addresses...),
libp2p.Security(libp2ptls.ID, libp2ptls.New),
libp2p.Security(noise.ID, noise.New),
libp2p.DefaultTransports,
Expand Down
6 changes: 6 additions & 0 deletions peer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type Config struct {
// PrivateKey is the base64 encoded private key.
PrivateKey string `json:"private_key"`

// Listen addresses for LibP2P
ListenAddresses []string `json:"listen_addresses"`

path string
}

Expand All @@ -40,6 +43,9 @@ func NewConfig(root string) (*Config, error) {
Author: data.NewAuthor(),
PrivateKey: encoded,
path: filepath.Join(root, ConfigFile),
ListenAddresses: []string{
"/ip4/0.0.0.0/tcp/9000",
},
}

return &config, nil
Expand Down
2 changes: 1 addition & 1 deletion peer/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func New(ctx context.Context, dstore datastore.Batching, config *Config) (*Node,
return nil, err
}

host, router, err := p2p.NewHost(ctx, priv)
host, router, err := p2p.NewHost(ctx, priv, config.ListenAddresses)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit abe236f

Please sign in to comment.