config.go 9.7 KB

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