dynamic.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
  2. package client
  3. import (
  4. "crypto/tls"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "sort"
  10. "time"
  11. "github.com/syncthing/syncthing/lib/osutil"
  12. "github.com/syncthing/syncthing/lib/relay/protocol"
  13. "github.com/syncthing/syncthing/lib/sync"
  14. )
  15. type dynamicClient struct {
  16. pooladdr *url.URL
  17. certs []tls.Certificate
  18. invitations chan protocol.SessionInvitation
  19. closeInvitationsOnFinish bool
  20. mut sync.RWMutex
  21. client RelayClient
  22. stop chan struct{}
  23. }
  24. func newDynamicClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation) RelayClient {
  25. closeInvitationsOnFinish := false
  26. if invitations == nil {
  27. closeInvitationsOnFinish = true
  28. invitations = make(chan protocol.SessionInvitation)
  29. }
  30. return &dynamicClient{
  31. pooladdr: uri,
  32. certs: certs,
  33. invitations: invitations,
  34. closeInvitationsOnFinish: closeInvitationsOnFinish,
  35. mut: sync.NewRWMutex(),
  36. }
  37. }
  38. func (c *dynamicClient) Serve() {
  39. c.mut.Lock()
  40. c.stop = make(chan struct{})
  41. c.mut.Unlock()
  42. uri := *c.pooladdr
  43. // Trim off the `dynamic+` prefix
  44. uri.Scheme = uri.Scheme[8:]
  45. l.Debugln(c, "looking up dynamic relays")
  46. data, err := http.Get(uri.String())
  47. if err != nil {
  48. l.Debugln(c, "failed to lookup dynamic relays", err)
  49. return
  50. }
  51. var ann dynamicAnnouncement
  52. err = json.NewDecoder(data.Body).Decode(&ann)
  53. data.Body.Close()
  54. if err != nil {
  55. l.Debugln(c, "failed to lookup dynamic relays", err)
  56. return
  57. }
  58. defer c.cleanup()
  59. var addrs []string
  60. for _, relayAnn := range ann.Relays {
  61. ruri, err := url.Parse(relayAnn.URL)
  62. if err != nil {
  63. l.Debugln(c, "failed to parse dynamic relay address", relayAnn.URL, err)
  64. continue
  65. }
  66. l.Debugln(c, "found", ruri)
  67. addrs = append(addrs, ruri.String())
  68. }
  69. for _, addr := range relayAddressesSortedByLatency(addrs) {
  70. select {
  71. case <-c.stop:
  72. l.Debugln(c, "stopping")
  73. return
  74. default:
  75. ruri, err := url.Parse(addr)
  76. if err != nil {
  77. l.Debugln(c, "skipping relay", addr, err)
  78. continue
  79. }
  80. client, err := NewClient(ruri, c.certs, c.invitations)
  81. if err != nil {
  82. continue
  83. }
  84. c.mut.Lock()
  85. c.client = client
  86. c.mut.Unlock()
  87. c.client.Serve()
  88. c.mut.Lock()
  89. c.client = nil
  90. c.mut.Unlock()
  91. }
  92. }
  93. l.Debugln(c, "could not find a connectable relay")
  94. }
  95. func (c *dynamicClient) Stop() {
  96. c.mut.RLock()
  97. defer c.mut.RUnlock()
  98. close(c.stop)
  99. if c.client == nil {
  100. return
  101. }
  102. c.client.Stop()
  103. }
  104. func (c *dynamicClient) StatusOK() bool {
  105. c.mut.RLock()
  106. defer c.mut.RUnlock()
  107. if c.client == nil {
  108. return false
  109. }
  110. return c.client.StatusOK()
  111. }
  112. func (c *dynamicClient) Latency() time.Duration {
  113. c.mut.RLock()
  114. defer c.mut.RUnlock()
  115. if c.client == nil {
  116. return time.Hour
  117. }
  118. return c.client.Latency()
  119. }
  120. func (c *dynamicClient) String() string {
  121. return fmt.Sprintf("DynamicClient:%p:%s@%s", c, c.URI(), c.pooladdr)
  122. }
  123. func (c *dynamicClient) URI() *url.URL {
  124. c.mut.RLock()
  125. defer c.mut.RUnlock()
  126. if c.client == nil {
  127. return c.pooladdr
  128. }
  129. return c.client.URI()
  130. }
  131. func (c *dynamicClient) Invitations() chan protocol.SessionInvitation {
  132. c.mut.RLock()
  133. inv := c.invitations
  134. c.mut.RUnlock()
  135. return inv
  136. }
  137. func (c *dynamicClient) cleanup() {
  138. c.mut.Lock()
  139. if c.closeInvitationsOnFinish {
  140. close(c.invitations)
  141. c.invitations = make(chan protocol.SessionInvitation)
  142. }
  143. c.mut.Unlock()
  144. }
  145. // This is the announcement recieved from the relay server;
  146. // {"relays": [{"url": "relay://10.20.30.40:5060"}, ...]}
  147. type dynamicAnnouncement struct {
  148. Relays []struct {
  149. URL string
  150. }
  151. }
  152. // relayAddressesSortedByLatency adds local latency to the relay, and sorts them
  153. // by sum latency, and returns the addresses.
  154. func relayAddressesSortedByLatency(input []string) []string {
  155. relays := make(relayList, len(input))
  156. for i, relay := range input {
  157. if latency, err := osutil.GetLatencyForURL(relay); err == nil {
  158. relays[i] = relayWithLatency{relay, int(latency / time.Millisecond)}
  159. } else {
  160. relays[i] = relayWithLatency{relay, int(time.Hour / time.Millisecond)}
  161. }
  162. }
  163. sort.Sort(relays)
  164. addresses := make([]string, len(relays))
  165. for i, relay := range relays {
  166. addresses[i] = relay.relay
  167. }
  168. return addresses
  169. }
  170. type relayWithLatency struct {
  171. relay string
  172. latency int
  173. }
  174. type relayList []relayWithLatency
  175. func (l relayList) Len() int {
  176. return len(l)
  177. }
  178. func (l relayList) Less(a, b int) bool {
  179. return l[a].latency < l[b].latency
  180. }
  181. func (l relayList) Swap(a, b int) {
  182. l[a], l[b] = l[b], l[a]
  183. }