service.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. s.model.OnHello(remoteID, c.RemoteAddr(), hello)
  160. // If we have a relay connection, and the new incoming connection is
  161. // not a relay connection, we should drop that, and prefer the this one.
  162. s.curConMut.Lock()
  163. ct, ok := s.currentConnection[remoteID]
  164. s.curConMut.Unlock()
  165. // Lower priority is better, just like nice etc.
  166. if ok && ct.Priority > c.Priority {
  167. l.Debugln("Switching connections", remoteID)
  168. s.model.Close(remoteID, protocol.ErrSwitchingConnections)
  169. } else if s.model.ConnectedTo(remoteID) {
  170. // We should not already be connected to the other party. TODO: This
  171. // could use some better handling. If the old connection is dead but
  172. // hasn't timed out yet we may want to drop *that* connection and keep
  173. // this one. But in case we are two devices connecting to each other
  174. // in parallel we don't want to do that or we end up with no
  175. // connections still established...
  176. l.Infof("Connected to already connected device (%s)", remoteID)
  177. c.Close()
  178. continue
  179. } else if s.model.IsPaused(remoteID) {
  180. l.Infof("Connection from paused device (%s)", remoteID)
  181. c.Close()
  182. continue
  183. }
  184. for deviceID, deviceCfg := range s.cfg.Devices() {
  185. if deviceID == remoteID {
  186. // Verify the name on the certificate. By default we set it to
  187. // "syncthing" when generating, but the user may have replaced
  188. // the certificate and used another name.
  189. certName := deviceCfg.CertName
  190. if certName == "" {
  191. certName = s.tlsDefaultCommonName
  192. }
  193. err := remoteCert.VerifyHostname(certName)
  194. if err != nil {
  195. // Incorrect certificate name is something the user most
  196. // likely wants to know about, since it's an advanced
  197. // config. Warn instead of Info.
  198. l.Warnf("Bad certificate from %s (%v): %v", remoteID, c.RemoteAddr(), err)
  199. c.Close()
  200. continue next
  201. }
  202. // If rate limiting is set, and based on the address we should
  203. // limit the connection, then we wrap it in a limiter.
  204. limit := s.shouldLimit(c.RemoteAddr())
  205. wr := io.Writer(c)
  206. if limit && s.writeRateLimit != nil {
  207. wr = NewWriteLimiter(c, s.writeRateLimit)
  208. }
  209. rd := io.Reader(c)
  210. if limit && s.readRateLimit != nil {
  211. rd = NewReadLimiter(c, s.readRateLimit)
  212. }
  213. name := fmt.Sprintf("%s-%s (%s)", c.LocalAddr(), c.RemoteAddr(), c.Type)
  214. protoConn := protocol.NewConnection(remoteID, rd, wr, s.model, name, deviceCfg.Compression)
  215. modelConn := Connection{c, protoConn}
  216. l.Infof("Established secure connection to %s at %s", remoteID, name)
  217. l.Debugf("cipher suite: %04X in lan: %t", c.ConnectionState().CipherSuite, !limit)
  218. s.model.AddConnection(modelConn, hello)
  219. s.curConMut.Lock()
  220. s.currentConnection[remoteID] = modelConn
  221. s.curConMut.Unlock()
  222. continue next
  223. }
  224. }
  225. l.Infof("Connection from %s (%s) with ignored device ID %s", c.RemoteAddr(), c.Type, remoteID)
  226. c.Close()
  227. }
  228. }
  229. func (s *Service) connect() {
  230. nextDial := make(map[string]time.Time)
  231. // Used as delay for the first few connection attempts, increases
  232. // exponentially
  233. initialRampup := time.Second
  234. // Calculated from actual dialers reconnectInterval
  235. var sleep time.Duration
  236. for {
  237. cfg := s.cfg.Raw()
  238. bestDialerPrio := 1<<31 - 1 // worse prio won't build on 32 bit
  239. for _, df := range dialers {
  240. if !df.Enabled(cfg) {
  241. continue
  242. }
  243. if prio := df.Priority(); prio < bestDialerPrio {
  244. bestDialerPrio = prio
  245. }
  246. }
  247. l.Debugln("Reconnect loop")
  248. now := time.Now()
  249. var seen []string
  250. nextDevice:
  251. for _, deviceCfg := range cfg.Devices {
  252. deviceID := deviceCfg.DeviceID
  253. if deviceID == s.myID {
  254. continue
  255. }
  256. paused := s.model.IsPaused(deviceID)
  257. if paused {
  258. continue
  259. }
  260. connected := s.model.ConnectedTo(deviceID)
  261. s.curConMut.Lock()
  262. ct := s.currentConnection[deviceID]
  263. s.curConMut.Unlock()
  264. if connected && ct.Priority == bestDialerPrio {
  265. // Things are already as good as they can get.
  266. continue
  267. }
  268. l.Debugln("Reconnect loop for", deviceID)
  269. var addrs []string
  270. for _, addr := range deviceCfg.Addresses {
  271. if addr == "dynamic" {
  272. if s.discoverer != nil {
  273. if t, err := s.discoverer.Lookup(deviceID); err == nil {
  274. addrs = append(addrs, t...)
  275. }
  276. }
  277. } else {
  278. addrs = append(addrs, addr)
  279. }
  280. }
  281. seen = append(seen, addrs...)
  282. for _, addr := range addrs {
  283. nextDialAt, ok := nextDial[addr]
  284. if ok && initialRampup >= sleep && nextDialAt.After(now) {
  285. l.Debugf("Not dialing %v as sleep is %v, next dial is at %s and current time is %s", addr, sleep, nextDialAt, now)
  286. continue
  287. }
  288. // If we fail at any step before actually getting the dialer
  289. // retry in a minute
  290. nextDial[addr] = now.Add(time.Minute)
  291. uri, err := url.Parse(addr)
  292. if err != nil {
  293. l.Infof("Dialer for %s: %v", addr, err)
  294. continue
  295. }
  296. dialerFactory, err := s.getDialerFactory(cfg, uri)
  297. if err == errDisabled {
  298. l.Debugln("Dialer for", uri, "is disabled")
  299. continue
  300. }
  301. if err != nil {
  302. l.Infof("Dialer for %v: %v", uri, err)
  303. continue
  304. }
  305. if connected && dialerFactory.Priority() >= ct.Priority {
  306. l.Debugf("Not dialing using %s as priority is less than current connection (%d >= %d)", dialerFactory, dialerFactory.Priority(), ct.Priority)
  307. continue
  308. }
  309. dialer := dialerFactory.New(s.cfg, s.tlsCfg)
  310. l.Debugln("dial", deviceCfg.DeviceID, uri)
  311. nextDial[addr] = now.Add(dialer.RedialFrequency())
  312. conn, err := dialer.Dial(deviceID, uri)
  313. if err != nil {
  314. l.Debugln("dial failed", deviceCfg.DeviceID, uri, err)
  315. continue
  316. }
  317. if connected {
  318. s.model.Close(deviceID, protocol.ErrSwitchingConnections)
  319. }
  320. s.conns <- conn
  321. continue nextDevice
  322. }
  323. }
  324. nextDial, sleep = filterAndFindSleepDuration(nextDial, seen, now)
  325. if initialRampup < sleep {
  326. l.Debugln("initial rampup; sleep", initialRampup, "and update to", initialRampup*2)
  327. time.Sleep(initialRampup)
  328. initialRampup *= 2
  329. } else {
  330. l.Debugln("sleep until next dial", sleep)
  331. time.Sleep(sleep)
  332. }
  333. }
  334. }
  335. func (s *Service) shouldLimit(addr net.Addr) bool {
  336. if s.cfg.Options().LimitBandwidthInLan {
  337. return true
  338. }
  339. tcpaddr, ok := addr.(*net.TCPAddr)
  340. if !ok {
  341. return true
  342. }
  343. for _, lan := range s.lans {
  344. if lan.Contains(tcpaddr.IP) {
  345. return false
  346. }
  347. }
  348. return !tcpaddr.IP.IsLoopback()
  349. }
  350. func (s *Service) createListener(factory listenerFactory, uri *url.URL) bool {
  351. // must be called with listenerMut held
  352. l.Debugln("Starting listener", uri)
  353. listener := factory.New(uri, s.cfg, s.tlsCfg, s.conns, s.natService)
  354. listener.OnAddressesChanged(s.logListenAddressesChangedEvent)
  355. s.listeners[uri.String()] = listener
  356. s.listenerTokens[uri.String()] = s.Add(listener)
  357. return true
  358. }
  359. func (s *Service) logListenAddressesChangedEvent(l genericListener) {
  360. events.Default.Log(events.ListenAddressesChanged, map[string]interface{}{
  361. "address": l.URI(),
  362. "lan": l.LANAddresses(),
  363. "wan": l.WANAddresses(),
  364. })
  365. }
  366. func (s *Service) VerifyConfiguration(from, to config.Configuration) error {
  367. return nil
  368. }
  369. func (s *Service) CommitConfiguration(from, to config.Configuration) bool {
  370. // We require a restart if a device as been removed.
  371. restart := false
  372. newDevices := make(map[protocol.DeviceID]bool, len(to.Devices))
  373. for _, dev := range to.Devices {
  374. newDevices[dev.DeviceID] = true
  375. }
  376. for _, dev := range from.Devices {
  377. if !newDevices[dev.DeviceID] {
  378. restart = true
  379. }
  380. }
  381. s.listenersMut.Lock()
  382. seen := make(map[string]struct{})
  383. for _, addr := range config.Wrap("", to).ListenAddresses() {
  384. if _, ok := s.listeners[addr]; ok {
  385. seen[addr] = struct{}{}
  386. continue
  387. }
  388. uri, err := url.Parse(addr)
  389. if err != nil {
  390. l.Infof("Listener for %s: %v", addr, err)
  391. continue
  392. }
  393. factory, err := s.getListenerFactory(to, uri)
  394. if err == errDisabled {
  395. l.Debugln("Listener for", uri, "is disabled")
  396. continue
  397. }
  398. if err != nil {
  399. l.Infof("Listener for %v: %v", uri, err)
  400. continue
  401. }
  402. s.createListener(factory, uri)
  403. seen[addr] = struct{}{}
  404. }
  405. for addr, listener := range s.listeners {
  406. if _, ok := seen[addr]; !ok || !listener.Factory().Enabled(to) {
  407. l.Debugln("Stopping listener", addr)
  408. s.Remove(s.listenerTokens[addr])
  409. delete(s.listenerTokens, addr)
  410. delete(s.listeners, addr)
  411. }
  412. }
  413. s.listenersMut.Unlock()
  414. if to.Options.NATEnabled && s.natServiceToken == nil {
  415. l.Debugln("Starting NAT service")
  416. token := s.Add(s.natService)
  417. s.natServiceToken = &token
  418. } else if !to.Options.NATEnabled && s.natServiceToken != nil {
  419. l.Debugln("Stopping NAT service")
  420. s.Remove(*s.natServiceToken)
  421. s.natServiceToken = nil
  422. }
  423. return !restart
  424. }
  425. func (s *Service) AllAddresses() []string {
  426. s.listenersMut.RLock()
  427. var addrs []string
  428. for _, listener := range s.listeners {
  429. for _, lanAddr := range listener.LANAddresses() {
  430. addrs = append(addrs, lanAddr.String())
  431. }
  432. for _, wanAddr := range listener.WANAddresses() {
  433. addrs = append(addrs, wanAddr.String())
  434. }
  435. }
  436. s.listenersMut.RUnlock()
  437. return util.UniqueStrings(addrs)
  438. }
  439. func (s *Service) ExternalAddresses() []string {
  440. s.listenersMut.RLock()
  441. var addrs []string
  442. for _, listener := range s.listeners {
  443. for _, wanAddr := range listener.WANAddresses() {
  444. addrs = append(addrs, wanAddr.String())
  445. }
  446. }
  447. s.listenersMut.RUnlock()
  448. return util.UniqueStrings(addrs)
  449. }
  450. func (s *Service) Status() map[string]interface{} {
  451. s.listenersMut.RLock()
  452. result := make(map[string]interface{})
  453. for addr, listener := range s.listeners {
  454. status := make(map[string]interface{})
  455. err := listener.Error()
  456. if err != nil {
  457. status["error"] = err.Error()
  458. }
  459. status["lanAddresses"] = urlsToStrings(listener.LANAddresses())
  460. status["wanAddresses"] = urlsToStrings(listener.WANAddresses())
  461. result[addr] = status
  462. }
  463. s.listenersMut.RUnlock()
  464. return result
  465. }
  466. func (s *Service) getDialerFactory(cfg config.Configuration, uri *url.URL) (dialerFactory, error) {
  467. dialerFactory, ok := dialers[uri.Scheme]
  468. if !ok {
  469. return nil, fmt.Errorf("unknown address scheme %q", uri.Scheme)
  470. }
  471. if !dialerFactory.Enabled(cfg) {
  472. return nil, errDisabled
  473. }
  474. return dialerFactory, nil
  475. }
  476. func (s *Service) getListenerFactory(cfg config.Configuration, uri *url.URL) (listenerFactory, error) {
  477. listenerFactory, ok := listeners[uri.Scheme]
  478. if !ok {
  479. return nil, fmt.Errorf("unknown address scheme %q", uri.Scheme)
  480. }
  481. if !listenerFactory.Enabled(cfg) {
  482. return nil, errDisabled
  483. }
  484. return listenerFactory, nil
  485. }
  486. func filterAndFindSleepDuration(nextDial map[string]time.Time, seen []string, now time.Time) (map[string]time.Time, time.Duration) {
  487. newNextDial := make(map[string]time.Time)
  488. for _, addr := range seen {
  489. nextDialAt, ok := nextDial[addr]
  490. if ok {
  491. newNextDial[addr] = nextDialAt
  492. }
  493. }
  494. min := time.Minute
  495. for _, next := range newNextDial {
  496. cur := next.Sub(now)
  497. if cur < min {
  498. min = cur
  499. }
  500. }
  501. return newNextDial, min
  502. }
  503. func urlsToStrings(urls []*url.URL) []string {
  504. strings := make([]string, len(urls))
  505. for i, url := range urls {
  506. strings[i] = url.String()
  507. }
  508. return strings
  509. }
  510. var warningLimiters = make(map[protocol.DeviceID]*ratelimit.Bucket)
  511. var warningLimitersMut = sync.NewMutex()
  512. func warningFor(dev protocol.DeviceID, msg string) {
  513. warningLimitersMut.Lock()
  514. defer warningLimitersMut.Unlock()
  515. lim, ok := warningLimiters[dev]
  516. if !ok {
  517. lim = ratelimit.NewBucketWithRate(perDeviceWarningRate, 1)
  518. warningLimiters[dev] = lim
  519. }
  520. if lim.TakeAvailable(1) == 1 {
  521. l.Warnln(msg)
  522. }
  523. }
  524. func tlsTimedHandshake(tc *tls.Conn) error {
  525. tc.SetDeadline(time.Now().Add(tlsHandshakeTimeout))
  526. defer tc.SetDeadline(time.Time{})
  527. return tc.Handshake()
  528. }