mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 17:46:58 -05:00
add jsonv5 conf loader
This commit is contained in:
parent
51f05b1bff
commit
7bf1d7a1ce
40
infra/conf/v5cfg/init.go
Normal file
40
infra/conf/v5cfg/init.go
Normal 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")
|
||||
}
|
||||
},
|
||||
}))
|
||||
}
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user