2018-02-14 17:57:40 -05:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2021-02-21 10:02:42 -05:00
|
|
|
"fmt"
|
2018-02-14 17:57:40 -05:00
|
|
|
"io"
|
2021-02-21 10:02:42 -05:00
|
|
|
"log"
|
|
|
|
"os"
|
2020-11-28 09:06:03 -05:00
|
|
|
"path/filepath"
|
2021-02-21 10:02:42 -05:00
|
|
|
"reflect"
|
2018-02-14 17:57:40 -05:00
|
|
|
"strings"
|
|
|
|
|
2021-03-13 18:44:47 -05:00
|
|
|
"google.golang.org/protobuf/proto"
|
2021-02-16 15:31:50 -05:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/buf"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/cmdarg"
|
2018-02-14 17:57:40 -05:00
|
|
|
)
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
const (
|
|
|
|
// FormatAuto represents all available formats by auto selecting
|
|
|
|
FormatAuto = "auto"
|
|
|
|
// FormatJSON represents json format
|
|
|
|
FormatJSON = "json"
|
|
|
|
// FormatTOML represents toml format
|
|
|
|
FormatTOML = "toml"
|
|
|
|
// FormatYAML represents yaml format
|
|
|
|
FormatYAML = "yaml"
|
|
|
|
// FormatProtobuf represents protobuf format
|
|
|
|
FormatProtobuf = "protobuf"
|
|
|
|
// FormatProtobufShort is the short of FormatProtobuf
|
|
|
|
FormatProtobufShort = "pb"
|
|
|
|
)
|
|
|
|
|
2018-02-16 08:04:02 -05:00
|
|
|
// ConfigFormat is a configurable format of V2Ray config file.
|
2018-02-14 17:57:40 -05:00
|
|
|
type ConfigFormat struct {
|
2020-11-28 09:06:03 -05:00
|
|
|
Name []string
|
2018-02-14 17:57:40 -05:00
|
|
|
Extension []string
|
|
|
|
Loader ConfigLoader
|
|
|
|
}
|
|
|
|
|
2018-04-02 03:52:16 -04:00
|
|
|
// ConfigLoader is a utility to load V2Ray config from external source.
|
2020-01-02 20:26:48 -05:00
|
|
|
type ConfigLoader func(input interface{}) (*Config, error)
|
2018-02-14 17:57:40 -05:00
|
|
|
|
|
|
|
var (
|
2021-02-21 10:02:42 -05:00
|
|
|
configLoaders = make([]*ConfigFormat, 0)
|
2018-02-14 17:57:40 -05:00
|
|
|
configLoaderByName = make(map[string]*ConfigFormat)
|
|
|
|
configLoaderByExt = make(map[string]*ConfigFormat)
|
|
|
|
)
|
|
|
|
|
|
|
|
// RegisterConfigLoader add a new ConfigLoader.
|
|
|
|
func RegisterConfigLoader(format *ConfigFormat) error {
|
2020-11-28 09:06:03 -05:00
|
|
|
for _, name := range format.Name {
|
2021-02-21 10:02:42 -05:00
|
|
|
if _, found := configLoaderByName[name]; found {
|
2020-11-28 09:06:03 -05:00
|
|
|
return newError(name, " already registered.")
|
|
|
|
}
|
2021-02-21 10:02:42 -05:00
|
|
|
configLoaderByName[name] = format
|
2018-02-14 17:57:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ext := range format.Extension {
|
|
|
|
lext := strings.ToLower(ext)
|
|
|
|
if f, found := configLoaderByExt[lext]; found {
|
|
|
|
return newError(ext, " already registered to ", f.Name)
|
|
|
|
}
|
|
|
|
configLoaderByExt[lext] = format
|
|
|
|
}
|
2021-02-21 10:02:42 -05:00
|
|
|
configLoaders = append(configLoaders, format)
|
2018-02-14 17:57:40 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getExtension(filename string) string {
|
2020-11-28 09:06:03 -05:00
|
|
|
ext := filepath.Ext(filename)
|
|
|
|
return strings.ToLower(ext)
|
|
|
|
}
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
// GetLoaderExtensions get config loader extensions.
|
|
|
|
func GetLoaderExtensions(formatName string) ([]string, error) {
|
|
|
|
if formatName == FormatAuto {
|
|
|
|
return GetAllExtensions(), nil
|
2020-11-28 09:06:03 -05:00
|
|
|
}
|
2021-02-21 10:02:42 -05:00
|
|
|
if f, found := configLoaderByName[formatName]; found {
|
|
|
|
return f.Extension, nil
|
2020-11-28 09:06:03 -05:00
|
|
|
}
|
2021-02-21 10:02:42 -05:00
|
|
|
return nil, newError("config loader not found: ", formatName).AtWarning()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllExtensions get all extensions supported
|
|
|
|
func GetAllExtensions() []string {
|
|
|
|
extensions := make([]string, 0)
|
|
|
|
for _, f := range configLoaderByName {
|
|
|
|
extensions = append(extensions, f.Extension...)
|
2018-02-14 17:57:40 -05:00
|
|
|
}
|
2021-02-21 10:02:42 -05:00
|
|
|
return extensions
|
2018-02-14 17:57:40 -05:00
|
|
|
}
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
// LoadConfig loads multiple config with given format from given source.
|
|
|
|
// input accepts:
|
|
|
|
// * string of a single filename/url(s) to open to read
|
2020-01-02 20:26:48 -05:00
|
|
|
// * []string slice of multiple filename/url(s) to open to read
|
|
|
|
// * io.Reader that reads a config content (the original way)
|
2021-02-21 10:02:42 -05:00
|
|
|
func LoadConfig(formatName string, input interface{}) (*Config, error) {
|
|
|
|
cnt := getInputCount(input)
|
|
|
|
if cnt == 0 {
|
|
|
|
log.Println("Using config from STDIN")
|
|
|
|
input = os.Stdin
|
|
|
|
cnt = 1
|
|
|
|
}
|
|
|
|
if formatName == FormatAuto && cnt == 1 {
|
|
|
|
// This ensures only to call auto loader for multiple files,
|
|
|
|
// so that it can only care about merging scenarios
|
|
|
|
return loadSingleConfigAutoFormat(input)
|
|
|
|
}
|
|
|
|
// if input is a slice with single element, extract it
|
|
|
|
// so that unmergeable loaders don't need to deal with
|
|
|
|
// slices
|
|
|
|
s := reflect.Indirect(reflect.ValueOf(input))
|
|
|
|
k := s.Kind()
|
|
|
|
if (k == reflect.Slice || k == reflect.Array) && s.Len() == 1 {
|
|
|
|
value := reflect.Indirect(s.Index(0))
|
|
|
|
if value.Kind() == reflect.String {
|
|
|
|
// string type alias
|
|
|
|
input = fmt.Sprint(value.Interface())
|
|
|
|
} else {
|
|
|
|
input = value.Interface()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f, found := configLoaderByName[formatName]
|
|
|
|
if !found {
|
|
|
|
return nil, newError("config loader not found: ", formatName).AtWarning()
|
2018-02-14 17:57:40 -05:00
|
|
|
}
|
2020-11-28 09:06:03 -05:00
|
|
|
return f.Loader(input)
|
2018-02-14 17:57:40 -05:00
|
|
|
}
|
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
// loadSingleConfigAutoFormat loads a single config with from given source.
|
|
|
|
// input accepts:
|
|
|
|
// * string of a single filename/url(s) to open to read
|
|
|
|
// * io.Reader that reads a config content (the original way)
|
|
|
|
func loadSingleConfigAutoFormat(input interface{}) (*Config, error) {
|
2022-07-21 09:56:50 -04:00
|
|
|
switch v := input.(type) {
|
|
|
|
case cmdarg.Arg:
|
|
|
|
return loadSingleConfigAutoFormatFromFile(v.String())
|
|
|
|
case string:
|
|
|
|
return loadSingleConfigByTryingAllLoaders(v)
|
|
|
|
case io.Reader:
|
|
|
|
data, err := buf.ReadAllToBytes(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-02-21 10:02:42 -05:00
|
|
|
}
|
2022-07-21 09:56:50 -04:00
|
|
|
return loadSingleConfigByTryingAllLoaders(data)
|
|
|
|
default:
|
|
|
|
return loadSingleConfigByTryingAllLoaders(v)
|
2021-02-21 10:02:42 -05:00
|
|
|
}
|
2022-07-21 09:56:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadSingleConfigAutoFormatFromFile(file string) (*Config, error) {
|
|
|
|
extension := getExtension(file)
|
|
|
|
if extension != "" {
|
|
|
|
lowerName := strings.ToLower(extension)
|
|
|
|
if f, found := configLoaderByExt[lowerName]; found {
|
|
|
|
return f.Loader(file)
|
|
|
|
}
|
|
|
|
return nil, newError("config loader not found for: ", extension).AtWarning()
|
|
|
|
}
|
|
|
|
|
|
|
|
return loadSingleConfigByTryingAllLoaders(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadSingleConfigByTryingAllLoaders(input interface{}) (*Config, error) {
|
2021-09-28 13:30:26 -04:00
|
|
|
var errorReasons strings.Builder
|
2022-07-21 09:56:50 -04:00
|
|
|
|
2021-02-21 10:02:42 -05:00
|
|
|
for _, f := range configLoaders {
|
|
|
|
if f.Name[0] == FormatAuto {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c, err := f.Loader(input)
|
|
|
|
if err == nil {
|
|
|
|
return c, nil
|
|
|
|
}
|
2021-11-27 04:16:41 -05:00
|
|
|
errorReasons.WriteString(fmt.Sprintf("unable to parse as %v:%v;", f.Name[0], err.Error()))
|
2021-02-21 10:02:42 -05:00
|
|
|
}
|
2022-07-21 09:56:50 -04:00
|
|
|
|
2021-09-28 13:30:26 -04:00
|
|
|
return nil, newError("tried all loaders but failed when attempting to parse: ", input, ";", errorReasons.String()).AtWarning()
|
2021-02-21 10:02:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func getInputCount(input interface{}) int {
|
|
|
|
s := reflect.Indirect(reflect.ValueOf(input))
|
|
|
|
k := s.Kind()
|
|
|
|
if k == reflect.Slice || k == reflect.Array {
|
|
|
|
return s.Len()
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-01-02 20:26:48 -05:00
|
|
|
func loadProtobufConfig(data []byte) (*Config, error) {
|
2018-02-14 17:57:40 -05:00
|
|
|
config := new(Config)
|
|
|
|
if err := proto.Unmarshal(data, config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(RegisterConfigLoader(&ConfigFormat{
|
2021-02-21 10:02:42 -05:00
|
|
|
Name: []string{FormatProtobuf, FormatProtobufShort},
|
2020-11-28 09:06:03 -05:00
|
|
|
Extension: []string{".pb"},
|
2020-01-02 20:26:48 -05:00
|
|
|
Loader: func(input interface{}) (*Config, error) {
|
|
|
|
switch v := input.(type) {
|
2021-02-21 10:02:42 -05:00
|
|
|
case string:
|
|
|
|
r, err := cmdarg.LoadArg(v)
|
2020-11-28 09:06:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-02 20:26:48 -05:00
|
|
|
data, err := buf.ReadAllToBytes(r)
|
2020-11-28 09:06:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-02 20:26:48 -05:00
|
|
|
return loadProtobufConfig(data)
|
|
|
|
case io.Reader:
|
|
|
|
data, err := buf.ReadAllToBytes(v)
|
2020-11-28 09:06:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-02 20:26:48 -05:00
|
|
|
return loadProtobufConfig(data)
|
|
|
|
default:
|
2021-09-04 14:34:31 -04:00
|
|
|
return nil, newError("unknown type")
|
2020-01-02 20:26:48 -05:00
|
|
|
}
|
|
|
|
},
|
2018-02-14 17:57:40 -05:00
|
|
|
}))
|
|
|
|
}
|