parse.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "os"
  19. "path/filepath"
  20. )
  21. func ParseClientConfig(filePath string) (
  22. cfg ClientCommonConf,
  23. pxyCfgs map[string]ProxyConf,
  24. visitorCfgs map[string]VisitorConf,
  25. err error,
  26. ) {
  27. var content []byte
  28. content, err = GetRenderedConfFromFile(filePath)
  29. if err != nil {
  30. return
  31. }
  32. configBuffer := bytes.NewBuffer(nil)
  33. configBuffer.Write(content)
  34. // Parse common section.
  35. cfg, err = UnmarshalClientConfFromIni(content)
  36. if err != nil {
  37. return
  38. }
  39. cfg.Complete()
  40. if err = cfg.Validate(); err != nil {
  41. err = fmt.Errorf("Parse config error: %v", err)
  42. return
  43. }
  44. // Aggregate proxy configs from include files.
  45. var buf []byte
  46. buf, err = getIncludeContents(cfg.IncludeConfigFiles)
  47. if err != nil {
  48. err = fmt.Errorf("getIncludeContents error: %v", err)
  49. return
  50. }
  51. configBuffer.WriteString("\n")
  52. configBuffer.Write(buf)
  53. // Parse all proxy and visitor configs.
  54. pxyCfgs, visitorCfgs, err = LoadAllProxyConfsFromIni(cfg.User, configBuffer.Bytes(), cfg.Start)
  55. if err != nil {
  56. return
  57. }
  58. return
  59. }
  60. // getIncludeContents renders all configs from paths.
  61. // files format can be a single file path or directory or regex path.
  62. func getIncludeContents(paths []string) ([]byte, error) {
  63. out := bytes.NewBuffer(nil)
  64. for _, path := range paths {
  65. absDir, err := filepath.Abs(filepath.Dir(path))
  66. if err != nil {
  67. return nil, err
  68. }
  69. if _, err := os.Stat(absDir); os.IsNotExist(err) {
  70. return nil, err
  71. }
  72. files, err := os.ReadDir(absDir)
  73. if err != nil {
  74. return nil, err
  75. }
  76. for _, fi := range files {
  77. if fi.IsDir() {
  78. continue
  79. }
  80. absFile := filepath.Join(absDir, fi.Name())
  81. if matched, _ := filepath.Match(filepath.Join(absDir, filepath.Base(path)), absFile); matched {
  82. tmpContent, err := GetRenderedConfFromFile(absFile)
  83. if err != nil {
  84. return nil, fmt.Errorf("render extra config %s error: %v", absFile, err)
  85. }
  86. out.Write(tmpContent)
  87. out.WriteString("\n")
  88. }
  89. }
  90. }
  91. return out.Bytes(), nil
  92. }