yaml.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package yaml
  2. import (
  3. "io"
  4. "github.com/xtls/xray-core/common"
  5. "github.com/xtls/xray-core/common/cmdarg"
  6. "github.com/xtls/xray-core/core"
  7. "github.com/xtls/xray-core/infra/conf"
  8. "github.com/xtls/xray-core/infra/conf/serial"
  9. "github.com/xtls/xray-core/main/confloader"
  10. )
  11. func init() {
  12. common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
  13. Name: "YAML",
  14. Extension: []string{"yaml", "yml"},
  15. Loader: func(input interface{}) (*core.Config, error) {
  16. switch v := input.(type) {
  17. case cmdarg.Arg:
  18. cf := &conf.Config{}
  19. for i, arg := range v {
  20. newError("Reading config: ", arg).AtInfo().WriteToLog()
  21. r, err := confloader.LoadConfig(arg)
  22. if err != nil {
  23. return nil, newError("failed to read config: ", arg).Base(err)
  24. }
  25. c, err := serial.DecodeYAMLConfig(r)
  26. if err != nil {
  27. return nil, newError("failed to decode config: ", arg).Base(err)
  28. }
  29. if i == 0 {
  30. // This ensure even if the muti-json parser do not support a setting,
  31. // It is still respected automatically for the first configure file
  32. *cf = *c
  33. continue
  34. }
  35. cf.Override(c, arg)
  36. }
  37. return cf.Build()
  38. case io.Reader:
  39. return serial.LoadYAMLConfig(v)
  40. default:
  41. return nil, newError("unknow type")
  42. }
  43. },
  44. }))
  45. }