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