config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright © 2021 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package node
  16. import (
  17. "context"
  18. "time"
  19. "github.com/ipfs/go-log"
  20. "github.com/libp2p/go-libp2p"
  21. "github.com/libp2p/go-libp2p-core/host"
  22. "github.com/libp2p/go-libp2p-core/network"
  23. "github.com/libp2p/go-libp2p-core/peer"
  24. "github.com/mudler/edgevpn/pkg/blockchain"
  25. discovery "github.com/mudler/edgevpn/pkg/discovery"
  26. hub "github.com/mudler/edgevpn/pkg/hub"
  27. protocol "github.com/mudler/edgevpn/pkg/protocol"
  28. )
  29. // Config is the node configuration
  30. type Config struct {
  31. // ExchangeKey is a Symmetric key used to seal the messages
  32. ExchangeKey string
  33. // RoomName is the OTP token gossip room where all peers are subscribed to
  34. RoomName string
  35. // ListenAddresses is the discovery peer initial bootstrap addresses
  36. ListenAddresses []discovery.AddrList
  37. // Insecure disables secure p2p e2e encrypted communication
  38. Insecure bool
  39. // Handlers are a list of handlers subscribed to messages received by the vpn interface
  40. Handlers, GenericChannelHandler []Handler
  41. MaxMessageSize int
  42. SealKeyInterval int
  43. ServiceDiscovery []ServiceDiscovery
  44. NetworkServices []NetworkService
  45. Logger log.StandardLogger
  46. SealKeyLength int
  47. InterfaceAddress string
  48. Store blockchain.Store
  49. // Handle is a handle consumed by HumanInterfaces to handle received messages
  50. Handle func(bool, *hub.Message)
  51. StreamHandlers map[protocol.Protocol]StreamHandler
  52. AdditionalOptions, Options []libp2p.Option
  53. DiscoveryInterval, LedgerSyncronizationTime, LedgerAnnounceTime time.Duration
  54. DiscoveryBootstrapPeers discovery.AddrList
  55. Whitelist, Blacklist []string
  56. // GenericHub enables generic hub
  57. GenericHub bool
  58. Sealer Sealer
  59. PeerGater Gater
  60. }
  61. type Gater interface {
  62. Gate(*Node, peer.ID) bool
  63. Enable()
  64. Disable()
  65. Enabled() bool
  66. }
  67. type Sealer interface {
  68. Seal(string, string) (string, error)
  69. Unseal(string, string) (string, error)
  70. }
  71. // NetworkService is a service running over the network. It takes a context, a node and a ledger
  72. type NetworkService func(context.Context, Config, *Node, *blockchain.Ledger) error
  73. type StreamHandler func(*Node, *blockchain.Ledger) func(stream network.Stream)
  74. type Handler func(*blockchain.Ledger, *hub.Message, chan *hub.Message) error
  75. type ServiceDiscovery interface {
  76. Run(log.StandardLogger, context.Context, host.Host) error
  77. Option(context.Context) func(c *libp2p.Config) error
  78. }
  79. type Option func(cfg *Config) error
  80. // Apply applies the given options to the config, returning the first error
  81. // encountered (if any).
  82. func (cfg *Config) Apply(opts ...Option) error {
  83. for _, opt := range opts {
  84. if opt == nil {
  85. continue
  86. }
  87. if err := opt(cfg); err != nil {
  88. return err
  89. }
  90. }
  91. return nil
  92. }