config.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package config
  2. import (
  3. //"github.com/davecgh/go-spew/spew"
  4. "encoding/base64"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "log"
  9. "os"
  10. "github.com/gravitl/netmaker/models"
  11. "github.com/gravitl/netmaker/netclient/ncutils"
  12. "github.com/urfave/cli/v2"
  13. "gopkg.in/yaml.v3"
  14. )
  15. // ClientConfig - struct for dealing with client configuration
  16. type ClientConfig struct {
  17. Server ServerConfig `yaml:"server"`
  18. Node models.Node `yaml:"node"`
  19. NetworkSettings models.Network `yaml:"networksettings"`
  20. Network string `yaml:"network"`
  21. Daemon string `yaml:"daemon"`
  22. OperatingSystem string `yaml:"operatingsystem"`
  23. DebugOn bool `yaml:"debugon"`
  24. }
  25. // ServerConfig - struct for dealing with the server information for a netclient
  26. type ServerConfig struct {
  27. CoreDNSAddr string `yaml:"corednsaddr"`
  28. GRPCAddress string `yaml:"grpcaddress"`
  29. AccessKey string `yaml:"accesskey"`
  30. GRPCSSL string `yaml:"grpcssl"`
  31. CommsNetwork string `yaml:"commsnetwork"`
  32. }
  33. // Write - writes the config of a client to disk
  34. func Write(config *ClientConfig, network string) error {
  35. if network == "" {
  36. err := errors.New("no network provided - exiting")
  37. return err
  38. }
  39. _, err := os.Stat(ncutils.GetNetclientPath() + "/config")
  40. if os.IsNotExist(err) {
  41. os.MkdirAll(ncutils.GetNetclientPath()+"/config", 0700)
  42. } else if err != nil {
  43. return err
  44. }
  45. home := ncutils.GetNetclientPathSpecific()
  46. file := fmt.Sprintf(home + "netconfig-" + network)
  47. f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
  48. if err != nil {
  49. return err
  50. }
  51. defer f.Close()
  52. err = yaml.NewEncoder(f).Encode(config)
  53. if err != nil {
  54. return err
  55. }
  56. return f.Sync()
  57. }
  58. // ConfigFileExists - return true if config file exists
  59. func (config *ClientConfig) ConfigFileExists() bool {
  60. home := ncutils.GetNetclientPathSpecific()
  61. file := fmt.Sprintf(home + "netconfig-" + config.Network)
  62. info, err := os.Stat(file)
  63. if os.IsNotExist(err) {
  64. return false
  65. }
  66. return !info.IsDir()
  67. }
  68. // ClientConfig.ReadConfig - used to read config from client disk into memory
  69. func (config *ClientConfig) ReadConfig() {
  70. nofile := false
  71. //home, err := homedir.Dir()
  72. home := ncutils.GetNetclientPathSpecific()
  73. file := fmt.Sprintf(home + "netconfig-" + config.Network)
  74. //f, err := os.Open(file)
  75. f, err := os.OpenFile(file, os.O_RDONLY, 0600)
  76. if err != nil {
  77. ncutils.PrintLog("trouble opening file: "+err.Error(), 1)
  78. nofile = true
  79. //fmt.Println("Could not access " + home + "/.netconfig, proceeding...")
  80. }
  81. defer f.Close()
  82. //var cfg ClientConfig
  83. if !nofile {
  84. decoder := yaml.NewDecoder(f)
  85. err = decoder.Decode(&config)
  86. if err != nil {
  87. fmt.Println("no config or invalid")
  88. fmt.Println(err)
  89. log.Fatal(err)
  90. }
  91. }
  92. }
  93. // ModConfig - overwrites the node inside client config on disk
  94. func ModConfig(node *models.Node) error {
  95. network := node.Network
  96. if network == "" {
  97. return errors.New("no network provided")
  98. }
  99. var modconfig ClientConfig
  100. if FileExists(ncutils.GetNetclientPathSpecific() + "netconfig-" + network) {
  101. useconfig, err := ReadConfig(network)
  102. if err != nil {
  103. return err
  104. }
  105. modconfig = *useconfig
  106. }
  107. modconfig.Node = (*node)
  108. modconfig.NetworkSettings = node.NetworkSettings
  109. return Write(&modconfig, network)
  110. }
  111. // ModConfig - overwrites the node inside client config on disk
  112. func SaveBackup(network string) error {
  113. var configPath = ncutils.GetNetclientPathSpecific() + "netconfig-" + network
  114. var backupPath = ncutils.GetNetclientPathSpecific() + "backup.netconfig-" + network
  115. if FileExists(configPath) {
  116. input, err := os.ReadFile(configPath)
  117. if err != nil {
  118. ncutils.Log("failed to read " + configPath + " to make a backup")
  119. return err
  120. }
  121. if err = os.WriteFile(backupPath, input, 0600); err != nil {
  122. ncutils.Log("failed to copy backup to " + backupPath)
  123. return err
  124. }
  125. }
  126. return nil
  127. }
  128. // ReplaceWithBackup - replaces netconfig file with backup
  129. func ReplaceWithBackup(network string) error {
  130. var backupPath = ncutils.GetNetclientPathSpecific() + "backup.netconfig-" + network
  131. var configPath = ncutils.GetNetclientPathSpecific() + "netconfig-" + network
  132. if FileExists(backupPath) {
  133. input, err := os.ReadFile(backupPath)
  134. if err != nil {
  135. ncutils.Log("failed to read file " + backupPath + " to backup network: " + network)
  136. return err
  137. }
  138. if err = os.WriteFile(configPath, input, 0600); err != nil {
  139. ncutils.Log("failed backup " + backupPath + " to " + configPath)
  140. return err
  141. }
  142. }
  143. ncutils.Log("used backup file for network: " + network)
  144. return nil
  145. }
  146. // GetCLIConfig - gets the cli flags as a config
  147. func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) {
  148. var cfg ClientConfig
  149. if c.String("token") != "" {
  150. tokenbytes, err := base64.StdEncoding.DecodeString(c.String("token"))
  151. if err != nil {
  152. log.Println("error decoding token")
  153. return cfg, "", err
  154. }
  155. var accesstoken models.AccessToken
  156. if err := json.Unmarshal(tokenbytes, &accesstoken); err != nil {
  157. log.Println("error converting token json to object", tokenbytes)
  158. return cfg, "", err
  159. }
  160. if accesstoken.ServerConfig.GRPCConnString != "" {
  161. cfg.Server.GRPCAddress = accesstoken.ServerConfig.GRPCConnString
  162. }
  163. cfg.Network = accesstoken.ClientConfig.Network
  164. cfg.Node.Network = accesstoken.ClientConfig.Network
  165. cfg.Server.AccessKey = accesstoken.ClientConfig.Key
  166. cfg.Node.LocalRange = accesstoken.ClientConfig.LocalRange
  167. cfg.Server.GRPCSSL = accesstoken.ServerConfig.GRPCSSL
  168. cfg.Server.CommsNetwork = accesstoken.ServerConfig.CommsNetwork
  169. if c.String("grpcserver") != "" {
  170. cfg.Server.GRPCAddress = c.String("grpcserver")
  171. }
  172. if c.String("key") != "" {
  173. cfg.Server.AccessKey = c.String("key")
  174. }
  175. if c.String("network") != "all" {
  176. cfg.Network = c.String("network")
  177. cfg.Node.Network = c.String("network")
  178. }
  179. if c.String("localrange") != "" {
  180. cfg.Node.LocalRange = c.String("localrange")
  181. }
  182. if c.String("grpcssl") != "" {
  183. cfg.Server.GRPCSSL = c.String("grpcssl")
  184. }
  185. if c.String("corednsaddr") != "" {
  186. cfg.Server.CoreDNSAddr = c.String("corednsaddr")
  187. }
  188. } else {
  189. cfg.Server.GRPCAddress = c.String("grpcserver")
  190. cfg.Server.AccessKey = c.String("key")
  191. cfg.Network = c.String("network")
  192. cfg.Node.Network = c.String("network")
  193. cfg.Node.LocalRange = c.String("localrange")
  194. cfg.Server.GRPCSSL = c.String("grpcssl")
  195. cfg.Server.CoreDNSAddr = c.String("corednsaddr")
  196. }
  197. cfg.Node.Name = c.String("name")
  198. cfg.Node.Interface = c.String("interface")
  199. cfg.Node.Password = c.String("password")
  200. cfg.Node.MacAddress = c.String("macaddress")
  201. cfg.Node.LocalAddress = c.String("localaddress")
  202. cfg.Node.Address = c.String("address")
  203. cfg.Node.Address6 = c.String("addressIPV6")
  204. //cfg.Node.Roaming = c.String("roaming")
  205. cfg.Node.DNSOn = c.String("dnson")
  206. cfg.Node.IsLocal = c.String("islocal")
  207. cfg.Node.IsStatic = c.String("isstatic")
  208. cfg.Node.IsDualStack = c.String("isdualstack")
  209. cfg.Node.PostUp = c.String("postup")
  210. cfg.Node.PostDown = c.String("postdown")
  211. cfg.Node.ListenPort = int32(c.Int("port"))
  212. cfg.Node.PersistentKeepalive = int32(c.Int("keepalive"))
  213. cfg.Node.PublicKey = c.String("publickey")
  214. privateKey := c.String("privatekey")
  215. cfg.Node.Endpoint = c.String("endpoint")
  216. cfg.Node.IPForwarding = c.String("ipforwarding")
  217. cfg.OperatingSystem = c.String("operatingsystem")
  218. cfg.Daemon = c.String("daemon")
  219. cfg.Node.UDPHolePunch = c.String("udpholepunch")
  220. cfg.Node.MTU = int32(c.Int("mtu"))
  221. return cfg, privateKey, nil
  222. }
  223. // ReadConfig - reads a config of a client from disk for specified network
  224. func ReadConfig(network string) (*ClientConfig, error) {
  225. if network == "" {
  226. err := errors.New("no network provided - exiting")
  227. return nil, err
  228. }
  229. nofile := false
  230. home := ncutils.GetNetclientPathSpecific()
  231. file := fmt.Sprintf(home + "netconfig-" + network)
  232. f, err := os.Open(file)
  233. if err != nil {
  234. if err = ReplaceWithBackup(network); err != nil {
  235. nofile = true
  236. }
  237. f, err = os.Open(file)
  238. if err != nil {
  239. nofile = true
  240. }
  241. }
  242. defer f.Close()
  243. var cfg ClientConfig
  244. if !nofile {
  245. decoder := yaml.NewDecoder(f)
  246. err = decoder.Decode(&cfg)
  247. if err != nil {
  248. fmt.Println("trouble decoding file")
  249. return nil, err
  250. }
  251. }
  252. return &cfg, err
  253. }
  254. // FileExists - checks if a file exists on disk
  255. func FileExists(f string) bool {
  256. info, err := os.Stat(f)
  257. if os.IsNotExist(err) {
  258. return false
  259. }
  260. return !info.IsDir()
  261. }
  262. // GetNode - parses a network specified client config for node data
  263. func GetNode(network string) models.Node {
  264. modcfg, err := ReadConfig(network)
  265. if err != nil {
  266. log.Fatalf("Error: %v", err)
  267. }
  268. var node models.Node
  269. node.Fill(&modcfg.Node)
  270. return node
  271. }