2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-06-01 13:50:14 -07:00
|
|
|
|
2013-12-15 03:43:31 -07:00
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
2014-06-29 16:42:03 -07:00
|
|
|
"bytes"
|
2014-02-20 09:40:15 -07:00
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2015-06-23 05:55:30 -07:00
|
|
|
"fmt"
|
2014-05-11 15:55:43 -07:00
|
|
|
"io"
|
2013-12-15 03:43:31 -07:00
|
|
|
"net"
|
2015-06-23 05:55:30 -07:00
|
|
|
"net/url"
|
|
|
|
"sort"
|
2013-12-15 03:43:31 -07:00
|
|
|
"time"
|
2013-12-24 09:10:49 -07:00
|
|
|
|
2015-01-13 05:22:56 -07:00
|
|
|
"github.com/syncthing/protocol"
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/beacon"
|
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2015-06-23 05:55:30 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2013-12-15 03:43:31 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Discoverer struct {
|
2015-09-02 09:56:44 -07:00
|
|
|
myID protocol.DeviceID
|
|
|
|
listenAddrs []string
|
|
|
|
relayStatusProvider relayStatusProvider
|
|
|
|
localBcastIntv time.Duration
|
|
|
|
localBcastStart time.Time
|
|
|
|
cacheLifetime time.Duration
|
|
|
|
negCacheCutoff time.Duration
|
|
|
|
beacons []beacon.Interface
|
2015-09-12 12:59:15 -07:00
|
|
|
extAddr externalAddr
|
2015-09-02 09:56:44 -07:00
|
|
|
localBcastTick <-chan time.Time
|
|
|
|
forcedBcastTick chan time.Time
|
2014-11-20 14:24:11 -07:00
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
registryLock sync.RWMutex
|
|
|
|
addressRegistry map[protocol.DeviceID][]CacheEntry
|
|
|
|
relayRegistry map[protocol.DeviceID][]CacheEntry
|
|
|
|
lastLookup map[protocol.DeviceID]time.Time
|
2015-03-25 08:55:40 -07:00
|
|
|
|
2014-11-20 14:24:11 -07:00
|
|
|
clients []Client
|
|
|
|
mut sync.RWMutex
|
2013-12-22 14:29:23 -07:00
|
|
|
}
|
|
|
|
|
2015-09-02 09:56:44 -07:00
|
|
|
type relayStatusProvider interface {
|
|
|
|
ClientStatus() map[string]bool
|
|
|
|
}
|
|
|
|
|
2015-09-12 12:59:15 -07:00
|
|
|
type externalAddr interface {
|
|
|
|
ExternalAddresses() []string
|
|
|
|
}
|
|
|
|
|
2014-10-15 13:52:06 -07:00
|
|
|
type CacheEntry struct {
|
|
|
|
Address string
|
|
|
|
Seen time.Time
|
2014-08-16 12:27:00 -07:00
|
|
|
}
|
|
|
|
|
2014-02-20 09:40:15 -07:00
|
|
|
var (
|
2014-02-24 05:29:30 -07:00
|
|
|
ErrIncorrectMagic = errors.New("incorrect magic number")
|
2014-02-20 09:40:15 -07:00
|
|
|
)
|
|
|
|
|
2015-09-02 09:56:44 -07:00
|
|
|
func NewDiscoverer(id protocol.DeviceID, addresses []string, relayStatusProvider relayStatusProvider) *Discoverer {
|
2014-08-19 16:00:21 -07:00
|
|
|
return &Discoverer{
|
2015-09-02 09:56:44 -07:00
|
|
|
myID: id,
|
|
|
|
listenAddrs: addresses,
|
|
|
|
relayStatusProvider: relayStatusProvider,
|
|
|
|
localBcastIntv: 30 * time.Second,
|
|
|
|
cacheLifetime: 5 * time.Minute,
|
|
|
|
negCacheCutoff: 3 * time.Minute,
|
|
|
|
addressRegistry: make(map[protocol.DeviceID][]CacheEntry),
|
|
|
|
relayRegistry: make(map[protocol.DeviceID][]CacheEntry),
|
|
|
|
lastLookup: make(map[protocol.DeviceID]time.Time),
|
|
|
|
registryLock: sync.NewRWMutex(),
|
|
|
|
mut: sync.NewRWMutex(),
|
2014-03-18 09:51:55 -07:00
|
|
|
}
|
2014-08-19 16:00:21 -07:00
|
|
|
}
|
2013-12-15 03:43:31 -07:00
|
|
|
|
2014-08-19 16:00:21 -07:00
|
|
|
func (d *Discoverer) StartLocal(localPort int, localMCAddr string) {
|
2014-08-17 06:01:48 -07:00
|
|
|
if localPort > 0 {
|
2015-04-02 12:45:52 -07:00
|
|
|
d.startLocalIPv4Broadcasts(localPort)
|
2014-08-17 06:01:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(localMCAddr) > 0 {
|
2015-04-02 12:45:52 -07:00
|
|
|
d.startLocalIPv6Multicasts(localMCAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(d.beacons) == 0 {
|
|
|
|
l.Warnln("Local discovery unavailable")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
d.localBcastTick = time.Tick(d.localBcastIntv)
|
|
|
|
d.forcedBcastTick = make(chan time.Time)
|
|
|
|
d.localBcastStart = time.Now()
|
|
|
|
go d.sendLocalAnnouncements()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discoverer) startLocalIPv4Broadcasts(localPort int) {
|
2015-06-10 01:07:51 -07:00
|
|
|
bb := beacon.NewBroadcast(localPort)
|
2015-04-02 12:45:52 -07:00
|
|
|
d.beacons = append(d.beacons, bb)
|
|
|
|
go d.recvAnnouncements(bb)
|
2015-06-10 01:07:51 -07:00
|
|
|
bb.ServeBackground()
|
2015-04-02 12:45:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discoverer) startLocalIPv6Multicasts(localMCAddr string) {
|
2015-08-23 06:02:18 -07:00
|
|
|
mb, err := beacon.NewMulticast(localMCAddr)
|
2015-04-02 12:45:52 -07:00
|
|
|
if err != nil {
|
2015-08-23 06:50:57 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugln("beacon.NewMulticast:", err)
|
|
|
|
}
|
2015-04-02 12:45:52 -07:00
|
|
|
l.Infoln("Local discovery over IPv6 unavailable")
|
|
|
|
return
|
|
|
|
}
|
2015-08-23 06:02:18 -07:00
|
|
|
d.beacons = append(d.beacons, mb)
|
|
|
|
go d.recvAnnouncements(mb)
|
2014-04-16 07:49:01 -07:00
|
|
|
}
|
2014-03-03 07:28:10 -07:00
|
|
|
|
2015-09-12 12:59:15 -07:00
|
|
|
func (d *Discoverer) StartGlobal(servers []string, extAddr externalAddr) {
|
2014-11-20 14:24:11 -07:00
|
|
|
d.mut.Lock()
|
|
|
|
defer d.mut.Unlock()
|
|
|
|
|
|
|
|
if len(d.clients) > 0 {
|
|
|
|
d.stopGlobal()
|
|
|
|
}
|
|
|
|
|
2015-09-12 12:59:15 -07:00
|
|
|
d.extAddr = extAddr
|
2015-04-22 15:54:31 -07:00
|
|
|
wg := sync.NewWaitGroup()
|
2014-11-20 14:24:11 -07:00
|
|
|
clients := make(chan Client, len(servers))
|
|
|
|
for _, address := range servers {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(addr string) {
|
|
|
|
defer wg.Done()
|
2015-07-23 12:01:25 -07:00
|
|
|
client, err := New(addr, d)
|
2014-11-20 14:24:11 -07:00
|
|
|
if err != nil {
|
|
|
|
l.Infoln("Error creating discovery client", addr, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
clients <- client
|
|
|
|
}(address)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
close(clients)
|
|
|
|
|
|
|
|
for client := range clients {
|
|
|
|
d.clients = append(d.clients, client)
|
|
|
|
}
|
2014-03-18 09:51:55 -07:00
|
|
|
}
|
2014-03-03 07:28:10 -07:00
|
|
|
|
2014-08-14 03:44:49 -07:00
|
|
|
func (d *Discoverer) StopGlobal() {
|
2014-11-20 14:24:11 -07:00
|
|
|
d.mut.Lock()
|
|
|
|
defer d.mut.Unlock()
|
|
|
|
d.stopGlobal()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discoverer) stopGlobal() {
|
|
|
|
for _, client := range d.clients {
|
|
|
|
client.Stop()
|
2014-09-09 15:56:44 -07:00
|
|
|
}
|
2014-11-20 14:24:11 -07:00
|
|
|
d.clients = []Client{}
|
2014-08-14 03:44:49 -07:00
|
|
|
}
|
|
|
|
|
2014-11-18 15:57:21 -07:00
|
|
|
func (d *Discoverer) ExtAnnounceOK() map[string]bool {
|
2014-11-20 14:24:11 -07:00
|
|
|
d.mut.RLock()
|
|
|
|
defer d.mut.RUnlock()
|
|
|
|
|
|
|
|
ret := make(map[string]bool)
|
|
|
|
for _, client := range d.clients {
|
|
|
|
ret[client.Address()] = client.StatusOK()
|
|
|
|
}
|
|
|
|
return ret
|
2014-04-16 08:36:09 -07:00
|
|
|
}
|
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
// Lookup returns a list of addresses the device is available at, as well as
|
|
|
|
// a list of relays the device is supposed to be available on sorted by the
|
|
|
|
// sum of latencies between this device, and the device in question.
|
|
|
|
func (d *Discoverer) Lookup(device protocol.DeviceID) ([]string, []string) {
|
2014-10-06 15:03:24 -07:00
|
|
|
d.registryLock.RLock()
|
2015-06-23 05:55:30 -07:00
|
|
|
cachedAddresses := d.filterCached(d.addressRegistry[device])
|
|
|
|
cachedRelays := d.filterCached(d.relayRegistry[device])
|
2015-03-25 08:55:40 -07:00
|
|
|
lastLookup := d.lastLookup[device]
|
2014-10-06 15:03:24 -07:00
|
|
|
d.registryLock.RUnlock()
|
2014-05-01 23:53:19 -07:00
|
|
|
|
2014-11-20 14:24:11 -07:00
|
|
|
d.mut.RLock()
|
|
|
|
defer d.mut.RUnlock()
|
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
relays := make([]string, len(cachedRelays))
|
|
|
|
for i := range cachedRelays {
|
|
|
|
relays[i] = cachedRelays[i].Address
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cachedAddresses) > 0 {
|
2015-03-25 08:55:40 -07:00
|
|
|
// There are cached address entries.
|
2015-06-23 05:55:30 -07:00
|
|
|
addrs := make([]string, len(cachedAddresses))
|
|
|
|
for i := range cachedAddresses {
|
|
|
|
addrs[i] = cachedAddresses[i].Address
|
2014-08-16 12:27:00 -07:00
|
|
|
}
|
2015-06-23 05:55:30 -07:00
|
|
|
return addrs, relays
|
2015-03-25 08:55:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if time.Since(lastLookup) < d.negCacheCutoff {
|
|
|
|
// We have recently tried to lookup this address and failed. Lets
|
|
|
|
// chill for a while.
|
2015-06-23 05:55:30 -07:00
|
|
|
return nil, relays
|
2015-03-25 08:55:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(d.clients) != 0 && time.Since(d.localBcastStart) > d.localBcastIntv {
|
2014-11-26 09:23:15 -07:00
|
|
|
// Only perform external lookups if we have at least one external
|
2014-11-20 14:24:11 -07:00
|
|
|
// server client and one local announcement interval has passed. This is
|
|
|
|
// to avoid finding local peers on their remote address at startup.
|
2015-06-23 05:55:30 -07:00
|
|
|
results := make(chan Announce, len(d.clients))
|
2015-04-22 15:54:31 -07:00
|
|
|
wg := sync.NewWaitGroup()
|
2014-11-20 14:24:11 -07:00
|
|
|
for _, client := range d.clients {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(c Client) {
|
|
|
|
defer wg.Done()
|
2015-06-23 05:55:30 -07:00
|
|
|
ann, err := c.Lookup(device)
|
|
|
|
if err == nil {
|
|
|
|
results <- ann
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:24:11 -07:00
|
|
|
}(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
close(results)
|
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
cachedAddresses := []CacheEntry{}
|
|
|
|
availableRelays := []Relay{}
|
|
|
|
seenAddresses := make(map[string]struct{})
|
|
|
|
seenRelays := make(map[string]struct{})
|
2014-11-20 14:24:11 -07:00
|
|
|
now := time.Now()
|
|
|
|
|
2015-03-25 08:55:40 -07:00
|
|
|
var addrs []string
|
2014-11-20 14:24:11 -07:00
|
|
|
for result := range results {
|
2015-06-23 05:55:30 -07:00
|
|
|
for _, addr := range result.This.Addresses {
|
|
|
|
_, ok := seenAddresses[addr]
|
2014-11-20 14:24:11 -07:00
|
|
|
if !ok {
|
2015-06-23 05:55:30 -07:00
|
|
|
cachedAddresses = append(cachedAddresses, CacheEntry{
|
2014-11-20 14:24:11 -07:00
|
|
|
Address: addr,
|
|
|
|
Seen: now,
|
|
|
|
})
|
2015-06-23 05:55:30 -07:00
|
|
|
seenAddresses[addr] = struct{}{}
|
2014-11-20 14:24:11 -07:00
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
2014-08-16 12:27:00 -07:00
|
|
|
}
|
2015-06-23 05:55:30 -07:00
|
|
|
|
|
|
|
for _, relay := range result.This.Relays {
|
|
|
|
_, ok := seenRelays[relay.Address]
|
|
|
|
if !ok {
|
|
|
|
availableRelays = append(availableRelays, relay)
|
|
|
|
seenRelays[relay.Address] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 09:56:44 -07:00
|
|
|
relays = RelayAddressesSortedByLatency(availableRelays)
|
2015-06-23 05:55:30 -07:00
|
|
|
cachedRelays := make([]CacheEntry, len(relays))
|
|
|
|
for i := range relays {
|
|
|
|
cachedRelays[i] = CacheEntry{
|
|
|
|
Address: relays[i],
|
|
|
|
Seen: now,
|
|
|
|
}
|
2014-08-16 12:27:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
d.registryLock.Lock()
|
2015-06-23 05:55:30 -07:00
|
|
|
d.addressRegistry[device] = cachedAddresses
|
|
|
|
d.relayRegistry[device] = cachedRelays
|
2015-03-25 08:55:40 -07:00
|
|
|
d.lastLookup[device] = time.Now()
|
2014-08-16 12:27:00 -07:00
|
|
|
d.registryLock.Unlock()
|
2015-03-25 08:55:40 -07:00
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
return addrs, relays
|
2014-05-01 23:53:19 -07:00
|
|
|
}
|
2015-03-25 08:55:40 -07:00
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
return nil, relays
|
2014-05-01 23:53:19 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (d *Discoverer) Hint(device string, addrs []string) {
|
2014-05-12 17:50:54 -07:00
|
|
|
resAddrs := resolveAddrs(addrs)
|
2014-09-28 04:00:38 -07:00
|
|
|
var id protocol.DeviceID
|
|
|
|
id.UnmarshalText([]byte(device))
|
|
|
|
d.registerDevice(nil, Device{
|
2014-05-12 17:50:54 -07:00
|
|
|
Addresses: resAddrs,
|
2014-06-29 16:42:03 -07:00
|
|
|
ID: id[:],
|
2014-05-12 17:50:54 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-10-15 13:52:06 -07:00
|
|
|
func (d *Discoverer) All() map[protocol.DeviceID][]CacheEntry {
|
2014-05-12 18:08:55 -07:00
|
|
|
d.registryLock.RLock()
|
2015-06-23 05:55:30 -07:00
|
|
|
devices := make(map[protocol.DeviceID][]CacheEntry, len(d.addressRegistry))
|
|
|
|
for device, addrs := range d.addressRegistry {
|
2014-10-15 13:52:06 -07:00
|
|
|
addrsCopy := make([]CacheEntry, len(addrs))
|
2014-05-12 18:08:55 -07:00
|
|
|
copy(addrsCopy, addrs)
|
2014-09-28 04:00:38 -07:00
|
|
|
devices[device] = addrsCopy
|
2014-05-12 18:08:55 -07:00
|
|
|
}
|
|
|
|
d.registryLock.RUnlock()
|
2014-09-28 04:00:38 -07:00
|
|
|
return devices
|
2014-05-12 18:08:55 -07:00
|
|
|
}
|
|
|
|
|
2015-07-23 12:01:25 -07:00
|
|
|
func (d *Discoverer) Announcement() Announce {
|
|
|
|
return d.announcementPkt(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discoverer) announcementPkt(allowExternal bool) Announce {
|
2015-06-23 05:55:30 -07:00
|
|
|
var addrs []string
|
2015-09-12 12:59:15 -07:00
|
|
|
if allowExternal && d.extAddr != nil {
|
|
|
|
addrs = d.extAddr.ExternalAddresses()
|
2014-11-18 15:57:21 -07:00
|
|
|
} else {
|
2015-07-17 13:22:07 -07:00
|
|
|
addrs = resolveAddrs(d.listenAddrs)
|
|
|
|
}
|
|
|
|
|
2015-07-23 12:01:25 -07:00
|
|
|
var relayAddrs []string
|
2015-09-02 09:56:44 -07:00
|
|
|
if d.relayStatusProvider != nil {
|
|
|
|
status := d.relayStatusProvider.ClientStatus()
|
2015-07-17 13:22:07 -07:00
|
|
|
for uri, ok := range status {
|
|
|
|
if ok {
|
|
|
|
relayAddrs = append(relayAddrs, uri)
|
2014-11-18 15:57:21 -07:00
|
|
|
}
|
2014-03-18 09:51:55 -07:00
|
|
|
}
|
|
|
|
}
|
2015-07-17 13:22:07 -07:00
|
|
|
|
2015-07-23 12:01:25 -07:00
|
|
|
return Announce{
|
2014-07-13 00:33:44 -07:00
|
|
|
Magic: AnnouncementMagic,
|
2015-07-17 13:22:07 -07:00
|
|
|
This: Device{d.myID[:], addrs, measureLatency(relayAddrs)},
|
2014-03-18 09:51:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discoverer) sendLocalAnnouncements() {
|
2015-07-17 13:22:07 -07:00
|
|
|
var pkt = d.announcementPkt(false)
|
2014-10-20 23:40:05 -07:00
|
|
|
msg := pkt.MustMarshalXDR()
|
2014-03-03 07:28:10 -07:00
|
|
|
|
2014-03-28 03:04:48 -07:00
|
|
|
for {
|
2015-04-02 12:45:52 -07:00
|
|
|
for _, b := range d.beacons {
|
|
|
|
b.Send(msg)
|
2014-08-17 06:01:48 -07:00
|
|
|
}
|
2014-02-23 07:01:37 -07:00
|
|
|
|
|
|
|
select {
|
2014-04-16 08:36:09 -07:00
|
|
|
case <-d.localBcastTick:
|
|
|
|
case <-d.forcedBcastTick:
|
2014-02-23 07:01:37 -07:00
|
|
|
}
|
2014-02-23 06:48:56 -07:00
|
|
|
}
|
2013-12-22 14:29:23 -07:00
|
|
|
}
|
2013-12-15 03:43:31 -07:00
|
|
|
|
2014-08-17 06:01:48 -07:00
|
|
|
func (d *Discoverer) recvAnnouncements(b beacon.Interface) {
|
2014-03-28 03:04:48 -07:00
|
|
|
for {
|
2014-08-17 06:01:48 -07:00
|
|
|
buf, addr := b.Recv()
|
2013-12-22 14:29:23 -07:00
|
|
|
|
2014-07-13 00:33:44 -07:00
|
|
|
var pkt Announce
|
2014-03-28 03:04:48 -07:00
|
|
|
err := pkt.UnmarshalXDR(buf)
|
2014-05-11 15:55:43 -07:00
|
|
|
if err != nil && err != io.EOF {
|
2014-12-18 16:12:12 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugf("discover: Failed to unmarshal local announcement from %s:\n%s", addr, hex.Dump(buf))
|
|
|
|
}
|
2013-12-15 03:43:31 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-12-18 16:12:12 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugf("discover: Received local announcement from %s for %s", addr, protocol.DeviceIDFromBytes(pkt.This.ID))
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
var newDevice bool
|
2014-06-29 16:42:03 -07:00
|
|
|
if bytes.Compare(pkt.This.ID, d.myID[:]) != 0 {
|
2014-09-28 04:00:38 -07:00
|
|
|
newDevice = d.registerDevice(addr, pkt.This)
|
2014-05-11 15:55:43 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
if newDevice {
|
2014-05-11 15:55:43 -07:00
|
|
|
select {
|
|
|
|
case d.forcedBcastTick <- time.Now():
|
2014-02-23 07:01:37 -07:00
|
|
|
}
|
2013-12-15 03:43:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (d *Discoverer) registerDevice(addr net.Addr, device Device) bool {
|
|
|
|
var id protocol.DeviceID
|
|
|
|
copy(id[:], device.ID)
|
2014-08-16 12:27:00 -07:00
|
|
|
|
2014-11-29 15:04:25 -07:00
|
|
|
d.registryLock.Lock()
|
|
|
|
defer d.registryLock.Unlock()
|
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
current := d.filterCached(d.addressRegistry[id])
|
2014-08-16 12:27:00 -07:00
|
|
|
|
|
|
|
orig := current
|
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
for _, deviceAddr := range device.Addresses {
|
|
|
|
uri, err := url.Parse(deviceAddr)
|
|
|
|
if err != nil {
|
|
|
|
if debug {
|
|
|
|
l.Debugf("discover: Failed to parse address %s: %s", deviceAddr, err)
|
|
|
|
}
|
|
|
|
continue
|
2014-05-11 15:55:43 -07:00
|
|
|
}
|
2015-06-23 05:55:30 -07:00
|
|
|
|
|
|
|
host, port, err := net.SplitHostPort(uri.Host)
|
|
|
|
if err != nil {
|
|
|
|
if debug {
|
|
|
|
l.Debugf("discover: Failed to split address host %s: %s", deviceAddr, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if host == "" {
|
|
|
|
uri.Host = net.JoinHostPort(addr.(*net.UDPAddr).IP.String(), port)
|
|
|
|
deviceAddr = uri.String()
|
|
|
|
}
|
|
|
|
|
2014-08-16 12:27:00 -07:00
|
|
|
for i := range current {
|
2014-10-15 13:52:06 -07:00
|
|
|
if current[i].Address == deviceAddr {
|
|
|
|
current[i].Seen = time.Now()
|
2014-08-16 12:27:00 -07:00
|
|
|
goto done
|
|
|
|
}
|
2014-05-11 15:55:43 -07:00
|
|
|
}
|
2014-10-15 13:52:06 -07:00
|
|
|
current = append(current, CacheEntry{
|
|
|
|
Address: deviceAddr,
|
|
|
|
Seen: time.Now(),
|
2014-08-16 12:27:00 -07:00
|
|
|
})
|
|
|
|
done:
|
2014-05-11 15:55:43 -07:00
|
|
|
}
|
2014-08-16 12:27:00 -07:00
|
|
|
|
2014-05-11 15:55:43 -07:00
|
|
|
if debug {
|
2014-12-18 16:12:12 -07:00
|
|
|
l.Debugf("discover: Caching %s addresses: %v", id, current)
|
2014-05-11 15:55:43 -07:00
|
|
|
}
|
2014-08-16 12:27:00 -07:00
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
d.addressRegistry[id] = current
|
2014-07-17 04:38:36 -07:00
|
|
|
|
2014-08-16 12:27:00 -07:00
|
|
|
if len(current) > len(orig) {
|
|
|
|
addrs := make([]string, len(current))
|
|
|
|
for i := range current {
|
2014-10-15 13:52:06 -07:00
|
|
|
addrs[i] = current[i].Address
|
2014-08-16 12:27:00 -07:00
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
events.Default.Log(events.DeviceDiscovered, map[string]interface{}{
|
2014-09-28 04:05:25 -07:00
|
|
|
"device": id.String(),
|
|
|
|
"addrs": addrs,
|
2014-07-17 04:38:36 -07:00
|
|
|
})
|
|
|
|
}
|
2014-08-16 12:27:00 -07:00
|
|
|
|
|
|
|
return len(current) > len(orig)
|
2014-05-11 15:55:43 -07:00
|
|
|
}
|
|
|
|
|
2014-10-15 13:52:06 -07:00
|
|
|
func (d *Discoverer) filterCached(c []CacheEntry) []CacheEntry {
|
2014-08-16 12:27:00 -07:00
|
|
|
for i := 0; i < len(c); {
|
2014-10-15 13:52:06 -07:00
|
|
|
if ago := time.Since(c[i].Seen); ago > d.cacheLifetime {
|
2014-08-16 12:27:00 -07:00
|
|
|
if debug {
|
2015-06-23 05:55:30 -07:00
|
|
|
l.Debugf("discover: Removing cached entry %s - seen %v ago", c[i].Address, ago)
|
2014-08-16 12:27:00 -07:00
|
|
|
}
|
|
|
|
c[i] = c[len(c)-1]
|
|
|
|
c = c[:len(c)-1]
|
|
|
|
} else {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
func addrToAddr(addr *net.TCPAddr) string {
|
2014-05-11 15:55:43 -07:00
|
|
|
if len(addr.IP) == 0 || addr.IP.IsUnspecified() {
|
2015-06-23 05:55:30 -07:00
|
|
|
return fmt.Sprintf(":%d", addr.Port)
|
2014-05-11 15:55:43 -07:00
|
|
|
} else if bs := addr.IP.To4(); bs != nil {
|
2015-06-23 05:55:30 -07:00
|
|
|
return fmt.Sprintf("%s:%d", bs.String(), addr.Port)
|
2014-05-11 15:55:43 -07:00
|
|
|
} else if bs := addr.IP.To16(); bs != nil {
|
2015-06-23 05:55:30 -07:00
|
|
|
return fmt.Sprintf("[%s]:%d", bs.String(), addr.Port)
|
2014-05-11 15:55:43 -07:00
|
|
|
}
|
2015-06-23 05:55:30 -07:00
|
|
|
return ""
|
2014-05-11 15:55:43 -07:00
|
|
|
}
|
|
|
|
|
2015-06-23 05:55:30 -07:00
|
|
|
func resolveAddrs(addrs []string) []string {
|
|
|
|
var raddrs []string
|
2014-05-11 15:55:43 -07:00
|
|
|
for _, addrStr := range addrs {
|
2015-06-23 05:55:30 -07:00
|
|
|
uri, err := url.Parse(addrStr)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addrRes, err := net.ResolveTCPAddr("tcp", uri.Host)
|
2014-05-11 15:55:43 -07:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addr := addrToAddr(addrRes)
|
2015-06-23 05:55:30 -07:00
|
|
|
if len(addr) > 0 {
|
|
|
|
uri.Host = addr
|
|
|
|
raddrs = append(raddrs, uri.String())
|
2014-05-11 15:55:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return raddrs
|
|
|
|
}
|
2015-06-23 05:55:30 -07:00
|
|
|
|
|
|
|
func measureLatency(relayAdresses []string) []Relay {
|
|
|
|
relays := make([]Relay, 0, len(relayAdresses))
|
|
|
|
for i, addr := range relayAdresses {
|
|
|
|
relay := Relay{
|
|
|
|
Address: addr,
|
|
|
|
Latency: int32(time.Hour / time.Millisecond),
|
|
|
|
}
|
|
|
|
relays = append(relays, relay)
|
|
|
|
|
2015-09-02 09:56:44 -07:00
|
|
|
if latency, err := osutil.GetLatencyForURL(addr); err == nil {
|
2015-06-23 05:55:30 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugf("Relay %s latency %s", addr, latency)
|
|
|
|
}
|
|
|
|
relays[i].Latency = int32(latency / time.Millisecond)
|
|
|
|
} else {
|
|
|
|
l.Debugf("Failed to get relay %s latency %s", addr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return relays
|
|
|
|
}
|
|
|
|
|
2015-09-02 09:56:44 -07:00
|
|
|
// RelayAddressesSortedByLatency adds local latency to the relay, and sorts them
|
2015-06-23 05:55:30 -07:00
|
|
|
// by sum latency, and returns the addresses.
|
2015-09-02 09:56:44 -07:00
|
|
|
func RelayAddressesSortedByLatency(input []Relay) []string {
|
2015-06-23 05:55:30 -07:00
|
|
|
relays := make([]Relay, len(input))
|
|
|
|
copy(relays, input)
|
|
|
|
for i, relay := range relays {
|
2015-09-02 09:56:44 -07:00
|
|
|
if latency, err := osutil.GetLatencyForURL(relay.Address); err == nil {
|
2015-06-23 05:55:30 -07:00
|
|
|
relays[i].Latency += int32(latency / time.Millisecond)
|
|
|
|
} else {
|
|
|
|
relays[i].Latency += int32(time.Hour / time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(relayList(relays))
|
|
|
|
|
|
|
|
addresses := make([]string, 0, len(relays))
|
|
|
|
for _, relay := range relays {
|
|
|
|
addresses = append(addresses, relay.Address)
|
|
|
|
}
|
|
|
|
return addresses
|
|
|
|
}
|
|
|
|
|
|
|
|
type relayList []Relay
|
|
|
|
|
|
|
|
func (l relayList) Len() int {
|
|
|
|
return len(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l relayList) Less(a, b int) bool {
|
|
|
|
return l[a].Latency < l[b].Latency
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l relayList) Swap(a, b int) {
|
|
|
|
l[a], l[b] = l[b], l[a]
|
|
|
|
}
|