peergater.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 trustzone
  14. import (
  15. "context"
  16. "sync"
  17. "time"
  18. "github.com/libp2p/go-libp2p/core/peer"
  19. "github.com/mudler/edgevpn/pkg/blockchain"
  20. "github.com/mudler/edgevpn/pkg/node"
  21. "github.com/mudler/edgevpn/pkg/protocol"
  22. )
  23. type PeerGater struct {
  24. sync.Mutex
  25. trustDB []peer.ID
  26. enabled, relaxed bool
  27. }
  28. // NewPeerGater returns a new peergater
  29. // In relaxed mode won't gate until the trustDB contains some auth data.
  30. func NewPeerGater(relaxed bool) *PeerGater {
  31. return &PeerGater{enabled: true, relaxed: relaxed}
  32. }
  33. // Enabled returns true if the PeerGater is enabled
  34. func (pg *PeerGater) Enabled() bool {
  35. pg.Lock()
  36. defer pg.Unlock()
  37. return pg.enabled
  38. }
  39. // Disable turn off the peer gating mechanism
  40. func (pg *PeerGater) Disable() {
  41. pg.Lock()
  42. defer pg.Unlock()
  43. pg.enabled = false
  44. }
  45. // Enable turns on peer gating mechanism
  46. func (pg *PeerGater) Enable() {
  47. pg.Lock()
  48. defer pg.Unlock()
  49. pg.enabled = true
  50. }
  51. // Implements peergating interface
  52. // resolves to peers in the trustDB. if peer is absent will return true
  53. func (pg *PeerGater) Gate(n *node.Node, p peer.ID) bool {
  54. pg.Lock()
  55. defer pg.Unlock()
  56. if !pg.enabled {
  57. return false
  58. }
  59. if pg.relaxed && len(pg.trustDB) == 0 {
  60. return false
  61. }
  62. for _, pp := range pg.trustDB {
  63. if pp == p {
  64. return false
  65. }
  66. }
  67. return true
  68. }
  69. // UpdaterService is a service responsible to sync back trustDB from the ledger state.
  70. // It is a network service which retrieves the senders ID listed in the Trusted Zone
  71. // and fills it in the trustDB used to gate blockchain messages
  72. func (pg *PeerGater) UpdaterService(duration time.Duration) node.NetworkService {
  73. return func(ctx context.Context, c node.Config, n *node.Node, b *blockchain.Ledger) error {
  74. b.Announce(ctx, duration, func() {
  75. db := []peer.ID{}
  76. tz, found := b.CurrentData()[protocol.TrustZoneKey]
  77. if found {
  78. for k, _ := range tz {
  79. db = append(db, peer.ID(k))
  80. }
  81. }
  82. pg.Lock()
  83. pg.trustDB = db
  84. pg.Unlock()
  85. })
  86. return nil
  87. }
  88. }