2
0

config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 []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. Sealer Sealer
  57. PeerGater Gater
  58. }
  59. type Gater interface {
  60. Gate(*Node, peer.ID) bool
  61. }
  62. type Sealer interface {
  63. Seal(string, string) (string, error)
  64. Unseal(string, string) (string, error)
  65. }
  66. // NetworkService is a service running over the network. It takes a context, a node and a ledger
  67. type NetworkService func(context.Context, Config, *Node, *blockchain.Ledger) error
  68. type StreamHandler func(*Node, *blockchain.Ledger) func(stream network.Stream)
  69. type Handler func(*hub.Message) error
  70. type ServiceDiscovery interface {
  71. Run(log.StandardLogger, context.Context, host.Host) error
  72. Option(context.Context) func(c *libp2p.Config) error
  73. }
  74. type Option func(cfg *Config) error
  75. // Apply applies the given options to the config, returning the first error
  76. // encountered (if any).
  77. func (cfg *Config) Apply(opts ...Option) error {
  78. for _, opt := range opts {
  79. if opt == nil {
  80. continue
  81. }
  82. if err := opt(cfg); err != nil {
  83. return err
  84. }
  85. }
  86. return nil
  87. }