parse.go 2.5 KB

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