structs.go 6.1 KB

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