structs.go 4.7 KB

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