daemon.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package functions
  2. import (
  3. "context"
  4. "encoding/json"
  5. "log"
  6. "os"
  7. "os/signal"
  8. "runtime"
  9. "syscall"
  10. "time"
  11. mqtt "github.com/eclipse/paho.mqtt.golang"
  12. "github.com/gravitl/netmaker/models"
  13. "github.com/gravitl/netmaker/netclient/config"
  14. "github.com/gravitl/netmaker/netclient/ncutils"
  15. "github.com/gravitl/netmaker/netclient/wireguard"
  16. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  17. )
  18. // Daemon runs netclient daemon from command line
  19. func Daemon() error {
  20. ctx, cancel := context.WithCancel(context.Background())
  21. networks, err := ncutils.GetSystemNetworks()
  22. if err != nil {
  23. cancel()
  24. return err
  25. }
  26. for _, network := range networks {
  27. go MessageQueue(ctx, network)
  28. }
  29. quit := make(chan os.Signal, 1)
  30. signal.Notify(quit, syscall.SIGTERM, os.Interrupt)
  31. <-quit
  32. cancel()
  33. ncutils.Log("all done")
  34. return nil
  35. }
  36. // SetupMQTT creates a connection to broker and return client
  37. func SetupMQTT(cfg config.ClientConfig) mqtt.Client {
  38. opts := mqtt.NewClientOptions()
  39. ncutils.Log("setting broker to " + cfg.Server.CoreDNSAddr + ":1883")
  40. opts.AddBroker(cfg.Server.CoreDNSAddr + ":1883")
  41. opts.SetDefaultPublishHandler(All)
  42. client := mqtt.NewClient(opts)
  43. if token := client.Connect(); token.Wait() && token.Error() != nil {
  44. log.Fatal(token.Error())
  45. }
  46. return client
  47. }
  48. // MessageQueue sets up Message Queue and subsribes/publishes updates to/from server
  49. func MessageQueue(ctx context.Context, network string) {
  50. ncutils.Log("netclient go routine started for " + network)
  51. var cfg config.ClientConfig
  52. cfg.Network = network
  53. cfg.ReadConfig()
  54. ncutils.Log("daemon started for network:" + network)
  55. client := SetupMQTT(cfg)
  56. if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil {
  57. log.Fatal(token.Error())
  58. }
  59. if token := client.Subscribe("update/"+cfg.Node.ID, 0, NodeUpdate); token.Wait() && token.Error() != nil {
  60. log.Fatal(token.Error())
  61. }
  62. if token := client.Subscribe("/update/peers/"+cfg.Node.ID, 0, UpdatePeers); token.Wait() && token.Error() != nil {
  63. log.Fatal(token.Error())
  64. }
  65. //addroute doesn't seem to work consistently
  66. //client.AddRoute("update/"+cfg.Node.ID, NodeUpdate)
  67. //client.AddRoute("update/peers/"+cfg.Node.ID, UpdatePeers)
  68. //handle key updates in node update
  69. //client.AddRoute("update/keys/"+cfg.Node.ID, UpdateKeys)
  70. defer client.Disconnect(250)
  71. go Checkin(ctx, cfg, network)
  72. <-ctx.Done()
  73. ncutils.Log("shutting down daemon")
  74. }
  75. // All -- mqtt message hander for all ('#') topics
  76. var All mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
  77. ncutils.Log("default message handler -- received message but not handling")
  78. ncutils.Log("Topic: " + string(msg.Topic()))
  79. //ncutils.Log("Message: " + string(msg.Payload()))
  80. }
  81. // NodeUpdate -- mqtt message handler for /update/<NodeID> topic
  82. var NodeUpdate mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
  83. ncutils.Log("received message to update node " + string(msg.Payload()))
  84. //potentiall blocking i/o so do this in a go routine
  85. go func() {
  86. var newNode models.Node
  87. var cfg config.ClientConfig
  88. cfg.Network = newNode.Network
  89. cfg.ReadConfig()
  90. err := json.Unmarshal(msg.Payload(), &newNode)
  91. if err != nil {
  92. ncutils.Log("error unmarshalling node update data" + err.Error())
  93. return
  94. }
  95. //check if interface name has changed if so delete.
  96. if cfg.Node.Interface != newNode.Interface {
  97. if err = wireguard.RemoveConf(cfg.Node.Interface, true); err != nil {
  98. ncutils.PrintLog("could not delete old interface "+cfg.Node.Interface+": "+err.Error(), 1)
  99. }
  100. }
  101. newNode.PullChanges = "no"
  102. //ensure that OS never changes
  103. newNode.OS = runtime.GOOS
  104. cfg.Node = newNode
  105. switch newNode.Action {
  106. case models.NODE_DELETE:
  107. if err := RemoveLocalInstance(&cfg, cfg.Network); err != nil {
  108. ncutils.PrintLog("error deleting local instance: "+err.Error(), 1)
  109. return
  110. }
  111. case models.NODE_UPDATE_KEY:
  112. UpdateKeys(&cfg, client)
  113. case models.NODE_NOOP:
  114. default:
  115. }
  116. //Save new config
  117. if err := config.Write(&cfg, cfg.Network); err != nil {
  118. ncutils.PrintLog("error updating node configuration: "+err.Error(), 1)
  119. }
  120. nameserver := cfg.Server.CoreDNSAddr
  121. privateKey, err := wireguard.RetrievePrivKey(newNode.Network)
  122. if err != nil {
  123. ncutils.Log("error reading PrivateKey " + err.Error())
  124. return
  125. }
  126. if err := wireguard.UpdateWgInterface(cfg.Node.Interface, privateKey, nameserver, newNode); err != nil {
  127. ncutils.Log("error updating wireguard config " + err.Error())
  128. return
  129. }
  130. // path hardcoded for now... should be updated
  131. err = wireguard.ApplyWGQuickConf("/etc/netclient/config/" + cfg.Node.Interface + ".conf")
  132. if err != nil {
  133. ncutils.Log("error restarting wg after node update " + err.Error())
  134. return
  135. }
  136. }()
  137. }
  138. // UpdatePeers -- mqtt message handler for /update/peers/<NodeID> topic
  139. var UpdatePeers mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
  140. go func() {
  141. var peerUpdate models.PeerUpdate
  142. err := json.Unmarshal(msg.Payload(), &peerUpdate)
  143. if err != nil {
  144. ncutils.Log("error unmarshalling peer data")
  145. return
  146. }
  147. ncutils.Log("update peer handler")
  148. var cfg config.ClientConfig
  149. cfg.Network = peerUpdate.Network
  150. cfg.ReadConfig()
  151. err = wireguard.UpdateWgPeers(cfg.Node.Interface, peerUpdate.Peers)
  152. if err != nil {
  153. ncutils.Log("error updating wireguard peers" + err.Error())
  154. return
  155. }
  156. file := ncutils.GetNetclientPathSpecific() + cfg.Node.Interface + ".conf"
  157. ncutils.Log("applyWGQuickConf to " + file)
  158. err = wireguard.ApplyWGQuickConf(file)
  159. if err != nil {
  160. ncutils.Log("error restarting wg after peer update " + err.Error())
  161. return
  162. }
  163. }()
  164. }
  165. // UpdateKeys -- updates private key and returns new publickey
  166. func UpdateKeys(cfg *config.ClientConfig, client mqtt.Client) (*config.ClientConfig, error) {
  167. ncutils.Log("received message to update keys")
  168. //potentiall blocking i/o so do this in a go routine
  169. key, err := wgtypes.GeneratePrivateKey()
  170. if err != nil {
  171. ncutils.Log("error generating privatekey " + err.Error())
  172. return cfg, err
  173. }
  174. if err := wireguard.UpdatePrivateKey(cfg.Node.Interface, key.String()); err != nil {
  175. ncutils.Log("error updating wireguard key " + err.Error())
  176. return cfg, err
  177. }
  178. publicKey := key.PublicKey()
  179. if token := client.Publish("update/publickey/"+cfg.Node.ID, 0, false, publicKey.String()); token.Wait() && token.Error() != nil {
  180. ncutils.Log("error publishing publickey update " + token.Error().Error())
  181. client.Disconnect(250)
  182. return cfg, err
  183. }
  184. if err := config.ModConfig(&cfg.Node); err != nil {
  185. ncutils.Log("error updating local config " + err.Error())
  186. }
  187. return cfg, nil
  188. }
  189. // Checkin -- go routine that checks for public or local ip changes, publishes changes
  190. // if there are no updates, simply "pings" the server as a checkin
  191. func Checkin(ctx context.Context, cfg config.ClientConfig, network string) {
  192. for {
  193. select {
  194. case <-ctx.Done():
  195. ncutils.Log("Checkin cancelled")
  196. return
  197. //delay should be configuraable -> use cfg.Node.NetworkSettings.DefaultCheckInInterval ??
  198. case <-time.After(time.Second * 60):
  199. ncutils.Log("Checkin running")
  200. //read latest config
  201. cfg.ReadConfig()
  202. if cfg.Node.Roaming == "yes" && cfg.Node.IsStatic != "yes" {
  203. extIP, err := ncutils.GetPublicIP()
  204. if err != nil {
  205. ncutils.PrintLog("error encountered checking ip addresses: "+err.Error(), 1)
  206. }
  207. if cfg.Node.Endpoint != extIP && extIP != "" {
  208. ncutils.PrintLog("endpoint has changed from "+cfg.Node.Endpoint+" to "+extIP, 1)
  209. UpdateEndpoint(cfg, network, extIP)
  210. }
  211. intIP, err := getPrivateAddr()
  212. if err != nil {
  213. ncutils.PrintLog("error encountered checking ip addresses: "+err.Error(), 1)
  214. }
  215. if cfg.Node.LocalAddress != intIP && intIP != "" {
  216. ncutils.PrintLog("local Address has changed from "+cfg.Node.LocalAddress+" to "+intIP, 1)
  217. UpdateLocalAddress(cfg, network, intIP)
  218. }
  219. } else {
  220. localIP, err := ncutils.GetLocalIP(cfg.Node.LocalRange)
  221. if err != nil {
  222. ncutils.PrintLog("error encountered checking ip addresses: "+err.Error(), 1)
  223. }
  224. if cfg.Node.Endpoint != localIP && localIP != "" {
  225. ncutils.PrintLog("endpoint has changed from "+cfg.Node.Endpoint+" to "+localIP, 1)
  226. UpdateEndpoint(cfg, network, localIP)
  227. }
  228. }
  229. Hello(cfg, network)
  230. ncutils.Log("Checkin complete")
  231. }
  232. }
  233. }
  234. // UpdateEndpoint -- publishes an endpoint update to broker
  235. func UpdateEndpoint(cfg config.ClientConfig, network, ip string) {
  236. ncutils.Log("Updating endpoint")
  237. client := SetupMQTT(cfg)
  238. if token := client.Publish("update/ip/"+cfg.Node.ID, 0, false, ip); token.Wait() && token.Error() != nil {
  239. ncutils.Log("error publishing endpoint update " + token.Error().Error())
  240. }
  241. cfg.Node.Endpoint = ip
  242. if err := config.Write(&cfg, cfg.Network); err != nil {
  243. ncutils.Log("error updating local config " + err.Error())
  244. }
  245. client.Disconnect(250)
  246. }
  247. // UpdateLocalAddress -- publishes a local address update to broker
  248. func UpdateLocalAddress(cfg config.ClientConfig, network, ip string) {
  249. ncutils.Log("Updating local address")
  250. client := SetupMQTT(cfg)
  251. if token := client.Publish("update/localaddress/"+cfg.Node.ID, 0, false, ip); token.Wait() && token.Error() != nil {
  252. ncutils.Log("error publishing local address update " + token.Error().Error())
  253. }
  254. cfg.Node.LocalAddress = ip
  255. ncutils.Log("updating local address in local config to: " + cfg.Node.LocalAddress)
  256. if err := config.Write(&cfg, cfg.Network); err != nil {
  257. ncutils.Log("error updating local config " + err.Error())
  258. }
  259. client.Disconnect(250)
  260. }
  261. // Hello -- ping the broker to let server know node is alive and doing fine
  262. func Hello(cfg config.ClientConfig, network string) {
  263. client := SetupMQTT(cfg)
  264. ncutils.Log("sending ping " + cfg.Node.ID)
  265. if token := client.Publish("ping/"+cfg.Node.ID, 2, false, "hello world!"); token.Wait() && token.Error() != nil {
  266. ncutils.Log("error publishing ping " + token.Error().Error())
  267. }
  268. client.Disconnect(250)
  269. }