service.go 31 KB

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