service.go 45 KB

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