config.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package core
  2. import (
  3. "io"
  4. "slices"
  5. "strings"
  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/common/errors"
  10. "github.com/xtls/xray-core/main/confloader"
  11. "google.golang.org/protobuf/proto"
  12. )
  13. // ConfigFormat is a configurable format of Xray config file.
  14. type ConfigFormat struct {
  15. Name string
  16. Extension []string
  17. Loader ConfigLoader
  18. }
  19. type ConfigSource struct {
  20. Name string
  21. Format string
  22. }
  23. // ConfigLoader is a utility to load Xray config from external source.
  24. type ConfigLoader func(input interface{}) (*Config, error)
  25. // ConfigBuilder is a builder to build core.Config from filenames and formats
  26. type ConfigBuilder func(files []*ConfigSource) (*Config, error)
  27. // ConfigsMerger merges multiple json configs into a single one
  28. type ConfigsMerger func(files []*ConfigSource) (string, error)
  29. var (
  30. configLoaderByName = make(map[string]*ConfigFormat)
  31. configLoaderByExt = make(map[string]*ConfigFormat)
  32. ConfigBuilderForFiles ConfigBuilder
  33. ConfigMergedFormFiles ConfigsMerger
  34. )
  35. // RegisterConfigLoader add a new ConfigLoader.
  36. func RegisterConfigLoader(format *ConfigFormat) error {
  37. name := strings.ToLower(format.Name)
  38. if _, found := configLoaderByName[name]; found {
  39. return errors.New(format.Name, " already registered.")
  40. }
  41. configLoaderByName[name] = format
  42. for _, ext := range format.Extension {
  43. lext := strings.ToLower(ext)
  44. if f, found := configLoaderByExt[lext]; found {
  45. return errors.New(ext, " already registered to ", f.Name)
  46. }
  47. configLoaderByExt[lext] = format
  48. }
  49. return nil
  50. }
  51. func GetMergedConfig(args cmdarg.Arg) (string, error) {
  52. var files []*ConfigSource
  53. supported := []string{"json", "yaml", "toml"}
  54. for _, file := range args {
  55. format := getFormat(file)
  56. if slices.Contains(supported, format) {
  57. files = append(files, &ConfigSource{
  58. Name: file,
  59. Format: format,
  60. })
  61. }
  62. }
  63. return ConfigMergedFormFiles(files)
  64. }
  65. func GetFormatByExtension(ext string) string {
  66. switch strings.ToLower(ext) {
  67. case "pb", "protobuf":
  68. return "protobuf"
  69. case "yaml", "yml":
  70. return "yaml"
  71. case "toml":
  72. return "toml"
  73. case "json", "jsonc":
  74. return "json"
  75. default:
  76. return ""
  77. }
  78. }
  79. func getExtension(filename string) string {
  80. idx := strings.LastIndexByte(filename, '.')
  81. if idx == -1 {
  82. return ""
  83. }
  84. return filename[idx+1:]
  85. }
  86. func getFormat(filename string) string {
  87. return GetFormatByExtension(getExtension(filename))
  88. }
  89. func LoadConfig(formatName string, input interface{}) (*Config, error) {
  90. switch v := input.(type) {
  91. case cmdarg.Arg:
  92. files := make([]*ConfigSource, len(v))
  93. hasProtobuf := false
  94. for i, file := range v {
  95. var f string
  96. if formatName == "auto" {
  97. if file != "stdin:" {
  98. f = getFormat(file)
  99. } else {
  100. f = "json"
  101. }
  102. } else {
  103. f = formatName
  104. }
  105. if f == "" {
  106. return nil, errors.New("Failed to get format of ", file).AtWarning()
  107. }
  108. if f == "protobuf" {
  109. hasProtobuf = true
  110. }
  111. files[i] = &ConfigSource{
  112. Name: file,
  113. Format: f,
  114. }
  115. }
  116. // only one protobuf config file is allowed
  117. if hasProtobuf {
  118. if len(v) == 1 {
  119. return configLoaderByName["protobuf"].Loader(v)
  120. } else {
  121. return nil, errors.New("Only one protobuf config file is allowed").AtWarning()
  122. }
  123. }
  124. // to avoid import cycle
  125. return ConfigBuilderForFiles(files)
  126. case io.Reader:
  127. if f, found := configLoaderByName[formatName]; found {
  128. return f.Loader(v)
  129. } else {
  130. return nil, errors.New("Unable to load config in", formatName).AtWarning()
  131. }
  132. }
  133. return nil, errors.New("Unable to load config").AtWarning()
  134. }
  135. func loadProtobufConfig(data []byte) (*Config, error) {
  136. config := new(Config)
  137. if err := proto.Unmarshal(data, config); err != nil {
  138. return nil, err
  139. }
  140. return config, nil
  141. }
  142. func init() {
  143. common.Must(RegisterConfigLoader(&ConfigFormat{
  144. Name: "Protobuf",
  145. Extension: []string{"pb"},
  146. Loader: func(input interface{}) (*Config, error) {
  147. switch v := input.(type) {
  148. case cmdarg.Arg:
  149. r, err := confloader.LoadConfig(v[0])
  150. common.Must(err)
  151. data, err := buf.ReadAllToBytes(r)
  152. common.Must(err)
  153. return loadProtobufConfig(data)
  154. case io.Reader:
  155. data, err := buf.ReadAllToBytes(v)
  156. common.Must(err)
  157. return loadProtobufConfig(data)
  158. default:
  159. return nil, errors.New("unknown type")
  160. }
  161. },
  162. }))
  163. }