structs.go 2.4 KB

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