structs.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. return c.connType.Transport()
  85. }
  86. func (c internalConn) String() string {
  87. return fmt.Sprintf("%s-%s/%s", c.LocalAddr(), c.RemoteAddr(), c.connType.String())
  88. }
  89. type dialerFactory interface {
  90. New(*config.Wrapper, *tls.Config) genericDialer
  91. Priority() int
  92. Enabled(config.Configuration) bool
  93. String() string
  94. }
  95. type genericDialer interface {
  96. Dial(protocol.DeviceID, *url.URL) (internalConn, error)
  97. RedialFrequency() time.Duration
  98. }
  99. type listenerFactory interface {
  100. New(*url.URL, *config.Wrapper, *tls.Config, chan internalConn, *nat.Service) genericListener
  101. Enabled(config.Configuration) bool
  102. }
  103. type genericListener interface {
  104. Serve()
  105. Stop()
  106. URI() *url.URL
  107. // A given address can potentially be mutated by the listener.
  108. // For example we bind to tcp://0.0.0.0, but that for example might return
  109. // tcp://gateway1.ip and tcp://gateway2.ip as WAN addresses due to there
  110. // being multiple gateways, and us managing to get a UPnP mapping on both
  111. // and tcp://192.168.0.1 and tcp://10.0.0.1 due to there being multiple
  112. // network interfaces. (The later case for LAN addresses is made up just
  113. // to provide an example)
  114. WANAddresses() []*url.URL
  115. LANAddresses() []*url.URL
  116. Error() error
  117. OnAddressesChanged(func(genericListener))
  118. String() string
  119. Factory() listenerFactory
  120. NATType() string
  121. }
  122. type Model interface {
  123. protocol.Model
  124. AddConnection(conn Connection, hello protocol.HelloResult)
  125. ConnectedTo(remoteID protocol.DeviceID) bool
  126. OnHello(protocol.DeviceID, net.Addr, protocol.HelloResult) error
  127. GetHello(protocol.DeviceID) protocol.HelloIntf
  128. }
  129. // serviceFunc wraps a function to create a suture.Service without stop
  130. // functionality.
  131. type serviceFunc func()
  132. func (f serviceFunc) Serve() { f() }
  133. func (f serviceFunc) Stop() {}
  134. type onAddressesChangedNotifier struct {
  135. callbacks []func(genericListener)
  136. }
  137. func (o *onAddressesChangedNotifier) OnAddressesChanged(callback func(genericListener)) {
  138. o.callbacks = append(o.callbacks, callback)
  139. }
  140. func (o *onAddressesChangedNotifier) notifyAddressesChanged(l genericListener) {
  141. for _, callback := range o.callbacks {
  142. callback(l)
  143. }
  144. }