config.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/mudler/edgevpn/pkg/blockchain"
  24. discovery "github.com/mudler/edgevpn/pkg/discovery"
  25. hub "github.com/mudler/edgevpn/pkg/hub"
  26. protocol "github.com/mudler/edgevpn/pkg/protocol"
  27. )
  28. // Config is the node configuration
  29. type Config struct {
  30. // ExchangeKey is a Symmetric key used to seal the messages
  31. ExchangeKey string
  32. // RoomName is the OTP token gossip room where all peers are subscribed to
  33. RoomName string
  34. // ListenAddresses is the discovery peer initial bootstrap addresses
  35. ListenAddresses []discovery.AddrList
  36. // Insecure disables secure p2p e2e encrypted communication
  37. Insecure bool
  38. // Handlers are a list of handlers subscribed to messages received by the vpn interface
  39. Handlers []Handler
  40. MaxMessageSize int
  41. SealKeyInterval int
  42. ServiceDiscovery []ServiceDiscovery
  43. NetworkServices []NetworkService
  44. Logger log.StandardLogger
  45. SealKeyLength int
  46. InterfaceAddress string
  47. Store blockchain.Store
  48. // Handle is a handle consumed by HumanInterfaces to handle received messages
  49. Handle func(bool, *hub.Message)
  50. StreamHandlers map[protocol.Protocol]StreamHandler
  51. AdditionalOptions, Options []libp2p.Option
  52. DiscoveryInterval, LedgerSyncronizationTime, LedgerAnnounceTime time.Duration
  53. DiscoveryBootstrapPeers discovery.AddrList
  54. Whitelist, Blacklist []string
  55. Sealer Sealer
  56. }
  57. type Sealer interface {
  58. Seal(string, string) (string, error)
  59. Unseal(string, string) (string, error)
  60. }
  61. // NetworkService is a service running over the network. It takes a context, a node and a ledger
  62. type NetworkService func(context.Context, Config, *Node, *blockchain.Ledger) error
  63. type StreamHandler func(*Node, *blockchain.Ledger) func(stream network.Stream)
  64. type Handler func(*hub.Message) error
  65. type ServiceDiscovery interface {
  66. Run(log.StandardLogger, context.Context, host.Host) error
  67. Option(context.Context) func(c *libp2p.Config) error
  68. }
  69. type Option func(cfg *Config) error
  70. // Apply applies the given options to the config, returning the first error
  71. // encountered (if any).
  72. func (cfg *Config) Apply(opts ...Option) error {
  73. for _, opt := range opts {
  74. if opt == nil {
  75. continue
  76. }
  77. if err := opt(cfg); err != nil {
  78. return err
  79. }
  80. }
  81. return nil
  82. }