service.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 http://mozilla.org/MPL/2.0/.
  6. package connections
  7. import (
  8. "crypto/tls"
  9. "errors"
  10. "fmt"
  11. "io"
  12. "net"
  13. "net/url"
  14. "time"
  15. "github.com/juju/ratelimit"
  16. "github.com/syncthing/syncthing/lib/config"
  17. "github.com/syncthing/syncthing/lib/discover"
  18. "github.com/syncthing/syncthing/lib/events"
  19. "github.com/syncthing/syncthing/lib/nat"
  20. "github.com/syncthing/syncthing/lib/protocol"
  21. "github.com/syncthing/syncthing/lib/sync"
  22. "github.com/syncthing/syncthing/lib/util"
  23. // Registers NAT service providers
  24. _ "github.com/syncthing/syncthing/lib/pmp"
  25. _ "github.com/syncthing/syncthing/lib/upnp"
  26. "github.com/thejerf/suture"
  27. )
  28. var (
  29. dialers = make(map[string]dialerFactory, 0)
  30. listeners = make(map[string]listenerFactory, 0)
  31. )
  32. const (
  33. perDeviceWarningRate = 1.0 / (15 * 60) // Once per 15 minutes
  34. tlsHandshakeTimeout = 10 * time.Second
  35. )
  36. // Service listens and dials all configured unconnected devices, via supported
  37. // dialers. Successful connections are handed to the model.
  38. type Service struct {
  39. *suture.Supervisor
  40. cfg *config.Wrapper
  41. myID protocol.DeviceID
  42. model Model
  43. tlsCfg *tls.Config
  44. discoverer discover.Finder
  45. conns chan IntermediateConnection
  46. bepProtocolName string
  47. tlsDefaultCommonName string
  48. lans []*net.IPNet
  49. writeRateLimit *ratelimit.Bucket
  50. readRateLimit *ratelimit.Bucket
  51. natService *nat.Service
  52. natServiceToken *suture.ServiceToken
  53. listenersMut sync.RWMutex
  54. listeners map[string]genericListener
  55. listenerTokens map[string]suture.ServiceToken
  56. curConMut sync.Mutex
  57. currentConnection map[protocol.DeviceID]Connection
  58. }
  59. func NewService(cfg *config.Wrapper, myID protocol.DeviceID, mdl Model, tlsCfg *tls.Config, discoverer discover.Finder,
  60. bepProtocolName string, tlsDefaultCommonName string, lans []*net.IPNet) *Service {
  61. service := &Service{
  62. Supervisor: suture.NewSimple("connections.Service"),
  63. cfg: cfg,
  64. myID: myID,
  65. model: mdl,
  66. tlsCfg: tlsCfg,
  67. discoverer: discoverer,
  68. conns: make(chan IntermediateConnection),
  69. bepProtocolName: bepProtocolName,
  70. tlsDefaultCommonName: tlsDefaultCommonName,
  71. lans: lans,
  72. natService: nat.NewService(myID, cfg),
  73. listenersMut: sync.NewRWMutex(),
  74. listeners: make(map[string]genericListener),
  75. listenerTokens: make(map[string]suture.ServiceToken),
  76. curConMut: sync.NewMutex(),
  77. currentConnection: make(map[protocol.DeviceID]Connection),
  78. }
  79. cfg.Subscribe(service)
  80. // The rate variables are in KiB/s in the UI (despite the camel casing
  81. // of the name). We multiply by 1024 here to get B/s.
  82. options := service.cfg.Options()
  83. if options.MaxSendKbps > 0 {
  84. service.writeRateLimit = ratelimit.NewBucketWithRate(float64(1024*options.MaxSendKbps), int64(5*1024*options.MaxSendKbps))
  85. }
  86. if options.MaxRecvKbps > 0 {
  87. service.readRateLimit = ratelimit.NewBucketWithRate(float64(1024*options.MaxRecvKbps), int64(5*1024*options.MaxRecvKbps))
  88. }
  89. // There are several moving parts here; one routine per listening address
  90. // (handled in configuration changing) to handle incoming connections,
  91. // one routine to periodically attempt outgoing connections, one routine to
  92. // the the common handling regardless of whether the connection was
  93. // incoming or outgoing.
  94. service.Add(serviceFunc(service.connect))
  95. service.Add(serviceFunc(service.handle))
  96. raw := cfg.Raw()
  97. // Actually starts the listeners and NAT service
  98. service.CommitConfiguration(raw, raw)
  99. return service
  100. }
  101. var (
  102. errDisabled = errors.New("disabled by configuration")
  103. )
  104. func (s *Service) handle() {
  105. next:
  106. for c := range s.conns {
  107. cs := c.ConnectionState()
  108. // We should have negotiated the next level protocol "bep/1.0" as part
  109. // of the TLS handshake. Unfortunately this can't be a hard error,
  110. // because there are implementations out there that don't support
  111. // protocol negotiation (iOS for one...).
  112. if !cs.NegotiatedProtocolIsMutual || cs.NegotiatedProtocol != s.bepProtocolName {
  113. l.Infof("Peer %s did not negotiate bep/1.0", c.RemoteAddr())
  114. }
  115. // We should have received exactly one certificate from the other
  116. // side. If we didn't, they don't have a device ID and we drop the
  117. // connection.
  118. certs := cs.PeerCertificates
  119. if cl := len(certs); cl != 1 {
  120. l.Infof("Got peer certificate list of length %d != 1 from %s; protocol error", cl, c.RemoteAddr())
  121. c.Close()
  122. continue
  123. }
  124. remoteCert := certs[0]
  125. remoteID := protocol.NewDeviceID(remoteCert.Raw)
  126. // The device ID should not be that of ourselves. It can happen
  127. // though, especially in the presence of NAT hairpinning, multiple
  128. // clients between the same NAT gateway, and global discovery.
  129. if remoteID == s.myID {
  130. l.Infof("Connected to myself (%s) - should not happen", remoteID)
  131. c.Close()
  132. continue
  133. }
  134. c.SetDeadline(time.Now().Add(20 * time.Second))
  135. hello, err := protocol.ExchangeHello(c, s.model.GetHello(remoteID))
  136. if err != nil {
  137. if protocol.IsVersionMismatch(err) {
  138. // The error will be a relatively user friendly description
  139. // of what's wrong with the version compatibility. By
  140. // default identify the other side by device ID and IP.
  141. remote := fmt.Sprintf("%v (%v)", remoteID, c.RemoteAddr())
  142. if hello.DeviceName != "" {
  143. // If the name was set in the hello return, use that to
  144. // give the user more info about which device is the
  145. // affected one. It probably says more than the remote
  146. // IP.
  147. remote = fmt.Sprintf("%q (%s %s, %v)", hello.DeviceName, hello.ClientName, hello.ClientVersion, remoteID)
  148. }
  149. msg := fmt.Sprintf("Connecting to %s: %s", remote, err)
  150. warningFor(remoteID, msg)
  151. } else {
  152. // It's something else - connection reset or whatever
  153. l.Infof("Failed to exchange Hello messages with %s (%s): %s", remoteID, c.RemoteAddr(), err)
  154. }
  155. c.Close()
  156. continue
  157. }
  158. c.SetDeadline(time.Time{})
  159. // The Model will return an error for devices that we don't want to
  160. // have a connection with for whatever reason, for example unknown devices.
  161. if err := s.model.OnHello(remoteID, c.RemoteAddr(), hello); err != nil {
  162. l.Infof("Connection from %s at %s (%s) rejected: %v", remoteID, c.RemoteAddr(), c.Type, err)
  163. c.Close()
  164. continue
  165. }
  166. // If we have a relay connection, and the new incoming connection is
  167. // not a relay connection, we should drop that, and prefer the this one.
  168. connected := s.model.ConnectedTo(remoteID)
  169. s.curConMut.Lock()
  170. ct, ok := s.currentConnection[remoteID]
  171. s.curConMut.Unlock()
  172. priorityKnown := ok && connected
  173. // Lower priority is better, just like nice etc.
  174. if priorityKnown && ct.Priority > c.Priority {
  175. l.Debugln("Switching connections", remoteID)
  176. s.model.Close(remoteID, protocol.ErrSwitchingConnections)
  177. } else if connected {
  178. // We should not already be connected to the other party. TODO: This
  179. // could use some better handling. If the old connection is dead but
  180. // hasn't timed out yet we may want to drop *that* connection and keep
  181. // this one. But in case we are two devices connecting to each other
  182. // in parallel we don't want to do that or we end up with no
  183. // connections still established...
  184. l.Infof("Connected to already connected device (%s)", remoteID)
  185. c.Close()
  186. continue
  187. }
  188. deviceCfg, ok := s.cfg.Device(remoteID)
  189. if !ok {
  190. panic("bug: unknown device should already have been rejected")
  191. }
  192. // Verify the name on the certificate. By default we set it to
  193. // "syncthing" when generating, but the user may have replaced
  194. // the certificate and used another name.
  195. certName := deviceCfg.CertName
  196. if certName == "" {
  197. certName = s.tlsDefaultCommonName
  198. }
  199. if err := remoteCert.VerifyHostname(certName); err != nil {
  200. // Incorrect certificate name is something the user most
  201. // likely wants to know about, since it's an advanced
  202. // config. Warn instead of Info.
  203. l.Warnf("Bad certificate from %s (%v): %v", remoteID, c.RemoteAddr(), err)
  204. c.Close()
  205. continue next
  206. }
  207. // If rate limiting is set, and based on the address we should
  208. // limit the connection, then we wrap it in a limiter.
  209. limit := s.shouldLimit(c.RemoteAddr())
  210. wr := io.Writer(c)
  211. if limit && s.writeRateLimit != nil {
  212. wr = NewWriteLimiter(c, s.writeRateLimit)
  213. }
  214. rd := io.Reader(c)
  215. if limit && s.readRateLimit != nil {
  216. rd = NewReadLimiter(c, s.readRateLimit)
  217. }
  218. name := fmt.Sprintf("%s-%s (%s)", c.LocalAddr(), c.RemoteAddr(), c.Type)
  219. protoConn := protocol.NewConnection(remoteID, rd, wr, s.model, name, deviceCfg.Compression)
  220. modelConn := Connection{c, protoConn}
  221. l.Infof("Established secure connection to %s at %s", remoteID, name)
  222. l.Debugf("cipher suite: %04X in lan: %t", c.ConnectionState().CipherSuite, !limit)
  223. s.model.AddConnection(modelConn, hello)
  224. s.curConMut.Lock()
  225. s.currentConnection[remoteID] = modelConn
  226. s.curConMut.Unlock()
  227. continue next
  228. }
  229. }
  230. func (s *Service) connect() {
  231. nextDial := make(map[string]time.Time)
  232. // Used as delay for the first few connection attempts, increases
  233. // exponentially
  234. initialRampup := time.Second
  235. // Calculated from actual dialers reconnectInterval
  236. var sleep time.Duration
  237. for {
  238. cfg := s.cfg.Raw()
  239. bestDialerPrio := 1<<31 - 1 // worse prio won't build on 32 bit
  240. for _, df := range dialers {
  241. if !df.Enabled(cfg) {
  242. continue
  243. }
  244. if prio := df.Priority(); prio < bestDialerPrio {
  245. bestDialerPrio = prio
  246. }
  247. }
  248. l.Debugln("Reconnect loop")
  249. now := time.Now()
  250. var seen []string
  251. nextDevice:
  252. for _, deviceCfg := range cfg.Devices {
  253. deviceID := deviceCfg.DeviceID
  254. if deviceID == s.myID {
  255. continue
  256. }
  257. paused := s.model.IsPaused(deviceID)
  258. if paused {
  259. continue
  260. }
  261. connected := s.model.ConnectedTo(deviceID)
  262. s.curConMut.Lock()
  263. ct, ok := s.currentConnection[deviceID]
  264. s.curConMut.Unlock()
  265. priorityKnown := ok && connected
  266. if priorityKnown && ct.Priority == bestDialerPrio {
  267. // Things are already as good as they can get.
  268. continue
  269. }
  270. l.Debugln("Reconnect loop for", deviceID)
  271. var addrs []string
  272. for _, addr := range deviceCfg.Addresses {
  273. if addr == "dynamic" {
  274. if s.discoverer != nil {
  275. if t, err := s.discoverer.Lookup(deviceID); err == nil {
  276. addrs = append(addrs, t...)
  277. }
  278. }
  279. } else {
  280. addrs = append(addrs, addr)
  281. }
  282. }
  283. seen = append(seen, addrs...)
  284. for _, addr := range addrs {
  285. nextDialAt, ok := nextDial[addr]
  286. if ok && initialRampup >= sleep && nextDialAt.After(now) {
  287. l.Debugf("Not dialing %v as sleep is %v, next dial is at %s and current time is %s", addr, sleep, nextDialAt, now)
  288. continue
  289. }
  290. // If we fail at any step before actually getting the dialer
  291. // retry in a minute
  292. nextDial[addr] = now.Add(time.Minute)
  293. uri, err := url.Parse(addr)
  294. if err != nil {
  295. l.Infof("Dialer for %s: %v", addr, err)
  296. continue
  297. }
  298. dialerFactory, err := s.getDialerFactory(cfg, uri)
  299. if err == errDisabled {
  300. l.Debugln("Dialer for", uri, "is disabled")
  301. continue
  302. }
  303. if err != nil {
  304. l.Infof("Dialer for %v: %v", uri, err)
  305. continue
  306. }
  307. if priorityKnown && dialerFactory.Priority() >= ct.Priority {
  308. l.Debugf("Not dialing using %s as priority is less than current connection (%d >= %d)", dialerFactory, dialerFactory.Priority(), ct.Priority)
  309. continue
  310. }
  311. dialer := dialerFactory.New(s.cfg, s.tlsCfg)
  312. l.Debugln("dial", deviceCfg.DeviceID, uri)
  313. nextDial[addr] = now.Add(dialer.RedialFrequency())
  314. conn, err := dialer.Dial(deviceID, uri)
  315. if err != nil {
  316. l.Debugln("dial failed", deviceCfg.DeviceID, uri, err)
  317. continue
  318. }
  319. s.conns <- conn
  320. continue nextDevice
  321. }
  322. }
  323. nextDial, sleep = filterAndFindSleepDuration(nextDial, seen, now)
  324. if initialRampup < sleep {
  325. l.Debugln("initial rampup; sleep", initialRampup, "and update to", initialRampup*2)
  326. time.Sleep(initialRampup)
  327. initialRampup *= 2
  328. } else {
  329. l.Debugln("sleep until next dial", sleep)
  330. time.Sleep(sleep)
  331. }
  332. }
  333. }
  334. func (s *Service) shouldLimit(addr net.Addr) bool {
  335. if s.cfg.Options().LimitBandwidthInLan {
  336. return true
  337. }
  338. tcpaddr, ok := addr.(*net.TCPAddr)
  339. if !ok {
  340. return true
  341. }
  342. for _, lan := range s.lans {
  343. if lan.Contains(tcpaddr.IP) {
  344. return false
  345. }
  346. }
  347. return !tcpaddr.IP.IsLoopback()
  348. }
  349. func (s *Service) createListener(factory listenerFactory, uri *url.URL) bool {
  350. // must be called with listenerMut held
  351. l.Debugln("Starting listener", uri)
  352. listener := factory.New(uri, s.cfg, s.tlsCfg, s.conns, s.natService)
  353. listener.OnAddressesChanged(s.logListenAddressesChangedEvent)
  354. s.listeners[uri.String()] = listener
  355. s.listenerTokens[uri.String()] = s.Add(listener)
  356. return true
  357. }
  358. func (s *Service) logListenAddressesChangedEvent(l genericListener) {
  359. events.Default.Log(events.ListenAddressesChanged, map[string]interface{}{
  360. "address": l.URI(),
  361. "lan": l.LANAddresses(),
  362. "wan": l.WANAddresses(),
  363. })
  364. }
  365. func (s *Service) VerifyConfiguration(from, to config.Configuration) error {
  366. return nil
  367. }
  368. func (s *Service) CommitConfiguration(from, to config.Configuration) bool {
  369. newDevices := make(map[protocol.DeviceID]bool, len(to.Devices))
  370. for _, dev := range to.Devices {
  371. newDevices[dev.DeviceID] = true
  372. }
  373. for _, dev := range from.Devices {
  374. if !newDevices[dev.DeviceID] {
  375. s.curConMut.Lock()
  376. delete(s.currentConnection, dev.DeviceID)
  377. s.curConMut.Unlock()
  378. warningLimitersMut.Lock()
  379. delete(warningLimiters, dev.DeviceID)
  380. warningLimitersMut.Unlock()
  381. }
  382. }
  383. s.listenersMut.Lock()
  384. seen := make(map[string]struct{})
  385. for _, addr := range config.Wrap("", to).ListenAddresses() {
  386. if _, ok := s.listeners[addr]; ok {
  387. seen[addr] = struct{}{}
  388. continue
  389. }
  390. uri, err := url.Parse(addr)
  391. if err != nil {
  392. l.Infof("Listener for %s: %v", addr, err)
  393. continue
  394. }
  395. factory, err := s.getListenerFactory(to, uri)
  396. if err == errDisabled {
  397. l.Debugln("Listener for", uri, "is disabled")
  398. continue
  399. }
  400. if err != nil {
  401. l.Infof("Listener for %v: %v", uri, err)
  402. continue
  403. }
  404. s.createListener(factory, uri)
  405. seen[addr] = struct{}{}
  406. }
  407. for addr, listener := range s.listeners {
  408. if _, ok := seen[addr]; !ok || !listener.Factory().Enabled(to) {
  409. l.Debugln("Stopping listener", addr)
  410. s.Remove(s.listenerTokens[addr])
  411. delete(s.listenerTokens, addr)
  412. delete(s.listeners, addr)
  413. }
  414. }
  415. s.listenersMut.Unlock()
  416. if to.Options.NATEnabled && s.natServiceToken == nil {
  417. l.Debugln("Starting NAT service")
  418. token := s.Add(s.natService)
  419. s.natServiceToken = &token
  420. } else if !to.Options.NATEnabled && s.natServiceToken != nil {
  421. l.Debugln("Stopping NAT service")
  422. s.Remove(*s.natServiceToken)
  423. s.natServiceToken = nil
  424. }
  425. return true
  426. }
  427. func (s *Service) AllAddresses() []string {
  428. s.listenersMut.RLock()
  429. var addrs []string
  430. for _, listener := range s.listeners {
  431. for _, lanAddr := range listener.LANAddresses() {
  432. addrs = append(addrs, lanAddr.String())
  433. }
  434. for _, wanAddr := range listener.WANAddresses() {
  435. addrs = append(addrs, wanAddr.String())
  436. }
  437. }
  438. s.listenersMut.RUnlock()
  439. return util.UniqueStrings(addrs)
  440. }
  441. func (s *Service) ExternalAddresses() []string {
  442. s.listenersMut.RLock()
  443. var addrs []string
  444. for _, listener := range s.listeners {
  445. for _, wanAddr := range listener.WANAddresses() {
  446. addrs = append(addrs, wanAddr.String())
  447. }
  448. }
  449. s.listenersMut.RUnlock()
  450. return util.UniqueStrings(addrs)
  451. }
  452. func (s *Service) Status() map[string]interface{} {
  453. s.listenersMut.RLock()
  454. result := make(map[string]interface{})
  455. for addr, listener := range s.listeners {
  456. status := make(map[string]interface{})
  457. err := listener.Error()
  458. if err != nil {
  459. status["error"] = err.Error()
  460. }
  461. status["lanAddresses"] = urlsToStrings(listener.LANAddresses())
  462. status["wanAddresses"] = urlsToStrings(listener.WANAddresses())
  463. result[addr] = status
  464. }
  465. s.listenersMut.RUnlock()
  466. return result
  467. }
  468. func (s *Service) getDialerFactory(cfg config.Configuration, uri *url.URL) (dialerFactory, error) {
  469. dialerFactory, ok := dialers[uri.Scheme]
  470. if !ok {
  471. return nil, fmt.Errorf("unknown address scheme %q", uri.Scheme)
  472. }
  473. if !dialerFactory.Enabled(cfg) {
  474. return nil, errDisabled
  475. }
  476. return dialerFactory, nil
  477. }
  478. func (s *Service) getListenerFactory(cfg config.Configuration, uri *url.URL) (listenerFactory, error) {
  479. listenerFactory, ok := listeners[uri.Scheme]
  480. if !ok {
  481. return nil, fmt.Errorf("unknown address scheme %q", uri.Scheme)
  482. }
  483. if !listenerFactory.Enabled(cfg) {
  484. return nil, errDisabled
  485. }
  486. return listenerFactory, nil
  487. }
  488. func filterAndFindSleepDuration(nextDial map[string]time.Time, seen []string, now time.Time) (map[string]time.Time, time.Duration) {
  489. newNextDial := make(map[string]time.Time)
  490. for _, addr := range seen {
  491. nextDialAt, ok := nextDial[addr]
  492. if ok {
  493. newNextDial[addr] = nextDialAt
  494. }
  495. }
  496. min := time.Minute
  497. for _, next := range newNextDial {
  498. cur := next.Sub(now)
  499. if cur < min {
  500. min = cur
  501. }
  502. }
  503. return newNextDial, min
  504. }
  505. func urlsToStrings(urls []*url.URL) []string {
  506. strings := make([]string, len(urls))
  507. for i, url := range urls {
  508. strings[i] = url.String()
  509. }
  510. return strings
  511. }
  512. var warningLimiters = make(map[protocol.DeviceID]*ratelimit.Bucket)
  513. var warningLimitersMut = sync.NewMutex()
  514. func warningFor(dev protocol.DeviceID, msg string) {
  515. warningLimitersMut.Lock()
  516. defer warningLimitersMut.Unlock()
  517. lim, ok := warningLimiters[dev]
  518. if !ok {
  519. lim = ratelimit.NewBucketWithRate(perDeviceWarningRate, 1)
  520. warningLimiters[dev] = lim
  521. }
  522. if lim.TakeAvailable(1) == 1 {
  523. l.Warnln(msg)
  524. }
  525. }
  526. func tlsTimedHandshake(tc *tls.Conn) error {
  527. tc.SetDeadline(time.Now().Add(tlsHandshakeTimeout))
  528. defer tc.SetDeadline(time.Time{})
  529. return tc.Handshake()
  530. }