config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //go:build !confonly
  2. // +build !confonly
  3. package core
  4. import (
  5. "io"
  6. "path/filepath"
  7. "strings"
  8. "google.golang.org/protobuf/proto"
  9. "github.com/v2fly/v2ray-core/v4/common"
  10. "github.com/v2fly/v2ray-core/v4/common/buf"
  11. "github.com/v2fly/v2ray-core/v4/common/cmdarg"
  12. )
  13. // ConfigFormat is a configurable format of V2Ray config file.
  14. type ConfigFormat struct {
  15. Name []string
  16. Extension []string
  17. Loader ConfigLoader
  18. }
  19. // ConfigLoader is a utility to load V2Ray config from external source.
  20. type ConfigLoader func(input interface{}) (*Config, error)
  21. var (
  22. configLoaderByName = make(map[string]*ConfigFormat)
  23. configLoaderByExt = make(map[string]*ConfigFormat)
  24. )
  25. // RegisterConfigLoader add a new ConfigLoader.
  26. func RegisterConfigLoader(format *ConfigFormat) error {
  27. for _, name := range format.Name {
  28. lname := strings.ToLower(name)
  29. if _, found := configLoaderByName[lname]; found {
  30. return newError(name, " already registered.")
  31. }
  32. configLoaderByName[lname] = format
  33. }
  34. for _, ext := range format.Extension {
  35. lext := strings.ToLower(ext)
  36. if f, found := configLoaderByExt[lext]; found {
  37. return newError(ext, " already registered to ", f.Name)
  38. }
  39. configLoaderByExt[lext] = format
  40. }
  41. return nil
  42. }
  43. func getExtension(filename string) string {
  44. ext := filepath.Ext(filename)
  45. return strings.ToLower(ext)
  46. }
  47. // GetConfigLoader get config loader by name and filename.
  48. // Specify formatName to explicitly select a loader.
  49. // Specify filename to choose loader by detect its extension.
  50. // Leave formatName and filename blank for default loader
  51. func GetConfigLoader(formatName string, filename string) (*ConfigFormat, error) {
  52. if formatName != "" {
  53. // if explicitly specified, we can safely assume that user knows what they are
  54. if f, found := configLoaderByName[formatName]; found {
  55. return f, nil
  56. }
  57. return nil, newError("Unable to load config in ", formatName).AtWarning()
  58. }
  59. // no explicitly specified loader, extenstion detect first
  60. if ext := getExtension(filename); len(ext) > 0 {
  61. if f, found := configLoaderByExt[ext]; found {
  62. return f, nil
  63. }
  64. }
  65. // default loader
  66. if f, found := configLoaderByName["json"]; found {
  67. return f, nil
  68. }
  69. panic("default loader not found")
  70. }
  71. // LoadConfig loads config with given format from given source.
  72. // input accepts 2 different types:
  73. // * []string slice of multiple filename/url(s) to open to read
  74. // * io.Reader that reads a config content (the original way)
  75. func LoadConfig(formatName string, filename string, input interface{}) (*Config, error) {
  76. f, err := GetConfigLoader(formatName, filename)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return f.Loader(input)
  81. }
  82. func loadProtobufConfig(data []byte) (*Config, error) {
  83. config := new(Config)
  84. if err := proto.Unmarshal(data, config); err != nil {
  85. return nil, err
  86. }
  87. return config, nil
  88. }
  89. func init() {
  90. common.Must(RegisterConfigLoader(&ConfigFormat{
  91. Name: []string{"Protobuf", "pb"},
  92. Extension: []string{".pb"},
  93. Loader: func(input interface{}) (*Config, error) {
  94. switch v := input.(type) {
  95. case cmdarg.Arg:
  96. r, err := cmdarg.LoadArg(v[0])
  97. if err != nil {
  98. return nil, err
  99. }
  100. data, err := buf.ReadAllToBytes(r)
  101. if err != nil {
  102. return nil, err
  103. }
  104. return loadProtobufConfig(data)
  105. case io.Reader:
  106. data, err := buf.ReadAllToBytes(v)
  107. if err != nil {
  108. return nil, err
  109. }
  110. return loadProtobufConfig(data)
  111. default:
  112. return nil, newError("unknow type")
  113. }
  114. },
  115. }))
  116. }