You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.1KB

  1. package main
  2. /*
  3. This is a small utility that is designed to accompany the vyatta-yggdrasil
  4. package. It takes a HJSON configuration file, makes changes to it based on
  5. the command line arguments, and then spits out an updated file.
  6. */
  7. import (
  8. "bytes"
  9. "encoding/json"
  10. "flag"
  11. "fmt"
  12. "io/ioutil"
  13. "strconv"
  14. "github.com/hjson/hjson-go"
  15. "golang.org/x/text/encoding/unicode"
  16. "github.com/yggdrasil-network/yggdrasil-go/src/config"
  17. )
  18. type nodeConfig = config.NodeConfig
  19. func main() {
  20. useconffile := flag.String("useconffile", "/etc/yggdrasil.conf", "update config at specified file path")
  21. flag.Parse()
  22. cfg := nodeConfig{}
  23. var config []byte
  24. var err error
  25. config, err = ioutil.ReadFile(*useconffile)
  26. if err != nil {
  27. panic(err)
  28. }
  29. if bytes.Compare(config[0:2], []byte{0xFF, 0xFE}) == 0 ||
  30. bytes.Compare(config[0:2], []byte{0xFE, 0xFF}) == 0 {
  31. utf := unicode.UTF16(unicode.BigEndian, unicode.UseBOM)
  32. decoder := utf.NewDecoder()
  33. config, err = decoder.Bytes(config)
  34. if err != nil {
  35. panic(err)
  36. }
  37. }
  38. var dat map[string]interface{}
  39. if err := hjson.Unmarshal(config, &dat); err != nil {
  40. panic(err)
  41. }
  42. confJson, err := json.Marshal(dat)
  43. if err != nil {
  44. panic(err)
  45. }
  46. json.Unmarshal(confJson, &cfg)
  47. switch flag.Arg(0) {
  48. case "setMTU":
  49. cfg.IfMTU, err = strconv.Atoi(flag.Arg(1))
  50. if err != nil {
  51. cfg.IfMTU = 1280
  52. }
  53. if mtu, _ := strconv.Atoi(flag.Arg(1)); mtu < 1280 {
  54. cfg.IfMTU = 1280
  55. }
  56. case "setIfName":
  57. cfg.IfName = flag.Arg(1)
  58. case "setListen":
  59. cfg.Listen = flag.Arg(1)
  60. case "setAdminListen":
  61. cfg.AdminListen = flag.Arg(1)
  62. case "setIfTapMode":
  63. if flag.Arg(1) == "true" {
  64. cfg.IfTAPMode = true
  65. } else {
  66. cfg.IfTAPMode = false
  67. }
  68. case "addPeer":
  69. found := false
  70. for _, v := range cfg.Peers {
  71. if v == flag.Arg(1) {
  72. found = true
  73. }
  74. }
  75. if !found {
  76. cfg.Peers = append(cfg.Peers, flag.Arg(1))
  77. }
  78. case "removePeer":
  79. for k, v := range cfg.Peers {
  80. if v == flag.Arg(1) {
  81. cfg.Peers = append(cfg.Peers[:k], cfg.Peers[k+1:]...)
  82. }
  83. }
  84. }
  85. bs, err := hjson.Marshal(cfg)
  86. if err != nil {
  87. panic(err)
  88. }
  89. fmt.Println(string(bs))
  90. return
  91. }