config.go 9.5 KB

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