hub.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright © 2022 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 hub
  16. import (
  17. "context"
  18. "errors"
  19. "strings"
  20. "sync"
  21. "time"
  22. "github.com/mudler/edgevpn/pkg/crypto"
  23. "github.com/xlzd/gotp"
  24. "github.com/libp2p/go-libp2p-core/host"
  25. "github.com/libp2p/go-libp2p-core/peer"
  26. pubsub "github.com/libp2p/go-libp2p-pubsub"
  27. )
  28. type MessageHub struct {
  29. sync.Mutex
  30. r *room
  31. otpKey string
  32. maxsize int
  33. keyLength int
  34. interval int
  35. ctxCancel context.CancelFunc
  36. Messages chan *Message
  37. }
  38. // roomBufSize is the number of incoming messages to buffer for each topic.
  39. const roomBufSize = 128
  40. func NewHub(otp string, maxsize, keyLength, interval int) *MessageHub {
  41. return &MessageHub{otpKey: otp, maxsize: maxsize, keyLength: keyLength, interval: interval,
  42. Messages: make(chan *Message, roomBufSize)}
  43. }
  44. func (m *MessageHub) topicKey() string {
  45. totp := gotp.NewTOTP(strings.ToUpper(m.otpKey), m.keyLength, m.interval, nil)
  46. return crypto.MD5(totp.Now())
  47. }
  48. func (m *MessageHub) joinRoom(host host.Host) error {
  49. m.Lock()
  50. defer m.Unlock()
  51. if m.ctxCancel != nil {
  52. m.ctxCancel()
  53. }
  54. ctx, cancel := context.WithCancel(context.Background())
  55. m.ctxCancel = cancel
  56. // create a new PubSub service using the GossipSub router
  57. ps, err := pubsub.NewGossipSub(ctx, host, pubsub.WithMaxMessageSize(m.maxsize))
  58. if err != nil {
  59. return err
  60. }
  61. // join the "chat" room
  62. cr, err := connect(ctx, ps, host.ID(), m.topicKey(), m.Messages)
  63. if err != nil {
  64. return err
  65. }
  66. m.r = cr
  67. return nil
  68. }
  69. func (m *MessageHub) Start(ctx context.Context, host host.Host) error {
  70. c := make(chan interface{})
  71. go func(c context.Context, cc chan interface{}) {
  72. k := ""
  73. for {
  74. select {
  75. default:
  76. currentKey := m.topicKey()
  77. if currentKey != k {
  78. k = currentKey
  79. cc <- nil
  80. }
  81. time.Sleep(1 * time.Second)
  82. case <-ctx.Done():
  83. close(cc)
  84. return
  85. }
  86. }
  87. }(ctx, c)
  88. for range c {
  89. m.joinRoom(host)
  90. }
  91. // Close eventual open contexts
  92. if m.ctxCancel != nil {
  93. m.ctxCancel()
  94. }
  95. return nil
  96. }
  97. func (m *MessageHub) PublishMessage(mess *Message) error {
  98. m.Lock()
  99. defer m.Unlock()
  100. if m.r != nil {
  101. return m.r.publishMessage(mess)
  102. }
  103. return errors.New("no message room available")
  104. }
  105. func (m *MessageHub) ListPeers() ([]peer.ID, error) {
  106. m.Lock()
  107. defer m.Unlock()
  108. if m.r != nil {
  109. return m.r.Topic.ListPeers(), nil
  110. }
  111. return nil, errors.New("no message room available")
  112. }