local.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright (C) 2014 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 go run ../../proto/scripts/protofmt.go local.proto
  7. //go:generate protoc -I ../../ -I . --gogofast_out=. local.proto
  8. package discover
  9. import (
  10. "context"
  11. "encoding/binary"
  12. "encoding/hex"
  13. "fmt"
  14. "io"
  15. "net"
  16. "net/url"
  17. "strconv"
  18. "time"
  19. "github.com/syncthing/syncthing/lib/beacon"
  20. "github.com/syncthing/syncthing/lib/events"
  21. "github.com/syncthing/syncthing/lib/protocol"
  22. "github.com/syncthing/syncthing/lib/rand"
  23. "github.com/syncthing/syncthing/lib/util"
  24. "github.com/thejerf/suture/v4"
  25. )
  26. type localClient struct {
  27. *suture.Supervisor
  28. myID protocol.DeviceID
  29. addrList AddressLister
  30. name string
  31. evLogger events.Logger
  32. beacon beacon.Interface
  33. localBcastStart time.Time
  34. localBcastTick <-chan time.Time
  35. forcedBcastTick chan time.Time
  36. *cache
  37. }
  38. const (
  39. BroadcastInterval = 30 * time.Second
  40. CacheLifeTime = 3 * BroadcastInterval
  41. Magic = uint32(0x2EA7D90B) // same as in BEP
  42. v13Magic = uint32(0x7D79BC40) // previous version
  43. )
  44. func NewLocal(id protocol.DeviceID, addr string, addrList AddressLister, evLogger events.Logger) (FinderService, error) {
  45. c := &localClient{
  46. Supervisor: suture.New("local", util.Spec()),
  47. myID: id,
  48. addrList: addrList,
  49. evLogger: evLogger,
  50. localBcastTick: time.NewTicker(BroadcastInterval).C,
  51. forcedBcastTick: make(chan time.Time),
  52. localBcastStart: time.Now(),
  53. cache: newCache(),
  54. }
  55. host, port, err := net.SplitHostPort(addr)
  56. if err != nil {
  57. return nil, err
  58. }
  59. if len(host) == 0 {
  60. // A broadcast client
  61. c.name = "IPv4 local"
  62. bcPort, err := strconv.Atoi(port)
  63. if err != nil {
  64. return nil, err
  65. }
  66. c.beacon = beacon.NewBroadcast(bcPort)
  67. } else {
  68. // A multicast client
  69. c.name = "IPv6 local"
  70. c.beacon = beacon.NewMulticast(addr)
  71. }
  72. c.Add(c.beacon)
  73. c.Add(util.AsService(c.recvAnnouncements, fmt.Sprintf("%s/recv", c)))
  74. c.Add(util.AsService(c.sendLocalAnnouncements, fmt.Sprintf("%s/sendLocal", c)))
  75. return c, nil
  76. }
  77. // Lookup returns a list of addresses the device is available at.
  78. func (c *localClient) Lookup(_ context.Context, device protocol.DeviceID) (addresses []string, err error) {
  79. if cache, ok := c.Get(device); ok {
  80. if time.Since(cache.when) < CacheLifeTime {
  81. addresses = cache.Addresses
  82. }
  83. }
  84. return
  85. }
  86. func (c *localClient) String() string {
  87. return c.name
  88. }
  89. func (c *localClient) Error() error {
  90. return c.beacon.Error()
  91. }
  92. // announcementPkt appends the local discovery packet to send to msg. Returns
  93. // true if the packet should be sent, false if there is nothing useful to
  94. // send.
  95. func (c *localClient) announcementPkt(instanceID int64, msg []byte) ([]byte, bool) {
  96. addrs := c.addrList.AllAddresses()
  97. if len(addrs) == 0 {
  98. // Nothing to announce
  99. return msg, false
  100. }
  101. if cap(msg) >= 4 {
  102. msg = msg[:4]
  103. } else {
  104. msg = make([]byte, 4)
  105. }
  106. binary.BigEndian.PutUint32(msg, Magic)
  107. pkt := Announce{
  108. ID: c.myID,
  109. Addresses: addrs,
  110. InstanceID: instanceID,
  111. }
  112. bs, _ := pkt.Marshal()
  113. msg = append(msg, bs...)
  114. return msg, true
  115. }
  116. func (c *localClient) sendLocalAnnouncements(ctx context.Context) error {
  117. var msg []byte
  118. var ok bool
  119. instanceID := rand.Int63()
  120. for {
  121. if msg, ok = c.announcementPkt(instanceID, msg[:0]); ok {
  122. c.beacon.Send(msg)
  123. }
  124. select {
  125. case <-c.localBcastTick:
  126. case <-c.forcedBcastTick:
  127. case <-ctx.Done():
  128. return ctx.Err()
  129. }
  130. }
  131. }
  132. func (c *localClient) recvAnnouncements(ctx context.Context) error {
  133. b := c.beacon
  134. warnedAbout := make(map[string]bool)
  135. for {
  136. select {
  137. case <-ctx.Done():
  138. return ctx.Err()
  139. default:
  140. }
  141. buf, addr := b.Recv()
  142. if addr == nil {
  143. continue
  144. }
  145. if len(buf) < 4 {
  146. l.Debugf("discover: short packet from %s", addr.String())
  147. continue
  148. }
  149. magic := binary.BigEndian.Uint32(buf)
  150. switch magic {
  151. case Magic:
  152. // All good
  153. case v13Magic:
  154. // Old version
  155. if !warnedAbout[addr.String()] {
  156. l.Warnf("Incompatible (v0.13) local discovery packet from %v - upgrade that device to connect", addr)
  157. warnedAbout[addr.String()] = true
  158. }
  159. continue
  160. default:
  161. l.Debugf("discover: Incorrect magic %x from %s", magic, addr)
  162. continue
  163. }
  164. var pkt Announce
  165. err := pkt.Unmarshal(buf[4:])
  166. if err != nil && err != io.EOF {
  167. l.Debugf("discover: Failed to unmarshal local announcement from %s:\n%s", addr, hex.Dump(buf))
  168. continue
  169. }
  170. l.Debugf("discover: Received local announcement from %s for %s", addr, pkt.ID)
  171. var newDevice bool
  172. if pkt.ID != c.myID {
  173. newDevice = c.registerDevice(addr, pkt)
  174. }
  175. if newDevice {
  176. // Force a transmit to announce ourselves, if we are ready to do
  177. // so right away.
  178. select {
  179. case c.forcedBcastTick <- time.Now():
  180. default:
  181. }
  182. }
  183. }
  184. }
  185. func (c *localClient) registerDevice(src net.Addr, device Announce) bool {
  186. // Remember whether we already had a valid cache entry for this device.
  187. // If the instance ID has changed the remote device has restarted since
  188. // we last heard from it, so we should treat it as a new device.
  189. ce, existsAlready := c.Get(device.ID)
  190. isNewDevice := !existsAlready || time.Since(ce.when) > CacheLifeTime || ce.instanceID != device.InstanceID
  191. // Any empty or unspecified addresses should be set to the source address
  192. // of the announcement. We also skip any addresses we can't parse.
  193. l.Debugln("discover: Registering addresses for", device.ID)
  194. var validAddresses []string
  195. for _, addr := range device.Addresses {
  196. u, err := url.Parse(addr)
  197. if err != nil {
  198. continue
  199. }
  200. tcpAddr, err := net.ResolveTCPAddr("tcp", u.Host)
  201. if err != nil {
  202. continue
  203. }
  204. if len(tcpAddr.IP) == 0 || tcpAddr.IP.IsUnspecified() {
  205. srcAddr, err := net.ResolveTCPAddr("tcp", src.String())
  206. if err != nil {
  207. continue
  208. }
  209. // Do not use IPv6 source address if requested scheme is tcp4
  210. if u.Scheme == "tcp4" && srcAddr.IP.To4() == nil {
  211. continue
  212. }
  213. // Do not use IPv4 source address if requested scheme is tcp6
  214. if u.Scheme == "tcp6" && srcAddr.IP.To4() != nil {
  215. continue
  216. }
  217. host, _, err := net.SplitHostPort(src.String())
  218. if err != nil {
  219. continue
  220. }
  221. u.Host = net.JoinHostPort(host, strconv.Itoa(tcpAddr.Port))
  222. l.Debugf("discover: Reconstructed URL is %#v", u)
  223. validAddresses = append(validAddresses, u.String())
  224. l.Debugf("discover: Replaced address %v in %s to get %s", tcpAddr.IP, addr, u.String())
  225. } else {
  226. validAddresses = append(validAddresses, addr)
  227. l.Debugf("discover: Accepted address %s verbatim", addr)
  228. }
  229. }
  230. c.Set(device.ID, CacheEntry{
  231. Addresses: validAddresses,
  232. when: time.Now(),
  233. found: true,
  234. instanceID: device.InstanceID,
  235. })
  236. if isNewDevice {
  237. c.evLogger.Log(events.DeviceDiscovered, map[string]interface{}{
  238. "device": device.ID.String(),
  239. "addrs": validAddresses,
  240. })
  241. }
  242. return isNewDevice
  243. }