diff --git a/cmd/gossamer/config.go b/cmd/gossamer/config.go index bc132c96d65..2dd38309d0d 100644 --- a/cmd/gossamer/config.go +++ b/cmd/gossamer/config.go @@ -647,6 +647,8 @@ func setDotNetworkConfig(ctx *cli.Context, tomlCfg ctoml.NetworkConfig, cfg *dot cfg.MaxPeers = tomlCfg.MaxPeers cfg.PersistentPeers = tomlCfg.PersistentPeers cfg.DiscoveryInterval = time.Second * time.Duration(tomlCfg.DiscoveryInterval) + cfg.NodeKey = tomlCfg.NodeKey + cfg.ListenAddress = tomlCfg.ListenAddress // check --port flag and update node configuration if port := ctx.Uint(PortFlag.Name); port != 0 { @@ -693,6 +695,11 @@ func setDotNetworkConfig(ctx *cli.Context, tomlCfg ctoml.NetworkConfig, cfg *dot cfg.NodeKey = nodekey } + // checx --listen-addr flag and update node configuration + if listenAddress := ctx.String(ListenAddressFlag.Name); listenAddress != "" { + cfg.ListenAddress = listenAddress + } + if len(cfg.PersistentPeers) == 0 { cfg.PersistentPeers = []string(nil) } diff --git a/cmd/gossamer/config_test.go b/cmd/gossamer/config_test.go index 113a1d902c1..5796a779615 100644 --- a/cmd/gossamer/config_test.go +++ b/cmd/gossamer/config_test.go @@ -117,6 +117,7 @@ func TestInitConfigFromFlags(t *testing.T) { // TestGlobalConfigFromFlags tests createDotGlobalConfig using relevant global flags func TestGlobalConfigFromFlags(t *testing.T) { + t.Parallel() defaultGlobalConfig := dot.GlobalConfig{ Name: "carpet-drill-8904", ID: "gssmr", @@ -400,6 +401,7 @@ func TestCoreConfigFromFlags(t *testing.T) { // TestNetworkConfigFromFlags tests createDotNetworkConfig using relevant network flags func TestNetworkConfigFromFlags(t *testing.T) { + t.Parallel() defaultNetworkCfg := dot.NetworkConfig{ Port: 7001, MinPeers: 1, @@ -500,6 +502,16 @@ func TestNetworkConfigFromFlags(t *testing.T) { NodeKey: "testkey", }, }, + "Test_gossamer_--listen-addr": { + []string{"app", "--listen-addr", "/ip4/0.0.0.0/tcp/1234/ws"}, + dot.NetworkConfig{ + Port: defaultNetworkCfg.Port, + DiscoveryInterval: defaultNetworkCfg.DiscoveryInterval, + MinPeers: defaultNetworkCfg.MinPeers, + MaxPeers: defaultNetworkCfg.MaxPeers, + ListenAddress: "/ip4/0.0.0.0/tcp/1234/ws", + }, + }, } for key, c := range testcases { diff --git a/cmd/gossamer/flags.go b/cmd/gossamer/flags.go index 394507c214c..335f7385bc1 100644 --- a/cmd/gossamer/flags.go +++ b/cmd/gossamer/flags.go @@ -239,6 +239,11 @@ var ( Name: "node-key", Usage: "Overrides the secret Ed25519 key to use for libp2p", } + // ListenAddressFlag uses the supplied multiaddress string as the address to listen on + ListenAddressFlag = cli.StringFlag{ + Name: "listen-addr", + Usage: "Listen on this multiaddress", + } ) // RPC service configuration flags @@ -418,6 +423,7 @@ var ( &PublicIPFlag, &PublicDNSFlag, &NodeKeyFlag, + &ListenAddressFlag, // rpc flags &RPCEnabledFlag, diff --git a/dot/config.go b/dot/config.go index 79c88dd9d1a..863997f126e 100644 --- a/dot/config.go +++ b/dot/config.go @@ -104,6 +104,7 @@ type NetworkConfig struct { PublicIP string PublicDNS string NodeKey string + ListenAddress string } // CoreConfig is to marshal/unmarshal toml core config vars diff --git a/dot/config/toml/config.go b/dot/config/toml/config.go index 9fce33aa2c4..2e01816c705 100644 --- a/dot/config/toml/config.go +++ b/dot/config/toml/config.go @@ -64,6 +64,8 @@ type NetworkConfig struct { DiscoveryInterval int `toml:"discovery-interval,omitempty"` PublicIP string `toml:"public-ip,omitempty"` PublicDNS string `toml:"public-dns,omitempty"` + NodeKey string `toml:"node-key,omitempty"` + ListenAddress string `toml:"listen-addr,omitempty"` } // CoreConfig is to marshal/unmarshal toml core config vars diff --git a/dot/network/config.go b/dot/network/config.go index 7a6d2d590a6..824a4e45c7f 100644 --- a/dot/network/config.go +++ b/dot/network/config.go @@ -84,6 +84,8 @@ type Config struct { NoBootstrap bool // NoMDNS disables MDNS discovery NoMDNS bool + // ListenAddress multiaddress to listen on + ListenAddress string MinPeers int MaxPeers int diff --git a/dot/network/host.go b/dot/network/host.go index 04f826dcc76..93335ec9ec1 100644 --- a/dot/network/host.go +++ b/dot/network/host.go @@ -82,7 +82,11 @@ type host struct { func newHost(ctx context.Context, cfg *Config) (*host, error) { // create multiaddress (without p2p identity) - addr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", cfg.Port)) + addrString := fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", cfg.Port) + if cfg.ListenAddress != "" { + addrString = cfg.ListenAddress + } + addr, err := ma.NewMultiaddr(addrString) if err != nil { return nil, err } diff --git a/dot/services.go b/dot/services.go index eca4f9e7226..60a26ca4ed5 100644 --- a/dot/services.go +++ b/dot/services.go @@ -308,6 +308,7 @@ func (nodeBuilder) createNetworkService(cfg *Config, stateSrvc *state.Service, PublicDNS: cfg.Network.PublicDNS, Metrics: metrics.NewIntervalConfig(cfg.Global.PublishMetrics), NodeKey: cfg.Network.NodeKey, + ListenAddress: cfg.Network.ListenAddress, } networkSrvc, err := network.NewService(&networkConfig)