github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/common/registry/restrict.go (about)

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