config.go 8.1 KB

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