daemon.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package functions
  2. import (
  3. "context"
  4. "encoding/json"
  5. "log"
  6. "os"
  7. "os/signal"
  8. "strings"
  9. "syscall"
  10. "time"
  11. mqtt "github.com/eclipse/paho.mqtt.golang"
  12. "github.com/gravitl/netmaker/netclient/config"
  13. "github.com/gravitl/netmaker/netclient/ncutils"
  14. "golang.zx2c4.com/wireguard/wgctrl"
  15. )
  16. // Daemon runs netclient daemon from command line
  17. func Daemon() error {
  18. ctx, cancel := context.WithCancel(context.Background())
  19. networks, err := ncutils.GetSystemNetworks()
  20. if err != nil {
  21. cancel()
  22. return err
  23. }
  24. for _, network := range networks {
  25. go Netclient(ctx, network)
  26. }
  27. quit := make(chan os.Signal, 1)
  28. signal.Notify(quit, syscall.SIGTERM, os.Interrupt)
  29. <-quit
  30. cancel()
  31. ncutils.Log("all done")
  32. return nil
  33. }
  34. // SetupMQTT creates a connection to broker and return client
  35. func SetupMQTT(cfg config.ClientConfig) mqtt.Client {
  36. opts := mqtt.NewClientOptions()
  37. ncutils.Log("setting broker to " + cfg.Server.CoreDNSAddr + ":1883")
  38. opts.AddBroker(cfg.Server.CoreDNSAddr + ":1883")
  39. opts.SetDefaultPublishHandler(All)
  40. client := mqtt.NewClient(opts)
  41. if token := client.Connect(); token.Wait() && token.Error() != nil {
  42. log.Fatal(token.Error())
  43. }
  44. return client
  45. }
  46. // Netclient sets up Message Queue and subsribes/publishes updates to/from server
  47. func Netclient(ctx context.Context, network string) {
  48. ncutils.Log("netclient go routine started for " + network)
  49. var cfg config.ClientConfig
  50. cfg.Network = network
  51. cfg.ReadConfig()
  52. //fix NodeID to remove ### so NodeID can be used as message topic
  53. //remove with GRA-73
  54. cfg.Node.ID = strings.Replace(cfg.Node.ID, "###", "-", 1)
  55. ncutils.Log("daemon started for network:" + network)
  56. client := SetupMQTT(cfg)
  57. if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil {
  58. log.Fatal(token.Error())
  59. }
  60. client.AddRoute("update/"+cfg.Node.ID, NodeUpdate)
  61. client.AddRoute("update/peers/"+cfg.Node.ID, UpdatePeers)
  62. client.AddRoute("update/keys/"+cfg.Node.ID, UpdateKeys)
  63. defer client.Disconnect(250)
  64. go Checkin(ctx, cfg, network)
  65. go Metrics(ctx, cfg, network)
  66. <-ctx.Done()
  67. ncutils.Log("shutting down daemon")
  68. }
  69. // All -- mqtt message hander for all ('#') topics
  70. var All mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
  71. ncutils.Log("Topic: " + string(msg.Topic()))
  72. ncutils.Log("Message: " + string(msg.Payload()))
  73. }
  74. // NodeUpdate -- mqtt message handler for /update/<NodeID> topic
  75. var NodeUpdate mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
  76. ncutils.Log("received message to update node " + string(msg.Payload()))
  77. }
  78. // UpdatePeers -- mqtt message handler for /update/peers/<NodeID> topic
  79. var UpdatePeers mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
  80. ncutils.Log("received message to update peers " + string(msg.Payload()))
  81. }
  82. // UpdateKeys -- mqtt message handler for /update/keys/<NodeID> topic
  83. var UpdateKeys mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
  84. ncutils.Log("received message to update keys " + string(msg.Payload()))
  85. }
  86. // Checkin -- go routine that checks for public or local ip changes, publishes changes
  87. // if there are no updates, simply "pings" the server as a checkin
  88. func Checkin(ctx context.Context, cfg config.ClientConfig, network string) {
  89. for {
  90. select {
  91. case <-ctx.Done():
  92. ncutils.Log("Checkin cancelled")
  93. return
  94. //delay should be configuraable -> use cfg.Node.NetworkSettings.DefaultCheckInInterval ??
  95. case <-time.After(time.Second * 10):
  96. ncutils.Log("Checkin running")
  97. if cfg.Node.Roaming == "yes" && cfg.Node.IsStatic != "yes" {
  98. extIP, err := ncutils.GetPublicIP()
  99. if err != nil {
  100. ncutils.PrintLog("error encountered checking ip addresses: "+err.Error(), 1)
  101. }
  102. if cfg.Node.Endpoint != extIP && extIP != "" {
  103. ncutils.PrintLog("endpoint has changed from "+cfg.Node.Endpoint+" to "+extIP, 1)
  104. UpdateEndpoint(cfg, network, extIP)
  105. }
  106. intIP, err := getPrivateAddr()
  107. if err != nil {
  108. ncutils.PrintLog("error encountered checking ip addresses: "+err.Error(), 1)
  109. }
  110. if cfg.Node.LocalAddress != intIP && intIP != "" {
  111. ncutils.PrintLog("local Address has changed from "+cfg.Node.LocalAddress+" to "+intIP, 1)
  112. UpdateLocalAddress(cfg, network, intIP)
  113. }
  114. } else {
  115. localIP, err := ncutils.GetLocalIP(cfg.Node.LocalRange)
  116. if err != nil {
  117. ncutils.PrintLog("error encountered checking ip addresses: "+err.Error(), 1)
  118. }
  119. if cfg.Node.Endpoint != localIP && localIP != "" {
  120. ncutils.PrintLog("endpoint has changed from "+cfg.Node.Endpoint+" to "+localIP, 1)
  121. UpdateEndpoint(cfg, network, localIP)
  122. }
  123. }
  124. Hello(cfg, network)
  125. ncutils.Log("Checkin complete")
  126. }
  127. }
  128. }
  129. // UpdateEndpoint -- publishes an endpoint update to broker
  130. func UpdateEndpoint(cfg config.ClientConfig, network, ip string) {
  131. ncutils.Log("Updating endpoint")
  132. client := SetupMQTT(cfg)
  133. if token := client.Publish("update/ip/"+cfg.Node.ID, 0, false, ip); token.Wait() && token.Error() != nil {
  134. ncutils.Log("error publishing endpoint update " + token.Error().Error())
  135. }
  136. client.Disconnect(250)
  137. }
  138. // UpdateLocalAddress -- publishes a local address update to broker
  139. func UpdateLocalAddress(cfg config.ClientConfig, network, ip string) {
  140. ncutils.Log("Updating local address")
  141. client := SetupMQTT(cfg)
  142. if token := client.Publish("update/localaddress/"+cfg.Node.ID, 0, false, ip); token.Wait() && token.Error() != nil {
  143. ncutils.Log("error publishing local address update " + token.Error().Error())
  144. }
  145. client.Disconnect(250)
  146. }
  147. // Hello -- ping the broker to let server know node is alive and doing fine
  148. func Hello(cfg config.ClientConfig, network string) {
  149. client := SetupMQTT(cfg)
  150. if token := client.Publish("ping/"+cfg.Node.ID, 0, false, "hello world!"); token.Wait() && token.Error() != nil {
  151. ncutils.Log("error publishing ping " + token.Error().Error())
  152. }
  153. client.Disconnect(250)
  154. }
  155. // Metics -- go routine that collects wireguard metrics and publishes to broker
  156. func Metrics(ctx context.Context, cfg config.ClientConfig, network string) {
  157. for {
  158. select {
  159. case <-ctx.Done():
  160. ncutils.Log("Metrics collection cancelled")
  161. return
  162. //delay should be configuraable -> use cfg.Node.NetworkSettings.DefaultCheckInInterval ??
  163. case <-time.After(time.Second * 60):
  164. ncutils.Log("Metrics collection running")
  165. ncutils.Log("Metrics running")
  166. wg, err := wgctrl.New()
  167. if err != nil {
  168. ncutils.Log("error getting devices " + err.Error())
  169. break
  170. }
  171. device, err := wg.Device(cfg.Node.Interface)
  172. if err != nil {
  173. ncutils.Log("error readind wg device " + err.Error())
  174. break
  175. }
  176. bytes, err := json.Marshal(device.Peers)
  177. if err != nil {
  178. ncutils.Log("error marshaling peers " + err.Error())
  179. break
  180. }
  181. client := SetupMQTT(cfg)
  182. if token := client.Publish("metrics/"+cfg.Node.ID, 1, false, bytes); token.Wait() && token.Error() != nil {
  183. ncutils.Log("error publishing metrics " + token.Error().Error())
  184. }
  185. wg.Close()
  186. client.Disconnect(250)
  187. ncutils.Log("metrics collection complete")
  188. }
  189. }
  190. }