config.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package config
  2. import (
  3. "errors"
  4. "github.com/cnlh/nps/lib/common"
  5. "github.com/cnlh/nps/lib/file"
  6. "regexp"
  7. "strings"
  8. )
  9. type CommonConfig struct {
  10. Server string
  11. VKey string
  12. Tp string //bridgeType kcp or tcp
  13. AutoReconnection bool
  14. Cnf *file.Config
  15. ProxyUrl string
  16. Client *file.Client
  17. }
  18. type LocalServer struct {
  19. Type string
  20. Port int
  21. Password string
  22. Target string
  23. }
  24. type Config struct {
  25. content string
  26. title []string
  27. CommonConfig *CommonConfig
  28. Hosts []*file.Host
  29. Tasks []*file.Tunnel
  30. Healths []*file.Health
  31. LocalServer []*LocalServer
  32. }
  33. func NewConfig(path string) (c *Config, err error) {
  34. c = new(Config)
  35. var b []byte
  36. if b, err = common.ReadAllFromFile(path); err != nil {
  37. return
  38. } else {
  39. if c.content, err = common.ParseStr(string(b)); err != nil {
  40. return nil, err
  41. }
  42. if c.title, err = getAllTitle(c.content); err != nil {
  43. return
  44. }
  45. var nowIndex int
  46. var nextIndex int
  47. var nowContent string
  48. for i := 0; i < len(c.title); i++ {
  49. nowIndex = strings.Index(c.content, c.title[i]) + len(c.title[i])
  50. if i < len(c.title)-1 {
  51. nextIndex = strings.Index(c.content, c.title[i+1])
  52. } else {
  53. nextIndex = len(c.content)
  54. }
  55. nowContent = c.content[nowIndex:nextIndex]
  56. if strings.Index(getTitleContent(c.title[i]), "secret") == 0 && !strings.Contains(nowContent, "mode") {
  57. local := delLocalService(nowContent)
  58. local.Type = "secret"
  59. c.LocalServer = append(c.LocalServer, local)
  60. continue
  61. }
  62. //except mode
  63. if strings.Index(getTitleContent(c.title[i]), "p2p") == 0 && !strings.Contains(nowContent, "mode") {
  64. local := delLocalService(nowContent)
  65. local.Type = "p2p"
  66. c.LocalServer = append(c.LocalServer, local)
  67. continue
  68. }
  69. //health set
  70. if strings.Index(getTitleContent(c.title[i]), "health") == 0 {
  71. c.Healths = append(c.Healths, dealHealth(nowContent))
  72. continue
  73. }
  74. switch c.title[i] {
  75. case "[common]":
  76. c.CommonConfig = dealCommon(nowContent)
  77. default:
  78. if strings.Index(nowContent, "host") > -1 {
  79. h := dealHost(nowContent)
  80. h.Remark = getTitleContent(c.title[i])
  81. c.Hosts = append(c.Hosts, h)
  82. } else {
  83. t := dealTunnel(nowContent)
  84. t.Remark = getTitleContent(c.title[i])
  85. c.Tasks = append(c.Tasks, t)
  86. }
  87. }
  88. }
  89. }
  90. return
  91. }
  92. func getTitleContent(s string) string {
  93. re, _ := regexp.Compile(`[\[\]]`)
  94. return re.ReplaceAllString(s, "")
  95. }
  96. func dealCommon(s string) *CommonConfig {
  97. c := &CommonConfig{}
  98. c.Cnf = new(file.Config)
  99. c.Client = file.NewClient("", true, true)
  100. for _, v := range strings.Split(s, "\n") {
  101. item := strings.Split(v, "=")
  102. if len(item) == 0 {
  103. continue
  104. } else if len(item) == 1 {
  105. item = append(item, "")
  106. }
  107. switch item[0] {
  108. case "server":
  109. c.Server = item[1]
  110. case "vkey":
  111. c.VKey = item[1]
  112. case "tp":
  113. c.Tp = item[1]
  114. case "auto_reconnection":
  115. c.AutoReconnection = common.GetBoolByStr(item[1])
  116. case "username":
  117. c.Cnf.U = item[1]
  118. case "password":
  119. c.Cnf.P = item[1]
  120. case "compress":
  121. c.Cnf.Compress = common.GetBoolByStr(item[1])
  122. case "crypt":
  123. c.Cnf.Crypt = common.GetBoolByStr(item[1])
  124. case "proxy_socks5_url":
  125. c.ProxyUrl = item[1]
  126. case "rate_limit":
  127. c.Client.RateLimit = common.GetIntNoErrByStr(item[1])
  128. case "flow_limit":
  129. c.Client.Flow.FlowLimit = int64(common.GetIntNoErrByStr(item[1]))
  130. case "max_conn":
  131. c.Client.MaxConn = common.GetIntNoErrByStr(item[1])
  132. case "remark":
  133. c.Client.Remark = item[1]
  134. }
  135. }
  136. return c
  137. }
  138. func dealHost(s string) *file.Host {
  139. h := &file.Host{}
  140. var headerChange string
  141. for _, v := range strings.Split(s, "\n") {
  142. item := strings.Split(v, "=")
  143. if len(item) == 0 {
  144. continue
  145. } else if len(item) == 1 {
  146. item = append(item, "")
  147. }
  148. switch strings.TrimSpace(item[0]) {
  149. case "host":
  150. h.Host = item[1]
  151. case "target":
  152. h.Target = strings.Replace(item[1], ",", "\n", -1)
  153. case "host_change":
  154. h.HostChange = item[1]
  155. case "schemego":
  156. h.Scheme = item[1]
  157. case "location":
  158. h.Location = item[1]
  159. default:
  160. if strings.Contains(item[0], "header") {
  161. headerChange += strings.Replace(item[0], "header_", "", -1) + ":" + item[1] + "\n"
  162. }
  163. h.HeaderChange = headerChange
  164. }
  165. }
  166. return h
  167. }
  168. func dealHealth(s string) *file.Health {
  169. h := &file.Health{}
  170. for _, v := range strings.Split(s, "\n") {
  171. item := strings.Split(v, "=")
  172. if len(item) == 0 {
  173. continue
  174. } else if len(item) == 1 {
  175. item = append(item, "")
  176. }
  177. switch strings.TrimSpace(item[0]) {
  178. case "health_check_timeout":
  179. h.HealthCheckTimeout = common.GetIntNoErrByStr(item[1])
  180. case "health_check_max_failed":
  181. h.HealthMaxFail = common.GetIntNoErrByStr(item[1])
  182. case "health_check_interval":
  183. h.HealthCheckInterval = common.GetIntNoErrByStr(item[1])
  184. case "health_http_url":
  185. h.HttpHealthUrl = item[1]
  186. case "health_check_type":
  187. h.HealthCheckType = item[1]
  188. case "health_check_target":
  189. h.HealthCheckTarget = item[1]
  190. }
  191. }
  192. return h
  193. }
  194. func dealTunnel(s string) *file.Tunnel {
  195. t := &file.Tunnel{}
  196. for _, v := range strings.Split(s, "\n") {
  197. item := strings.Split(v, "=")
  198. if len(item) == 0 {
  199. continue
  200. } else if len(item) == 1 {
  201. item = append(item, "")
  202. }
  203. switch strings.TrimSpace(item[0]) {
  204. case "port":
  205. t.Ports = item[1]
  206. case "mode":
  207. t.Mode = item[1]
  208. case "target":
  209. t.Target = strings.Replace(item[1], ",", "\n", -1)
  210. case "targetAddr":
  211. t.TargetAddr = item[1]
  212. case "password":
  213. t.Password = item[1]
  214. case "local_path":
  215. t.LocalPath = item[1]
  216. case "strip_pre":
  217. t.StripPre = item[1]
  218. }
  219. }
  220. return t
  221. }
  222. func delLocalService(s string) *LocalServer {
  223. l := new(LocalServer)
  224. for _, v := range strings.Split(s, "\n") {
  225. item := strings.Split(v, "=")
  226. if len(item) == 0 {
  227. continue
  228. } else if len(item) == 1 {
  229. item = append(item, "")
  230. }
  231. switch item[0] {
  232. case "port":
  233. l.Port = common.GetIntNoErrByStr(item[1])
  234. case "password":
  235. l.Password = item[1]
  236. case "target":
  237. l.Target = item[1]
  238. }
  239. }
  240. return l
  241. }
  242. func getAllTitle(content string) (arr []string, err error) {
  243. var re *regexp.Regexp
  244. re, err = regexp.Compile(`\[.+?\]`)
  245. if err != nil {
  246. return
  247. }
  248. arr = re.FindAllString(content, -1)
  249. m := make(map[string]bool)
  250. for _, v := range arr {
  251. if _, ok := m[v]; ok {
  252. err = errors.New("Item names are not allowed to be duplicated")
  253. return
  254. }
  255. m[v] = true
  256. }
  257. return
  258. }