parse.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2021 The frp Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package config
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. )
  22. func ParseClientConfig(filePath string) (
  23. cfg ClientCommonConf,
  24. pxyCfgs map[string]ProxyConf,
  25. visitorCfgs map[string]VisitorConf,
  26. err error,
  27. ) {
  28. var content []byte
  29. content, err = GetRenderedConfFromFile(filePath)
  30. if err != nil {
  31. return
  32. }
  33. configBuffer := bytes.NewBuffer(nil)
  34. configBuffer.Write(content)
  35. // Parse common section.
  36. cfg, err = UnmarshalClientConfFromIni(content)
  37. if err != nil {
  38. return
  39. }
  40. cfg.Complete()
  41. if err = cfg.Validate(); err != nil {
  42. err = fmt.Errorf("Parse config error: %v", err)
  43. return
  44. }
  45. // Aggregate proxy configs from include files.
  46. var buf []byte
  47. buf, err = getIncludeContents(cfg.IncludeConfigFiles)
  48. if err != nil {
  49. err = fmt.Errorf("getIncludeContents error: %v", err)
  50. return
  51. }
  52. configBuffer.WriteString("\n")
  53. configBuffer.Write(buf)
  54. // Parse all proxy and visitor configs.
  55. pxyCfgs, visitorCfgs, err = LoadAllProxyConfsFromIni(cfg.User, configBuffer.Bytes(), cfg.Start)
  56. if err != nil {
  57. return
  58. }
  59. return
  60. }
  61. // getIncludeContents renders all configs from paths.
  62. // files format can be a single file path or directory or regex path.
  63. func getIncludeContents(paths []string) ([]byte, error) {
  64. out := bytes.NewBuffer(nil)
  65. for _, path := range paths {
  66. absDir, err := filepath.Abs(filepath.Dir(path))
  67. if err != nil {
  68. return nil, err
  69. }
  70. if _, err := os.Stat(absDir); os.IsNotExist(err) {
  71. return nil, err
  72. }
  73. files, err := ioutil.ReadDir(absDir)
  74. if err != nil {
  75. return nil, err
  76. }
  77. for _, fi := range files {
  78. if fi.IsDir() {
  79. continue
  80. }
  81. absFile := filepath.Join(absDir, fi.Name())
  82. if matched, _ := filepath.Match(filepath.Join(absDir, filepath.Base(path)), absFile); matched {
  83. tmpContent, err := GetRenderedConfFromFile(absFile)
  84. if err != nil {
  85. return nil, fmt.Errorf("render extra config %s error: %v", absFile, err)
  86. }
  87. out.Write(tmpContent)
  88. out.WriteString("\n")
  89. }
  90. }
  91. }
  92. return out.Bytes(), nil
  93. }