2
0

config.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. Network string `yaml:"network"`
  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. AllowedIPs string `yaml:"allowedips"`
  32. PostUp string `yaml:"postup"`
  33. PostDown string `yaml:"postdown"`
  34. Port int32 `yaml:"port"`
  35. KeepAlive int32 `yaml:"keepalive"`
  36. PublicKey string `yaml:"publickey"`
  37. PrivateKey string `yaml:"privatekey"`
  38. Endpoint string `yaml:"endpoint"`
  39. PostChanges string `yaml:"postchanges"`
  40. }
  41. //reading in the env file
  42. func Write(config *ClientConfig, network string) error{
  43. if network == "" {
  44. err := errors.New("No network provided. Exiting.")
  45. return err
  46. }
  47. nofile := false
  48. //home, err := homedir.Dir()
  49. _, err := os.Stat("/etc/netclient")
  50. if os.IsNotExist(err) {
  51. os.Mkdir("/etc/netclient", 744)
  52. } else if err != nil {
  53. return err
  54. }
  55. home := "/etc/netclient"
  56. if err != nil {
  57. log.Fatal(err)
  58. }
  59. file := fmt.Sprintf(home + "/netconfig-" + network)
  60. f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
  61. if err != nil {
  62. nofile = true
  63. //fmt.Println("Could not access " + home + "/netconfig, proceeding...")
  64. }
  65. defer f.Close()
  66. if !nofile {
  67. err = yaml.NewEncoder(f).Encode(config)
  68. if err != nil {
  69. fmt.Println("trouble writing file")
  70. return err
  71. }
  72. } else {
  73. newf, err := os.Create(home + "/netconfig-" + network)
  74. err = yaml.NewEncoder(newf).Encode(config)
  75. defer newf.Close()
  76. if err != nil {
  77. return err
  78. }
  79. }
  80. return err
  81. }
  82. func WriteServer(server string, accesskey string, network string) error{
  83. if network == "" {
  84. err := errors.New("No network provided. Exiting.")
  85. return err
  86. }
  87. nofile := false
  88. //home, err := homedir.Dir()
  89. _, err := os.Stat("/etc/netclient")
  90. if os.IsNotExist(err) {
  91. os.Mkdir("/etc/netclient", 744)
  92. } else if err != nil {
  93. fmt.Println("couldnt find or create /etc/netclient")
  94. return err
  95. }
  96. home := "/etc/netclient"
  97. file := fmt.Sprintf(home + "/netconfig-" + network)
  98. //f, err := os.Open(file)
  99. f, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR, 0666)
  100. //f, err := ioutil.ReadFile(file)
  101. if err != nil {
  102. fmt.Println("couldnt open netconfig-" + network)
  103. fmt.Println(err)
  104. nofile = true
  105. //err = nil
  106. return err
  107. }
  108. defer f.Close()
  109. //cfg := &ClientConfig{}
  110. var cfg ClientConfig
  111. if !nofile {
  112. fmt.Println("Writing to existing config file at " + home + "/netconfig-" + network)
  113. decoder := yaml.NewDecoder(f)
  114. err = decoder.Decode(&cfg)
  115. //err = yaml.Unmarshal(f, &cfg)
  116. if err != nil {
  117. //fmt.Println(err)
  118. //return err
  119. }
  120. f.Close()
  121. f, err = os.OpenFile(file, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
  122. if err != nil {
  123. fmt.Println("couldnt open netconfig")
  124. fmt.Println(err)
  125. nofile = true
  126. //err = nil
  127. return err
  128. }
  129. defer f.Close()
  130. if err != nil {
  131. fmt.Println("trouble opening file")
  132. fmt.Println(err)
  133. }
  134. cfg.Server.Address = server
  135. cfg.Server.AccessKey = accesskey
  136. err = yaml.NewEncoder(f).Encode(cfg)
  137. //_, err = yaml.Marshal(f, &cfg)
  138. if err != nil {
  139. fmt.Println("trouble encoding file")
  140. return err
  141. }
  142. } else {
  143. fmt.Println("Creating new config file at " + home + "/netconfig-" + network)
  144. cfg.Server.Address = server
  145. cfg.Server.AccessKey = accesskey
  146. newf, err := os.Create(home + "/netconfig-" + network)
  147. err = yaml.NewEncoder(newf).Encode(cfg)
  148. defer newf.Close()
  149. if err != nil {
  150. return err
  151. }
  152. }
  153. return err
  154. }
  155. func(config *ClientConfig) ReadConfig() {
  156. nofile := false
  157. //home, err := homedir.Dir()
  158. home := "/etc/netclient"
  159. file := fmt.Sprintf(home + "/netconfig-" + config.Network)
  160. //f, err := os.Open(file)
  161. f, err := os.OpenFile(file, os.O_RDONLY, 0666)
  162. if err != nil {
  163. fmt.Println("trouble opening file")
  164. fmt.Println(err)
  165. nofile = true
  166. //fmt.Println("Could not access " + home + "/.netconfig, proceeding...")
  167. }
  168. defer f.Close()
  169. //var cfg ClientConfig
  170. if !nofile {
  171. decoder := yaml.NewDecoder(f)
  172. err = decoder.Decode(&config)
  173. if err != nil {
  174. fmt.Println("no config or invalid")
  175. fmt.Println(err)
  176. log.Fatal(err)
  177. } else {
  178. //config = cfg
  179. }
  180. }
  181. }
  182. func ReadConfig(network string) (*ClientConfig, error) {
  183. if network == "" {
  184. err := errors.New("No network provided. Exiting.")
  185. return nil, err
  186. }
  187. nofile := false
  188. //home, err := homedir.Dir()
  189. home := "/etc/netclient"
  190. file := fmt.Sprintf(home + "/netconfig-" + network)
  191. f, err := os.Open(file)
  192. if err != nil {
  193. nofile = true
  194. }
  195. defer f.Close()
  196. var cfg ClientConfig
  197. if !nofile {
  198. decoder := yaml.NewDecoder(f)
  199. err = decoder.Decode(&cfg)
  200. if err != nil {
  201. fmt.Println("trouble decoding file")
  202. return nil, err
  203. }
  204. }
  205. return &cfg, err
  206. }
  207. /*
  208. func init() {
  209. Config = readConfig()
  210. }
  211. */