util.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package mq
  2. import (
  3. "errors"
  4. "fmt"
  5. "runtime"
  6. "strings"
  7. "time"
  8. "github.com/gravitl/netmaker/logic"
  9. "github.com/gravitl/netmaker/models"
  10. "github.com/gravitl/netmaker/netclient/ncutils"
  11. )
  12. func decryptMsgWithHost(host *models.Host, msg []byte) ([]byte, error) {
  13. if host.OS == models.OS_Types.IoT { // just pass along IoT messages
  14. return msg, nil
  15. }
  16. trafficKey, trafficErr := logic.RetrievePrivateTrafficKey() // get server private key
  17. if trafficErr != nil {
  18. return nil, trafficErr
  19. }
  20. serverPrivTKey, err := ncutils.ConvertBytesToKey(trafficKey)
  21. if err != nil {
  22. return nil, err
  23. }
  24. nodePubTKey, err := ncutils.ConvertBytesToKey(host.TrafficKeyPublic)
  25. if err != nil {
  26. return nil, err
  27. }
  28. return ncutils.DeChunk(msg, nodePubTKey, serverPrivTKey)
  29. }
  30. func DecryptMsg(node *models.Node, msg []byte) ([]byte, error) {
  31. if len(msg) <= 24 { // make sure message is of appropriate length
  32. return nil, fmt.Errorf("received invalid message from broker %v", msg)
  33. }
  34. host, err := logic.GetHost(node.HostID.String())
  35. if err != nil {
  36. return nil, err
  37. }
  38. return decryptMsgWithHost(host, msg)
  39. }
  40. func encryptMsg(host *models.Host, msg []byte) ([]byte, error) {
  41. if host.OS == models.OS_Types.IoT {
  42. return msg, nil
  43. }
  44. // fetch server public key to be certain hasn't changed in transit
  45. trafficKey, trafficErr := logic.RetrievePrivateTrafficKey()
  46. if trafficErr != nil {
  47. return nil, trafficErr
  48. }
  49. serverPrivKey, err := ncutils.ConvertBytesToKey(trafficKey)
  50. if err != nil {
  51. return nil, err
  52. }
  53. nodePubKey, err := ncutils.ConvertBytesToKey(host.TrafficKeyPublic)
  54. if err != nil {
  55. return nil, err
  56. }
  57. if strings.Contains(host.Version, "0.10.0") {
  58. return ncutils.BoxEncrypt(msg, nodePubKey, serverPrivKey)
  59. }
  60. return ncutils.Chunk(msg, nodePubKey, serverPrivKey)
  61. }
  62. func publish(host *models.Host, dest string, msg []byte) error {
  63. if len(msg) == 0 {
  64. fmt.Println("-----> $$$$$$ ZERO MSG_-------> ", string(msg))
  65. pc, _, _, ok := runtime.Caller(1)
  66. details := runtime.FuncForPC(pc)
  67. if ok && details != nil {
  68. fmt.Printf("\n------> ####$$$ Called from %s\n", details.Name())
  69. }
  70. }
  71. // encrypted, encryptErr := encryptMsg(host, msg)
  72. // if encryptErr != nil {
  73. // return encryptErr
  74. // }
  75. if mqclient == nil {
  76. return errors.New("cannot publish ... mqclient not connected")
  77. }
  78. if token := mqclient.Publish(dest, 0, true, msg); !token.WaitTimeout(MQ_TIMEOUT*time.Second) || token.Error() != nil {
  79. var err error
  80. if token.Error() == nil {
  81. err = errors.New("connection timeout")
  82. } else {
  83. err = token.Error()
  84. }
  85. return err
  86. }
  87. return nil
  88. }
  89. // decodes a message queue topic and returns the embedded node.ID
  90. func GetID(topic string) (string, error) {
  91. parts := strings.Split(topic, "/")
  92. count := len(parts)
  93. if count == 1 {
  94. return "", fmt.Errorf("invalid topic")
  95. }
  96. //the last part of the topic will be the node.ID
  97. return parts[count-1], nil
  98. }