1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 02:16:10 -05:00
v2fly/common/registry/restrict.go

36 lines
876 B
Go
Raw Normal View History

2023-11-21 18:03:20 -05:00
package registry
import (
"context"
"google.golang.org/protobuf/proto"
"github.com/v2fly/v2ray-core/v5/common/protoext"
)
const restrictedLoadModeCtx = "restrictedLoadModeCtx"
func CreateRestrictedModeContext(ctx context.Context) context.Context {
return context.WithValue(ctx, restrictedLoadModeCtx, true) //nolint: staticcheck
}
func isRestrictedModeContext(ctx context.Context) bool {
v := ctx.Value(restrictedLoadModeCtx)
if v == nil {
return false
}
return v.(bool)
}
func enforceRestriction(config proto.Message) error {
configDescriptor := config.ProtoReflect().Descriptor()
msgOpts, err := protoext.GetMessageOptions(configDescriptor)
if err != nil {
return newError("unable to find message options").Base(err)
}
if !msgOpts.AllowRestrictedModeLoad {
return newError("component has not opted in for load in restricted mode")
}
return nil
}