confloader.go 986 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package confloader
  2. import (
  3. "context"
  4. "io"
  5. "os"
  6. "github.com/xtls/xray-core/common/errors"
  7. )
  8. type (
  9. configFileLoader func(string) (io.Reader, error)
  10. extconfigLoader func([]string, io.Reader) (io.Reader, error)
  11. )
  12. var (
  13. EffectiveConfigFileLoader configFileLoader
  14. EffectiveExtConfigLoader extconfigLoader
  15. )
  16. // LoadConfig reads from a path/url/stdin
  17. // actual work is in external module
  18. func LoadConfig(file string) (io.Reader, error) {
  19. if EffectiveConfigFileLoader == nil {
  20. errors.LogInfo(context.Background(), "external config module not loaded, reading from stdin")
  21. return os.Stdin, nil
  22. }
  23. return EffectiveConfigFileLoader(file)
  24. }
  25. // LoadExtConfig calls xctl to handle multiple config
  26. // the actual work also in external module
  27. func LoadExtConfig(files []string, reader io.Reader) (io.Reader, error) {
  28. if EffectiveExtConfigLoader == nil {
  29. return nil, errors.New("external config module not loaded").AtError()
  30. }
  31. return EffectiveExtConfigLoader(files, reader)
  32. }