structs.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. RemoteAddr() net.Addr
  25. }
  26. // completeConn is the aggregation of an internalConn and the
  27. // protocol.Connection running on top of it. It implements the Connection
  28. // interface.
  29. type completeConn struct {
  30. internalConn
  31. protocol.Connection
  32. }
  33. // internalConn is the raw TLS connection plus some metadata on where it
  34. // came from (type, priority).
  35. type internalConn struct {
  36. *tls.Conn
  37. connType connType
  38. priority int
  39. }
  40. type connType int
  41. const (
  42. connTypeRelayClient connType = iota
  43. connTypeRelayServer
  44. connTypeTCPClient
  45. connTypeTCPServer
  46. )
  47. func (t connType) String() string {
  48. switch t {
  49. case connTypeRelayClient:
  50. return "relay-client"
  51. case connTypeRelayServer:
  52. return "relay-server"
  53. case connTypeTCPClient:
  54. return "tcp-client"
  55. case connTypeTCPServer:
  56. return "tcp-server"
  57. default:
  58. return "unknown-type"
  59. }
  60. }
  61. func (c internalConn) Type() string {
  62. return c.connType.String()
  63. }
  64. func (c internalConn) String() string {
  65. return fmt.Sprintf("%s-%s/%s", c.LocalAddr(), c.RemoteAddr(), c.connType.String())
  66. }
  67. type dialerFactory interface {
  68. New(*config.Wrapper, *tls.Config) genericDialer
  69. Priority() int
  70. Enabled(config.Configuration) bool
  71. String() string
  72. }
  73. type genericDialer interface {
  74. Dial(protocol.DeviceID, *url.URL) (internalConn, error)
  75. RedialFrequency() time.Duration
  76. }
  77. type listenerFactory interface {
  78. New(*url.URL, *config.Wrapper, *tls.Config, chan internalConn, *nat.Service) genericListener
  79. Enabled(config.Configuration) bool
  80. }
  81. type genericListener interface {
  82. Serve()
  83. Stop()
  84. URI() *url.URL
  85. // A given address can potentially be mutated by the listener.
  86. // For example we bind to tcp://0.0.0.0, but that for example might return
  87. // tcp://gateway1.ip and tcp://gateway2.ip as WAN addresses due to there
  88. // being multiple gateways, and us managing to get a UPnP mapping on both
  89. // and tcp://192.168.0.1 and tcp://10.0.0.1 due to there being multiple
  90. // network interfaces. (The later case for LAN addresses is made up just
  91. // to provide an example)
  92. WANAddresses() []*url.URL
  93. LANAddresses() []*url.URL
  94. Error() error
  95. OnAddressesChanged(func(genericListener))
  96. String() string
  97. Factory() listenerFactory
  98. }
  99. type Model interface {
  100. protocol.Model
  101. AddConnection(conn Connection, hello protocol.HelloResult)
  102. ConnectedTo(remoteID protocol.DeviceID) bool
  103. OnHello(protocol.DeviceID, net.Addr, protocol.HelloResult) error
  104. GetHello(protocol.DeviceID) protocol.HelloIntf
  105. }
  106. // serviceFunc wraps a function to create a suture.Service without stop
  107. // functionality.
  108. type serviceFunc func()
  109. func (f serviceFunc) Serve() { f() }
  110. func (f serviceFunc) Stop() {}
  111. type onAddressesChangedNotifier struct {
  112. callbacks []func(genericListener)
  113. }
  114. func (o *onAddressesChangedNotifier) OnAddressesChanged(callback func(genericListener)) {
  115. o.callbacks = append(o.callbacks, callback)
  116. }
  117. func (o *onAddressesChangedNotifier) notifyAddressesChanged(l genericListener) {
  118. for _, callback := range o.callbacks {
  119. callback(l)
  120. }
  121. }