Skip to content

Commit

Permalink
fix(network): custom and default bind option
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Mar 11, 2019
1 parent 0f6c783 commit 2a9604f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
3 changes: 2 additions & 1 deletion core/cmd/berty/daemon.go
Expand Up @@ -73,7 +73,7 @@ func daemonSetupFlags(flags *pflag.FlagSet, opts *daemonOptions) {
flags.BoolVar(&opts.dhtServer, "dht-server", false, "enable dht server")
flags.BoolVar(&opts.ble, "ble", false, "enable ble transport")
flags.BoolVar(&opts.PrivateNetwork, "private-network", true, "enable private network with the default swarm key")
flags.StringSliceVar(&opts.bindP2P, "bind-p2p", []string{"/ip4/0.0.0.0/tcp/0", "/ip4/0.0.0.0/udp/0/quic", "/ble/00000000-0000-0000-0000-000000000000"}, "p2p listening address")
flags.StringSliceVar(&opts.bindP2P, "bind-p2p", []string{}, "p2p listening address")
flags.StringVar(&opts.SwarmKeyPath, "swarm-key", "", "path to a custom swarm key, only peers that use the same swarm key will be able to talk with you")
// flags.StringSliceVar(&opts.bindP2P, "bind-p2p", []string{"/ip4/0.0.0.0/tcp/0"}, "p2p listening address")
_ = viper.BindPFlags(flags)
Expand Down Expand Up @@ -149,6 +149,7 @@ func daemon(opts *daemonOptions) error {
network.New(ctx,
network.WithDefaultOptions(),
network.WithConfig(&network_config.Config{
DefaultBind: len(opts.bindP2P) == 0,
Bind: opts.bindP2P,
MDNS: opts.mdns,
DHT: opts.dhtServer,
Expand Down
24 changes: 18 additions & 6 deletions core/network/config/config.go
Expand Up @@ -58,7 +58,8 @@ var BootstrapIpfs = []string{
type Config struct {
libp2p_config.Config `json:"-"`

Bind []string
DefaultBind bool
Bind []string

MDNS bool
DHT bool
Expand Down Expand Up @@ -90,6 +91,8 @@ type Option func(cfg *Config) error

// Override override safely the current config
func (cfg *Config) Override(override *Config) error {
cfg.DefaultBind = override.DefaultBind
cfg.Bind = override.Bind
cfg.MDNS = override.MDNS
cfg.WS = override.WS
cfg.TCP = override.TCP
Expand Down Expand Up @@ -132,11 +135,6 @@ func (cfg *Config) Apply(ctx context.Context, opts ...Option) error {
cfg.Bootstrap = append(cfg.Bootstrap, DefaultBootstrap...)
}

libp2pOpts = append(libp2pOpts, libp2p.DefaultListenAddrs)
if len(cfg.Bind) > 0 {
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings(cfg.Bind...))
}

// add ws transport
if cfg.WS {
libp2pOpts = append(libp2pOpts, libp2p.Transport(ws.New))
Expand All @@ -145,16 +143,30 @@ func (cfg *Config) Apply(ctx context.Context, opts ...Option) error {
// add tcp transport
if cfg.TCP {
libp2pOpts = append(libp2pOpts, libp2p.Transport(tcp.NewTCPTransport))
if cfg.DefaultBind {
cfg.Bind = append(cfg.Bind, "/ip4/0.0.0.0/tcp/0", "/ip6/::/tcp/0")
}
}

// add ble transport
if cfg.BLE {
libp2pOpts = append(libp2pOpts, libp2p.Transport(ble.NewTransport))
if cfg.DefaultBind {
cfg.Bind = append(cfg.Bind, "/ble/00000000-0000-0000-0000-000000000000")
}
}

// add quic transport
if cfg.QUIC {
libp2pOpts = append(libp2pOpts, libp2p.Transport(quic.NewTransport))
if cfg.DefaultBind {
cfg.Bind = append(cfg.Bind, "/ip4/0.0.0.0/udp/0/quic", "/ip6/::/udp/0/quic")
}
}

// add listening adresses after setup transport
if len(cfg.Bind) > 0 {
libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings(cfg.Bind...))
}

// relay
Expand Down
24 changes: 23 additions & 1 deletion core/network/options.go
Expand Up @@ -17,10 +17,11 @@ func WithConfig(override *config.Config) config.Option {

func WithDefaultOptions() config.Option {
return ChainOptions(
EnableDefaultBind(),
EnableDefaultBootstrap(),
EnablePing(),
EnableMDNS(),
EnableWS(),
DisableWS(),
EnableTCP(),
EnableBLE(),
EnableQUIC(),
Expand Down Expand Up @@ -144,6 +145,27 @@ func Bootstrap(addr ...string) config.Option {
}
}

func EnableDefaultBind() config.Option {
return func(cfg *config.Config) error {
cfg.DefaultBind = true
return nil
}
}

func DisableDefaultBind() config.Option {
return func(cfg *config.Config) error {
cfg.DefaultBind = false
return nil
}
}

func Bind(maddr ...string) config.Option {
return func(cfg *config.Config) error {
cfg.Bind = append(cfg.Bind, maddr...)
return nil
}
}

func EnableDHT() config.Option {
return func(cfg *config.Config) error {
cfg.DHT = true
Expand Down

0 comments on commit 2a9604f

Please sign in to comment.