hub.go 3.7 KB

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