config.go 6.1 KB

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