structs.go 2.7 KB

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