2020-11-25 14:01:53 +03:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
|
|
"github.com/xtls/xray-core/common/cmdarg"
|
2024-06-29 21:32:57 +03:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/main/confloader"
|
2023-08-10 07:43:34 +03:00
|
|
|
"google.golang.org/protobuf/proto"
|
2020-11-25 14:01:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// ConfigFormat is a configurable format of Xray config file.
|
|
|
|
type ConfigFormat struct {
|
|
|
|
Name string
|
|
|
|
Extension []string
|
|
|
|
Loader ConfigLoader
|
|
|
|
}
|
|
|
|
|
2024-08-16 08:32:05 +03:00
|
|
|
type ConfigSource struct {
|
|
|
|
Name string
|
|
|
|
Format string
|
|
|
|
}
|
|
|
|
|
2020-11-25 14:01:53 +03:00
|
|
|
// ConfigLoader is a utility to load Xray config from external source.
|
|
|
|
type ConfigLoader func(input interface{}) (*Config, error)
|
|
|
|
|
2021-02-12 17:12:58 +03:00
|
|
|
// ConfigBuilder is a builder to build core.Config from filenames and formats
|
2024-08-16 08:32:05 +03:00
|
|
|
type ConfigBuilder func(files []*ConfigSource) (*Config, error)
|
2021-02-12 17:12:58 +03:00
|
|
|
|
2024-11-06 17:27:06 +03:00
|
|
|
// ConfigsMerger merges multiple json configs into a single one
|
2024-08-16 08:32:05 +03:00
|
|
|
type ConfigsMerger func(files []*ConfigSource) (string, error)
|
2023-12-29 19:16:48 +03:00
|
|
|
|
2020-11-25 14:01:53 +03:00
|
|
|
var (
|
2021-02-12 17:12:58 +03:00
|
|
|
configLoaderByName = make(map[string]*ConfigFormat)
|
|
|
|
configLoaderByExt = make(map[string]*ConfigFormat)
|
|
|
|
ConfigBuilderForFiles ConfigBuilder
|
2023-12-29 19:16:48 +03:00
|
|
|
ConfigMergedFormFiles ConfigsMerger
|
2020-11-25 14:01:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// RegisterConfigLoader add a new ConfigLoader.
|
|
|
|
func RegisterConfigLoader(format *ConfigFormat) error {
|
|
|
|
name := strings.ToLower(format.Name)
|
|
|
|
if _, found := configLoaderByName[name]; found {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New(format.Name, " already registered.")
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
configLoaderByName[name] = format
|
|
|
|
|
|
|
|
for _, ext := range format.Extension {
|
|
|
|
lext := strings.ToLower(ext)
|
|
|
|
if f, found := configLoaderByExt[lext]; found {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New(ext, " already registered to ", f.Name)
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
configLoaderByExt[lext] = format
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-29 19:16:48 +03:00
|
|
|
func GetMergedConfig(args cmdarg.Arg) (string, error) {
|
2024-08-16 08:32:05 +03:00
|
|
|
var files []*ConfigSource
|
2023-12-29 19:16:48 +03:00
|
|
|
supported := []string{"json", "yaml", "toml"}
|
|
|
|
for _, file := range args {
|
|
|
|
format := getFormat(file)
|
2024-01-11 18:34:26 +03:00
|
|
|
for _, s := range supported {
|
|
|
|
if s == format {
|
2024-08-16 08:32:05 +03:00
|
|
|
files = append(files, &ConfigSource{
|
|
|
|
Name: file,
|
|
|
|
Format: format,
|
|
|
|
})
|
2024-01-11 18:34:26 +03:00
|
|
|
break
|
|
|
|
}
|
2023-12-29 19:16:48 +03:00
|
|
|
}
|
|
|
|
}
|
2024-08-16 08:32:05 +03:00
|
|
|
return ConfigMergedFormFiles(files)
|
2023-12-29 19:16:48 +03:00
|
|
|
}
|
|
|
|
|
2021-02-12 17:12:58 +03:00
|
|
|
func GetFormatByExtension(ext string) string {
|
|
|
|
switch strings.ToLower(ext) {
|
|
|
|
case "pb", "protobuf":
|
|
|
|
return "protobuf"
|
|
|
|
case "yaml", "yml":
|
|
|
|
return "yaml"
|
|
|
|
case "toml":
|
|
|
|
return "toml"
|
2023-08-06 09:47:31 +03:00
|
|
|
case "json", "jsonc":
|
2021-02-12 17:12:58 +03:00
|
|
|
return "json"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 14:01:53 +03:00
|
|
|
func getExtension(filename string) string {
|
|
|
|
idx := strings.LastIndexByte(filename, '.')
|
|
|
|
if idx == -1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return filename[idx+1:]
|
|
|
|
}
|
|
|
|
|
2021-02-12 17:12:58 +03:00
|
|
|
func getFormat(filename string) string {
|
|
|
|
return GetFormatByExtension(getExtension(filename))
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig(formatName string, input interface{}) (*Config, error) {
|
|
|
|
switch v := input.(type) {
|
|
|
|
case cmdarg.Arg:
|
2024-08-16 08:32:05 +03:00
|
|
|
files := make([]*ConfigSource, len(v))
|
2021-02-12 17:12:58 +03:00
|
|
|
hasProtobuf := false
|
|
|
|
for i, file := range v {
|
2021-03-21 12:13:51 +03:00
|
|
|
var f string
|
|
|
|
|
|
|
|
if formatName == "auto" {
|
2021-03-28 18:45:13 +03:00
|
|
|
if file != "stdin:" {
|
|
|
|
f = getFormat(file)
|
|
|
|
} else {
|
|
|
|
f = "json"
|
2021-03-21 12:13:51 +03:00
|
|
|
}
|
2021-03-28 18:45:13 +03:00
|
|
|
} else {
|
|
|
|
f = formatName
|
2021-03-21 12:13:51 +03:00
|
|
|
}
|
|
|
|
|
2021-02-12 17:12:58 +03:00
|
|
|
if f == "" {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("Failed to get format of ", file).AtWarning()
|
2021-02-12 17:12:58 +03:00
|
|
|
}
|
2021-03-21 12:13:51 +03:00
|
|
|
|
2021-02-12 17:12:58 +03:00
|
|
|
if f == "protobuf" {
|
|
|
|
hasProtobuf = true
|
|
|
|
}
|
2024-08-16 08:32:05 +03:00
|
|
|
files[i] = &ConfigSource{
|
|
|
|
Name: file,
|
|
|
|
Format: f,
|
|
|
|
}
|
2021-02-12 17:12:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// only one protobuf config file is allowed
|
|
|
|
if hasProtobuf {
|
|
|
|
if len(v) == 1 {
|
|
|
|
return configLoaderByName["protobuf"].Loader(v)
|
|
|
|
} else {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("Only one protobuf config file is allowed").AtWarning()
|
2021-02-12 17:12:58 +03:00
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
2021-02-12 17:12:58 +03:00
|
|
|
// to avoid import cycle
|
2024-08-16 08:32:05 +03:00
|
|
|
return ConfigBuilderForFiles(files)
|
2021-02-12 17:12:58 +03:00
|
|
|
case io.Reader:
|
|
|
|
if f, found := configLoaderByName[formatName]; found {
|
|
|
|
return f.Loader(v)
|
|
|
|
} else {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("Unable to load config in", formatName).AtWarning()
|
2021-02-12 17:12:58 +03:00
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("Unable to load config").AtWarning()
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadProtobufConfig(data []byte) (*Config, error) {
|
|
|
|
config := new(Config)
|
|
|
|
if err := proto.Unmarshal(data, config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(RegisterConfigLoader(&ConfigFormat{
|
|
|
|
Name: "Protobuf",
|
|
|
|
Extension: []string{"pb"},
|
|
|
|
Loader: func(input interface{}) (*Config, error) {
|
|
|
|
switch v := input.(type) {
|
|
|
|
case cmdarg.Arg:
|
|
|
|
r, err := confloader.LoadConfig(v[0])
|
|
|
|
common.Must(err)
|
|
|
|
data, err := buf.ReadAllToBytes(r)
|
|
|
|
common.Must(err)
|
|
|
|
return loadProtobufConfig(data)
|
|
|
|
case io.Reader:
|
|
|
|
data, err := buf.ReadAllToBytes(v)
|
|
|
|
common.Must(err)
|
|
|
|
return loadProtobufConfig(data)
|
|
|
|
default:
|
2024-07-12 01:20:06 +03:00
|
|
|
return nil, errors.New("unknown type")
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
}
|