daemon.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. ncutils.Log("recieved " + string(len(peerUpdate.Peers)) + "peers to update")
  149. ncutils.Log(string(msg.Payload()))
  150. ncutils.Log(peerUpdate.Network)
  151. for _, peer := range peerUpdate.Peers {
  152. key := peer.PublicKey.String()
  153. ncutils.Log(key)
  154. }
  155. var cfg config.ClientConfig
  156. cfg.Network = peerUpdate.Network
  157. cfg.ReadConfig()
  158. err = wireguard.UpdateWgPeers(cfg.Node.Interface, peerUpdate.Peers)
  159. if err != nil {
  160. ncutils.Log("error updating wireguard peers" + err.Error())
  161. return
  162. }
  163. file := ncutils.GetNetclientPathSpecific() + cfg.Node.Interface + ".conf"
  164. ncutils.Log("applyWGQuickConf to " + file)
  165. err = wireguard.ApplyWGQuickConf(file)
  166. if err != nil {
  167. ncutils.Log("error restarting wg after peer update " + err.Error())
  168. return
  169. }
  170. }()
  171. }
  172. // UpdateKeys -- updates private key and returns new publickey
  173. func UpdateKeys(cfg *config.ClientConfig, client mqtt.Client) (*config.ClientConfig, error) {
  174. ncutils.Log("received message to update keys")
  175. //potentiall blocking i/o so do this in a go routine
  176. key, err := wgtypes.GeneratePrivateKey()
  177. if err != nil {
  178. ncutils.Log("error generating privatekey " + err.Error())
  179. return cfg, err
  180. }
  181. if err := wireguard.UpdatePrivateKey(cfg.Node.Interface, key.String()); err != nil {
  182. ncutils.Log("error updating wireguard key " + err.Error())
  183. return cfg, err
  184. }
  185. publicKey := key.PublicKey()
  186. if token := client.Publish("update/publickey/"+cfg.Node.ID, 0, false, publicKey.String()); token.Wait() && token.Error() != nil {
  187. ncutils.Log("error publishing publickey update " + token.Error().Error())
  188. client.Disconnect(250)
  189. return cfg, err
  190. }
  191. if err := config.ModConfig(&cfg.Node); err != nil {
  192. ncutils.Log("error updating local config " + err.Error())
  193. }
  194. return cfg, nil
  195. }
  196. // Checkin -- go routine that checks for public or local ip changes, publishes changes
  197. // if there are no updates, simply "pings" the server as a checkin
  198. func Checkin(ctx context.Context, cfg config.ClientConfig, network string) {
  199. for {
  200. select {
  201. case <-ctx.Done():
  202. ncutils.Log("Checkin cancelled")
  203. return
  204. //delay should be configuraable -> use cfg.Node.NetworkSettings.DefaultCheckInInterval ??
  205. case <-time.After(time.Second * 60):
  206. ncutils.Log("Checkin running")
  207. //read latest config
  208. cfg.ReadConfig()
  209. if cfg.Node.Roaming == "yes" && cfg.Node.IsStatic != "yes" {
  210. extIP, err := ncutils.GetPublicIP()
  211. if err != nil {
  212. ncutils.PrintLog("error encountered checking ip addresses: "+err.Error(), 1)
  213. }
  214. if cfg.Node.Endpoint != extIP && extIP != "" {
  215. ncutils.PrintLog("endpoint has changed from "+cfg.Node.Endpoint+" to "+extIP, 1)
  216. UpdateEndpoint(cfg, network, extIP)
  217. }
  218. intIP, err := getPrivateAddr()
  219. if err != nil {
  220. ncutils.PrintLog("error encountered checking ip addresses: "+err.Error(), 1)
  221. }
  222. if cfg.Node.LocalAddress != intIP && intIP != "" {
  223. ncutils.PrintLog("local Address has changed from "+cfg.Node.LocalAddress+" to "+intIP, 1)
  224. UpdateLocalAddress(cfg, network, intIP)
  225. }
  226. } else {
  227. localIP, err := ncutils.GetLocalIP(cfg.Node.LocalRange)
  228. if err != nil {
  229. ncutils.PrintLog("error encountered checking ip addresses: "+err.Error(), 1)
  230. }
  231. if cfg.Node.Endpoint != localIP && localIP != "" {
  232. ncutils.PrintLog("endpoint has changed from "+cfg.Node.Endpoint+" to "+localIP, 1)
  233. UpdateEndpoint(cfg, network, localIP)
  234. }
  235. }
  236. Hello(cfg, network)
  237. ncutils.Log("Checkin complete")
  238. }
  239. }
  240. }
  241. // UpdateEndpoint -- publishes an endpoint update to broker
  242. func UpdateEndpoint(cfg config.ClientConfig, network, ip string) {
  243. ncutils.Log("Updating endpoint")
  244. client := SetupMQTT(cfg)
  245. if token := client.Publish("update/ip/"+cfg.Node.ID, 0, false, ip); token.Wait() && token.Error() != nil {
  246. ncutils.Log("error publishing endpoint update " + token.Error().Error())
  247. }
  248. cfg.Node.Endpoint = ip
  249. if err := config.Write(&cfg, cfg.Network); err != nil {
  250. ncutils.Log("error updating local config " + err.Error())
  251. }
  252. client.Disconnect(250)
  253. }
  254. // UpdateLocalAddress -- publishes a local address update to broker
  255. func UpdateLocalAddress(cfg config.ClientConfig, network, ip string) {
  256. ncutils.Log("Updating local address")
  257. client := SetupMQTT(cfg)
  258. if token := client.Publish("update/localaddress/"+cfg.Node.ID, 0, false, ip); token.Wait() && token.Error() != nil {
  259. ncutils.Log("error publishing local address update " + token.Error().Error())
  260. }
  261. cfg.Node.LocalAddress = ip
  262. ncutils.Log("updating local address in local config to: " + cfg.Node.LocalAddress)
  263. if err := config.Write(&cfg, cfg.Network); err != nil {
  264. ncutils.Log("error updating local config " + err.Error())
  265. }
  266. client.Disconnect(250)
  267. }
  268. // Hello -- ping the broker to let server know node is alive and doing fine
  269. func Hello(cfg config.ClientConfig, network string) {
  270. client := SetupMQTT(cfg)
  271. ncutils.Log("sending ping " + cfg.Node.ID)
  272. if token := client.Publish("ping/"+cfg.Node.ID, 2, false, "hello world!"); token.Wait() && token.Error() != nil {
  273. ncutils.Log("error publishing ping " + token.Error().Error())
  274. }
  275. client.Disconnect(250)
  276. }