config.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright © 2021-2022 Ettore Di Giacinto <[email protected]>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package node
  14. import (
  15. "context"
  16. "time"
  17. "github.com/ipfs/go-log"
  18. "github.com/libp2p/go-libp2p"
  19. "github.com/libp2p/go-libp2p/core/host"
  20. "github.com/libp2p/go-libp2p/core/network"
  21. "github.com/libp2p/go-libp2p/core/peer"
  22. "github.com/mudler/edgevpn/pkg/blockchain"
  23. discovery "github.com/mudler/edgevpn/pkg/discovery"
  24. hub "github.com/mudler/edgevpn/pkg/hub"
  25. protocol "github.com/mudler/edgevpn/pkg/protocol"
  26. )
  27. // Config is the node configuration
  28. type Config struct {
  29. // ExchangeKey is a Symmetric key used to seal the messages
  30. ExchangeKey string
  31. // RoomName is the OTP token gossip room where all peers are subscribed to
  32. RoomName string
  33. // ListenAddresses is the discovery peer initial bootstrap addresses
  34. ListenAddresses []discovery.AddrList
  35. // Insecure disables secure p2p e2e encrypted communication
  36. Insecure bool
  37. // Handlers are a list of handlers subscribed to messages received by the vpn interface
  38. Handlers, GenericChannelHandler []Handler
  39. MaxMessageSize int
  40. SealKeyInterval int
  41. ServiceDiscovery []ServiceDiscovery
  42. NetworkServices []NetworkService
  43. Logger log.StandardLogger
  44. SealKeyLength int
  45. InterfaceAddress string
  46. Store blockchain.Store
  47. // Handle is a handle consumed by HumanInterfaces to handle received messages
  48. Handle func(bool, *hub.Message)
  49. StreamHandlers map[protocol.Protocol]StreamHandler
  50. AdditionalOptions, Options []libp2p.Option
  51. DiscoveryInterval, LedgerSyncronizationTime, LedgerAnnounceTime time.Duration
  52. DiscoveryBootstrapPeers discovery.AddrList
  53. Whitelist, Blacklist []string
  54. // GenericHub enables generic hub
  55. GenericHub bool
  56. PrivateKey []byte
  57. PeerTable map[string]peer.ID
  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. }