structs.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. "crypto/tls"
  9. "fmt"
  10. "net"
  11. "net/url"
  12. "time"
  13. "github.com/syncthing/syncthing/lib/config"
  14. "github.com/syncthing/syncthing/lib/nat"
  15. "github.com/syncthing/syncthing/lib/protocol"
  16. )
  17. // Connection is what we expose to the outside. It is a protocol.Connection
  18. // that can be closed and has some metadata.
  19. type Connection interface {
  20. protocol.Connection
  21. Type() string
  22. Transport() string
  23. RemoteAddr() net.Addr
  24. Priority() int
  25. String() string
  26. }
  27. // completeConn is the aggregation of an internalConn and the
  28. // protocol.Connection running on top of it. It implements the Connection
  29. // interface.
  30. type completeConn struct {
  31. internalConn
  32. protocol.Connection
  33. }
  34. func (c completeConn) Close(err error) {
  35. c.Connection.Close(err)
  36. c.internalConn.Close()
  37. }
  38. // internalConn is the raw TLS connection plus some metadata on where it
  39. // came from (type, priority).
  40. type internalConn struct {
  41. *tls.Conn
  42. connType connType
  43. priority int
  44. }
  45. type connType int
  46. const (
  47. connTypeRelayClient connType = iota
  48. connTypeRelayServer
  49. connTypeTCPClient
  50. connTypeTCPServer
  51. )
  52. func (t connType) String() string {
  53. switch t {
  54. case connTypeRelayClient:
  55. return "relay-client"
  56. case connTypeRelayServer:
  57. return "relay-server"
  58. case connTypeTCPClient:
  59. return "tcp-client"
  60. case connTypeTCPServer:
  61. return "tcp-server"
  62. default:
  63. return "unknown-type"
  64. }
  65. }
  66. func (t connType) Transport() string {
  67. switch t {
  68. case connTypeRelayClient, connTypeRelayServer:
  69. return "relay"
  70. case connTypeTCPClient, connTypeTCPServer:
  71. return "tcp"
  72. default:
  73. return "unknown"
  74. }
  75. }
  76. func (c internalConn) Close() {
  77. // *tls.Conn.Close() does more than it says on the tin. Specifically, it
  78. // sends a TLS alert message, which might block forever if the
  79. // connection is dead and we don't have a deadline set.
  80. c.SetWriteDeadline(time.Now().Add(250 * time.Millisecond))
  81. c.Conn.Close()
  82. }
  83. func (c internalConn) Type() string {
  84. return c.connType.String()
  85. }
  86. func (c internalConn) Priority() int {
  87. return c.priority
  88. }
  89. func (c internalConn) Transport() string {
  90. transport := c.connType.Transport()
  91. host, _, err := net.SplitHostPort(c.LocalAddr().String())
  92. if err != nil {
  93. return transport
  94. }
  95. ip := net.ParseIP(host)
  96. if ip == nil {
  97. return transport
  98. }
  99. if ip.To4() != nil {
  100. return transport + "4"
  101. }
  102. return transport + "6"
  103. }
  104. func (c internalConn) String() string {
  105. return fmt.Sprintf("%s-%s/%s", c.LocalAddr(), c.RemoteAddr(), c.Type())
  106. }
  107. type dialerFactory interface {
  108. New(config.Wrapper, *tls.Config) genericDialer
  109. Priority() int
  110. AlwaysWAN() bool
  111. Valid(config.Configuration) error
  112. String() string
  113. }
  114. type genericDialer interface {
  115. Dial(protocol.DeviceID, *url.URL) (internalConn, error)
  116. RedialFrequency() time.Duration
  117. }
  118. type listenerFactory interface {
  119. New(*url.URL, config.Wrapper, *tls.Config, chan internalConn, *nat.Service) genericListener
  120. Valid(config.Configuration) error
  121. }
  122. type genericListener interface {
  123. Serve()
  124. Stop()
  125. URI() *url.URL
  126. // A given address can potentially be mutated by the listener.
  127. // For example we bind to tcp://0.0.0.0, but that for example might return
  128. // tcp://gateway1.ip and tcp://gateway2.ip as WAN addresses due to there
  129. // being multiple gateways, and us managing to get a UPnP mapping on both
  130. // and tcp://192.168.0.1 and tcp://10.0.0.1 due to there being multiple
  131. // network interfaces. (The later case for LAN addresses is made up just
  132. // to provide an example)
  133. WANAddresses() []*url.URL
  134. LANAddresses() []*url.URL
  135. Error() error
  136. OnAddressesChanged(func(genericListener))
  137. String() string
  138. Factory() listenerFactory
  139. NATType() string
  140. }
  141. type Model interface {
  142. protocol.Model
  143. AddConnection(conn Connection, hello protocol.HelloResult)
  144. Connection(remoteID protocol.DeviceID) (Connection, bool)
  145. OnHello(protocol.DeviceID, net.Addr, protocol.HelloResult) error
  146. GetHello(protocol.DeviceID) protocol.HelloIntf
  147. }
  148. // serviceFunc wraps a function to create a suture.Service without stop
  149. // functionality.
  150. type serviceFunc func()
  151. func (f serviceFunc) Serve() { f() }
  152. func (f serviceFunc) Stop() {}
  153. type onAddressesChangedNotifier struct {
  154. callbacks []func(genericListener)
  155. }
  156. func (o *onAddressesChangedNotifier) OnAddressesChanged(callback func(genericListener)) {
  157. o.callbacks = append(o.callbacks, callback)
  158. }
  159. func (o *onAddressesChangedNotifier) notifyAddressesChanged(l genericListener) {
  160. for _, callback := range o.callbacks {
  161. callback(l)
  162. }
  163. }
  164. type dialTarget struct {
  165. dialer genericDialer
  166. priority int
  167. uri *url.URL
  168. deviceID protocol.DeviceID
  169. }
  170. func (t dialTarget) Dial() (internalConn, error) {
  171. l.Debugln("dialing", t.deviceID, t.uri, "prio", t.priority)
  172. conn, err := t.dialer.Dial(t.deviceID, t.uri)
  173. if err != nil {
  174. l.Debugln("dialing", t.deviceID, t.uri, "error:", err)
  175. } else {
  176. l.Debugln("dialing", t.deviceID, t.uri, "success:", conn)
  177. }
  178. return conn, err
  179. }