diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index dc22f3c2d..49549e062 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -468,22 +468,20 @@ next: } func discovery() *discover.Discoverer { - if !cfg.Options.LocalAnnEnabled { + disc, err := discover.NewDiscoverer(myID, cfg.Options.ListenAddress) + if err != nil { + warnf("No discovery possible (%v)", err) return nil } - infoln("Sending local discovery announcements") - - if !cfg.Options.GlobalAnnEnabled { - cfg.Options.GlobalAnnServer = "" - } else { - infoln("Sending external discovery announcements") + if cfg.Options.LocalAnnEnabled { + infoln("Sending local discovery announcements") + disc.StartLocal() } - disc, err := discover.NewDiscoverer(myID, cfg.Options.ListenAddress, cfg.Options.GlobalAnnServer) - - if err != nil { - warnf("No discovery possible (%v)", err) + if cfg.Options.GlobalAnnEnabled { + infoln("Sending external discovery announcements") + disc.StartGlobal(cfg.Options.GlobalAnnServer) } return disc diff --git a/discover/discover.go b/discover/discover.go index fdc8a7faf..1967c983b 100644 --- a/discover/discover.go +++ b/discover/discover.go @@ -40,7 +40,7 @@ var ( // When we hit this many errors in succession, we stop. const maxErrors = 30 -func NewDiscoverer(id string, addresses []string, extServer string) (*Discoverer, error) { +func NewDiscoverer(id string, addresses []string) (*Discoverer, error) { disc := &Discoverer{ MyID: id, ListenAddresses: addresses, @@ -48,32 +48,26 @@ func NewDiscoverer(id string, addresses []string, extServer string) (*Discoverer ExtBroadcastIntv: 1800 * time.Second, beacon: mc.NewBeacon("239.21.0.25", 21025), registry: make(map[string][]string), - extServer: extServer, } // Receive announcements sent to the local multicast group. go disc.recvAnnouncements() - // If we got a list of addresses that we listen on, announce those - // locally. - - if len(disc.ListenAddresses) > 0 { - disc.localBroadcastTick = time.Tick(disc.BroadcastIntv) - disc.forcedBroadcastTick = make(chan time.Time) - go disc.sendLocalAnnouncements() - - // If we have an external server address, also announce to that - // server. - - if len(disc.extServer) > 0 { - go disc.sendExternalAnnouncements() - } - } - return disc, nil } +func (d *Discoverer) StartLocal() { + d.localBroadcastTick = time.Tick(d.BroadcastIntv) + d.forcedBroadcastTick = make(chan time.Time) + go d.sendLocalAnnouncements() +} + +func (d *Discoverer) StartGlobal(server string) { + d.extServer = server + go d.sendExternalAnnouncements() +} + func (d *Discoverer) announcementPkt() []byte { var addrs []Address for _, astr := range d.ListenAddresses {