service.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. //go:generate -command counterfeiter go run github.com/maxbrunsfeld/counterfeiter/v6
  7. //go:generate counterfeiter -o mocks/service.go --fake-name Service . Service
  8. package connections
  9. import (
  10. "context"
  11. "crypto/tls"
  12. "crypto/x509"
  13. "fmt"
  14. "math"
  15. "net"
  16. "net/url"
  17. "sort"
  18. "strings"
  19. stdsync "sync"
  20. "time"
  21. "github.com/syncthing/syncthing/lib/config"
  22. "github.com/syncthing/syncthing/lib/connections/registry"
  23. "github.com/syncthing/syncthing/lib/discover"
  24. "github.com/syncthing/syncthing/lib/events"
  25. "github.com/syncthing/syncthing/lib/nat"
  26. "github.com/syncthing/syncthing/lib/osutil"
  27. "github.com/syncthing/syncthing/lib/protocol"
  28. "github.com/syncthing/syncthing/lib/svcutil"
  29. "github.com/syncthing/syncthing/lib/sync"
  30. "github.com/syncthing/syncthing/lib/util"
  31. // Registers NAT service providers
  32. _ "github.com/syncthing/syncthing/lib/pmp"
  33. _ "github.com/syncthing/syncthing/lib/upnp"
  34. "github.com/pkg/errors"
  35. "github.com/thejerf/suture/v4"
  36. "golang.org/x/time/rate"
  37. )
  38. var (
  39. dialers = make(map[string]dialerFactory)
  40. listeners = make(map[string]listenerFactory)
  41. )
  42. var (
  43. // Dialers and listeners return errUnsupported (or a wrapped variant)
  44. // when they are intentionally out of service due to configuration,
  45. // build, etc. This is not logged loudly.
  46. errUnsupported = errors.New("unsupported protocol")
  47. // These are specific explanations for errUnsupported.
  48. errDisabled = fmt.Errorf("%w: disabled by configuration", errUnsupported)
  49. errDeprecated = fmt.Errorf("%w: deprecated", errUnsupported)
  50. // Various reasons to reject a connection
  51. errNetworkNotAllowed = errors.New("network not allowed")
  52. errDeviceAlreadyConnected = errors.New("already connected to this device")
  53. errDeviceIgnored = errors.New("device is ignored")
  54. errConnLimitReached = errors.New("connection limit reached")
  55. errDevicePaused = errors.New("device is paused")
  56. )
  57. const (
  58. perDeviceWarningIntv = 15 * time.Minute
  59. tlsHandshakeTimeout = 10 * time.Second
  60. minConnectionReplaceAge = 10 * time.Second
  61. minConnectionLoopSleep = 5 * time.Second
  62. stdConnectionLoopSleep = time.Minute
  63. worstDialerPriority = math.MaxInt32
  64. recentlySeenCutoff = 7 * 24 * time.Hour
  65. shortLivedConnectionThreshold = 5 * time.Second
  66. dialMaxParallel = 64
  67. dialMaxParallelPerDevice = 8
  68. )
  69. // From go/src/crypto/tls/cipher_suites.go
  70. var tlsCipherSuiteNames = map[uint16]string{
  71. // TLS 1.2
  72. 0x0005: "TLS_RSA_WITH_RC4_128_SHA",
  73. 0x000a: "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
  74. 0x002f: "TLS_RSA_WITH_AES_128_CBC_SHA",
  75. 0x0035: "TLS_RSA_WITH_AES_256_CBC_SHA",
  76. 0x003c: "TLS_RSA_WITH_AES_128_CBC_SHA256",
  77. 0x009c: "TLS_RSA_WITH_AES_128_GCM_SHA256",
  78. 0x009d: "TLS_RSA_WITH_AES_256_GCM_SHA384",
  79. 0xc007: "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA",
  80. 0xc009: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
  81. 0xc00a: "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
  82. 0xc011: "TLS_ECDHE_RSA_WITH_RC4_128_SHA",
  83. 0xc012: "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
  84. 0xc013: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
  85. 0xc014: "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
  86. 0xc023: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
  87. 0xc027: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
  88. 0xc02f: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  89. 0xc02b: "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
  90. 0xc030: "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
  91. 0xc02c: "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
  92. 0xcca8: "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305",
  93. 0xcca9: "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305",
  94. // TLS 1.3
  95. 0x1301: "TLS_AES_128_GCM_SHA256",
  96. 0x1302: "TLS_AES_256_GCM_SHA384",
  97. 0x1303: "TLS_CHACHA20_POLY1305_SHA256",
  98. }
  99. var tlsVersionNames = map[uint16]string{
  100. tls.VersionTLS12: "TLS1.2",
  101. tls.VersionTLS13: "TLS1.3",
  102. }
  103. // Service listens and dials all configured unconnected devices, via supported
  104. // dialers. Successful connections are handed to the model.
  105. type Service interface {
  106. suture.Service
  107. discover.AddressLister
  108. ListenerStatus() map[string]ListenerStatusEntry
  109. ConnectionStatus() map[string]ConnectionStatusEntry
  110. NATType() string
  111. }
  112. type ListenerStatusEntry struct {
  113. Error *string `json:"error"`
  114. LANAddresses []string `json:"lanAddresses"`
  115. WANAddresses []string `json:"wanAddresses"`
  116. }
  117. type ConnectionStatusEntry struct {
  118. When time.Time `json:"when"`
  119. Error *string `json:"error"`
  120. }
  121. type connWithHello struct {
  122. c internalConn
  123. hello protocol.Hello
  124. err error
  125. remoteID protocol.DeviceID
  126. remoteCert *x509.Certificate
  127. }
  128. type service struct {
  129. *suture.Supervisor
  130. connectionStatusHandler
  131. cfg config.Wrapper
  132. myID protocol.DeviceID
  133. model Model
  134. tlsCfg *tls.Config
  135. discoverer discover.Finder
  136. conns chan internalConn
  137. hellos chan *connWithHello
  138. bepProtocolName string
  139. tlsDefaultCommonName string
  140. limiter *limiter
  141. natService *nat.Service
  142. evLogger events.Logger
  143. registry *registry.Registry
  144. dialNow chan struct{}
  145. dialNowDevices map[protocol.DeviceID]struct{}
  146. dialNowDevicesMut sync.Mutex
  147. listenersMut sync.RWMutex
  148. listeners map[string]genericListener
  149. listenerTokens map[string]suture.ServiceToken
  150. }
  151. func NewService(cfg config.Wrapper, myID protocol.DeviceID, mdl Model, tlsCfg *tls.Config, discoverer discover.Finder, bepProtocolName string, tlsDefaultCommonName string, evLogger events.Logger, registry *registry.Registry) Service {
  152. spec := svcutil.SpecWithInfoLogger(l)
  153. service := &service{
  154. Supervisor: suture.New("connections.Service", spec),
  155. connectionStatusHandler: newConnectionStatusHandler(),
  156. cfg: cfg,
  157. myID: myID,
  158. model: mdl,
  159. tlsCfg: tlsCfg,
  160. discoverer: discoverer,
  161. conns: make(chan internalConn),
  162. hellos: make(chan *connWithHello),
  163. bepProtocolName: bepProtocolName,
  164. tlsDefaultCommonName: tlsDefaultCommonName,
  165. limiter: newLimiter(myID, cfg),
  166. natService: nat.NewService(myID, cfg),
  167. evLogger: evLogger,
  168. registry: registry,
  169. dialNowDevicesMut: sync.NewMutex(),
  170. dialNow: make(chan struct{}, 1),
  171. dialNowDevices: make(map[protocol.DeviceID]struct{}),
  172. listenersMut: sync.NewRWMutex(),
  173. listeners: make(map[string]genericListener),
  174. listenerTokens: make(map[string]suture.ServiceToken),
  175. }
  176. cfg.Subscribe(service)
  177. raw := cfg.RawCopy()
  178. // Actually starts the listeners and NAT service
  179. // Need to start this before service.connect so that any dials that
  180. // try punch through already have a listener to cling on.
  181. service.CommitConfiguration(raw, raw)
  182. // There are several moving parts here; one routine per listening address
  183. // (handled in configuration changing) to handle incoming connections,
  184. // one routine to periodically attempt outgoing connections, one routine to
  185. // the common handling regardless of whether the connection was
  186. // incoming or outgoing.
  187. service.Add(svcutil.AsService(service.connect, fmt.Sprintf("%s/connect", service)))
  188. service.Add(svcutil.AsService(service.handleConns, fmt.Sprintf("%s/handleConns", service)))
  189. service.Add(svcutil.AsService(service.handleHellos, fmt.Sprintf("%s/handleHellos", service)))
  190. service.Add(service.natService)
  191. svcutil.OnSupervisorDone(service.Supervisor, func() {
  192. service.cfg.Unsubscribe(service.limiter)
  193. service.cfg.Unsubscribe(service)
  194. })
  195. return service
  196. }
  197. func (s *service) handleConns(ctx context.Context) error {
  198. for {
  199. var c internalConn
  200. select {
  201. case <-ctx.Done():
  202. return ctx.Err()
  203. case c = <-s.conns:
  204. }
  205. cs := c.ConnectionState()
  206. // We should have negotiated the next level protocol "bep/1.0" as part
  207. // of the TLS handshake. Unfortunately this can't be a hard error,
  208. // because there are implementations out there that don't support
  209. // protocol negotiation (iOS for one...).
  210. if cs.NegotiatedProtocol != s.bepProtocolName {
  211. l.Infof("Peer at %s did not negotiate bep/1.0", c)
  212. }
  213. // We should have received exactly one certificate from the other
  214. // side. If we didn't, they don't have a device ID and we drop the
  215. // connection.
  216. certs := cs.PeerCertificates
  217. if cl := len(certs); cl != 1 {
  218. l.Infof("Got peer certificate list of length %d != 1 from peer at %s; protocol error", cl, c)
  219. c.Close()
  220. continue
  221. }
  222. remoteCert := certs[0]
  223. remoteID := protocol.NewDeviceID(remoteCert.Raw)
  224. // The device ID should not be that of ourselves. It can happen
  225. // though, especially in the presence of NAT hairpinning, multiple
  226. // clients between the same NAT gateway, and global discovery.
  227. if remoteID == s.myID {
  228. l.Debugf("Connected to myself (%s) at %s", remoteID, c)
  229. c.Close()
  230. continue
  231. }
  232. if err := s.connectionCheckEarly(remoteID, c); err != nil {
  233. l.Infof("Connection from %s at %s (%s) rejected: %v", remoteID, c.RemoteAddr(), c.Type(), err)
  234. c.Close()
  235. continue
  236. }
  237. _ = c.SetDeadline(time.Now().Add(20 * time.Second))
  238. go func() {
  239. hello, err := protocol.ExchangeHello(c, s.model.GetHello(remoteID))
  240. select {
  241. case s.hellos <- &connWithHello{c, hello, err, remoteID, remoteCert}:
  242. case <-ctx.Done():
  243. }
  244. }()
  245. }
  246. }
  247. func (s *service) connectionCheckEarly(remoteID protocol.DeviceID, c internalConn) error {
  248. if s.cfg.IgnoredDevice(remoteID) {
  249. return errDeviceIgnored
  250. }
  251. if max := s.cfg.Options().ConnectionLimitMax; max > 0 && s.model.NumConnections() >= max {
  252. // We're not allowed to accept any more connections.
  253. return errConnLimitReached
  254. }
  255. cfg, ok := s.cfg.Device(remoteID)
  256. if !ok {
  257. // We do go ahead exchanging hello messages to get information about the device.
  258. return nil
  259. }
  260. if cfg.Paused {
  261. return errDevicePaused
  262. }
  263. if len(cfg.AllowedNetworks) > 0 && !IsAllowedNetwork(c.RemoteAddr().String(), cfg.AllowedNetworks) {
  264. // The connection is not from an allowed network.
  265. return errNetworkNotAllowed
  266. }
  267. // Lower priority is better, just like nice etc.
  268. if ct, ok := s.model.Connection(remoteID); ok {
  269. if ct.Priority() > c.priority || time.Since(ct.Statistics().StartedAt) > minConnectionReplaceAge {
  270. l.Debugf("Switching connections %s (existing: %s new: %s)", remoteID, ct, c)
  271. } else {
  272. // We should not already be connected to the other party. TODO: This
  273. // could use some better handling. If the old connection is dead but
  274. // hasn't timed out yet we may want to drop *that* connection and keep
  275. // this one. But in case we are two devices connecting to each other
  276. // in parallel we don't want to do that or we end up with no
  277. // connections still established...
  278. return errDeviceAlreadyConnected
  279. }
  280. }
  281. return nil
  282. }
  283. func (s *service) handleHellos(ctx context.Context) error {
  284. var c internalConn
  285. var hello protocol.Hello
  286. var err error
  287. var remoteID protocol.DeviceID
  288. var remoteCert *x509.Certificate
  289. for {
  290. select {
  291. case <-ctx.Done():
  292. return ctx.Err()
  293. case withHello := <-s.hellos:
  294. c = withHello.c
  295. hello = withHello.hello
  296. err = withHello.err
  297. remoteID = withHello.remoteID
  298. remoteCert = withHello.remoteCert
  299. }
  300. if err != nil {
  301. if protocol.IsVersionMismatch(err) {
  302. // The error will be a relatively user friendly description
  303. // of what's wrong with the version compatibility. By
  304. // default identify the other side by device ID and IP.
  305. remote := fmt.Sprintf("%v (%v)", remoteID, c.RemoteAddr())
  306. if hello.DeviceName != "" {
  307. // If the name was set in the hello return, use that to
  308. // give the user more info about which device is the
  309. // affected one. It probably says more than the remote
  310. // IP.
  311. remote = fmt.Sprintf("%q (%s %s, %v)", hello.DeviceName, hello.ClientName, hello.ClientVersion, remoteID)
  312. }
  313. msg := fmt.Sprintf("Connecting to %s: %s", remote, err)
  314. warningFor(remoteID, msg)
  315. } else {
  316. // It's something else - connection reset or whatever
  317. l.Infof("Failed to exchange Hello messages with %s at %s: %s", remoteID, c, err)
  318. }
  319. c.Close()
  320. continue
  321. }
  322. _ = c.SetDeadline(time.Time{})
  323. // The Model will return an error for devices that we don't want to
  324. // have a connection with for whatever reason, for example unknown devices.
  325. if err := s.model.OnHello(remoteID, c.RemoteAddr(), hello); err != nil {
  326. l.Infof("Connection from %s at %s (%s) rejected: %v", remoteID, c.RemoteAddr(), c.Type(), err)
  327. c.Close()
  328. continue
  329. }
  330. deviceCfg, ok := s.cfg.Device(remoteID)
  331. if !ok {
  332. l.Infof("Device %s removed from config during connection attempt at %s", remoteID, c)
  333. c.Close()
  334. continue
  335. }
  336. // Verify the name on the certificate. By default we set it to
  337. // "syncthing" when generating, but the user may have replaced
  338. // the certificate and used another name.
  339. certName := deviceCfg.CertName
  340. if certName == "" {
  341. certName = s.tlsDefaultCommonName
  342. }
  343. if remoteCert.Subject.CommonName == certName {
  344. // All good. We do this check because our old style certificates
  345. // have "syncthing" in the CommonName field and no SANs, which
  346. // is not accepted by VerifyHostname() any more as of Go 1.15.
  347. } else if err := remoteCert.VerifyHostname(certName); err != nil {
  348. // Incorrect certificate name is something the user most
  349. // likely wants to know about, since it's an advanced
  350. // config. Warn instead of Info.
  351. l.Warnf("Bad certificate from %s at %s: %v", remoteID, c, err)
  352. c.Close()
  353. continue
  354. }
  355. // Wrap the connection in rate limiters. The limiter itself will
  356. // keep up with config changes to the rate and whether or not LAN
  357. // connections are limited.
  358. isLAN := s.isLAN(c.RemoteAddr())
  359. rd, wr := s.limiter.getLimiters(remoteID, c, isLAN)
  360. protoConn := protocol.NewConnection(remoteID, rd, wr, c, s.model, c, deviceCfg.Compression, s.cfg.FolderPasswords(remoteID))
  361. go func() {
  362. <-protoConn.Closed()
  363. s.dialNowDevicesMut.Lock()
  364. s.dialNowDevices[remoteID] = struct{}{}
  365. s.scheduleDialNow()
  366. s.dialNowDevicesMut.Unlock()
  367. }()
  368. l.Infof("Established secure connection to %s at %s", remoteID, c)
  369. s.model.AddConnection(protoConn, hello)
  370. continue
  371. }
  372. }
  373. func (s *service) connect(ctx context.Context) error {
  374. // Map of when to earliest dial each given device + address again
  375. nextDialAt := make(nextDialRegistry)
  376. // Used as delay for the first few connection attempts (adjusted up to
  377. // minConnectionLoopSleep), increased exponentially until it reaches
  378. // stdConnectionLoopSleep, at which time the normal sleep mechanism
  379. // kicks in.
  380. initialRampup := time.Second
  381. for {
  382. cfg := s.cfg.RawCopy()
  383. bestDialerPriority := s.bestDialerPriority(cfg)
  384. isInitialRampup := initialRampup < stdConnectionLoopSleep
  385. l.Debugln("Connection loop")
  386. if isInitialRampup {
  387. l.Debugln("Connection loop in initial rampup")
  388. }
  389. // Used for consistency throughout this loop run, as time passes
  390. // while we try connections etc.
  391. now := time.Now()
  392. // Attempt to dial all devices that are unconnected or can be connection-upgraded
  393. s.dialDevices(ctx, now, cfg, bestDialerPriority, nextDialAt, isInitialRampup)
  394. var sleep time.Duration
  395. if isInitialRampup {
  396. // We are in the initial rampup time, so we slowly, statically
  397. // increase the sleep time.
  398. sleep = initialRampup
  399. initialRampup *= 2
  400. } else {
  401. // The sleep time is until the next dial scheduled in nextDialAt,
  402. // clamped by stdConnectionLoopSleep as we don't want to sleep too
  403. // long (config changes might happen).
  404. sleep = nextDialAt.sleepDurationAndCleanup(now)
  405. }
  406. // ... while making sure not to loop too quickly either.
  407. if sleep < minConnectionLoopSleep {
  408. sleep = minConnectionLoopSleep
  409. }
  410. l.Debugln("Next connection loop in", sleep)
  411. timeout := time.NewTimer(sleep)
  412. select {
  413. case <-s.dialNow:
  414. // Remove affected devices from nextDialAt to dial immediately,
  415. // regardless of when we last dialed it (there's cool down in the
  416. // registry for too many repeat dials).
  417. s.dialNowDevicesMut.Lock()
  418. for device := range s.dialNowDevices {
  419. nextDialAt.redialDevice(device, now)
  420. }
  421. s.dialNowDevices = make(map[protocol.DeviceID]struct{})
  422. s.dialNowDevicesMut.Unlock()
  423. timeout.Stop()
  424. case <-timeout.C:
  425. case <-ctx.Done():
  426. return ctx.Err()
  427. }
  428. }
  429. }
  430. func (*service) bestDialerPriority(cfg config.Configuration) int {
  431. bestDialerPriority := worstDialerPriority
  432. for _, df := range dialers {
  433. if df.Valid(cfg) != nil {
  434. continue
  435. }
  436. if prio := df.Priority(); prio < bestDialerPriority {
  437. bestDialerPriority = prio
  438. }
  439. }
  440. return bestDialerPriority
  441. }
  442. func (s *service) dialDevices(ctx context.Context, now time.Time, cfg config.Configuration, bestDialerPriority int, nextDialAt nextDialRegistry, initial bool) {
  443. // Figure out current connection limits up front to see if there's any
  444. // point in resolving devices and such at all.
  445. allowAdditional := 0 // no limit
  446. connectionLimit := cfg.Options.LowestConnectionLimit()
  447. if connectionLimit > 0 {
  448. current := s.model.NumConnections()
  449. allowAdditional = connectionLimit - current
  450. if allowAdditional <= 0 {
  451. l.Debugf("Skipping dial because we've reached the connection limit, current %d >= limit %d", current, connectionLimit)
  452. return
  453. }
  454. }
  455. // Get device statistics for the last seen time of each device. This
  456. // isn't critical, so ignore the potential error.
  457. stats, _ := s.model.DeviceStatistics()
  458. queue := make(dialQueue, 0, len(cfg.Devices))
  459. for _, deviceCfg := range cfg.Devices {
  460. // Don't attempt to connect to ourselves...
  461. if deviceCfg.DeviceID == s.myID {
  462. continue
  463. }
  464. // Don't attempt to connect to paused devices...
  465. if deviceCfg.Paused {
  466. continue
  467. }
  468. // See if we are already connected and, if so, what our cutoff is
  469. // for dialer priority.
  470. priorityCutoff := worstDialerPriority
  471. connection, connected := s.model.Connection(deviceCfg.DeviceID)
  472. if connected {
  473. priorityCutoff = connection.Priority()
  474. if bestDialerPriority >= priorityCutoff {
  475. // Our best dialer is not any better than what we already
  476. // have, so nothing to do here.
  477. continue
  478. }
  479. }
  480. dialTargets := s.resolveDialTargets(ctx, now, cfg, deviceCfg, nextDialAt, initial, priorityCutoff)
  481. if len(dialTargets) > 0 {
  482. queue = append(queue, dialQueueEntry{
  483. id: deviceCfg.DeviceID,
  484. lastSeen: stats[deviceCfg.DeviceID].LastSeen,
  485. shortLived: stats[deviceCfg.DeviceID].LastConnectionDurationS < shortLivedConnectionThreshold.Seconds(),
  486. targets: dialTargets,
  487. })
  488. }
  489. }
  490. // Sort the queue in an order we think will be useful (most recent
  491. // first, deprioriting unstable devices, randomizing those we haven't
  492. // seen in a long while). If we don't do connection limiting the sorting
  493. // doesn't have much effect, but it may result in getting up and running
  494. // quicker if only a subset of configured devices are actually reachable
  495. // (by prioritizing those that were reachable recently).
  496. queue.Sort()
  497. // Perform dials according to the queue, stopping when we've reached the
  498. // allowed additional number of connections (if limited).
  499. numConns := 0
  500. var numConnsMut stdsync.Mutex
  501. dialSemaphore := util.NewSemaphore(dialMaxParallel)
  502. dialWG := new(stdsync.WaitGroup)
  503. dialCtx, dialCancel := context.WithCancel(ctx)
  504. defer func() {
  505. dialWG.Wait()
  506. dialCancel()
  507. }()
  508. for i := range queue {
  509. select {
  510. case <-dialCtx.Done():
  511. return
  512. default:
  513. }
  514. dialWG.Add(1)
  515. go func(entry dialQueueEntry) {
  516. defer dialWG.Done()
  517. conn, ok := s.dialParallel(dialCtx, entry.id, entry.targets, dialSemaphore)
  518. if !ok {
  519. return
  520. }
  521. numConnsMut.Lock()
  522. if allowAdditional == 0 || numConns < allowAdditional {
  523. select {
  524. case s.conns <- conn:
  525. numConns++
  526. if allowAdditional > 0 && numConns >= allowAdditional {
  527. dialCancel()
  528. }
  529. case <-dialCtx.Done():
  530. }
  531. }
  532. numConnsMut.Unlock()
  533. }(queue[i])
  534. }
  535. }
  536. func (s *service) resolveDialTargets(ctx context.Context, now time.Time, cfg config.Configuration, deviceCfg config.DeviceConfiguration, nextDialAt nextDialRegistry, initial bool, priorityCutoff int) []dialTarget {
  537. deviceID := deviceCfg.DeviceID
  538. addrs := s.resolveDeviceAddrs(ctx, deviceCfg)
  539. l.Debugln("Resolved device", deviceID, "addresses:", addrs)
  540. dialTargets := make([]dialTarget, 0, len(addrs))
  541. for _, addr := range addrs {
  542. // Use both device and address, as you might have two devices connected
  543. // to the same relay
  544. if !initial && nextDialAt.get(deviceID, addr).After(now) {
  545. l.Debugf("Not dialing %s via %v as it's not time yet", deviceID, addr)
  546. continue
  547. }
  548. // If we fail at any step before actually getting the dialer
  549. // retry in a minute
  550. nextDialAt.set(deviceID, addr, now.Add(time.Minute))
  551. uri, err := url.Parse(addr)
  552. if err != nil {
  553. s.setConnectionStatus(addr, err)
  554. l.Infof("Parsing dialer address %s: %v", addr, err)
  555. continue
  556. }
  557. if len(deviceCfg.AllowedNetworks) > 0 {
  558. if !IsAllowedNetwork(uri.Host, deviceCfg.AllowedNetworks) {
  559. s.setConnectionStatus(addr, errors.New("network disallowed"))
  560. l.Debugln("Network for", uri, "is disallowed")
  561. continue
  562. }
  563. }
  564. dialerFactory, err := getDialerFactory(cfg, uri)
  565. if err != nil {
  566. s.setConnectionStatus(addr, err)
  567. }
  568. if errors.Is(err, errUnsupported) {
  569. l.Debugf("Dialer for %v: %v", uri, err)
  570. continue
  571. } else if err != nil {
  572. l.Infof("Dialer for %v: %v", uri, err)
  573. continue
  574. }
  575. priority := dialerFactory.Priority()
  576. if priority >= priorityCutoff {
  577. l.Debugf("Not dialing using %s as priority is not better than current connection (%d >= %d)", dialerFactory, dialerFactory.Priority(), priorityCutoff)
  578. continue
  579. }
  580. dialer := dialerFactory.New(s.cfg.Options(), s.tlsCfg, s.registry)
  581. nextDialAt.set(deviceID, addr, now.Add(dialer.RedialFrequency()))
  582. // For LAN addresses, increase the priority so that we
  583. // try these first.
  584. switch {
  585. case dialerFactory.AlwaysWAN():
  586. // Do nothing.
  587. case s.isLANHost(uri.Host):
  588. priority--
  589. }
  590. dialTargets = append(dialTargets, dialTarget{
  591. addr: addr,
  592. dialer: dialer,
  593. priority: priority,
  594. deviceID: deviceID,
  595. uri: uri,
  596. })
  597. }
  598. return dialTargets
  599. }
  600. func (s *service) resolveDeviceAddrs(ctx context.Context, cfg config.DeviceConfiguration) []string {
  601. var addrs []string
  602. for _, addr := range cfg.Addresses {
  603. if addr == "dynamic" {
  604. if s.discoverer != nil {
  605. if t, err := s.discoverer.Lookup(ctx, cfg.DeviceID); err == nil {
  606. addrs = append(addrs, t...)
  607. }
  608. }
  609. } else {
  610. addrs = append(addrs, addr)
  611. }
  612. }
  613. return util.UniqueTrimmedStrings(addrs)
  614. }
  615. func (s *service) isLANHost(host string) bool {
  616. // Probably we are called with an ip:port combo which we can resolve as
  617. // a TCP address.
  618. if addr, err := net.ResolveTCPAddr("tcp", host); err == nil {
  619. return s.isLAN(addr)
  620. }
  621. // ... but this function looks general enough that someone might try
  622. // with just an IP as well in the future so lets allow that.
  623. if addr, err := net.ResolveIPAddr("ip", host); err == nil {
  624. return s.isLAN(addr)
  625. }
  626. return false
  627. }
  628. func (s *service) isLAN(addr net.Addr) bool {
  629. var ip net.IP
  630. switch addr := addr.(type) {
  631. case *net.IPAddr:
  632. ip = addr.IP
  633. case *net.TCPAddr:
  634. ip = addr.IP
  635. case *net.UDPAddr:
  636. ip = addr.IP
  637. default:
  638. // From the standard library, just Unix sockets.
  639. // If you invent your own, handle it.
  640. return false
  641. }
  642. if ip.IsLoopback() {
  643. return true
  644. }
  645. for _, lan := range s.cfg.Options().AlwaysLocalNets {
  646. _, ipnet, err := net.ParseCIDR(lan)
  647. if err != nil {
  648. l.Debugln("Network", lan, "is malformed:", err)
  649. continue
  650. }
  651. if ipnet.Contains(ip) {
  652. return true
  653. }
  654. }
  655. lans, _ := osutil.GetLans()
  656. for _, lan := range lans {
  657. if lan.Contains(ip) {
  658. return true
  659. }
  660. }
  661. return false
  662. }
  663. func (s *service) createListener(factory listenerFactory, uri *url.URL) bool {
  664. // must be called with listenerMut held
  665. l.Debugln("Starting listener", uri)
  666. listener := factory.New(uri, s.cfg, s.tlsCfg, s.conns, s.natService, s.registry)
  667. listener.OnAddressesChanged(s.logListenAddressesChangedEvent)
  668. // Retrying a listener many times in rapid succession is unlikely to help,
  669. // thus back off quickly. A listener may soon be functional again, e.g. due
  670. // to a network interface coming back online - retry every minute.
  671. spec := svcutil.SpecWithInfoLogger(l)
  672. spec.FailureThreshold = 2
  673. spec.FailureBackoff = time.Minute
  674. sup := suture.New(fmt.Sprintf("listenerSupervisor@%v", listener), spec)
  675. sup.Add(listener)
  676. s.listeners[uri.String()] = listener
  677. s.listenerTokens[uri.String()] = s.Add(sup)
  678. return true
  679. }
  680. func (s *service) logListenAddressesChangedEvent(l ListenerAddresses) {
  681. s.evLogger.Log(events.ListenAddressesChanged, map[string]interface{}{
  682. "address": l.URI,
  683. "lan": l.LANAddresses,
  684. "wan": l.WANAddresses,
  685. })
  686. }
  687. func (s *service) CommitConfiguration(from, to config.Configuration) bool {
  688. newDevices := make(map[protocol.DeviceID]bool, len(to.Devices))
  689. for _, dev := range to.Devices {
  690. newDevices[dev.DeviceID] = true
  691. }
  692. for _, dev := range from.Devices {
  693. if !newDevices[dev.DeviceID] {
  694. warningLimitersMut.Lock()
  695. delete(warningLimiters, dev.DeviceID)
  696. warningLimitersMut.Unlock()
  697. }
  698. }
  699. s.checkAndSignalConnectLoopOnUpdatedDevices(from, to)
  700. s.listenersMut.Lock()
  701. seen := make(map[string]struct{})
  702. for _, addr := range to.Options.ListenAddresses() {
  703. if addr == "" {
  704. // We can get an empty address if there is an empty listener
  705. // element in the config, indicating no listeners should be
  706. // used. This is not an error.
  707. continue
  708. }
  709. uri, err := url.Parse(addr)
  710. if err != nil {
  711. l.Warnf("Skipping malformed listener URL %q: %v", addr, err)
  712. continue
  713. }
  714. // Make sure we always have the canonical representation of the URL.
  715. // This is for consistency as we use it as a map key, but also to
  716. // avoid misunderstandings. We do not just use the canonicalized
  717. // version, because an URL that looks very similar to a human might
  718. // mean something entirely different to the computer (e.g.,
  719. // tcp:/127.0.0.1:22000 in fact being equivalent to tcp://:22000).
  720. if canonical := uri.String(); canonical != addr {
  721. l.Warnf("Skipping malformed listener URL %q (not canonical)", addr)
  722. continue
  723. }
  724. if _, ok := s.listeners[addr]; ok {
  725. seen[addr] = struct{}{}
  726. continue
  727. }
  728. factory, err := getListenerFactory(to, uri)
  729. if errors.Is(err, errUnsupported) {
  730. l.Debugf("Listener for %v: %v", uri, err)
  731. continue
  732. } else if err != nil {
  733. l.Infof("Listener for %v: %v", uri, err)
  734. continue
  735. }
  736. s.createListener(factory, uri)
  737. seen[addr] = struct{}{}
  738. }
  739. for addr, listener := range s.listeners {
  740. if _, ok := seen[addr]; !ok || listener.Factory().Valid(to) != nil {
  741. l.Debugln("Stopping listener", addr)
  742. s.Remove(s.listenerTokens[addr])
  743. delete(s.listenerTokens, addr)
  744. delete(s.listeners, addr)
  745. }
  746. }
  747. s.listenersMut.Unlock()
  748. return true
  749. }
  750. func (s *service) checkAndSignalConnectLoopOnUpdatedDevices(from, to config.Configuration) {
  751. oldDevices := from.DeviceMap()
  752. dial := false
  753. s.dialNowDevicesMut.Lock()
  754. for _, dev := range to.Devices {
  755. if dev.Paused {
  756. continue
  757. }
  758. if oldDev, ok := oldDevices[dev.DeviceID]; !ok || oldDev.Paused {
  759. s.dialNowDevices[dev.DeviceID] = struct{}{}
  760. dial = true
  761. } else if !util.EqualStrings(oldDev.Addresses, dev.Addresses) {
  762. dial = true
  763. }
  764. }
  765. if dial {
  766. s.scheduleDialNow()
  767. }
  768. s.dialNowDevicesMut.Unlock()
  769. }
  770. func (s *service) scheduleDialNow() {
  771. select {
  772. case s.dialNow <- struct{}{}:
  773. default:
  774. // channel is blocked - a config update is already pending for the connection loop.
  775. }
  776. }
  777. func (s *service) AllAddresses() []string {
  778. s.listenersMut.RLock()
  779. var addrs []string
  780. for _, listener := range s.listeners {
  781. for _, lanAddr := range listener.LANAddresses() {
  782. addrs = append(addrs, lanAddr.String())
  783. }
  784. for _, wanAddr := range listener.WANAddresses() {
  785. addrs = append(addrs, wanAddr.String())
  786. }
  787. }
  788. s.listenersMut.RUnlock()
  789. return util.UniqueTrimmedStrings(addrs)
  790. }
  791. func (s *service) ExternalAddresses() []string {
  792. if s.cfg.Options().AnnounceLANAddresses {
  793. return s.AllAddresses()
  794. }
  795. s.listenersMut.RLock()
  796. var addrs []string
  797. for _, listener := range s.listeners {
  798. for _, wanAddr := range listener.WANAddresses() {
  799. addrs = append(addrs, wanAddr.String())
  800. }
  801. }
  802. s.listenersMut.RUnlock()
  803. return util.UniqueTrimmedStrings(addrs)
  804. }
  805. func (s *service) ListenerStatus() map[string]ListenerStatusEntry {
  806. result := make(map[string]ListenerStatusEntry)
  807. s.listenersMut.RLock()
  808. for addr, listener := range s.listeners {
  809. var status ListenerStatusEntry
  810. if err := listener.Error(); err != nil {
  811. errStr := err.Error()
  812. status.Error = &errStr
  813. }
  814. status.LANAddresses = urlsToStrings(listener.LANAddresses())
  815. status.WANAddresses = urlsToStrings(listener.WANAddresses())
  816. result[addr] = status
  817. }
  818. s.listenersMut.RUnlock()
  819. return result
  820. }
  821. type connectionStatusHandler struct {
  822. connectionStatusMut sync.RWMutex
  823. connectionStatus map[string]ConnectionStatusEntry // address -> latest error/status
  824. }
  825. func newConnectionStatusHandler() connectionStatusHandler {
  826. return connectionStatusHandler{
  827. connectionStatusMut: sync.NewRWMutex(),
  828. connectionStatus: make(map[string]ConnectionStatusEntry),
  829. }
  830. }
  831. func (s *connectionStatusHandler) ConnectionStatus() map[string]ConnectionStatusEntry {
  832. result := make(map[string]ConnectionStatusEntry)
  833. s.connectionStatusMut.RLock()
  834. for k, v := range s.connectionStatus {
  835. result[k] = v
  836. }
  837. s.connectionStatusMut.RUnlock()
  838. return result
  839. }
  840. func (s *connectionStatusHandler) setConnectionStatus(address string, err error) {
  841. if errors.Is(err, context.Canceled) {
  842. return
  843. }
  844. status := ConnectionStatusEntry{When: time.Now().UTC().Truncate(time.Second)}
  845. if err != nil {
  846. errStr := err.Error()
  847. status.Error = &errStr
  848. }
  849. s.connectionStatusMut.Lock()
  850. s.connectionStatus[address] = status
  851. s.connectionStatusMut.Unlock()
  852. }
  853. func (s *service) NATType() string {
  854. s.listenersMut.RLock()
  855. defer s.listenersMut.RUnlock()
  856. for _, listener := range s.listeners {
  857. natType := listener.NATType()
  858. if natType != "unknown" {
  859. return natType
  860. }
  861. }
  862. return "unknown"
  863. }
  864. func getDialerFactory(cfg config.Configuration, uri *url.URL) (dialerFactory, error) {
  865. dialerFactory, ok := dialers[uri.Scheme]
  866. if !ok {
  867. return nil, fmt.Errorf("unknown address scheme %q", uri.Scheme)
  868. }
  869. if err := dialerFactory.Valid(cfg); err != nil {
  870. return nil, err
  871. }
  872. return dialerFactory, nil
  873. }
  874. func getListenerFactory(cfg config.Configuration, uri *url.URL) (listenerFactory, error) {
  875. listenerFactory, ok := listeners[uri.Scheme]
  876. if !ok {
  877. return nil, fmt.Errorf("unknown address scheme %q", uri.Scheme)
  878. }
  879. if err := listenerFactory.Valid(cfg); err != nil {
  880. return nil, err
  881. }
  882. return listenerFactory, nil
  883. }
  884. func urlsToStrings(urls []*url.URL) []string {
  885. strings := make([]string, len(urls))
  886. for i, url := range urls {
  887. strings[i] = url.String()
  888. }
  889. return strings
  890. }
  891. var warningLimiters = make(map[protocol.DeviceID]*rate.Limiter)
  892. var warningLimitersMut = sync.NewMutex()
  893. func warningFor(dev protocol.DeviceID, msg string) {
  894. warningLimitersMut.Lock()
  895. defer warningLimitersMut.Unlock()
  896. lim, ok := warningLimiters[dev]
  897. if !ok {
  898. lim = rate.NewLimiter(rate.Every(perDeviceWarningIntv), 1)
  899. warningLimiters[dev] = lim
  900. }
  901. if lim.Allow() {
  902. l.Warnln(msg)
  903. }
  904. }
  905. func tlsTimedHandshake(tc *tls.Conn) error {
  906. tc.SetDeadline(time.Now().Add(tlsHandshakeTimeout))
  907. defer tc.SetDeadline(time.Time{})
  908. return tc.Handshake()
  909. }
  910. // IsAllowedNetwork returns true if the given host (IP or resolvable
  911. // hostname) is in the set of allowed networks (CIDR format only).
  912. func IsAllowedNetwork(host string, allowed []string) bool {
  913. if hostNoPort, _, err := net.SplitHostPort(host); err == nil {
  914. host = hostNoPort
  915. }
  916. addr, err := net.ResolveIPAddr("ip", host)
  917. if err != nil {
  918. return false
  919. }
  920. for _, n := range allowed {
  921. result := true
  922. if strings.HasPrefix(n, "!") {
  923. result = false
  924. n = n[1:]
  925. }
  926. _, cidr, err := net.ParseCIDR(n)
  927. if err != nil {
  928. continue
  929. }
  930. if cidr.Contains(addr.IP) {
  931. return result
  932. }
  933. }
  934. return false
  935. }
  936. func (s *service) dialParallel(ctx context.Context, deviceID protocol.DeviceID, dialTargets []dialTarget, parentSema *util.Semaphore) (internalConn, bool) {
  937. // Group targets into buckets by priority
  938. dialTargetBuckets := make(map[int][]dialTarget, len(dialTargets))
  939. for _, tgt := range dialTargets {
  940. dialTargetBuckets[tgt.priority] = append(dialTargetBuckets[tgt.priority], tgt)
  941. }
  942. // Get all available priorities
  943. priorities := make([]int, 0, len(dialTargetBuckets))
  944. for prio := range dialTargetBuckets {
  945. priorities = append(priorities, prio)
  946. }
  947. // Sort the priorities so that we dial lowest first (which means highest...)
  948. sort.Ints(priorities)
  949. sema := util.MultiSemaphore{util.NewSemaphore(dialMaxParallelPerDevice), parentSema}
  950. for _, prio := range priorities {
  951. tgts := dialTargetBuckets[prio]
  952. res := make(chan internalConn, len(tgts))
  953. wg := stdsync.WaitGroup{}
  954. for _, tgt := range tgts {
  955. sema.Take(1)
  956. wg.Add(1)
  957. go func(tgt dialTarget) {
  958. defer func() {
  959. wg.Done()
  960. sema.Give(1)
  961. }()
  962. conn, err := tgt.Dial(ctx)
  963. if err == nil {
  964. // Closes the connection on error
  965. err = s.validateIdentity(conn, deviceID)
  966. }
  967. s.setConnectionStatus(tgt.addr, err)
  968. if err != nil {
  969. l.Debugln("dialing", deviceID, tgt.uri, "error:", err)
  970. } else {
  971. l.Debugln("dialing", deviceID, tgt.uri, "success:", conn)
  972. res <- conn
  973. }
  974. }(tgt)
  975. }
  976. // Spawn a routine which will unblock main routine in case we fail
  977. // to connect to anyone.
  978. go func() {
  979. wg.Wait()
  980. close(res)
  981. }()
  982. // Wait for the first connection, or for channel closure.
  983. if conn, ok := <-res; ok {
  984. // Got a connection, means more might come back, hence spawn a
  985. // routine that will do the discarding.
  986. l.Debugln("connected to", deviceID, prio, "using", conn, conn.priority)
  987. go func(deviceID protocol.DeviceID, prio int) {
  988. wg.Wait()
  989. l.Debugln("discarding", len(res), "connections while connecting to", deviceID, prio)
  990. for conn := range res {
  991. conn.Close()
  992. }
  993. }(deviceID, prio)
  994. return conn, ok
  995. }
  996. // Failed to connect, report that fact.
  997. l.Debugln("failed to connect to", deviceID, prio)
  998. }
  999. return internalConn{}, false
  1000. }
  1001. func (s *service) validateIdentity(c internalConn, expectedID protocol.DeviceID) error {
  1002. cs := c.ConnectionState()
  1003. // We should have received exactly one certificate from the other
  1004. // side. If we didn't, they don't have a device ID and we drop the
  1005. // connection.
  1006. certs := cs.PeerCertificates
  1007. if cl := len(certs); cl != 1 {
  1008. l.Infof("Got peer certificate list of length %d != 1 from peer at %s; protocol error", cl, c)
  1009. c.Close()
  1010. return fmt.Errorf("expected 1 certificate, got %d", cl)
  1011. }
  1012. remoteCert := certs[0]
  1013. remoteID := protocol.NewDeviceID(remoteCert.Raw)
  1014. // The device ID should not be that of ourselves. It can happen
  1015. // though, especially in the presence of NAT hairpinning, multiple
  1016. // clients between the same NAT gateway, and global discovery.
  1017. if remoteID == s.myID {
  1018. l.Debugf("Connected to myself (%s) at %s", remoteID, c)
  1019. c.Close()
  1020. return errors.New("connected to self")
  1021. }
  1022. // We should see the expected device ID
  1023. if !remoteID.Equals(expectedID) {
  1024. c.Close()
  1025. return fmt.Errorf("unexpected device id, expected %s got %s", expectedID, remoteID)
  1026. }
  1027. return nil
  1028. }
  1029. type nextDialRegistry map[protocol.DeviceID]nextDialDevice
  1030. type nextDialDevice struct {
  1031. nextDial map[string]time.Time
  1032. coolDownIntervalStart time.Time
  1033. attempts int
  1034. }
  1035. func (r nextDialRegistry) get(device protocol.DeviceID, addr string) time.Time {
  1036. return r[device].nextDial[addr]
  1037. }
  1038. const (
  1039. dialCoolDownInterval = 2 * time.Minute
  1040. dialCoolDownDelay = 5 * time.Minute
  1041. dialCoolDownMaxAttemps = 3
  1042. )
  1043. // redialDevice marks the device for immediate redial, unless the remote keeps
  1044. // dropping established connections. Thus we keep track of when the first forced
  1045. // re-dial happened, and how many attempts happen in the dialCoolDownInterval
  1046. // after that. If it's more than dialCoolDownMaxAttempts, don't force-redial
  1047. // that device for dialCoolDownDelay (regular dials still happen).
  1048. func (r nextDialRegistry) redialDevice(device protocol.DeviceID, now time.Time) {
  1049. dev, ok := r[device]
  1050. if !ok {
  1051. r[device] = nextDialDevice{
  1052. nextDial: make(map[string]time.Time),
  1053. coolDownIntervalStart: now,
  1054. attempts: 1,
  1055. }
  1056. return
  1057. }
  1058. if dev.attempts == 0 || now.Before(dev.coolDownIntervalStart.Add(dialCoolDownInterval)) {
  1059. if dev.attempts >= dialCoolDownMaxAttemps {
  1060. // Device has been force redialed too often - let it cool down.
  1061. return
  1062. }
  1063. if dev.attempts == 0 {
  1064. dev.coolDownIntervalStart = now
  1065. }
  1066. dev.attempts++
  1067. dev.nextDial = make(map[string]time.Time)
  1068. return
  1069. }
  1070. if dev.attempts >= dialCoolDownMaxAttemps && now.Before(dev.coolDownIntervalStart.Add(dialCoolDownDelay)) {
  1071. return // Still cooling down
  1072. }
  1073. delete(r, device)
  1074. }
  1075. func (r nextDialRegistry) set(device protocol.DeviceID, addr string, next time.Time) {
  1076. if _, ok := r[device]; !ok {
  1077. r[device] = nextDialDevice{nextDial: make(map[string]time.Time)}
  1078. }
  1079. r[device].nextDial[addr] = next
  1080. }
  1081. func (r nextDialRegistry) sleepDurationAndCleanup(now time.Time) time.Duration {
  1082. sleep := stdConnectionLoopSleep
  1083. for id, dev := range r {
  1084. for address, next := range dev.nextDial {
  1085. if next.Before(now) {
  1086. // Expired entry, address was not seen in last pass(es)
  1087. delete(dev.nextDial, address)
  1088. continue
  1089. }
  1090. if cur := next.Sub(now); cur < sleep {
  1091. sleep = cur
  1092. }
  1093. }
  1094. if dev.attempts > 0 {
  1095. interval := dialCoolDownInterval
  1096. if dev.attempts >= dialCoolDownMaxAttemps {
  1097. interval = dialCoolDownDelay
  1098. }
  1099. if now.After(dev.coolDownIntervalStart.Add(interval)) {
  1100. dev.attempts = 0
  1101. }
  1102. }
  1103. if len(dev.nextDial) == 0 && dev.attempts == 0 {
  1104. delete(r, id)
  1105. }
  1106. }
  1107. return sleep
  1108. }