handlers.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package mq
  2. import (
  3. "encoding/json"
  4. mqtt "github.com/eclipse/paho.mqtt.golang"
  5. "github.com/gravitl/netmaker/database"
  6. "github.com/gravitl/netmaker/logger"
  7. "github.com/gravitl/netmaker/logic"
  8. "github.com/gravitl/netmaker/models"
  9. "github.com/gravitl/netmaker/netclient/ncutils"
  10. )
  11. // DefaultHandler default message queue handler - only called when GetDebug == true
  12. func DefaultHandler(client mqtt.Client, msg mqtt.Message) {
  13. logger.Log(0, "MQTT Message: Topic: ", string(msg.Topic()), " Message: ", string(msg.Payload()))
  14. }
  15. // Ping message Handler -- handles ping topic from client nodes
  16. func Ping(client mqtt.Client, msg mqtt.Message) {
  17. go func() {
  18. id, err := getID(msg.Topic())
  19. if err != nil {
  20. logger.Log(0, "error getting node.ID sent on ping topic ")
  21. return
  22. }
  23. node, err := logic.GetNodeByID(id)
  24. if err != nil {
  25. logger.Log(0, "mq-ping error getting node: ", err.Error())
  26. record, err := database.FetchRecord(database.NODES_TABLE_NAME, id)
  27. if err != nil {
  28. logger.Log(0, "error reading database ", err.Error())
  29. return
  30. }
  31. logger.Log(0, "record from database")
  32. logger.Log(0, record)
  33. return
  34. }
  35. _, decryptErr := decryptMsg(&node, msg.Payload())
  36. if decryptErr != nil {
  37. logger.Log(0, "error decrypting when updating node ", node.ID, decryptErr.Error())
  38. return
  39. }
  40. node.SetLastCheckIn()
  41. if err := logic.UpdateNode(&node, &node); err != nil {
  42. logger.Log(0, "error updating node", node.Name, node.ID, " on checkin", err.Error())
  43. return
  44. }
  45. logger.Log(3, "ping processed for node", node.ID)
  46. // --TODO --set client version once feature is implemented.
  47. //node.SetClientVersion(msg.Payload())
  48. }()
  49. }
  50. // UpdateNode message Handler -- handles updates from client nodes
  51. func UpdateNode(client mqtt.Client, msg mqtt.Message) {
  52. go func() {
  53. id, err := getID(msg.Topic())
  54. if err != nil {
  55. logger.Log(1, "error getting node.ID sent on ", msg.Topic(), err.Error())
  56. return
  57. }
  58. currentNode, err := logic.GetNodeByID(id)
  59. if err != nil {
  60. logger.Log(1, "error getting node ", id, err.Error())
  61. return
  62. }
  63. decrypted, decryptErr := decryptMsg(&currentNode, msg.Payload())
  64. if decryptErr != nil {
  65. logger.Log(1, "failed to decrypt message for node ", id, decryptErr.Error())
  66. return
  67. }
  68. var newNode models.Node
  69. if err := json.Unmarshal(decrypted, &newNode); err != nil {
  70. logger.Log(1, "error unmarshaling payload ", err.Error())
  71. return
  72. }
  73. if err := logic.UpdateNode(&currentNode, &newNode); err != nil {
  74. logger.Log(1, "error saving node", err.Error())
  75. return
  76. }
  77. logger.Log(1, "updated node", id, newNode.Name)
  78. }()
  79. }
  80. // ClientPeerUpdate message handler -- handles updating peers after signal from client nodes
  81. func ClientPeerUpdate(client mqtt.Client, msg mqtt.Message) {
  82. go func() {
  83. id, err := getID(msg.Topic())
  84. if err != nil {
  85. logger.Log(1, "error getting node.ID sent on ", msg.Topic(), err.Error())
  86. return
  87. }
  88. currentNode, err := logic.GetNodeByID(id)
  89. if err != nil {
  90. logger.Log(1, "error getting node ", id, err.Error())
  91. return
  92. }
  93. decrypted, decryptErr := decryptMsg(&currentNode, msg.Payload())
  94. if decryptErr != nil {
  95. logger.Log(1, "failed to decrypt message during client peer update for node ", id, decryptErr.Error())
  96. return
  97. }
  98. switch decrypted[0] {
  99. case ncutils.ACK:
  100. currentServerNode, err := logic.GetNetworkServerLocal(currentNode.Network)
  101. if err != nil {
  102. return
  103. }
  104. if err := logic.ServerUpdate(&currentServerNode, false); err != nil {
  105. logger.Log(1, "server node:", currentServerNode.ID, "failed update")
  106. return
  107. }
  108. case ncutils.DONE:
  109. currentServerNode, err := logic.GetNetworkServerLocal(currentNode.Network)
  110. if err != nil {
  111. return
  112. }
  113. if err := logic.ServerUpdate(&currentServerNode, false); err != nil {
  114. logger.Log(1, "server node:", currentServerNode.ID, "failed update")
  115. return
  116. }
  117. if err := PublishPeerUpdate(&currentNode); err != nil {
  118. logger.Log(1, "error publishing peer update ", err.Error())
  119. return
  120. }
  121. }
  122. logger.Log(1, "sent peer updates after signal received from", id, currentNode.Name)
  123. }()
  124. }