github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/features/policy/policy.go (about)

     1  package policy
     2  
     3  import (
     4  	"context"
     5  	"runtime"
     6  	"time"
     7  
     8  	"v2ray.com/core/common/platform"
     9  	"v2ray.com/core/features"
    10  )
    11  
    12  // Timeout contains limits for connection timeout.
    13  type Timeout struct {
    14  	// Timeout for handshake phase in a connection.
    15  	Handshake time.Duration
    16  	// Timeout for connection being idle, i.e., there is no egress or ingress traffic in this connection.
    17  	ConnectionIdle time.Duration
    18  	// Timeout for an uplink only connection, i.e., the downlink of the connection has been closed.
    19  	UplinkOnly time.Duration
    20  	// Timeout for an downlink only connection, i.e., the uplink of the connection has been closed.
    21  	DownlinkOnly time.Duration
    22  }
    23  
    24  // Stats contains settings for stats counters.
    25  type Stats struct {
    26  	// Whether or not to enable stat counter for user uplink traffic.
    27  	UserUplink bool
    28  	// Whether or not to enable stat counter for user downlink traffic.
    29  	UserDownlink bool
    30  }
    31  
    32  // Buffer contains settings for internal buffer.
    33  type Buffer struct {
    34  	// Size of buffer per connection, in bytes. -1 for unlimited buffer.
    35  	PerConnection int32
    36  	Rate          uint64
    37  }
    38  
    39  // SystemStats contains stat policy settings on system level.
    40  type SystemStats struct {
    41  	// Whether or not to enable stat counter for uplink traffic in inbound handlers.
    42  	InboundUplink bool
    43  	// Whether or not to enable stat counter for downlink traffic in inbound handlers.
    44  	InboundDownlink bool
    45  	// Whether or not to enable stat counter for uplink traffic in outbound handlers.
    46  	OutboundUplink bool
    47  	// Whether or not to enable stat counter for downlink traffic in outbound handlers.
    48  	OutboundDownlink bool
    49  }
    50  
    51  // System contains policy settings at system level.
    52  type System struct {
    53  	Stats  SystemStats
    54  	Buffer Buffer
    55  }
    56  
    57  // Session is session based settings for controlling V2Ray requests. It contains various settings (or limits) that may differ for different users in the context.
    58  type Session struct {
    59  	Timeouts Timeout // Timeout settings
    60  	Stats    Stats
    61  	Buffer   Buffer
    62  }
    63  
    64  // Manager is a feature that provides Policy for the given user by its id or level.
    65  //
    66  // v2ray:api:stable
    67  type Manager interface {
    68  	features.Feature
    69  
    70  	// ForLevel returns the Session policy for the given user level.
    71  	ForLevel(level uint32) Session
    72  
    73  	// ForSystem returns the System policy for V2Ray system.
    74  	ForSystem() System
    75  }
    76  
    77  // ManagerType returns the type of Manager interface. Can be used to implement common.HasType.
    78  //
    79  // v2ray:api:stable
    80  func ManagerType() interface{} {
    81  	return (*Manager)(nil)
    82  }
    83  
    84  var defaultBufferSize int32
    85  var defaultRate uint64
    86  
    87  func init() {
    88  	const key = "v2ray.ray.buffer.size"
    89  	const defaultValue = -17
    90  	size := platform.EnvFlag{
    91  		Name:    key,
    92  		AltName: platform.NormalizeEnvName(key),
    93  	}.GetValueAsInt(defaultValue)
    94  
    95  	switch size {
    96  	case 0:
    97  		defaultBufferSize = -1 // For pipe to use unlimited size
    98  	case defaultValue: // Env flag not defined. Use default values per CPU-arch.
    99  		switch runtime.GOARCH {
   100  		case "arm", "mips", "mipsle":
   101  			defaultBufferSize = 0
   102  		case "arm64", "mips64", "mips64le":
   103  			defaultBufferSize = 4 * 1024 // 4k cache for low-end devices
   104  		default:
   105  			defaultBufferSize = 512 * 1024
   106  		}
   107  	default:
   108  		defaultBufferSize = int32(size) * 1024 * 1024
   109  	}
   110  }
   111  
   112  func defaultBufferPolicy() Buffer {
   113  	return Buffer{
   114  		PerConnection: defaultBufferSize,
   115  		Rate:          defaultRate,
   116  	}
   117  }
   118  
   119  // SessionDefault returns the Policy when user is not specified.
   120  func SessionDefault() Session {
   121  	return Session{
   122  		Timeouts: Timeout{
   123  			//Align Handshake timeout with nginx client_header_timeout
   124  			//So that this value will not indicate server identity
   125  			Handshake:      time.Second * 60,
   126  			ConnectionIdle: time.Second * 300,
   127  			UplinkOnly:     time.Second * 1,
   128  			DownlinkOnly:   time.Second * 1,
   129  		},
   130  		Stats: Stats{
   131  			UserUplink:   false,
   132  			UserDownlink: false,
   133  		},
   134  		Buffer: defaultBufferPolicy(),
   135  	}
   136  }
   137  
   138  type policyKey int32
   139  
   140  const (
   141  	bufferPolicyKey policyKey = 0
   142  )
   143  
   144  func ContextWithBufferPolicy(ctx context.Context, p Buffer) context.Context {
   145  	return context.WithValue(ctx, bufferPolicyKey, p)
   146  }
   147  
   148  func BufferPolicyFromContext(ctx context.Context) Buffer {
   149  	pPolicy := ctx.Value(bufferPolicyKey)
   150  	if pPolicy == nil {
   151  		return defaultBufferPolicy()
   152  	}
   153  	return pPolicy.(Buffer)
   154  }