From: yahtoo Date: Thu, 18 Apr 2019 09:33:30 +0000 (+0800) Subject: Add LAN discover cli config option (#1712) X-Git-Tag: 2.0.0-alpha~80^2~10 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=82856be0f2ccc9c5b399e965bcbc3a8fd2354b29;p=bytom%2Fbytom.git Add LAN discover cli config option (#1712) * Add LAN discover cli config option * Fix test file error * Fix review bug --- diff --git a/cmd/bytomd/commands/run_node.go b/cmd/bytomd/commands/run_node.go index c61ed341..67a5cfa4 100644 --- a/cmd/bytomd/commands/run_node.go +++ b/cmd/bytomd/commands/run_node.go @@ -41,6 +41,7 @@ func init() { runNodeCmd.Flags().String("p2p.seeds", config.P2P.Seeds, "Comma delimited host:port seed nodes") runNodeCmd.Flags().String("p2p.node_key", config.P2P.PrivateKey, "Node key for p2p communication") runNodeCmd.Flags().Bool("p2p.skip_upnp", config.P2P.SkipUPNP, "Skip UPNP configuration") + runNodeCmd.Flags().Bool("p2p.lan_discoverable", config.P2P.LANDiscover, "Whether the node can be discovered by nodes in the LAN") runNodeCmd.Flags().Int("p2p.max_num_peers", config.P2P.MaxNumPeers, "Set max num peers") runNodeCmd.Flags().Int("p2p.handshake_timeout", config.P2P.HandshakeTimeout, "Set handshake timeout") runNodeCmd.Flags().Int("p2p.dial_timeout", config.P2P.DialTimeout, "Set dial timeout") diff --git a/config/config.go b/config/config.go index f1de23c3..eddb1787 100644 --- a/config/config.go +++ b/config/config.go @@ -149,6 +149,7 @@ type P2PConfig struct { PrivateKey string `mapstructure:"node_key"` NodeKeyFile string `mapstructure:"node_key_file"` SkipUPNP bool `mapstructure:"skip_upnp"` + LANDiscover bool `mapstructure:"lan_discoverable"` MaxNumPeers int `mapstructure:"max_num_peers"` HandshakeTimeout int `mapstructure:"handshake_timeout"` DialTimeout int `mapstructure:"dial_timeout"` @@ -164,6 +165,7 @@ func DefaultP2PConfig() *P2PConfig { ListenAddress: "tcp://0.0.0.0:46656", NodeKeyFile: "nodekey", SkipUPNP: false, + LANDiscover: true, MaxNumPeers: 50, HandshakeTimeout: 30, DialTimeout: 3, diff --git a/p2p/switch.go b/p2p/switch.go index ecddbeb2..f2148d8b 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -105,8 +105,9 @@ func NewSwitch(config *cfg.Config) (*Switch, error) { if err != nil { return nil, err } - - lanDiscv = mdns.NewLANDiscover(mdns.NewProtocol(), int(l.ExternalAddress().Port)) + if config.P2P.LANDiscover { + lanDiscv = mdns.NewLANDiscover(mdns.NewProtocol(), int(l.ExternalAddress().Port)) + } } return newSwitch(config, discv, lanDiscv, blacklistDB, l, privKey, listenAddr) @@ -157,9 +158,10 @@ func (sw *Switch) OnStart() error { // OnStop implements BaseService. It stops all listeners, peers, and reactors. func (sw *Switch) OnStop() { - if sw.lanDiscv != nil { + if sw.Config.P2P.LANDiscover { sw.lanDiscv.Stop() } + for _, listener := range sw.listeners { listener.Stop() } @@ -395,7 +397,7 @@ func (sw *Switch) connectLANPeers(lanPeer mdns.LANPeerEvent) { } func (sw *Switch) connectLANPeersRoutine() { - if sw.lanDiscv == nil { + if !sw.Config.P2P.LANDiscover { return } diff --git a/p2p/test_util.go b/p2p/test_util.go index 1d95a8b0..d263fa7e 100644 --- a/p2p/test_util.go +++ b/p2p/test_util.go @@ -91,6 +91,7 @@ func (m *mockDiscv) ReadRandomNodes(buf []*dht.Node) (n int) { func MakeSwitch(cfg *cfg.Config, testdb dbm.DB, privKey crypto.PrivKeyEd25519, initSwitch func(*Switch) *Switch) *Switch { // new switch, add reactors l, listenAddr := GetListener(cfg.P2P) + cfg.P2P.LANDiscover = false sw, err := newSwitch(cfg, new(mockDiscv), nil, testdb, l, privKey, listenAddr) if err != nil { log.Errorf("create switch error: %s", err)