handlers.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. package mq
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. mqtt "github.com/eclipse/paho.mqtt.golang"
  7. "github.com/gravitl/netmaker/database"
  8. "github.com/gravitl/netmaker/logger"
  9. "github.com/gravitl/netmaker/logic"
  10. "github.com/gravitl/netmaker/models"
  11. "github.com/gravitl/netmaker/netclient/ncutils"
  12. "github.com/gravitl/netmaker/servercfg"
  13. )
  14. // DefaultHandler default message queue handler -- NOT USED
  15. func DefaultHandler(client mqtt.Client, msg mqtt.Message) {
  16. logger.Log(0, "MQTT Message: Topic: ", string(msg.Topic()), " Message: ", string(msg.Payload()))
  17. }
  18. // Ping message Handler -- handles ping topic from client nodes
  19. func Ping(client mqtt.Client, msg mqtt.Message) {
  20. go func() {
  21. id, err := getID(msg.Topic())
  22. if err != nil {
  23. logger.Log(0, "error getting node.ID sent on ping topic ")
  24. return
  25. }
  26. node, err := logic.GetNodeByID(id)
  27. if err != nil {
  28. logger.Log(0, "mq-ping error getting node: ", err.Error())
  29. record, err := database.FetchRecord(database.NODES_TABLE_NAME, id)
  30. if err != nil {
  31. logger.Log(0, "error reading database ", err.Error())
  32. return
  33. }
  34. logger.Log(0, "record from database")
  35. logger.Log(0, record)
  36. return
  37. }
  38. decrypted, decryptErr := decryptMsg(&node, msg.Payload())
  39. if decryptErr != nil {
  40. logger.Log(0, "error decrypting when updating node ", node.ID.String(), decryptErr.Error())
  41. return
  42. }
  43. var checkin models.NodeCheckin
  44. if err := json.Unmarshal(decrypted, &checkin); err != nil {
  45. logger.Log(1, "error unmarshaling payload ", err.Error())
  46. return
  47. }
  48. host, err := logic.GetHost(node.HostID.String())
  49. if err != nil {
  50. logger.Log(0, "error retrieving host for node ", node.ID.String(), err.Error())
  51. return
  52. }
  53. node.SetLastCheckIn()
  54. host.Version = checkin.Version
  55. node.Connected = checkin.Connected
  56. host.Interfaces = checkin.Ifaces
  57. for i := range host.Interfaces {
  58. host.Interfaces[i].AddressString = host.Interfaces[i].Address.String()
  59. }
  60. if err := logic.UpdateNode(&node, &node); err != nil {
  61. logger.Log(0, "error updating node", node.ID.String(), " on checkin", err.Error())
  62. return
  63. }
  64. logger.Log(3, "ping processed for node", node.ID.String())
  65. // --TODO --set client version once feature is implemented.
  66. //node.SetClientVersion(msg.Payload())
  67. }()
  68. }
  69. // UpdateNode message Handler -- handles updates from client nodes
  70. func UpdateNode(client mqtt.Client, msg mqtt.Message) {
  71. go func() {
  72. id, err := getID(msg.Topic())
  73. if err != nil {
  74. logger.Log(1, "error getting node.ID sent on ", msg.Topic(), err.Error())
  75. return
  76. }
  77. currentNode, err := logic.GetNodeByID(id)
  78. if err != nil {
  79. logger.Log(1, "error getting node ", id, err.Error())
  80. return
  81. }
  82. decrypted, decryptErr := decryptMsg(&currentNode, msg.Payload())
  83. if decryptErr != nil {
  84. logger.Log(1, "failed to decrypt message for node ", id, decryptErr.Error())
  85. return
  86. }
  87. var newNode models.Node
  88. if err := json.Unmarshal(decrypted, &newNode); err != nil {
  89. logger.Log(1, "error unmarshaling payload ", err.Error())
  90. return
  91. }
  92. ifaceDelta := logic.IfaceDelta(&currentNode, &newNode)
  93. if servercfg.Is_EE && ifaceDelta {
  94. if err = logic.EnterpriseResetAllPeersFailovers(currentNode.ID, currentNode.Network); err != nil {
  95. logger.Log(1, "failed to reset failover list during node update", currentNode.ID.String(), currentNode.Network)
  96. }
  97. }
  98. newNode.SetLastCheckIn()
  99. if err := logic.UpdateNode(&currentNode, &newNode); err != nil {
  100. logger.Log(1, "error saving node", err.Error())
  101. return
  102. }
  103. if ifaceDelta { // reduce number of unneeded updates, by only sending on iface changes
  104. if err = PublishPeerUpdate(); err != nil {
  105. logger.Log(0, "error updating peers when node", currentNode.ID.String(), "informed the server of an interface change", err.Error())
  106. }
  107. }
  108. logger.Log(1, "updated node", id, newNode.ID.String())
  109. }()
  110. }
  111. // UpdateHost message Handler -- handles host updates from clients
  112. func UpdateHost(client mqtt.Client, msg mqtt.Message) {
  113. go func(msg mqtt.Message) {
  114. id, err := getHostID(msg.Topic())
  115. if err != nil {
  116. logger.Log(1, "error getting host.ID sent on ", msg.Topic(), err.Error())
  117. return
  118. }
  119. currentHost, err := logic.GetHost(id)
  120. if err != nil {
  121. logger.Log(1, "error getting host ", id, err.Error())
  122. return
  123. }
  124. decrypted, decryptErr := decryptMsgWithHost(currentHost, msg.Payload())
  125. if decryptErr != nil {
  126. logger.Log(1, "failed to decrypt message for host ", id, decryptErr.Error())
  127. return
  128. }
  129. var hostUpdate models.HostUpdate
  130. if err := json.Unmarshal(decrypted, &hostUpdate); err != nil {
  131. logger.Log(1, "error unmarshaling payload ", err.Error())
  132. return
  133. }
  134. logger.Log(0, "recieved host update for host: ", id)
  135. var sendPeerUpdate bool
  136. switch hostUpdate.Action {
  137. case models.UpdateHost:
  138. sendPeerUpdate = updateHostFromClient(&hostUpdate.Host, currentHost)
  139. err := logic.UpsertHost(currentHost)
  140. if err != nil {
  141. logger.Log(0, "failed to update host: ", currentHost.ID.String(), err.Error())
  142. return
  143. }
  144. case models.DeleteHost:
  145. if err := logic.DisassociateAllNodesFromHost(currentHost.ID.String()); err != nil {
  146. logger.Log(0, "failed to delete all nodes of host: ", currentHost.ID.String(), err.Error())
  147. return
  148. }
  149. if err := logic.RemoveHostByID(currentHost.ID.String()); err != nil {
  150. logger.Log(0, "failed to delete host: ", currentHost.ID.String(), err.Error())
  151. return
  152. }
  153. sendPeerUpdate = true
  154. }
  155. if sendPeerUpdate {
  156. err := PublishPeerUpdate()
  157. if err != nil {
  158. logger.Log(0, "failed to pulish peer update: ", err.Error())
  159. }
  160. }
  161. // if servercfg.Is_EE && ifaceDelta {
  162. // if err = logic.EnterpriseResetAllPeersFailovers(currentHost.ID.String(), currentHost.Network); err != nil {
  163. // logger.Log(1, "failed to reset failover list during node update", currentHost.ID.String(), currentHost.Network)
  164. // }
  165. // }
  166. }(msg)
  167. }
  168. // used for updating host on server with update recieved from client
  169. func updateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool) {
  170. if newHost.ListenPort != 0 && currHost.ListenPort != newHost.ListenPort {
  171. currHost.ListenPort = newHost.ListenPort
  172. sendPeerUpdate = true
  173. }
  174. if newHost.ProxyListenPort != 0 && currHost.ProxyListenPort != newHost.ProxyListenPort {
  175. currHost.ProxyListenPort = newHost.ProxyListenPort
  176. sendPeerUpdate = true
  177. }
  178. if newHost.PublicListenPort != 0 && currHost.PublicListenPort != newHost.PublicListenPort {
  179. currHost.PublicListenPort = newHost.PublicListenPort
  180. sendPeerUpdate = true
  181. }
  182. if currHost.ProxyEnabled != newHost.ProxyEnabled {
  183. currHost.ProxyEnabled = newHost.ProxyEnabled
  184. sendPeerUpdate = true
  185. }
  186. if currHost.EndpointIP.String() != newHost.EndpointIP.String() {
  187. currHost.EndpointIP = newHost.EndpointIP
  188. sendPeerUpdate = true
  189. }
  190. currHost.DaemonInstalled = newHost.DaemonInstalled
  191. currHost.Debug = newHost.Debug
  192. currHost.Verbosity = newHost.Verbosity
  193. currHost.Version = newHost.Version
  194. currHost.Name = newHost.Name
  195. return
  196. }
  197. // UpdateMetrics message Handler -- handles updates from client nodes for metrics
  198. func UpdateMetrics(client mqtt.Client, msg mqtt.Message) {
  199. if servercfg.Is_EE {
  200. go func() {
  201. id, err := getID(msg.Topic())
  202. if err != nil {
  203. logger.Log(1, "error getting node.ID sent on ", msg.Topic(), err.Error())
  204. return
  205. }
  206. currentNode, err := logic.GetNodeByID(id)
  207. if err != nil {
  208. logger.Log(1, "error getting node ", id, err.Error())
  209. return
  210. }
  211. decrypted, decryptErr := decryptMsg(&currentNode, msg.Payload())
  212. if decryptErr != nil {
  213. logger.Log(1, "failed to decrypt message for node ", id, decryptErr.Error())
  214. return
  215. }
  216. var newMetrics models.Metrics
  217. if err := json.Unmarshal(decrypted, &newMetrics); err != nil {
  218. logger.Log(1, "error unmarshaling payload ", err.Error())
  219. return
  220. }
  221. shouldUpdate := updateNodeMetrics(&currentNode, &newMetrics)
  222. if err = logic.UpdateMetrics(id, &newMetrics); err != nil {
  223. logger.Log(1, "faield to update node metrics", id, err.Error())
  224. return
  225. }
  226. if servercfg.IsMetricsExporter() {
  227. if err := pushMetricsToExporter(newMetrics); err != nil {
  228. logger.Log(2, fmt.Sprintf("failed to push node: [%s] metrics to exporter, err: %v",
  229. currentNode.ID, err))
  230. }
  231. }
  232. if newMetrics.Connectivity != nil {
  233. err := logic.EnterpriseFailoverFunc(&currentNode)
  234. if err != nil {
  235. logger.Log(0, "failed to failover for node", currentNode.ID.String(), "on network", currentNode.Network, "-", err.Error())
  236. }
  237. }
  238. if shouldUpdate {
  239. logger.Log(2, "updating peers after node", currentNode.ID.String(), currentNode.Network, "detected connectivity issues")
  240. host, err := logic.GetHost(currentNode.HostID.String())
  241. if err == nil {
  242. if err = PublishSingleHostUpdate(host); err != nil {
  243. logger.Log(0, "failed to publish update after failover peer change for node", currentNode.ID.String(), currentNode.Network)
  244. }
  245. }
  246. }
  247. logger.Log(1, "updated node metrics", id)
  248. }()
  249. }
  250. }
  251. // ClientPeerUpdate message handler -- handles updating peers after signal from client nodes
  252. func ClientPeerUpdate(client mqtt.Client, msg mqtt.Message) {
  253. go func() {
  254. id, err := getID(msg.Topic())
  255. if err != nil {
  256. logger.Log(1, "error getting node.ID sent on ", msg.Topic(), err.Error())
  257. return
  258. }
  259. currentNode, err := logic.GetNodeByID(id)
  260. if err != nil {
  261. logger.Log(1, "error getting node ", id, err.Error())
  262. return
  263. }
  264. decrypted, decryptErr := decryptMsg(&currentNode, msg.Payload())
  265. if decryptErr != nil {
  266. logger.Log(1, "failed to decrypt message during client peer update for node ", id, decryptErr.Error())
  267. return
  268. }
  269. switch decrypted[0] {
  270. case ncutils.ACK:
  271. //do we still need this
  272. case ncutils.DONE:
  273. updateNodePeers(&currentNode)
  274. }
  275. logger.Log(1, "sent peer updates after signal received from", id)
  276. }()
  277. }
  278. func updateNodePeers(currentNode *models.Node) {
  279. if err := PublishPeerUpdate(); err != nil {
  280. logger.Log(1, "error publishing peer update ", err.Error())
  281. return
  282. }
  283. }
  284. func updateNodeMetrics(currentNode *models.Node, newMetrics *models.Metrics) bool {
  285. if newMetrics.FailoverPeers == nil {
  286. newMetrics.FailoverPeers = make(map[string]string)
  287. }
  288. oldMetrics, err := logic.GetMetrics(currentNode.ID.String())
  289. if err != nil {
  290. logger.Log(1, "error finding old metrics for node", currentNode.ID.String())
  291. return false
  292. }
  293. if oldMetrics.FailoverPeers == nil {
  294. oldMetrics.FailoverPeers = make(map[string]string)
  295. }
  296. var attachedClients []models.ExtClient
  297. if currentNode.IsIngressGateway {
  298. clients, err := logic.GetExtClientsByID(currentNode.ID.String(), currentNode.Network)
  299. if err == nil {
  300. attachedClients = clients
  301. }
  302. }
  303. if len(attachedClients) > 0 {
  304. // associate ext clients with IDs
  305. for i := range attachedClients {
  306. extMetric := newMetrics.Connectivity[attachedClients[i].PublicKey]
  307. if len(extMetric.NodeName) == 0 &&
  308. len(newMetrics.Connectivity[attachedClients[i].ClientID].NodeName) > 0 { // cover server clients
  309. extMetric = newMetrics.Connectivity[attachedClients[i].ClientID]
  310. if extMetric.TotalReceived > 0 && extMetric.TotalSent > 0 {
  311. extMetric.Connected = true
  312. }
  313. }
  314. extMetric.NodeName = attachedClients[i].ClientID
  315. delete(newMetrics.Connectivity, attachedClients[i].PublicKey)
  316. newMetrics.Connectivity[attachedClients[i].ClientID] = extMetric
  317. }
  318. }
  319. // run through metrics for each peer
  320. for k := range newMetrics.Connectivity {
  321. currMetric := newMetrics.Connectivity[k]
  322. oldMetric := oldMetrics.Connectivity[k]
  323. currMetric.TotalTime += oldMetric.TotalTime
  324. currMetric.Uptime += oldMetric.Uptime // get the total uptime for this connection
  325. if currMetric.Uptime == 0 || currMetric.TotalTime == 0 {
  326. currMetric.PercentUp = 0
  327. } else {
  328. currMetric.PercentUp = 100.0 * (float64(currMetric.Uptime) / float64(currMetric.TotalTime))
  329. }
  330. totalUpMinutes := currMetric.Uptime * ncutils.CheckInInterval
  331. currMetric.ActualUptime = time.Duration(totalUpMinutes) * time.Minute
  332. delete(oldMetrics.Connectivity, k) // remove from old data
  333. newMetrics.Connectivity[k] = currMetric
  334. }
  335. // add nodes that need failover
  336. nodes, err := logic.GetNetworkNodes(currentNode.Network)
  337. if err != nil {
  338. logger.Log(0, "failed to retrieve nodes while updating metrics")
  339. return false
  340. }
  341. for _, node := range nodes {
  342. if !newMetrics.Connectivity[node.ID.String()].Connected &&
  343. len(newMetrics.Connectivity[node.ID.String()].NodeName) > 0 &&
  344. node.Connected == true &&
  345. len(node.FailoverNode) > 0 &&
  346. !node.Failover {
  347. newMetrics.FailoverPeers[node.ID.String()] = node.FailoverNode.String()
  348. }
  349. }
  350. shouldUpdate := len(oldMetrics.FailoverPeers) == 0 && len(newMetrics.FailoverPeers) > 0
  351. for k, v := range oldMetrics.FailoverPeers {
  352. if len(newMetrics.FailoverPeers[k]) > 0 && len(v) == 0 {
  353. shouldUpdate = true
  354. }
  355. if len(v) > 0 && len(newMetrics.FailoverPeers[k]) == 0 {
  356. newMetrics.FailoverPeers[k] = v
  357. }
  358. }
  359. for k := range oldMetrics.Connectivity { // cleanup any left over data, self healing
  360. delete(newMetrics.Connectivity, k)
  361. }
  362. return shouldUpdate
  363. }