config.go 6.2 KB

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