1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-21 15:05:23 +00:00

add jsonv5 conf loader

This commit is contained in:
Shelikhoo 2021-09-04 19:54:07 +01:00
parent 51f05b1bff
commit 7bf1d7a1ce
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316
2 changed files with 56 additions and 0 deletions

40
infra/conf/v5cfg/init.go Normal file
View File

@ -0,0 +1,40 @@
package v5cfg
import (
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/buf"
"github.com/v2fly/v2ray-core/v4/common/cmdarg"
"io"
)
const jsonV5 = "jsonv5"
func init() {
common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
Name: []string{jsonV5},
Extension: []string{".json"},
Loader: func(input interface{}) (*core.Config, error) {
switch v := input.(type) {
case string:
r, err := cmdarg.LoadArg(v)
if err != nil {
return nil, err
}
data, err := buf.ReadAllToBytes(r)
if err != nil {
return nil, err
}
return loadJsonConfig(data)
case io.Reader:
data, err := buf.ReadAllToBytes(v)
if err != nil {
return nil, err
}
return loadJsonConfig(data)
default:
return nil, newError("unknown type")
}
},
}))
}

View File

@ -2,6 +2,7 @@ package v5cfg
import (
"context"
"encoding/json"
"github.com/golang/protobuf/proto"
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/app/dispatcher"
@ -71,3 +72,18 @@ func (c RootConfig) BuildV5(ctx context.Context) (proto.Message, error) {
}
return config, nil
}
func loadJsonConfig(data []byte) (*core.Config, error) {
rootConfig := &RootConfig{}
err := json.Unmarshal(data, rootConfig)
if err != nil {
return nil, newError("unable to load json").Base(err)
}
message, err := rootConfig.BuildV5(context.TODO())
if err != nil {
return nil, newError("unable to build config").Base(err)
}
return message.(*core.Config), nil
}