structs.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 http://mozilla.org/MPL/2.0/.
  6. package connections
  7. import (
  8. "crypto/tls"
  9. "net"
  10. "net/url"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/config"
  13. "github.com/syncthing/syncthing/lib/nat"
  14. "github.com/syncthing/syncthing/lib/protocol"
  15. )
  16. type IntermediateConnection struct {
  17. *tls.Conn
  18. Type string
  19. Priority int
  20. }
  21. type Connection struct {
  22. IntermediateConnection
  23. protocol.Connection
  24. }
  25. type dialerFactory interface {
  26. New(*config.Wrapper, *tls.Config) genericDialer
  27. Priority() int
  28. }
  29. type genericDialer interface {
  30. Dial(protocol.DeviceID, *url.URL) (IntermediateConnection, error)
  31. Priority() int
  32. RedialFrequency() time.Duration
  33. String() string
  34. }
  35. type listenerFactory func(*url.URL, *tls.Config, chan IntermediateConnection, *nat.Service) genericListener
  36. type genericListener interface {
  37. Serve()
  38. Stop()
  39. URI() *url.URL
  40. // A given address can potentially be mutated by the listener.
  41. // For example we bind to tcp://0.0.0.0, but that for example might return
  42. // tcp://gateway1.ip and tcp://gateway2.ip as WAN addresses due to there
  43. // being multiple gateways, and us managing to get a UPnP mapping on both
  44. // and tcp://192.168.0.1 and tcp://10.0.0.1 due to there being multiple
  45. // network interfaces. (The later case for LAN addresses is made up just
  46. // to provide an example)
  47. WANAddresses() []*url.URL
  48. LANAddresses() []*url.URL
  49. Error() error
  50. OnAddressesChanged(func(genericListener))
  51. String() string
  52. }
  53. type Model interface {
  54. protocol.Model
  55. AddConnection(conn Connection, hello protocol.HelloMessage)
  56. ConnectedTo(remoteID protocol.DeviceID) bool
  57. IsPaused(remoteID protocol.DeviceID) bool
  58. OnHello(protocol.DeviceID, net.Addr, protocol.HelloMessage)
  59. GetHello(protocol.DeviceID) protocol.HelloMessage
  60. }
  61. // serviceFunc wraps a function to create a suture.Service without stop
  62. // functionality.
  63. type serviceFunc func()
  64. func (f serviceFunc) Serve() { f() }
  65. func (f serviceFunc) Stop() {}
  66. type onAddressesChangedNotifier struct {
  67. callbacks []func(genericListener)
  68. }
  69. func (o *onAddressesChangedNotifier) OnAddressesChanged(callback func(genericListener)) {
  70. o.callbacks = append(o.callbacks, callback)
  71. }
  72. func (o *onAddressesChangedNotifier) notifyAddressesChanged(l genericListener) {
  73. for _, callback := range o.callbacks {
  74. callback(l)
  75. }
  76. }