structs.go 2.6 KB

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