util.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package mq
  2. import (
  3. "fmt"
  4. "github.com/gravitl/netmaker/logic"
  5. "github.com/gravitl/netmaker/models"
  6. "github.com/gravitl/netmaker/netclient/ncutils"
  7. )
  8. func decryptMsg(msg []byte) ([]byte, error) {
  9. trafficKey, trafficErr := logic.RetrievePrivateTrafficKey()
  10. if trafficErr != nil {
  11. return nil, trafficErr
  12. }
  13. return ncutils.DestructMessage(string(msg), &trafficKey), nil
  14. }
  15. func encrypt(node *models.Node, dest string, msg []byte) ([]byte, error) {
  16. fmt.Printf("original length: %d \n", len(msg))
  17. node.TrafficKeys.Mine.N = &node.TrafficKeys.Mod
  18. node.TrafficKeys.Mine.E = node.TrafficKeys.E
  19. encrypted := ncutils.BuildMessage(msg, &node.TrafficKeys.Mine)
  20. if encrypted == "" {
  21. return nil, fmt.Errorf("could not encrypt message")
  22. }
  23. fmt.Printf("resulting length: %d \n", len(encrypted))
  24. return []byte(encrypted), nil
  25. }
  26. func publish(node *models.Node, dest string, msg []byte) error {
  27. client := SetupMQTT()
  28. defer client.Disconnect(250)
  29. encrypted, encryptErr := encrypt(node, dest, msg)
  30. if encryptErr != nil {
  31. return encryptErr
  32. }
  33. if token := client.Publish(dest, 0, false, encrypted); token.Wait() && token.Error() != nil {
  34. return token.Error()
  35. }
  36. return nil
  37. }