config.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package config
  2. import (
  3. // "github.com/davecgh/go-spew/spew"
  4. "os"
  5. "fmt"
  6. "log"
  7. "gopkg.in/yaml.v3"
  8. //homedir "github.com/mitchellh/go-homedir"
  9. )
  10. var Config *ClientConfig
  11. // Configurations exported
  12. type ClientConfig struct {
  13. Server ServerConfig `yaml:"server"`
  14. Node NodeConfig `yaml:"node"`
  15. }
  16. type ServerConfig struct {
  17. Address string `yaml:"address"`
  18. AccessKey string `yaml:"accesskey"`
  19. }
  20. type NodeConfig struct {
  21. Name string `yaml:"name"`
  22. Interface string `yaml:"interface"`
  23. Group string `yaml:"group"`
  24. Password string `yaml:"password"`
  25. MacAddress string `yaml:"macaddress"`
  26. LocalAddress string `yaml:"localaddress"`
  27. WGAddress string `yaml:"wgaddress"`
  28. PostUp string `yaml:"postup"`
  29. PreUp string `yaml:"preup"`
  30. Port int32 `yaml:"port"`
  31. KeepAlive int32 `yaml:"keepalive"`
  32. PublicKey string `yaml:"publickey"`
  33. PrivateKey string `yaml:"privatekey"`
  34. Endpoint string `yaml:"endpoint"`
  35. PostChanges string `yaml:"postchanges"`
  36. }
  37. //reading in the env file
  38. func Write(config *ClientConfig) error{
  39. nofile := false
  40. //home, err := homedir.Dir()
  41. _, err := os.Stat("/etc/netclient")
  42. if os.IsNotExist(err) {
  43. os.Mkdir("/etc/netclient", 744)
  44. } else if err != nil {
  45. return err
  46. }
  47. home := "/etc/netclient"
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. file := fmt.Sprintf(home + "/.netconfig")
  52. f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
  53. if err != nil {
  54. nofile = true
  55. //fmt.Println("Could not access " + home + "/.netconfig, proceeding...")
  56. }
  57. defer f.Close()
  58. if !nofile {
  59. err = yaml.NewEncoder(f).Encode(config)
  60. if err != nil {
  61. fmt.Println("trouble writing file")
  62. return err
  63. }
  64. } else {
  65. newf, err := os.Create(home + "/.netconfig")
  66. err = yaml.NewEncoder(newf).Encode(config)
  67. defer newf.Close()
  68. if err != nil {
  69. return err
  70. }
  71. }
  72. return err
  73. }
  74. func WriteServer(server string, accesskey string) error{
  75. nofile := false
  76. //home, err := homedir.Dir()
  77. _, err := os.Stat("/etc/netclient")
  78. if os.IsNotExist(err) {
  79. os.Mkdir("/etc/netclient", 744)
  80. } else if err != nil {
  81. fmt.Println("couldnt find or create /etc/netclient")
  82. return err
  83. }
  84. home := "/etc/netclient"
  85. file := fmt.Sprintf(home + "/.netconfig")
  86. //f, err := os.Open(file)
  87. f, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR, 0666)
  88. //f, err := ioutil.ReadFile(file)
  89. if err != nil {
  90. fmt.Println("couldnt open netconfig")
  91. fmt.Println(err)
  92. nofile = true
  93. //err = nil
  94. return err
  95. }
  96. defer f.Close()
  97. //cfg := &ClientConfig{}
  98. var cfg ClientConfig
  99. if !nofile {
  100. fmt.Println("Writing to existing config file at " + home + "/.netconfig")
  101. decoder := yaml.NewDecoder(f)
  102. err = decoder.Decode(&cfg)
  103. //err = yaml.Unmarshal(f, &cfg)
  104. if err != nil {
  105. //fmt.Println(err)
  106. //return err
  107. }
  108. f.Close()
  109. f, err = os.OpenFile(file, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
  110. if err != nil {
  111. fmt.Println("couldnt open netconfig")
  112. fmt.Println(err)
  113. nofile = true
  114. //err = nil
  115. return err
  116. }
  117. defer f.Close()
  118. if err != nil {
  119. fmt.Println("trouble opening file")
  120. fmt.Println(err)
  121. }
  122. cfg.Server.Address = server
  123. cfg.Server.AccessKey = accesskey
  124. err = yaml.NewEncoder(f).Encode(cfg)
  125. //_, err = yaml.Marshal(f, &cfg)
  126. if err != nil {
  127. fmt.Println("trouble encoding file")
  128. return err
  129. }
  130. } else {
  131. fmt.Println("Creating new config file at " + home + "/.netconfig")
  132. cfg.Server.Address = server
  133. cfg.Server.AccessKey = accesskey
  134. newf, err := os.Create(home + "/.netconfig")
  135. err = yaml.NewEncoder(newf).Encode(cfg)
  136. defer newf.Close()
  137. if err != nil {
  138. return err
  139. }
  140. }
  141. return err
  142. }
  143. func(config *ClientConfig) ReadConfig() {
  144. nofile := false
  145. //home, err := homedir.Dir()
  146. home := "/etc/netclient"
  147. file := fmt.Sprintf(home + "/.netconfig")
  148. //f, err := os.Open(file)
  149. f, err := os.OpenFile(file, os.O_RDONLY, 0666)
  150. if err != nil {
  151. fmt.Println("trouble opening file")
  152. fmt.Println(err)
  153. nofile = true
  154. //fmt.Println("Could not access " + home + "/.netconfig, proceeding...")
  155. }
  156. defer f.Close()
  157. //var cfg ClientConfig
  158. if !nofile {
  159. decoder := yaml.NewDecoder(f)
  160. err = decoder.Decode(&config)
  161. if err != nil {
  162. fmt.Println("no config or invalid")
  163. fmt.Println(err)
  164. log.Fatal(err)
  165. } else {
  166. //config = cfg
  167. }
  168. }
  169. }
  170. func readConfig() *ClientConfig {
  171. nofile := false
  172. //home, err := homedir.Dir()
  173. home := "/etc/netclient"
  174. file := fmt.Sprintf(home + "/.netconfig")
  175. f, err := os.Open(file)
  176. if err != nil {
  177. nofile = true
  178. fmt.Println("Could not access " + home + "/.netconfig, proceeding...")
  179. }
  180. defer f.Close()
  181. var cfg ClientConfig
  182. if !nofile {
  183. decoder := yaml.NewDecoder(f)
  184. err = decoder.Decode(&cfg)
  185. if err != nil {
  186. fmt.Println("trouble decoding file")
  187. log.Fatal(err)
  188. }
  189. }
  190. return &cfg
  191. }
  192. func init() {
  193. Config = readConfig()
  194. }