config.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. package config
  2. import (
  3. //"github.com/davecgh/go-spew/spew"
  4. "encoding/base64"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io/ioutil"
  9. "log"
  10. "os"
  11. "github.com/gravitl/netmaker/models"
  12. "github.com/gravitl/netmaker/netclient/ncutils"
  13. "github.com/urfave/cli/v2"
  14. "gopkg.in/yaml.v3"
  15. )
  16. // GlobalConfig - struct for handling IntClients currently
  17. type GlobalConfig struct {
  18. GRPCWireGuard string `yaml:"grpcwg"`
  19. Client models.IntClient
  20. }
  21. // ClientConfig - struct for dealing with client configuration
  22. type ClientConfig struct {
  23. Server ServerConfig `yaml:"server"`
  24. Node models.Node `yaml:"node"`
  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. // WriteServer - writes the config of a server to disk for client
  66. func WriteServer(server string, accesskey string, network string) error {
  67. if network == "" {
  68. err := errors.New("no network provided - exiting")
  69. return err
  70. }
  71. nofile := false
  72. //home, err := homedir.Dir()
  73. _, err := os.Stat(ncutils.GetNetclientPath() + "/config")
  74. if os.IsNotExist(err) {
  75. os.MkdirAll(ncutils.GetNetclientPath()+"/config", 0744)
  76. } else if err != nil {
  77. fmt.Println("couldnt find or create", ncutils.GetNetclientPath())
  78. return err
  79. }
  80. home := ncutils.GetNetclientPathSpecific()
  81. file := fmt.Sprintf(home + "netconfig-" + network)
  82. //f, err := os.Open(file)
  83. f, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR, 0666)
  84. //f, err := ioutil.ReadFile(file)
  85. if err != nil {
  86. fmt.Println("couldnt open netconfig-" + network)
  87. fmt.Println(err)
  88. nofile = true
  89. //err = nil
  90. return err
  91. }
  92. defer f.Close()
  93. //cfg := &ClientConfig{}
  94. var cfg ClientConfig
  95. if !nofile {
  96. fmt.Println("Writing to existing config file at " + home + "netconfig-" + network)
  97. decoder := yaml.NewDecoder(f)
  98. err = decoder.Decode(&cfg)
  99. //err = yaml.Unmarshal(f, &cfg)
  100. if err != nil {
  101. //fmt.Println(err)
  102. //return err
  103. }
  104. f.Close()
  105. f, err = os.OpenFile(file, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
  106. if err != nil {
  107. fmt.Println("couldnt open netconfig")
  108. fmt.Println(err)
  109. nofile = true
  110. //err = nil
  111. return err
  112. }
  113. defer f.Close()
  114. if err != nil {
  115. fmt.Println("trouble opening file")
  116. fmt.Println(err)
  117. }
  118. cfg.Server.GRPCAddress = server
  119. cfg.Server.AccessKey = accesskey
  120. err = yaml.NewEncoder(f).Encode(cfg)
  121. //_, err = yaml.Marshal(f, &cfg)
  122. if err != nil {
  123. fmt.Println("trouble encoding file")
  124. return err
  125. }
  126. } else {
  127. fmt.Println("Creating new config file at " + home + "netconfig-" + network)
  128. cfg.Server.GRPCAddress = server
  129. cfg.Server.AccessKey = accesskey
  130. newf, err := os.Create(home + "netconfig-" + network)
  131. err = yaml.NewEncoder(newf).Encode(cfg)
  132. defer newf.Close()
  133. if err != nil {
  134. return err
  135. }
  136. }
  137. return err
  138. }
  139. // ClientConfig.ReadConfig - used to read config from client disk into memory
  140. func (config *ClientConfig) ReadConfig() {
  141. nofile := false
  142. //home, err := homedir.Dir()
  143. home := ncutils.GetNetclientPathSpecific()
  144. file := fmt.Sprintf(home + "netconfig-" + config.Network)
  145. //f, err := os.Open(file)
  146. f, err := os.OpenFile(file, os.O_RDONLY, 0666)
  147. if err != nil {
  148. fmt.Println("trouble opening file")
  149. fmt.Println(err)
  150. nofile = true
  151. //fmt.Println("Could not access " + home + "/.netconfig, proceeding...")
  152. }
  153. defer f.Close()
  154. //var cfg ClientConfig
  155. if !nofile {
  156. decoder := yaml.NewDecoder(f)
  157. err = decoder.Decode(&config)
  158. if err != nil {
  159. fmt.Println("no config or invalid")
  160. fmt.Println(err)
  161. log.Fatal(err)
  162. } else {
  163. config.Node.SetID()
  164. //config = cfg
  165. }
  166. }
  167. }
  168. // ModConfig - overwrites the node inside client config on disk
  169. func ModConfig(node *models.Node) error {
  170. network := node.Network
  171. if network == "" {
  172. return errors.New("no network provided")
  173. }
  174. var modconfig ClientConfig
  175. var err error
  176. if FileExists(ncutils.GetNetclientPathSpecific() + "netconfig-" + network) {
  177. useconfig, err := ReadConfig(network)
  178. if err != nil {
  179. return err
  180. }
  181. modconfig = *useconfig
  182. }
  183. modconfig.Node = (*node)
  184. err = Write(&modconfig, network)
  185. return err
  186. }
  187. // ModConfig - overwrites the node inside client config on disk
  188. func SaveBackup(network string) error {
  189. var configPath = ncutils.GetNetclientPathSpecific() + "netconfig-" + network
  190. var backupPath = ncutils.GetNetclientPathSpecific() + "backup.netconfig-" + network
  191. if FileExists(configPath) {
  192. input, err := ioutil.ReadFile(configPath)
  193. if err != nil {
  194. ncutils.Log("failed to read " + configPath + " to make a backup")
  195. return err
  196. }
  197. if err = ioutil.WriteFile(backupPath, input, 0644); err != nil {
  198. ncutils.Log("failed to copy backup to " + backupPath)
  199. return err
  200. }
  201. }
  202. return nil
  203. }
  204. // ReplaceWithBackup - replaces netconfig file with backup
  205. func ReplaceWithBackup(network string) error {
  206. var backupPath = ncutils.GetNetclientPathSpecific() + "backup.netconfig-" + network
  207. var configPath = ncutils.GetNetclientPathSpecific() + "netconfig-" + network
  208. if FileExists(backupPath) {
  209. input, err := ioutil.ReadFile(backupPath)
  210. if err != nil {
  211. ncutils.Log("failed to read file " + backupPath + " to backup network: " + network)
  212. return err
  213. }
  214. if err = ioutil.WriteFile(configPath, input, 0644); err != nil {
  215. ncutils.Log("failed backup " + backupPath + " to " + configPath)
  216. return err
  217. }
  218. }
  219. ncutils.Log("used backup file for network: " + network)
  220. return nil
  221. }
  222. // GetCLIConfig - gets the cli flags as a config
  223. func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) {
  224. var cfg ClientConfig
  225. if c.String("token") != "" {
  226. tokenbytes, err := base64.StdEncoding.DecodeString(c.String("token"))
  227. if err != nil {
  228. log.Println("error decoding token")
  229. return cfg, "", err
  230. }
  231. var accesstoken models.AccessToken
  232. if err := json.Unmarshal(tokenbytes, &accesstoken); err != nil {
  233. log.Println("error converting token json to object", tokenbytes)
  234. return cfg, "", err
  235. }
  236. if accesstoken.ServerConfig.APIConnString != "" {
  237. cfg.Server.APIAddress = accesstoken.ServerConfig.APIConnString
  238. } else {
  239. cfg.Server.APIAddress = accesstoken.ServerConfig.APIHost
  240. if accesstoken.ServerConfig.APIPort != "" {
  241. cfg.Server.APIAddress = cfg.Server.APIAddress + ":" + accesstoken.ServerConfig.APIPort
  242. }
  243. }
  244. if accesstoken.ServerConfig.GRPCConnString != "" {
  245. cfg.Server.GRPCAddress = accesstoken.ServerConfig.GRPCConnString
  246. } else {
  247. cfg.Server.GRPCAddress = accesstoken.ServerConfig.GRPCHost
  248. if accesstoken.ServerConfig.GRPCPort != "" {
  249. cfg.Server.GRPCAddress = cfg.Server.GRPCAddress + ":" + accesstoken.ServerConfig.GRPCPort
  250. }
  251. }
  252. cfg.Network = accesstoken.ClientConfig.Network
  253. cfg.Node.Network = accesstoken.ClientConfig.Network
  254. cfg.Server.AccessKey = accesstoken.ClientConfig.Key
  255. cfg.Node.LocalRange = accesstoken.ClientConfig.LocalRange
  256. cfg.Server.GRPCSSL = accesstoken.ServerConfig.GRPCSSL
  257. cfg.Server.CheckinInterval = accesstoken.ServerConfig.CheckinInterval
  258. cfg.Server.GRPCWireGuard = accesstoken.WG.GRPCWireGuard
  259. cfg.Server.CoreDNSAddr = accesstoken.ServerConfig.CoreDNSAddr
  260. if c.String("grpcserver") != "" {
  261. cfg.Server.GRPCAddress = c.String("grpcserver")
  262. }
  263. if c.String("apiserver") != "" {
  264. cfg.Server.APIAddress = c.String("apiserver")
  265. }
  266. if c.String("key") != "" {
  267. cfg.Server.AccessKey = c.String("key")
  268. }
  269. if c.String("network") != "all" {
  270. cfg.Network = c.String("network")
  271. cfg.Node.Network = c.String("network")
  272. }
  273. if c.String("localrange") != "" {
  274. cfg.Node.LocalRange = c.String("localrange")
  275. }
  276. if c.String("grpcssl") != "" {
  277. cfg.Server.GRPCSSL = c.String("grpcssl")
  278. }
  279. if c.String("corednsaddr") != "" {
  280. cfg.Server.CoreDNSAddr = c.String("corednsaddr")
  281. }
  282. if c.String("grpcwg") != "" {
  283. cfg.Server.GRPCWireGuard = c.String("grpcwg")
  284. }
  285. if c.String("checkininterval") != "" {
  286. cfg.Server.CheckinInterval = c.String("checkininterval")
  287. }
  288. } else {
  289. cfg.Server.GRPCAddress = c.String("grpcserver")
  290. cfg.Server.APIAddress = c.String("apiserver")
  291. cfg.Server.AccessKey = c.String("key")
  292. cfg.Network = c.String("network")
  293. cfg.Node.Network = c.String("network")
  294. cfg.Node.LocalRange = c.String("localrange")
  295. cfg.Server.GRPCWireGuard = c.String("grpcwg")
  296. cfg.Server.GRPCSSL = c.String("grpcssl")
  297. cfg.Server.CoreDNSAddr = c.String("corednsaddr")
  298. cfg.Server.CheckinInterval = c.String("checkininterval")
  299. }
  300. cfg.Node.Name = c.String("name")
  301. cfg.Node.Interface = c.String("interface")
  302. cfg.Node.Password = c.String("password")
  303. cfg.Node.MacAddress = c.String("macaddress")
  304. cfg.Node.LocalAddress = c.String("localaddress")
  305. cfg.Node.Address = c.String("address")
  306. cfg.Node.Address6 = c.String("addressIPV6")
  307. cfg.Node.Roaming = c.String("roaming")
  308. cfg.Node.DNSOn = c.String("dnson")
  309. cfg.Node.IsLocal = c.String("islocal")
  310. cfg.Node.IsDualStack = c.String("isdualstack")
  311. cfg.Node.PostUp = c.String("postup")
  312. cfg.Node.PostDown = c.String("postdown")
  313. cfg.Node.ListenPort = int32(c.Int("port"))
  314. cfg.Node.PersistentKeepalive = int32(c.Int("keepalive"))
  315. cfg.Node.PublicKey = c.String("publickey")
  316. privateKey := c.String("privatekey")
  317. cfg.Node.Endpoint = c.String("endpoint")
  318. cfg.Node.IPForwarding = c.String("ipforwarding")
  319. cfg.OperatingSystem = c.String("operatingsystem")
  320. cfg.Daemon = c.String("daemon")
  321. cfg.Node.UDPHolePunch = c.String("udpholepunch")
  322. cfg.Node.MTU = int32(c.Int("mtu"))
  323. if cfg.Server.CheckinInterval == "" {
  324. cfg.Server.CheckinInterval = "15"
  325. }
  326. return cfg, privateKey, nil
  327. }
  328. // ReadConfig - reads a config of a client from disk for specified network
  329. func ReadConfig(network string) (*ClientConfig, error) {
  330. if network == "" {
  331. err := errors.New("no network provided - exiting")
  332. return nil, err
  333. }
  334. nofile := false
  335. home := ncutils.GetNetclientPathSpecific()
  336. file := fmt.Sprintf(home + "netconfig-" + network)
  337. f, err := os.Open(file)
  338. if err != nil {
  339. if err = ReplaceWithBackup(network); err != nil {
  340. nofile = true
  341. }
  342. f, err = os.Open(file)
  343. if err != nil {
  344. nofile = true
  345. }
  346. }
  347. defer f.Close()
  348. var cfg ClientConfig
  349. if !nofile {
  350. decoder := yaml.NewDecoder(f)
  351. err = decoder.Decode(&cfg)
  352. if err != nil {
  353. fmt.Println("trouble decoding file")
  354. return nil, err
  355. }
  356. }
  357. return &cfg, err
  358. }
  359. // FileExists - checks if a file exists on disk
  360. func FileExists(f string) bool {
  361. info, err := os.Stat(f)
  362. if os.IsNotExist(err) {
  363. return false
  364. }
  365. return !info.IsDir()
  366. }
  367. // GetNode - parses a network specified client config for node data
  368. func GetNode(network string) models.Node {
  369. modcfg, err := ReadConfig(network)
  370. if err != nil {
  371. log.Fatalf("Error: %v", err)
  372. }
  373. var node models.Node
  374. node.Fill(&modcfg.Node)
  375. return node
  376. }