2021-02-21 10:02:42 -05:00
|
|
|
package formats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v5"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/infra/conf/merge"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/infra/conf/mergers"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/infra/conf/serial"
|
2021-02-21 10:02:42 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2021-12-23 00:14:43 -05:00
|
|
|
extensions, err := mergers.GetExtensions(formatName)
|
2021-02-21 10:02:42 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &core.ConfigFormat{
|
|
|
|
Name: []string{formatName},
|
2021-12-23 00:14:43 -05:00
|
|
|
Extension: extensions,
|
2021-02-21 10:02:42 -05:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|