global.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. package discover
  7. import (
  8. "bytes"
  9. "crypto/tls"
  10. "encoding/json"
  11. "errors"
  12. "io"
  13. "io/ioutil"
  14. "net/http"
  15. "net/url"
  16. "strconv"
  17. stdsync "sync"
  18. "time"
  19. "github.com/thejerf/suture"
  20. "github.com/syncthing/syncthing/lib/dialer"
  21. "github.com/syncthing/syncthing/lib/events"
  22. "github.com/syncthing/syncthing/lib/protocol"
  23. "github.com/syncthing/syncthing/lib/util"
  24. )
  25. type globalClient struct {
  26. suture.Service
  27. server string
  28. addrList AddressLister
  29. announceClient httpClient
  30. queryClient httpClient
  31. noAnnounce bool
  32. noLookup bool
  33. evLogger events.Logger
  34. errorHolder
  35. }
  36. type httpClient interface {
  37. Get(url string) (*http.Response, error)
  38. Post(url, ctype string, data io.Reader) (*http.Response, error)
  39. }
  40. const (
  41. defaultReannounceInterval = 30 * time.Minute
  42. announceErrorRetryInterval = 5 * time.Minute
  43. requestTimeout = 5 * time.Second
  44. )
  45. type announcement struct {
  46. Addresses []string `json:"addresses"`
  47. }
  48. type serverOptions struct {
  49. insecure bool // don't check certificate
  50. noAnnounce bool // don't announce
  51. noLookup bool // don't use for lookups
  52. id string // expected server device ID
  53. }
  54. // A lookupError is any other error but with a cache validity time attached.
  55. type lookupError struct {
  56. error
  57. cacheFor time.Duration
  58. }
  59. func (e lookupError) CacheFor() time.Duration {
  60. return e.cacheFor
  61. }
  62. func NewGlobal(server string, cert tls.Certificate, addrList AddressLister, evLogger events.Logger) (FinderService, error) {
  63. server, opts, err := parseOptions(server)
  64. if err != nil {
  65. return nil, err
  66. }
  67. var devID protocol.DeviceID
  68. if opts.id != "" {
  69. devID, err = protocol.DeviceIDFromString(opts.id)
  70. if err != nil {
  71. return nil, err
  72. }
  73. }
  74. // The http.Client used for announcements. It needs to have our
  75. // certificate to prove our identity, and may or may not verify the server
  76. // certificate depending on the insecure setting.
  77. var announceClient httpClient = &http.Client{
  78. Timeout: requestTimeout,
  79. Transport: &http.Transport{
  80. Dial: dialer.Dial,
  81. Proxy: http.ProxyFromEnvironment,
  82. TLSClientConfig: &tls.Config{
  83. InsecureSkipVerify: opts.insecure,
  84. Certificates: []tls.Certificate{cert},
  85. },
  86. },
  87. }
  88. if opts.id != "" {
  89. announceClient = newIDCheckingHTTPClient(announceClient, devID)
  90. }
  91. // The http.Client used for queries. We don't need to present our
  92. // certificate here, so lets not include it. May be insecure if requested.
  93. var queryClient httpClient = &http.Client{
  94. Timeout: requestTimeout,
  95. Transport: &http.Transport{
  96. Dial: dialer.Dial,
  97. Proxy: http.ProxyFromEnvironment,
  98. TLSClientConfig: &tls.Config{
  99. InsecureSkipVerify: opts.insecure,
  100. },
  101. },
  102. }
  103. if opts.id != "" {
  104. queryClient = newIDCheckingHTTPClient(queryClient, devID)
  105. }
  106. cl := &globalClient{
  107. server: server,
  108. addrList: addrList,
  109. announceClient: announceClient,
  110. queryClient: queryClient,
  111. noAnnounce: opts.noAnnounce,
  112. noLookup: opts.noLookup,
  113. evLogger: evLogger,
  114. }
  115. cl.Service = util.AsService(cl.serve)
  116. if !opts.noAnnounce {
  117. // If we are supposed to annonce, it's an error until we've done so.
  118. cl.setError(errors.New("not announced"))
  119. }
  120. return cl, nil
  121. }
  122. // Lookup returns the list of addresses where the given device is available
  123. func (c *globalClient) Lookup(device protocol.DeviceID) (addresses []string, err error) {
  124. if c.noLookup {
  125. return nil, lookupError{
  126. error: errors.New("lookups not supported"),
  127. cacheFor: time.Hour,
  128. }
  129. }
  130. qURL, err := url.Parse(c.server)
  131. if err != nil {
  132. return nil, err
  133. }
  134. q := qURL.Query()
  135. q.Set("device", device.String())
  136. qURL.RawQuery = q.Encode()
  137. resp, err := c.queryClient.Get(qURL.String())
  138. if err != nil {
  139. l.Debugln("globalClient.Lookup", qURL, err)
  140. return nil, err
  141. }
  142. if resp.StatusCode != 200 {
  143. resp.Body.Close()
  144. l.Debugln("globalClient.Lookup", qURL, resp.Status)
  145. err := errors.New(resp.Status)
  146. if secs, atoiErr := strconv.Atoi(resp.Header.Get("Retry-After")); atoiErr == nil && secs > 0 {
  147. err = lookupError{
  148. error: err,
  149. cacheFor: time.Duration(secs) * time.Second,
  150. }
  151. }
  152. return nil, err
  153. }
  154. bs, err := ioutil.ReadAll(resp.Body)
  155. if err != nil {
  156. return nil, err
  157. }
  158. resp.Body.Close()
  159. var ann announcement
  160. err = json.Unmarshal(bs, &ann)
  161. return ann.Addresses, err
  162. }
  163. func (c *globalClient) String() string {
  164. return "global@" + c.server
  165. }
  166. func (c *globalClient) serve(stop chan struct{}) {
  167. if c.noAnnounce {
  168. // We're configured to not do announcements, only lookups. To maintain
  169. // the same interface, we just pause here if Serve() is run.
  170. <-stop
  171. return
  172. }
  173. timer := time.NewTimer(0)
  174. defer timer.Stop()
  175. eventSub := c.evLogger.Subscribe(events.ListenAddressesChanged)
  176. defer eventSub.Unsubscribe()
  177. for {
  178. select {
  179. case <-eventSub.C():
  180. // Defer announcement by 2 seconds, essentially debouncing
  181. // if we have a stream of events incoming in quick succession.
  182. timer.Reset(2 * time.Second)
  183. case <-timer.C:
  184. c.sendAnnouncement(timer)
  185. case <-stop:
  186. return
  187. }
  188. }
  189. }
  190. func (c *globalClient) sendAnnouncement(timer *time.Timer) {
  191. var ann announcement
  192. if c.addrList != nil {
  193. ann.Addresses = c.addrList.ExternalAddresses()
  194. }
  195. if len(ann.Addresses) == 0 {
  196. // There are legitimate cases for not having anything to announce,
  197. // yet still using global discovery for lookups. Do not error out
  198. // here.
  199. c.setError(nil)
  200. timer.Reset(announceErrorRetryInterval)
  201. return
  202. }
  203. // The marshal doesn't fail, I promise.
  204. postData, _ := json.Marshal(ann)
  205. l.Debugf("Announcement: %s", postData)
  206. resp, err := c.announceClient.Post(c.server, "application/json", bytes.NewReader(postData))
  207. if err != nil {
  208. l.Debugln("announce POST:", err)
  209. c.setError(err)
  210. timer.Reset(announceErrorRetryInterval)
  211. return
  212. }
  213. l.Debugln("announce POST:", resp.Status)
  214. resp.Body.Close()
  215. if resp.StatusCode < 200 || resp.StatusCode > 299 {
  216. l.Debugln("announce POST:", resp.Status)
  217. c.setError(errors.New(resp.Status))
  218. if h := resp.Header.Get("Retry-After"); h != "" {
  219. // The server has a recommendation on when we should
  220. // retry. Follow it.
  221. if secs, err := strconv.Atoi(h); err == nil && secs > 0 {
  222. l.Debugln("announce Retry-After:", secs, err)
  223. timer.Reset(time.Duration(secs) * time.Second)
  224. return
  225. }
  226. }
  227. timer.Reset(announceErrorRetryInterval)
  228. return
  229. }
  230. c.setError(nil)
  231. if h := resp.Header.Get("Reannounce-After"); h != "" {
  232. // The server has a recommendation on when we should
  233. // reannounce. Follow it.
  234. if secs, err := strconv.Atoi(h); err == nil && secs > 0 {
  235. l.Debugln("announce Reannounce-After:", secs, err)
  236. timer.Reset(time.Duration(secs) * time.Second)
  237. return
  238. }
  239. }
  240. timer.Reset(defaultReannounceInterval)
  241. }
  242. func (c *globalClient) Cache() map[protocol.DeviceID]CacheEntry {
  243. // The globalClient doesn't do caching
  244. return nil
  245. }
  246. // parseOptions parses and strips away any ?query=val options, setting the
  247. // corresponding field in the serverOptions struct. Unknown query options are
  248. // ignored and removed.
  249. func parseOptions(dsn string) (server string, opts serverOptions, err error) {
  250. p, err := url.Parse(dsn)
  251. if err != nil {
  252. return "", serverOptions{}, err
  253. }
  254. // Grab known options from the query string
  255. q := p.Query()
  256. opts.id = q.Get("id")
  257. opts.insecure = opts.id != "" || queryBool(q, "insecure")
  258. opts.noAnnounce = queryBool(q, "noannounce")
  259. opts.noLookup = queryBool(q, "nolookup")
  260. // Check for disallowed combinations
  261. if p.Scheme == "http" {
  262. if !opts.insecure {
  263. return "", serverOptions{}, errors.New("http without insecure not supported")
  264. }
  265. if !opts.noAnnounce {
  266. return "", serverOptions{}, errors.New("http without noannounce not supported")
  267. }
  268. } else if p.Scheme != "https" {
  269. return "", serverOptions{}, errors.New("unsupported scheme " + p.Scheme)
  270. }
  271. // Remove the query string
  272. p.RawQuery = ""
  273. server = p.String()
  274. return
  275. }
  276. // queryBool returns the query parameter parsed as a boolean. An empty value
  277. // ("?foo") is considered true, as is any value string except false
  278. // ("?foo=false").
  279. func queryBool(q url.Values, key string) bool {
  280. if _, ok := q[key]; !ok {
  281. return false
  282. }
  283. return q.Get(key) != "false"
  284. }
  285. type idCheckingHTTPClient struct {
  286. httpClient
  287. id protocol.DeviceID
  288. }
  289. func newIDCheckingHTTPClient(client httpClient, id protocol.DeviceID) *idCheckingHTTPClient {
  290. return &idCheckingHTTPClient{
  291. httpClient: client,
  292. id: id,
  293. }
  294. }
  295. func (c *idCheckingHTTPClient) check(resp *http.Response) error {
  296. if resp.TLS == nil {
  297. return errors.New("security: not TLS")
  298. }
  299. if len(resp.TLS.PeerCertificates) == 0 {
  300. return errors.New("security: no certificates")
  301. }
  302. id := protocol.NewDeviceID(resp.TLS.PeerCertificates[0].Raw)
  303. if !id.Equals(c.id) {
  304. return errors.New("security: incorrect device id")
  305. }
  306. return nil
  307. }
  308. func (c *idCheckingHTTPClient) Get(url string) (*http.Response, error) {
  309. resp, err := c.httpClient.Get(url)
  310. if err != nil {
  311. return nil, err
  312. }
  313. if err := c.check(resp); err != nil {
  314. return nil, err
  315. }
  316. return resp, nil
  317. }
  318. func (c *idCheckingHTTPClient) Post(url, ctype string, data io.Reader) (*http.Response, error) {
  319. resp, err := c.httpClient.Post(url, ctype, data)
  320. if err != nil {
  321. return nil, err
  322. }
  323. if err := c.check(resp); err != nil {
  324. return nil, err
  325. }
  326. return resp, nil
  327. }
  328. type errorHolder struct {
  329. err error
  330. mut stdsync.Mutex // uses stdlib sync as I want this to be trivially embeddable, and there is no risk of blocking
  331. }
  332. func (e *errorHolder) setError(err error) {
  333. e.mut.Lock()
  334. e.err = err
  335. e.mut.Unlock()
  336. }
  337. func (e *errorHolder) Error() error {
  338. e.mut.Lock()
  339. err := e.err
  340. e.mut.Unlock()
  341. return err
  342. }