config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package config
  2. import (
  3. "github.com/cnlh/nps/lib/common"
  4. "github.com/cnlh/nps/lib/file"
  5. "regexp"
  6. "strings"
  7. )
  8. type CommonConfig struct {
  9. Server string
  10. VKey string
  11. Tp string //bridgeType kcp or tcp
  12. AutoReconnection bool
  13. Cnf *file.Config
  14. }
  15. type Config struct {
  16. content string
  17. title []string
  18. CommonConfig *CommonConfig
  19. Hosts []*file.Host
  20. Tasks []*file.Tunnel
  21. }
  22. func NewConfig(path string) (c *Config, err error) {
  23. c = new(Config)
  24. var b []byte
  25. if b, err = common.ReadAllFromFile(path); err != nil {
  26. return
  27. } else {
  28. c.content = string(b)
  29. if c.title, err = getAllTitle(c.content); err != nil {
  30. return
  31. }
  32. var nowIndex int
  33. var nextIndex int
  34. var nowContent string
  35. for i := 0; i < len(c.title); i++ {
  36. nowIndex = strings.Index(c.content, c.title[i]) + len(c.title[i])
  37. if i < len(c.title)-1 {
  38. nextIndex = strings.Index(c.content, c.title[i+1])
  39. } else {
  40. nextIndex = len(c.content)
  41. }
  42. nowContent = c.content[nowIndex:nextIndex]
  43. switch c.title[i] {
  44. case "[common]":
  45. c.CommonConfig = dealCommon(nowContent)
  46. default:
  47. if strings.Index(nowContent, "host") > -1 {
  48. h := dealHost(nowContent)
  49. h.Remark = getTitleContent(c.title[i])
  50. c.Hosts = append(c.Hosts, h)
  51. } else {
  52. t := dealTunnel(nowContent)
  53. t.Remark = getTitleContent(c.title[i])
  54. c.Tasks = append(c.Tasks, t)
  55. }
  56. }
  57. }
  58. }
  59. return
  60. }
  61. func getTitleContent(s string) string {
  62. re, _ := regexp.Compile(`[\[\]]`)
  63. return re.ReplaceAllString(s, "")
  64. }
  65. func dealCommon(s string) *CommonConfig {
  66. c := &CommonConfig{}
  67. c.Cnf = new(file.Config)
  68. for _, v := range strings.Split(s, "\n") {
  69. item := strings.Split(v, "=")
  70. if len(item) == 0 {
  71. continue
  72. } else if len(item) == 1 {
  73. item = append(item, "")
  74. }
  75. switch item[0] {
  76. case "server":
  77. c.Server = item[1]
  78. case "vkey":
  79. c.VKey = item[1]
  80. case "tp":
  81. c.Tp = item[1]
  82. case "auto_reconnection":
  83. c.AutoReconnection = common.GetBoolByStr(item[1])
  84. case "username":
  85. c.Cnf.U = item[1]
  86. case "password":
  87. c.Cnf.P = item[1]
  88. case "compress":
  89. c.Cnf.Compress = item[1]
  90. case "crypt":
  91. c.Cnf.Crypt = common.GetBoolByStr(item[1])
  92. }
  93. }
  94. return c
  95. }
  96. func dealHost(s string) *file.Host {
  97. h := &file.Host{}
  98. var headerChange string
  99. for _, v := range strings.Split(s, "\n") {
  100. item := strings.Split(v, "=")
  101. if len(item) == 0 {
  102. continue
  103. } else if len(item) == 1 {
  104. item = append(item, "")
  105. }
  106. switch item[0] {
  107. case "host":
  108. h.Host = item[1]
  109. case "target":
  110. h.Target = strings.Replace(item[1], ",", "\n", -1)
  111. case "host_change":
  112. h.HostChange = item[1]
  113. default:
  114. if strings.Contains(item[0], "header") {
  115. headerChange += strings.Replace(item[0], "header_", "", -1) + ":" + item[1] + "\n"
  116. }
  117. h.HeaderChange = headerChange
  118. }
  119. }
  120. return h
  121. }
  122. func dealTunnel(s string) *file.Tunnel {
  123. t := &file.Tunnel{}
  124. for _, v := range strings.Split(s, "\n") {
  125. item := strings.Split(v, "=")
  126. if len(item) == 0 {
  127. continue
  128. } else if len(item) == 1 {
  129. item = append(item, "")
  130. }
  131. switch item[0] {
  132. case "port":
  133. t.Ports = item[1]
  134. case "mode":
  135. t.Mode = item[1]
  136. case "target":
  137. t.Target = item[1]
  138. }
  139. }
  140. return t
  141. }
  142. func getAllTitle(content string) (arr []string, err error) {
  143. var re *regexp.Regexp
  144. re, err = regexp.Compile(`\[.+?\]`)
  145. if err != nil {
  146. return
  147. }
  148. arr = re.FindAllString(content, -1)
  149. return
  150. }