checkin.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package functions
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "errors"
  6. "log"
  7. "strings"
  8. nodepb "github.com/gravitl/netmaker/grpc"
  9. "github.com/gravitl/netmaker/models"
  10. "github.com/gravitl/netmaker/netclient/auth"
  11. "github.com/gravitl/netmaker/netclient/config"
  12. "github.com/gravitl/netmaker/netclient/local"
  13. "github.com/gravitl/netmaker/netclient/wireguard"
  14. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  15. "google.golang.org/grpc"
  16. "google.golang.org/grpc/credentials"
  17. "google.golang.org/grpc/metadata"
  18. //homedir "github.com/mitchellh/go-homedir"
  19. )
  20. func isDeleteError(err error) bool {
  21. return err != nil && strings.Contains(err.Error(), models.NODE_DELETE)
  22. }
  23. func checkIP(node *models.Node, servercfg config.ServerConfig, cliconf config.ClientConfig, network string) bool {
  24. ipchange := false
  25. var err error
  26. if node.Roaming == "yes" && node.IsStatic != "yes" {
  27. if node.IsLocal == "no" {
  28. log.Println("Checking to see if public addresses have changed")
  29. extIP, err := getPublicIP()
  30. if err != nil {
  31. log.Println("error encountered checking ip addresses:", err)
  32. }
  33. if node.Endpoint != extIP && extIP != "" {
  34. log.Println("Endpoint has changed from " +
  35. node.Endpoint + " to " + extIP)
  36. log.Println("Updating address")
  37. node.Endpoint = extIP
  38. ipchange = true
  39. }
  40. intIP, err := getPrivateAddr()
  41. if err != nil {
  42. log.Println("error encountered checking ip addresses:", err)
  43. }
  44. if node.LocalAddress != intIP && intIP != "" {
  45. log.Println("Local Address has changed from " +
  46. node.LocalAddress + " to " + intIP)
  47. log.Println("Updating address")
  48. node.LocalAddress = intIP
  49. ipchange = true
  50. }
  51. } else {
  52. log.Println("Checking to see if local addresses have changed")
  53. localIP, err := getLocalIP(node.LocalRange)
  54. if err != nil {
  55. log.Println("error encountered checking ip addresses:", err)
  56. }
  57. if node.Endpoint != localIP && localIP != "" {
  58. log.Println("Endpoint has changed from " +
  59. node.Endpoint + " to " + localIP)
  60. log.Println("Updating address")
  61. node.Endpoint = localIP
  62. node.LocalAddress = localIP
  63. ipchange = true
  64. }
  65. }
  66. }
  67. if ipchange {
  68. err = config.ModConfig(node)
  69. if err != nil {
  70. log.Println("Error:", err)
  71. return false
  72. }
  73. err = wireguard.SetWGConfig(network, false)
  74. if err != nil {
  75. log.Println("Error:", err)
  76. return false
  77. }
  78. }
  79. return ipchange && err == nil
  80. }
  81. func setDNS(node *models.Node, servercfg config.ServerConfig, nodecfg *models.Node) {
  82. if nodecfg.DNSOn == "yes" {
  83. log.Println("setting dns")
  84. ifacename := node.Interface
  85. nameserver := servercfg.CoreDNSAddr
  86. network := node.Network
  87. _ = local.UpdateDNS(ifacename, network, nameserver)
  88. }
  89. }
  90. func checkNodeActions(node *models.Node, networkName string, servercfg config.ServerConfig, localNode *models.Node, cfg *config.ClientConfig) string {
  91. if (node.Action == models.NODE_UPDATE_KEY || localNode.Action == models.NODE_UPDATE_KEY) &&
  92. node.IsStatic != "yes" {
  93. err := wireguard.SetWGKeyConfig(networkName, servercfg.GRPCAddress)
  94. if err != nil {
  95. log.Println("Unable to process reset keys request:", err)
  96. return ""
  97. }
  98. }
  99. if node.Action == models.NODE_DELETE || localNode.Action == models.NODE_DELETE {
  100. err := RemoveLocalInstance(cfg, networkName)
  101. if err != nil {
  102. log.Println("Error:", err)
  103. }
  104. return models.NODE_DELETE
  105. }
  106. return ""
  107. }
  108. /**
  109. * Pull changes if any (interface refresh)
  110. * - Save it
  111. * Check local changes for (ipAddress, publickey, configfile changes) (interface refresh)
  112. * - Save it
  113. * - Push it
  114. * Pull Peers (sync)
  115. */
  116. func CheckConfig(cliconf config.ClientConfig) error {
  117. network := cliconf.Network
  118. cfg, err := config.ReadConfig(network)
  119. if err != nil {
  120. return err
  121. }
  122. servercfg := cfg.Server
  123. currentNode := cfg.Node
  124. newNode, err := Pull(network, false)
  125. if isDeleteError(err) {
  126. return RemoveLocalInstance(cfg, network)
  127. }
  128. if err != nil {
  129. return err
  130. }
  131. if newNode.IsPending == "yes" {
  132. return errors.New("node is pending")
  133. }
  134. actionCompleted := checkNodeActions(newNode, network, servercfg, &currentNode, cfg)
  135. if actionCompleted == models.NODE_DELETE {
  136. return errors.New("node has been removed")
  137. }
  138. // Check if ip changed and push if so
  139. checkIP(newNode, servercfg, cliconf, network)
  140. return Push(network)
  141. }
  142. /**
  143. * Pull the latest node from server
  144. * Perform action if necessary
  145. */
  146. func Pull(network string, manual bool) (*models.Node, error) {
  147. cfg, err := config.ReadConfig(network)
  148. node := cfg.Node
  149. if err != nil {
  150. return nil, err
  151. }
  152. servercfg := cfg.Server
  153. var header metadata.MD
  154. if cfg.Node.IPForwarding == "yes" {
  155. if err = local.SetIPForwarding(); err != nil {
  156. return nil, err
  157. }
  158. }
  159. var requestOpts grpc.DialOption
  160. requestOpts = grpc.WithInsecure()
  161. if cfg.Server.GRPCSSL == "on" {
  162. h2creds := credentials.NewTLS(&tls.Config{NextProtos: []string{"h2"}})
  163. requestOpts = grpc.WithTransportCredentials(h2creds)
  164. }
  165. conn, err := grpc.Dial(servercfg.GRPCAddress, requestOpts)
  166. if err != nil {
  167. log.Println("Cant dial GRPC server:", err)
  168. return nil, err
  169. }
  170. wcclient := nodepb.NewNodeServiceClient(conn)
  171. ctx, err := auth.SetJWT(wcclient, network)
  172. if err != nil {
  173. log.Println("Failed to authenticate:", err)
  174. return nil, err
  175. }
  176. req := &nodepb.Object{
  177. Data: node.MacAddress + "###" + node.Network,
  178. Type: nodepb.STRING_TYPE,
  179. }
  180. readres, err := wcclient.ReadNode(ctx, req, grpc.Header(&header))
  181. if err != nil {
  182. return nil, err
  183. }
  184. var resNode models.Node
  185. if err = json.Unmarshal([]byte(readres.Data), &resNode); err != nil {
  186. return nil, err
  187. }
  188. if resNode.PullChanges == "yes" || manual {
  189. // check for interface change
  190. if cfg.Node.Interface != resNode.Interface {
  191. if err = DeleteInterface(cfg.Node.Interface, cfg.Node.PostDown); err != nil {
  192. log.Println("could not delete old interface", cfg.Node.Interface)
  193. }
  194. }
  195. resNode.PullChanges = "no"
  196. if err = config.ModConfig(&resNode); err != nil {
  197. return nil, err
  198. }
  199. if err = wireguard.SetWGConfig(network, false); err != nil {
  200. return nil, err
  201. }
  202. nodeData, err := json.Marshal(&resNode)
  203. if err != nil {
  204. return &resNode, err
  205. }
  206. req := &nodepb.Object{
  207. Data: string(nodeData),
  208. Type: nodepb.NODE_TYPE,
  209. Metadata: "",
  210. }
  211. _, err = wcclient.UpdateNode(ctx, req, grpc.Header(&header))
  212. if err != nil {
  213. return &resNode, err
  214. }
  215. } else {
  216. if err = wireguard.SetWGConfig(network, true); err != nil {
  217. return nil, err
  218. }
  219. }
  220. setDNS(&resNode, servercfg, &cfg.Node)
  221. return &resNode, err
  222. }
  223. func Push(network string) error {
  224. cfg, err := config.ReadConfig(network)
  225. postnode := cfg.Node
  226. if err != nil {
  227. return err
  228. }
  229. servercfg := cfg.Server
  230. var header metadata.MD
  231. var wcclient nodepb.NodeServiceClient
  232. var requestOpts grpc.DialOption
  233. requestOpts = grpc.WithInsecure()
  234. if cfg.Server.GRPCSSL == "on" {
  235. h2creds := credentials.NewTLS(&tls.Config{NextProtos: []string{"h2"}})
  236. requestOpts = grpc.WithTransportCredentials(h2creds)
  237. }
  238. conn, err := grpc.Dial(servercfg.GRPCAddress, requestOpts)
  239. if err != nil {
  240. log.Println("Cant dial GRPC server:", err)
  241. return err
  242. }
  243. wcclient = nodepb.NewNodeServiceClient(conn)
  244. ctx, err := auth.SetJWT(wcclient, network)
  245. if err != nil {
  246. log.Println("Failed to authenticate:", err)
  247. return err
  248. }
  249. if postnode.IsPending != "yes" {
  250. privateKey, err := wireguard.RetrievePrivKey(network)
  251. if err != nil {
  252. return err
  253. }
  254. privateKeyWG, err := wgtypes.ParseKey(privateKey)
  255. if err != nil {
  256. return err
  257. }
  258. if postnode.PublicKey != privateKeyWG.PublicKey().String() {
  259. postnode.PublicKey = privateKeyWG.PublicKey().String()
  260. }
  261. }
  262. postnode.SetLastCheckIn()
  263. nodeData, err := json.Marshal(&postnode)
  264. if err != nil {
  265. return err
  266. }
  267. req := &nodepb.Object{
  268. Data: string(nodeData),
  269. Type: nodepb.NODE_TYPE,
  270. Metadata: "",
  271. }
  272. data, err := wcclient.UpdateNode(ctx, req, grpc.Header(&header))
  273. if err != nil {
  274. return err
  275. }
  276. err = json.Unmarshal([]byte(data.Data), &postnode)
  277. if err != nil {
  278. return err
  279. }
  280. err = config.ModConfig(&postnode)
  281. return err
  282. }