structs.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright (C) 2016 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 connections
  7. import (
  8. "context"
  9. "crypto/tls"
  10. "fmt"
  11. "io"
  12. "net"
  13. "net/url"
  14. "time"
  15. "github.com/syncthing/syncthing/lib/config"
  16. "github.com/syncthing/syncthing/lib/nat"
  17. "github.com/syncthing/syncthing/lib/protocol"
  18. "github.com/syncthing/syncthing/lib/stats"
  19. "github.com/thejerf/suture/v4"
  20. )
  21. type tlsConn interface {
  22. io.ReadWriteCloser
  23. ConnectionState() tls.ConnectionState
  24. RemoteAddr() net.Addr
  25. SetDeadline(time.Time) error
  26. SetWriteDeadline(time.Time) error
  27. LocalAddr() net.Addr
  28. }
  29. // internalConn is the raw TLS connection plus some metadata on where it
  30. // came from (type, priority).
  31. type internalConn struct {
  32. tlsConn
  33. connType connType
  34. priority int
  35. establishedAt time.Time
  36. }
  37. type connType int
  38. const (
  39. connTypeRelayClient connType = iota
  40. connTypeRelayServer
  41. connTypeTCPClient
  42. connTypeTCPServer
  43. connTypeQUICClient
  44. connTypeQUICServer
  45. )
  46. func (t connType) String() string {
  47. switch t {
  48. case connTypeRelayClient:
  49. return "relay-client"
  50. case connTypeRelayServer:
  51. return "relay-server"
  52. case connTypeTCPClient:
  53. return "tcp-client"
  54. case connTypeTCPServer:
  55. return "tcp-server"
  56. case connTypeQUICClient:
  57. return "quic-client"
  58. case connTypeQUICServer:
  59. return "quic-server"
  60. default:
  61. return "unknown-type"
  62. }
  63. }
  64. func (t connType) Transport() string {
  65. switch t {
  66. case connTypeRelayClient, connTypeRelayServer:
  67. return "relay"
  68. case connTypeTCPClient, connTypeTCPServer:
  69. return "tcp"
  70. case connTypeQUICClient, connTypeQUICServer:
  71. return "quic"
  72. default:
  73. return "unknown"
  74. }
  75. }
  76. func newInternalConn(tc tlsConn, connType connType, priority int) internalConn {
  77. return internalConn{
  78. tlsConn: tc,
  79. connType: connType,
  80. priority: priority,
  81. establishedAt: time.Now(),
  82. }
  83. }
  84. func (c internalConn) Close() error {
  85. // *tls.Conn.Close() does more than it says on the tin. Specifically, it
  86. // sends a TLS alert message, which might block forever if the
  87. // connection is dead and we don't have a deadline set.
  88. _ = c.SetWriteDeadline(time.Now().Add(250 * time.Millisecond))
  89. return c.tlsConn.Close()
  90. }
  91. func (c internalConn) Type() string {
  92. return c.connType.String()
  93. }
  94. func (c internalConn) Priority() int {
  95. return c.priority
  96. }
  97. func (c internalConn) Crypto() string {
  98. cs := c.ConnectionState()
  99. return fmt.Sprintf("%s-%s", tlsVersionNames[cs.Version], tlsCipherSuiteNames[cs.CipherSuite])
  100. }
  101. func (c internalConn) Transport() string {
  102. transport := c.connType.Transport()
  103. host, _, err := net.SplitHostPort(c.LocalAddr().String())
  104. if err != nil {
  105. return transport
  106. }
  107. ip := net.ParseIP(host)
  108. if ip == nil {
  109. return transport
  110. }
  111. if ip.To4() != nil {
  112. return transport + "4"
  113. }
  114. return transport + "6"
  115. }
  116. func (c internalConn) EstablishedAt() time.Time {
  117. return c.establishedAt
  118. }
  119. func (c internalConn) String() string {
  120. return fmt.Sprintf("%s-%s/%s/%s", c.LocalAddr(), c.RemoteAddr(), c.Type(), c.Crypto())
  121. }
  122. type dialerFactory interface {
  123. New(config.OptionsConfiguration, *tls.Config) genericDialer
  124. Priority() int
  125. AlwaysWAN() bool
  126. Valid(config.Configuration) error
  127. String() string
  128. }
  129. type commonDialer struct {
  130. trafficClass int
  131. reconnectInterval time.Duration
  132. tlsCfg *tls.Config
  133. }
  134. func (d *commonDialer) RedialFrequency() time.Duration {
  135. return d.reconnectInterval
  136. }
  137. type genericDialer interface {
  138. Dial(context.Context, protocol.DeviceID, *url.URL) (internalConn, error)
  139. RedialFrequency() time.Duration
  140. }
  141. type listenerFactory interface {
  142. New(*url.URL, config.Wrapper, *tls.Config, chan internalConn, *nat.Service) genericListener
  143. Valid(config.Configuration) error
  144. }
  145. type ListenerAddresses struct {
  146. URI *url.URL
  147. WANAddresses []*url.URL
  148. LANAddresses []*url.URL
  149. }
  150. type genericListener interface {
  151. suture.Service
  152. URI() *url.URL
  153. // A given address can potentially be mutated by the listener.
  154. // For example we bind to tcp://0.0.0.0, but that for example might return
  155. // tcp://gateway1.ip and tcp://gateway2.ip as WAN addresses due to there
  156. // being multiple gateways, and us managing to get a UPnP mapping on both
  157. // and tcp://192.168.0.1 and tcp://10.0.0.1 due to there being multiple
  158. // network interfaces. (The later case for LAN addresses is made up just
  159. // to provide an example)
  160. WANAddresses() []*url.URL
  161. LANAddresses() []*url.URL
  162. Error() error
  163. OnAddressesChanged(func(ListenerAddresses))
  164. String() string
  165. Factory() listenerFactory
  166. NATType() string
  167. }
  168. type Model interface {
  169. protocol.Model
  170. AddConnection(conn protocol.Connection, hello protocol.Hello)
  171. NumConnections() int
  172. Connection(remoteID protocol.DeviceID) (protocol.Connection, bool)
  173. OnHello(protocol.DeviceID, net.Addr, protocol.Hello) error
  174. GetHello(protocol.DeviceID) protocol.HelloIntf
  175. DeviceStatistics() (map[protocol.DeviceID]stats.DeviceStatistics, error)
  176. }
  177. type onAddressesChangedNotifier struct {
  178. callbacks []func(ListenerAddresses)
  179. }
  180. func (o *onAddressesChangedNotifier) OnAddressesChanged(callback func(ListenerAddresses)) {
  181. o.callbacks = append(o.callbacks, callback)
  182. }
  183. func (o *onAddressesChangedNotifier) notifyAddressesChanged(l genericListener) {
  184. o.notifyAddresses(ListenerAddresses{
  185. URI: l.URI(),
  186. WANAddresses: l.WANAddresses(),
  187. LANAddresses: l.LANAddresses(),
  188. })
  189. }
  190. func (o *onAddressesChangedNotifier) clearAddresses(l genericListener) {
  191. o.notifyAddresses(ListenerAddresses{
  192. URI: l.URI(),
  193. })
  194. }
  195. func (o *onAddressesChangedNotifier) notifyAddresses(l ListenerAddresses) {
  196. for _, callback := range o.callbacks {
  197. callback(l)
  198. }
  199. }
  200. type dialTarget struct {
  201. addr string
  202. dialer genericDialer
  203. priority int
  204. uri *url.URL
  205. deviceID protocol.DeviceID
  206. }
  207. func (t dialTarget) Dial(ctx context.Context) (internalConn, error) {
  208. l.Debugln("dialing", t.deviceID, t.uri, "prio", t.priority)
  209. return t.dialer.Dial(ctx, t.deviceID, t.uri)
  210. }