1
0

structs.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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, isLocal bool, priority int) internalConn {
  80. return internalConn{
  81. tlsConn: tc,
  82. connType: connType,
  83. isLocal: isLocal,
  84. priority: priority,
  85. establishedAt: time.Now().Truncate(time.Second),
  86. }
  87. }
  88. func (c internalConn) Close() error {
  89. // *tls.Conn.Close() does more than it says on the tin. Specifically, it
  90. // sends a TLS alert message, which might block forever if the
  91. // connection is dead and we don't have a deadline set.
  92. _ = c.SetWriteDeadline(time.Now().Add(250 * time.Millisecond))
  93. return c.tlsConn.Close()
  94. }
  95. func (c internalConn) Type() string {
  96. return c.connType.String()
  97. }
  98. func (c internalConn) IsLocal() bool {
  99. return c.isLocal
  100. }
  101. func (c internalConn) Priority() int {
  102. return c.priority
  103. }
  104. func (c internalConn) Crypto() string {
  105. cs := c.ConnectionState()
  106. return fmt.Sprintf("%s-%s", tlsVersionNames[cs.Version], tlsCipherSuiteNames[cs.CipherSuite])
  107. }
  108. func (c internalConn) Transport() string {
  109. transport := c.connType.Transport()
  110. ip, err := osutil.IPFromAddr(c.LocalAddr())
  111. if err != nil {
  112. return transport
  113. }
  114. if ip.To4() != nil {
  115. return transport + "4"
  116. }
  117. return transport + "6"
  118. }
  119. func (c internalConn) EstablishedAt() time.Time {
  120. return c.establishedAt
  121. }
  122. func (c internalConn) String() string {
  123. t := "WAN"
  124. if c.isLocal {
  125. t = "LAN"
  126. }
  127. return fmt.Sprintf("%s-%s/%s/%s/%s-P%d", c.LocalAddr(), c.RemoteAddr(), c.Type(), c.Crypto(), t, c.Priority())
  128. }
  129. type dialerFactory interface {
  130. New(config.OptionsConfiguration, *tls.Config, *registry.Registry, *lanChecker) genericDialer
  131. AlwaysWAN() bool
  132. Valid(config.Configuration) error
  133. String() string
  134. }
  135. type commonDialer struct {
  136. trafficClass int
  137. reconnectInterval time.Duration
  138. tlsCfg *tls.Config
  139. lanChecker *lanChecker
  140. lanPriority int
  141. wanPriority int
  142. }
  143. func (d *commonDialer) RedialFrequency() time.Duration {
  144. return d.reconnectInterval
  145. }
  146. func (d *commonDialer) Priority(host string) int {
  147. if d.lanChecker.isLANHost(host) {
  148. return d.lanPriority
  149. }
  150. return d.wanPriority
  151. }
  152. type genericDialer interface {
  153. Dial(context.Context, protocol.DeviceID, *url.URL) (internalConn, error)
  154. RedialFrequency() time.Duration
  155. Priority(host string) int
  156. }
  157. type listenerFactory interface {
  158. New(*url.URL, config.Wrapper, *tls.Config, chan internalConn, *nat.Service, *registry.Registry, *lanChecker) genericListener
  159. Valid(config.Configuration) error
  160. }
  161. type ListenerAddresses struct {
  162. URI *url.URL
  163. WANAddresses []*url.URL
  164. LANAddresses []*url.URL
  165. }
  166. type genericListener interface {
  167. suture.Service
  168. URI() *url.URL
  169. // A given address can potentially be mutated by the listener.
  170. // For example we bind to tcp://0.0.0.0, but that for example might return
  171. // tcp://gateway1.ip and tcp://gateway2.ip as WAN addresses due to there
  172. // being multiple gateways, and us managing to get a UPnP mapping on both
  173. // and tcp://192.168.0.1 and tcp://10.0.0.1 due to there being multiple
  174. // network interfaces. (The later case for LAN addresses is made up just
  175. // to provide an example)
  176. WANAddresses() []*url.URL
  177. LANAddresses() []*url.URL
  178. Error() error
  179. OnAddressesChanged(func(ListenerAddresses))
  180. String() string
  181. Factory() listenerFactory
  182. NATType() string
  183. }
  184. type Model interface {
  185. protocol.Model
  186. AddConnection(conn protocol.Connection, hello protocol.Hello)
  187. NumConnections() int
  188. Connection(remoteID protocol.DeviceID) (protocol.Connection, bool)
  189. OnHello(protocol.DeviceID, net.Addr, protocol.Hello) error
  190. GetHello(protocol.DeviceID) protocol.HelloIntf
  191. DeviceStatistics() (map[protocol.DeviceID]stats.DeviceStatistics, error)
  192. }
  193. type onAddressesChangedNotifier struct {
  194. callbacks []func(ListenerAddresses)
  195. }
  196. func (o *onAddressesChangedNotifier) OnAddressesChanged(callback func(ListenerAddresses)) {
  197. o.callbacks = append(o.callbacks, callback)
  198. }
  199. func (o *onAddressesChangedNotifier) notifyAddressesChanged(l genericListener) {
  200. o.notifyAddresses(ListenerAddresses{
  201. URI: l.URI(),
  202. WANAddresses: l.WANAddresses(),
  203. LANAddresses: l.LANAddresses(),
  204. })
  205. }
  206. func (o *onAddressesChangedNotifier) clearAddresses(l genericListener) {
  207. o.notifyAddresses(ListenerAddresses{
  208. URI: l.URI(),
  209. })
  210. }
  211. func (o *onAddressesChangedNotifier) notifyAddresses(l ListenerAddresses) {
  212. for _, callback := range o.callbacks {
  213. callback(l)
  214. }
  215. }
  216. type dialTarget struct {
  217. addr string
  218. dialer genericDialer
  219. priority int
  220. uri *url.URL
  221. deviceID protocol.DeviceID
  222. }
  223. func (t dialTarget) Dial(ctx context.Context) (internalConn, error) {
  224. l.Debugln("dialing", t.deviceID, t.uri, "prio", t.priority)
  225. return t.dialer.Dial(ctx, t.deviceID, t.uri)
  226. }