config.go 6.0 KB

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