1
0

service.go 18 KB

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