1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 07:26:24 -05:00

Direct reflective json loader

This commit is contained in:
Shelikhoo 2021-03-06 17:48:39 +00:00
parent 99f761a12e
commit f518b63157
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316
5 changed files with 22 additions and 4 deletions

View File

@ -1,3 +1,5 @@
// +build !confonly
package observatory
import (

1
go.sum
View File

@ -325,6 +325,7 @@ google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.25.1-0.20200805231151-a709e31e5d12 h1:OwhZOOMuf7leLaSCuxtQ9FW7ui2L2L6UKOtKAUqovUQ=
google.golang.org/protobuf v1.25.1-0.20200805231151-a709e31e5d12/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=

View File

@ -1,7 +1,6 @@
package conf
import (
"bytes"
"encoding/json"
"github.com/golang/protobuf/jsonpb"
"github.com/jhump/protoreflect/desc"
@ -13,13 +12,13 @@ func (c *Config) BuildServices(service map[string]*json.RawMessage) ([]*serial.T
var ret []*serial.TypedMessage
for k, v := range service {
message, err := desc.LoadMessageDescriptor(k)
if err != nil {
if err != nil || message == nil {
return nil, newError("Cannot find service", k, "").Base(err)
}
serviceConfig := dynamic.NewMessage(message)
if err := jsonpb.Unmarshal(bytes.NewReader(*v), serviceConfig); err != nil {
if err := serviceConfig.UnmarshalJSONPB(&jsonpb.Unmarshaler{AllowUnknownFields: false}, *v); err != nil {
return nil, newError("Cannot interpret service configure file", k, "").Base(err)
}

View File

@ -2,6 +2,8 @@ package conf
import (
"encoding/json"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"log"
"os"
"strings"
@ -575,7 +577,18 @@ func (c *Config) Build() (*core.Config, error) {
developererr := newError("Loading a V2Ray Features as a service is intended for developers only. " +
"This is used for developers to prototype new features or for an advanced client to use special features in V2Ray," +
" instead of allowing end user to enable it without special tool and knowledge.")
return nil, newError("Cannot load service").Base(developererr).Base(err)
sb := strings.Builder{}
protoregistry.GlobalTypes.RangeMessages(func(messageType protoreflect.MessageType) bool {
switch d := messageType.Descriptor().(type) {
case protoreflect.MessageDescriptor:
sb.WriteString(string(d.Name()))
sb.WriteString(":")
sb.WriteString(string(d.FullName()))
sb.WriteString("\n")
}
return true
})
return nil, newError("Cannot load service").Base(developererr).Base(err).Base(newError(sb.String()))
} else {
config.App = append(config.App, msg...)
}

View File

@ -26,6 +26,9 @@ import (
// Fix dependency cycle caused by core import in internet package
_ "github.com/v2fly/v2ray-core/v4/transport/internet/tagged/taggedimpl"
// Developer preview features
_ "github.com/v2fly/v2ray-core/v4/app/observatory"
// Inbound and outbound proxies.
_ "github.com/v2fly/v2ray-core/v4/proxy/blackhole"
_ "github.com/v2fly/v2ray-core/v4/proxy/dns"