2021-09-05 09:06:13 -04:00
|
|
|
package protofilter
|
|
|
|
|
|
|
|
import (
|
2021-09-05 10:02:51 -04:00
|
|
|
"context"
|
2021-10-28 06:34:19 -04:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common/environment/envctx"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/environment/filesystemcap"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/protoext"
|
2021-09-05 09:06:13 -04:00
|
|
|
)
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
|
2021-09-05 09:06:13 -04:00
|
|
|
|
2021-09-05 10:02:51 -04:00
|
|
|
func FilterProtoConfig(ctx context.Context, config proto.Message) error {
|
2021-09-05 09:06:13 -04:00
|
|
|
messageProtoReflect := config.ProtoReflect()
|
2021-09-05 10:02:51 -04:00
|
|
|
return filterMessage(ctx, messageProtoReflect)
|
2021-09-05 09:06:13 -04:00
|
|
|
}
|
|
|
|
|
2021-09-05 10:02:51 -04:00
|
|
|
func filterMessage(ctx context.Context, message protoreflect.Message) error {
|
2021-09-05 09:06:13 -04:00
|
|
|
var err error
|
|
|
|
type fileRead struct {
|
|
|
|
filename string
|
|
|
|
field string
|
|
|
|
}
|
|
|
|
var fileReadingQueue []fileRead
|
2021-09-18 16:04:30 -04:00
|
|
|
|
|
|
|
type pendingWrite struct {
|
|
|
|
field protoreflect.FieldDescriptor
|
|
|
|
value protoreflect.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
var pendingWriteQueue []pendingWrite
|
|
|
|
|
2021-09-05 09:06:13 -04:00
|
|
|
message.Range(func(descriptor protoreflect.FieldDescriptor, value protoreflect.Value) bool {
|
|
|
|
v2extension, ferr := protoext.GetFieldOptions(descriptor)
|
2021-09-05 11:29:12 -04:00
|
|
|
if ferr == nil {
|
2021-09-05 09:06:13 -04:00
|
|
|
if v2extension.Forbidden {
|
|
|
|
if value.Bool() {
|
|
|
|
err = newError("a forbidden value is set ", descriptor.FullName())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v2extension.ConvertTimeReadFileInto != "" {
|
|
|
|
fileReadingQueue = append(fileReadingQueue, fileRead{
|
|
|
|
filename: value.String(),
|
|
|
|
field: v2extension.ConvertTimeReadFileInto,
|
|
|
|
})
|
|
|
|
}
|
2021-09-18 16:04:30 -04:00
|
|
|
|
|
|
|
if v2extension.ConvertTimeParseIp != "" {
|
2021-11-27 04:16:41 -05:00
|
|
|
ipValue := net.ParseIP(value.String())
|
2021-09-18 16:04:30 -04:00
|
|
|
target := message.Descriptor().Fields().ByTextName(v2extension.ConvertTimeParseIp)
|
2022-05-11 10:03:32 -04:00
|
|
|
if ipValue.To4() != nil {
|
|
|
|
ipValue = ipValue.To4()
|
|
|
|
}
|
2021-09-18 16:04:30 -04:00
|
|
|
pendingWriteQueue = append(pendingWriteQueue, pendingWrite{
|
|
|
|
field: target,
|
2022-05-11 10:03:32 -04:00
|
|
|
value: protoreflect.ValueOf([]byte(ipValue)),
|
2021-09-18 16:04:30 -04:00
|
|
|
})
|
|
|
|
}
|
2021-09-05 09:06:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch descriptor.Kind() {
|
|
|
|
case protoreflect.MessageKind:
|
|
|
|
if descriptor.IsMap() {
|
2021-09-05 10:02:51 -04:00
|
|
|
err = filterMap(ctx, value.Map())
|
2021-09-05 09:06:13 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if descriptor.IsList() {
|
2021-09-05 10:02:51 -04:00
|
|
|
err = filterList(ctx, value.List())
|
2021-09-05 09:06:13 -04:00
|
|
|
break
|
|
|
|
}
|
2021-09-05 10:02:51 -04:00
|
|
|
err = filterMessage(ctx, value.Message())
|
2021-09-05 09:06:13 -04:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2021-09-05 10:38:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fsenvironment := envctx.EnvironmentFromContext(ctx)
|
|
|
|
fsifce := fsenvironment.(filesystemcap.FileSystemCapabilitySet)
|
2021-09-05 09:06:13 -04:00
|
|
|
for _, v := range fileReadingQueue {
|
2021-09-05 10:55:55 -04:00
|
|
|
field := message.Descriptor().Fields().ByTextName(v.field)
|
|
|
|
if v.filename == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(message.Get(field).Bytes()) > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-05 10:38:15 -04:00
|
|
|
file, err := fsifce.OpenFileForRead()(v.filename)
|
|
|
|
if err != nil {
|
|
|
|
return newError("unable to open file").Base(err)
|
|
|
|
}
|
|
|
|
fileContent, err := io.ReadAll(file)
|
2021-09-05 09:06:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return newError("unable to read file").Base(err)
|
|
|
|
}
|
2021-09-05 10:38:15 -04:00
|
|
|
file.Close()
|
|
|
|
message.Set(field, protoreflect.ValueOf(fileContent))
|
2021-09-05 09:06:13 -04:00
|
|
|
}
|
2021-09-18 16:04:30 -04:00
|
|
|
|
|
|
|
for _, v := range pendingWriteQueue {
|
|
|
|
message.Set(v.field, v.value)
|
|
|
|
}
|
2021-09-05 09:06:13 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-05 10:02:51 -04:00
|
|
|
func filterMap(ctx context.Context, mapValue protoreflect.Map) error {
|
2021-09-05 09:06:13 -04:00
|
|
|
var err error
|
|
|
|
mapValue.Range(func(key protoreflect.MapKey, value protoreflect.Value) bool {
|
2021-09-05 10:02:51 -04:00
|
|
|
err = filterMessage(ctx, value.Message())
|
2021-11-27 04:16:41 -05:00
|
|
|
return err == nil
|
2021-09-05 09:06:13 -04:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-05 10:02:51 -04:00
|
|
|
func filterList(ctx context.Context, listValue protoreflect.List) error {
|
2021-09-05 09:06:13 -04:00
|
|
|
var err error
|
|
|
|
size := listValue.Len()
|
|
|
|
for i := 0; i < size; i++ {
|
2021-09-05 10:02:51 -04:00
|
|
|
err = filterMessage(ctx, listValue.Get(i).Message())
|
2021-09-05 09:06:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|