restrict.go 876 B

1234567891011121314151617181920212223242526272829303132333435
  1. package registry
  2. import (
  3. "context"
  4. "google.golang.org/protobuf/proto"
  5. "github.com/v2fly/v2ray-core/v5/common/protoext"
  6. )
  7. const restrictedLoadModeCtx = "restrictedLoadModeCtx"
  8. func CreateRestrictedModeContext(ctx context.Context) context.Context {
  9. return context.WithValue(ctx, restrictedLoadModeCtx, true) //nolint: staticcheck
  10. }
  11. func isRestrictedModeContext(ctx context.Context) bool {
  12. v := ctx.Value(restrictedLoadModeCtx)
  13. if v == nil {
  14. return false
  15. }
  16. return v.(bool)
  17. }
  18. func enforceRestriction(config proto.Message) error {
  19. configDescriptor := config.ProtoReflect().Descriptor()
  20. msgOpts, err := protoext.GetMessageOptions(configDescriptor)
  21. if err != nil {
  22. return newError("unable to find message options").Base(err)
  23. }
  24. if !msgOpts.AllowRestrictedModeLoad {
  25. return newError("component has not opted in for load in restricted mode")
  26. }
  27. return nil
  28. }