1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-04 01:06:30 -05:00
v2fly/infra/conf/v2jsonpb/v2jsonpb.go

71 lines
1.8 KiB
Go
Raw Normal View History

2021-06-19 15:20:31 -04:00
package v2jsonpb
import (
2021-10-28 06:34:19 -04:00
"io"
2021-06-19 15:20:31 -04:00
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/cmdarg"
"github.com/v2fly/v2ray-core/v5/common/serial"
2021-06-19 15:20:31 -04:00
)
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
2021-06-19 15:20:31 -04:00
func loadV2JsonPb(data []byte) (*core.Config, error) {
coreconf := &core.Config{}
jsonpbloader := &protojson.UnmarshalOptions{Resolver: anyresolverv2{serial.GetResolver()}, AllowPartial: true}
2021-06-19 15:42:17 -04:00
err := jsonpbloader.Unmarshal(data, &V2JsonProtobufFollower{coreconf.ProtoReflect()})
2021-06-19 15:20:31 -04:00
if err != nil {
return nil, err
}
return coreconf, nil
}
func dumpV2JsonPb(config proto.Message) ([]byte, error) {
jsonpbdumper := &protojson.MarshalOptions{Resolver: anyresolverv2{serial.GetResolver()}, AllowPartial: true}
2021-06-19 15:20:31 -04:00
bytew, err := jsonpbdumper.Marshal(&V2JsonProtobufFollower{config.ProtoReflect()})
if err != nil {
return nil, err
}
return bytew, nil
}
func DumpV2JsonPb(config proto.Message) ([]byte, error) {
return dumpV2JsonPb(config)
}
const FormatProtobufV2JSONPB = "v2jsonpb"
func init() {
common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
Name: []string{FormatProtobufV2JSONPB},
Extension: []string{".v2pb.json", ".v2pbjson"},
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 loadV2JsonPb(data)
case io.Reader:
data, err := buf.ReadAllToBytes(v)
if err != nil {
return nil, err
}
return loadV2JsonPb(data)
default:
2021-12-26 23:33:08 -05:00
return nil, newError("unknown type")
2021-06-19 15:20:31 -04:00
}
},
}))
}