1
0

config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package core
  2. import (
  3. "io"
  4. "strings"
  5. "github.com/golang/protobuf/proto"
  6. "github.com/xtls/xray-core/common"
  7. "github.com/xtls/xray-core/common/buf"
  8. "github.com/xtls/xray-core/common/cmdarg"
  9. "github.com/xtls/xray-core/main/confloader"
  10. )
  11. // ConfigFormat is a configurable format of Xray config file.
  12. type ConfigFormat struct {
  13. Name string
  14. Extension []string
  15. Loader ConfigLoader
  16. }
  17. // ConfigLoader is a utility to load Xray config from external source.
  18. type ConfigLoader func(input interface{}) (*Config, error)
  19. // ConfigBuilder is a builder to build core.Config from filenames and formats
  20. type ConfigBuilder func(files []string, formats []string) (*Config, error)
  21. var (
  22. configLoaderByName = make(map[string]*ConfigFormat)
  23. configLoaderByExt = make(map[string]*ConfigFormat)
  24. ConfigBuilderForFiles ConfigBuilder
  25. )
  26. // RegisterConfigLoader add a new ConfigLoader.
  27. func RegisterConfigLoader(format *ConfigFormat) error {
  28. name := strings.ToLower(format.Name)
  29. if _, found := configLoaderByName[name]; found {
  30. return newError(format.Name, " already registered.")
  31. }
  32. configLoaderByName[name] = format
  33. for _, ext := range format.Extension {
  34. lext := strings.ToLower(ext)
  35. if f, found := configLoaderByExt[lext]; found {
  36. return newError(ext, " already registered to ", f.Name)
  37. }
  38. configLoaderByExt[lext] = format
  39. }
  40. return nil
  41. }
  42. func GetFormatByExtension(ext string) string {
  43. switch strings.ToLower(ext) {
  44. case "pb", "protobuf":
  45. return "protobuf"
  46. case "yaml", "yml":
  47. return "yaml"
  48. case "toml":
  49. return "toml"
  50. case "json":
  51. return "json"
  52. default:
  53. return ""
  54. }
  55. }
  56. func getExtension(filename string) string {
  57. idx := strings.LastIndexByte(filename, '.')
  58. if idx == -1 {
  59. return ""
  60. }
  61. return filename[idx+1:]
  62. }
  63. func getFormat(filename string) string {
  64. return GetFormatByExtension(getExtension(filename))
  65. }
  66. func LoadConfig(formatName string, input interface{}) (*Config, error) {
  67. switch v := input.(type) {
  68. case cmdarg.Arg:
  69. formats := make([]string, len(v))
  70. hasProtobuf := false
  71. for i, file := range v {
  72. f := getFormat(file)
  73. if f == "" {
  74. f = formatName
  75. }
  76. if f == "protobuf" {
  77. hasProtobuf = true
  78. }
  79. formats[i] = f
  80. }
  81. // only one protobuf config file is allowed
  82. if hasProtobuf {
  83. if len(v) == 1 {
  84. return configLoaderByName["protobuf"].Loader(v)
  85. } else {
  86. return nil, newError("Only one protobuf config file is allowed").AtWarning()
  87. }
  88. }
  89. // to avoid import cycle
  90. return ConfigBuilderForFiles(v, formats)
  91. case io.Reader:
  92. if f, found := configLoaderByName[formatName]; found {
  93. return f.Loader(v)
  94. } else {
  95. return nil, newError("Unable to load config in", formatName).AtWarning()
  96. }
  97. }
  98. return nil, newError("Unable to load config").AtWarning()
  99. }
  100. func loadProtobufConfig(data []byte) (*Config, error) {
  101. config := new(Config)
  102. if err := proto.Unmarshal(data, config); err != nil {
  103. return nil, err
  104. }
  105. return config, nil
  106. }
  107. func init() {
  108. common.Must(RegisterConfigLoader(&ConfigFormat{
  109. Name: "Protobuf",
  110. Extension: []string{"pb"},
  111. Loader: func(input interface{}) (*Config, error) {
  112. switch v := input.(type) {
  113. case cmdarg.Arg:
  114. r, err := confloader.LoadConfig(v[0])
  115. common.Must(err)
  116. data, err := buf.ReadAllToBytes(r)
  117. common.Must(err)
  118. return loadProtobufConfig(data)
  119. case io.Reader:
  120. data, err := buf.ReadAllToBytes(v)
  121. common.Must(err)
  122. return loadProtobufConfig(data)
  123. default:
  124. return nil, newError("unknow type")
  125. }
  126. },
  127. }))
  128. }