Skip to content

Commit 10bee26

Browse files
committed
Add configuration file option for LibP2P listen addresses
1 parent 4a3b9b7 commit 10bee26

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

p2p/host.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ import (
1717
"github.com/libp2p/go-libp2p-tls"
1818
)
1919

20-
// ListenAddresses is a list of addresses to listen on.
21-
var ListenAddresses = []string{
22-
"/ip4/0.0.0.0/tcp/9000",
23-
}
24-
2520
const (
2621
// LowWater is the minimum amount of connections to keep.
2722
LowWater = 100
@@ -32,13 +27,13 @@ const (
3227
)
3328

3429
// NewHost returns a new libp2p host and router.
35-
func NewHost(ctx context.Context, priv crypto.PrivKey) (host.Host, routing.Routing, error) {
30+
func NewHost(ctx context.Context, priv crypto.PrivKey, listen_addresses []string) (host.Host, routing.Routing, error) {
3631
var router routing.Routing
3732
var err error
3833

3934
host, err := libp2p.New(ctx,
4035
libp2p.Identity(priv),
41-
libp2p.ListenAddrStrings(ListenAddresses...),
36+
libp2p.ListenAddrStrings(listen_addresses...),
4237
libp2p.Security(libp2ptls.ID, libp2ptls.New),
4338
libp2p.Security(noise.ID, noise.New),
4439
libp2p.DefaultTransports,

peer/config.go

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ type Config struct {
2121
// PrivateKey is the base64 encoded private key.
2222
PrivateKey string `json:"private_key"`
2323

24+
// Listen addresses for LibP2P
25+
ListenAddresses []string `json:"listen_addresses"`
26+
2427
path string
2528
}
2629

@@ -40,6 +43,9 @@ func NewConfig(root string) (*Config, error) {
4043
Author: data.NewAuthor(),
4144
PrivateKey: encoded,
4245
path: filepath.Join(root, ConfigFile),
46+
ListenAddresses: []string{
47+
"/ip4/0.0.0.0/tcp/9000",
48+
},
4349
}
4450

4551
return &config, nil

peer/node.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func New(ctx context.Context, dstore datastore.Batching, config *Config) (*Node,
4141
return nil, err
4242
}
4343

44-
host, router, err := p2p.NewHost(ctx, priv)
44+
host, router, err := p2p.NewHost(ctx, priv, config.ListenAddresses)
4545
if err != nil {
4646
return nil, err
4747
}

0 commit comments

Comments
 (0)