1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 09:56:18 -05:00
v2fly/main/formats/formats.go
Jebbs 2523d77919
improve commands
(rebased from ebbf31f07e)
2021-09-04 11:11:45 +01:00

57 lines
1.3 KiB
Go

package formats
import (
"bytes"
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/infra/conf/merge"
"github.com/v2fly/v2ray-core/v4/infra/conf/mergers"
"github.com/v2fly/v2ray-core/v4/infra/conf/serial"
)
func init() {
for _, formatName := range mergers.GetAllNames() {
loader, err := makeMergeLoader(formatName)
if err != nil {
panic(err)
}
if formatName == core.FormatAuto {
loader.Extension = nil
}
common.Must(core.RegisterConfigLoader(loader))
}
}
func makeMergeLoader(formatName string) (*core.ConfigFormat, error) {
extenstoins, err := mergers.GetExtensions(formatName)
if err != nil {
return nil, err
}
return &core.ConfigFormat{
Name: []string{formatName},
Extension: extenstoins,
Loader: makeLoaderFunc(formatName),
}, nil
}
func makeLoaderFunc(formatName string) core.ConfigLoader {
return func(input interface{}) (*core.Config, error) {
m := make(map[string]interface{})
err := mergers.MergeAs(formatName, input, m)
if err != nil {
return nil, err
}
data, err := merge.FromMap(m)
if err != nil {
return nil, err
}
r := bytes.NewReader(data)
cf, err := serial.DecodeJSONConfig(r)
if err != nil {
return nil, err
}
return cf.Build()
}
}