static.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
  2. package client
  3. import (
  4. "crypto/tls"
  5. "fmt"
  6. "net"
  7. "net/url"
  8. "time"
  9. "github.com/syncthing/syncthing/lib/dialer"
  10. syncthingprotocol "github.com/syncthing/syncthing/lib/protocol"
  11. "github.com/syncthing/syncthing/lib/relay/protocol"
  12. "github.com/syncthing/syncthing/lib/sync"
  13. )
  14. type staticClient struct {
  15. uri *url.URL
  16. invitations chan protocol.SessionInvitation
  17. closeInvitationsOnFinish bool
  18. config *tls.Config
  19. messageTimeout time.Duration
  20. connectTimeout time.Duration
  21. stop chan struct{}
  22. stopped chan struct{}
  23. conn *tls.Conn
  24. mut sync.RWMutex
  25. err error
  26. connected bool
  27. latency time.Duration
  28. }
  29. func newStaticClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) RelayClient {
  30. closeInvitationsOnFinish := false
  31. if invitations == nil {
  32. closeInvitationsOnFinish = true
  33. invitations = make(chan protocol.SessionInvitation)
  34. }
  35. return &staticClient{
  36. uri: uri,
  37. invitations: invitations,
  38. closeInvitationsOnFinish: closeInvitationsOnFinish,
  39. config: configForCerts(certs),
  40. messageTimeout: time.Minute * 2,
  41. connectTimeout: timeout,
  42. stop: make(chan struct{}),
  43. stopped: make(chan struct{}),
  44. mut: sync.NewRWMutex(),
  45. }
  46. }
  47. func (c *staticClient) Serve() {
  48. defer c.cleanup()
  49. c.stop = make(chan struct{})
  50. c.stopped = make(chan struct{})
  51. defer close(c.stopped)
  52. if err := c.connect(); err != nil {
  53. l.Infof("Could not connect to relay %s: %s", c.uri, err)
  54. c.setError(err)
  55. return
  56. }
  57. l.Debugln(c, "connected", c.conn.RemoteAddr())
  58. if err := c.join(); err != nil {
  59. c.conn.Close()
  60. l.Infof("Could not join relay %s: %s", c.uri, err)
  61. c.setError(err)
  62. return
  63. }
  64. if err := c.conn.SetDeadline(time.Time{}); err != nil {
  65. c.conn.Close()
  66. l.Infoln("Relay set deadline:", err)
  67. c.setError(err)
  68. return
  69. }
  70. l.Infof("Joined relay %s://%s", c.uri.Scheme, c.uri.Host)
  71. defer l.Infof("Disconnected from relay %s://%s", c.uri.Scheme, c.uri.Host)
  72. c.mut.Lock()
  73. c.connected = true
  74. c.mut.Unlock()
  75. messages := make(chan interface{})
  76. errors := make(chan error, 1)
  77. go messageReader(c.conn, messages, errors)
  78. timeout := time.NewTimer(c.messageTimeout)
  79. for {
  80. select {
  81. case message := <-messages:
  82. timeout.Reset(c.messageTimeout)
  83. l.Debugf("%s received message %T", c, message)
  84. switch msg := message.(type) {
  85. case protocol.Ping:
  86. if err := protocol.WriteMessage(c.conn, protocol.Pong{}); err != nil {
  87. l.Infoln("Relay write:", err)
  88. c.setError(err)
  89. c.disconnect()
  90. } else {
  91. l.Debugln(c, "sent pong")
  92. }
  93. case protocol.SessionInvitation:
  94. ip := net.IP(msg.Address)
  95. if len(ip) == 0 || ip.IsUnspecified() {
  96. msg.Address = remoteIPBytes(c.conn)
  97. }
  98. c.invitations <- msg
  99. case protocol.RelayFull:
  100. l.Infof("Disconnected from relay %s due to it becoming full.", c.uri)
  101. c.setError(fmt.Errorf("Relay full"))
  102. c.disconnect()
  103. default:
  104. l.Infoln("Relay: protocol error: unexpected message %v", msg)
  105. c.setError(fmt.Errorf("protocol error: unexpected message %v", msg))
  106. c.disconnect()
  107. }
  108. case <-c.stop:
  109. l.Debugln(c, "stopping")
  110. c.setError(nil)
  111. c.disconnect()
  112. // We always exit via this branch of the select, to make sure the
  113. // the reader routine exits.
  114. case err := <-errors:
  115. close(errors)
  116. close(messages)
  117. c.mut.Lock()
  118. if c.connected {
  119. c.conn.Close()
  120. c.connected = false
  121. l.Infof("Disconnecting from relay %s due to error: %s", c.uri, err)
  122. c.err = err
  123. } else {
  124. c.err = nil
  125. }
  126. c.mut.Unlock()
  127. return
  128. case <-timeout.C:
  129. l.Debugln(c, "timed out")
  130. c.disconnect()
  131. c.setError(fmt.Errorf("timed out"))
  132. }
  133. }
  134. }
  135. func (c *staticClient) Stop() {
  136. if c.stop == nil {
  137. return
  138. }
  139. close(c.stop)
  140. <-c.stopped
  141. }
  142. func (c *staticClient) StatusOK() bool {
  143. c.mut.RLock()
  144. con := c.connected
  145. c.mut.RUnlock()
  146. return con
  147. }
  148. func (c *staticClient) Latency() time.Duration {
  149. c.mut.RLock()
  150. lat := c.latency
  151. c.mut.RUnlock()
  152. return lat
  153. }
  154. func (c *staticClient) String() string {
  155. return fmt.Sprintf("StaticClient:%p@%s", c, c.URI())
  156. }
  157. func (c *staticClient) URI() *url.URL {
  158. return c.uri
  159. }
  160. func (c *staticClient) Invitations() chan protocol.SessionInvitation {
  161. c.mut.RLock()
  162. inv := c.invitations
  163. c.mut.RUnlock()
  164. return inv
  165. }
  166. func (c *staticClient) cleanup() {
  167. c.mut.Lock()
  168. if c.closeInvitationsOnFinish {
  169. close(c.invitations)
  170. c.invitations = make(chan protocol.SessionInvitation)
  171. }
  172. c.mut.Unlock()
  173. }
  174. func (c *staticClient) connect() error {
  175. if c.uri.Scheme != "relay" {
  176. return fmt.Errorf("Unsupported relay schema: %v", c.uri.Scheme)
  177. }
  178. t0 := time.Now()
  179. tcpConn, err := dialer.DialTimeout("tcp", c.uri.Host, c.connectTimeout)
  180. if err != nil {
  181. return err
  182. }
  183. c.mut.Lock()
  184. c.latency = time.Since(t0)
  185. c.mut.Unlock()
  186. conn := tls.Client(tcpConn, c.config)
  187. if err := conn.SetDeadline(time.Now().Add(c.connectTimeout)); err != nil {
  188. conn.Close()
  189. return err
  190. }
  191. if err := performHandshakeAndValidation(conn, c.uri); err != nil {
  192. conn.Close()
  193. return err
  194. }
  195. c.conn = conn
  196. return nil
  197. }
  198. func (c *staticClient) disconnect() {
  199. l.Debugln(c, "disconnecting")
  200. c.mut.Lock()
  201. c.connected = false
  202. c.mut.Unlock()
  203. c.conn.Close()
  204. }
  205. func (c *staticClient) setError(err error) {
  206. c.mut.Lock()
  207. c.err = err
  208. c.mut.Unlock()
  209. }
  210. func (c *staticClient) Error() error {
  211. c.mut.RLock()
  212. err := c.err
  213. c.mut.RUnlock()
  214. return err
  215. }
  216. func (c *staticClient) join() error {
  217. if err := protocol.WriteMessage(c.conn, protocol.JoinRelayRequest{}); err != nil {
  218. return err
  219. }
  220. message, err := protocol.ReadMessage(c.conn)
  221. if err != nil {
  222. return err
  223. }
  224. switch msg := message.(type) {
  225. case protocol.Response:
  226. if msg.Code != 0 {
  227. return fmt.Errorf("Incorrect response code %d: %s", msg.Code, msg.Message)
  228. }
  229. case protocol.RelayFull:
  230. return fmt.Errorf("relay full")
  231. default:
  232. return fmt.Errorf("protocol error: expecting response got %v", msg)
  233. }
  234. return nil
  235. }
  236. func performHandshakeAndValidation(conn *tls.Conn, uri *url.URL) error {
  237. if err := conn.Handshake(); err != nil {
  238. return err
  239. }
  240. cs := conn.ConnectionState()
  241. if !cs.NegotiatedProtocolIsMutual || cs.NegotiatedProtocol != protocol.ProtocolName {
  242. return fmt.Errorf("protocol negotiation error")
  243. }
  244. q := uri.Query()
  245. relayIDs := q.Get("id")
  246. if relayIDs != "" {
  247. relayID, err := syncthingprotocol.DeviceIDFromString(relayIDs)
  248. if err != nil {
  249. return fmt.Errorf("relay address contains invalid verification id: %s", err)
  250. }
  251. certs := cs.PeerCertificates
  252. if cl := len(certs); cl != 1 {
  253. return fmt.Errorf("unexpected certificate count: %d", cl)
  254. }
  255. remoteID := syncthingprotocol.NewDeviceID(certs[0].Raw)
  256. if remoteID != relayID {
  257. return fmt.Errorf("relay id does not match. Expected %v got %v", relayID, remoteID)
  258. }
  259. }
  260. return nil
  261. }
  262. func messageReader(conn net.Conn, messages chan<- interface{}, errors chan<- error) {
  263. for {
  264. msg, err := protocol.ReadMessage(conn)
  265. if err != nil {
  266. errors <- err
  267. return
  268. }
  269. messages <- msg
  270. }
  271. }