github.com/imannamdari/v2ray-core/v5@v5.0.5/features/policy/policy.go (about)

     1  package policy
     2  
     3  import (
     4  	"context"
     5  	"runtime"
     6  	"time"
     7  
     8  	"github.com/imannamdari/v2ray-core/v5/common/platform"
     9  	"github.com/imannamdari/v2ray-core/v5/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  }
    37  
    38  // SystemStats contains stat policy settings on system level.
    39  type SystemStats struct {
    40  	// Whether or not to enable stat counter for uplink traffic in inbound handlers.
    41  	InboundUplink bool
    42  	// Whether or not to enable stat counter for downlink traffic in inbound handlers.
    43  	InboundDownlink bool
    44  	// Whether or not to enable stat counter for uplink traffic in outbound handlers.
    45  	OutboundUplink bool
    46  	// Whether or not to enable stat counter for downlink traffic in outbound handlers.
    47  	OutboundDownlink bool
    48  }
    49  
    50  // System contains policy settings at system level.
    51  type System struct {
    52  	Stats                 SystemStats
    53  	OverrideAccessLogDest bool
    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  
    86  func init() {
    87  	const key = "v2ray.ray.buffer.size"
    88  	const defaultValue = -17
    89  	size := platform.EnvFlag{
    90  		Name:    key,
    91  		AltName: platform.NormalizeEnvName(key),
    92  	}.GetValueAsInt(defaultValue)
    93  
    94  	switch size {
    95  	case 0:
    96  		defaultBufferSize = -1 // For pipe to use unlimited size
    97  	case defaultValue: // Env flag not defined. Use default values per CPU-arch.
    98  		switch runtime.GOARCH {
    99  		case "arm", "mips", "mipsle":
   100  			defaultBufferSize = 0
   101  		case "arm64", "mips64", "mips64le":
   102  			defaultBufferSize = 4 * 1024 // 4k cache for low-end devices
   103  		default:
   104  			defaultBufferSize = 512 * 1024
   105  		}
   106  	default:
   107  		defaultBufferSize = int32(size) * 1024 * 1024
   108  	}
   109  }
   110  
   111  func defaultBufferPolicy() Buffer {
   112  	return Buffer{
   113  		PerConnection: defaultBufferSize,
   114  	}
   115  }
   116  
   117  // SessionDefault returns the Policy when user is not specified.
   118  func SessionDefault() Session {
   119  	return Session{
   120  		Timeouts: Timeout{
   121  			// Align Handshake timeout with nginx client_header_timeout
   122  			// So that this value will not indicate server identity
   123  			Handshake:      time.Second * 60,
   124  			ConnectionIdle: time.Second * 300,
   125  			UplinkOnly:     time.Second * 1,
   126  			DownlinkOnly:   time.Second * 1,
   127  		},
   128  		Stats: Stats{
   129  			UserUplink:   false,
   130  			UserDownlink: false,
   131  		},
   132  		Buffer: defaultBufferPolicy(),
   133  	}
   134  }
   135  
   136  type policyKey int32
   137  
   138  const (
   139  	bufferPolicyKey policyKey = 0
   140  )
   141  
   142  func ContextWithBufferPolicy(ctx context.Context, p Buffer) context.Context {
   143  	return context.WithValue(ctx, bufferPolicyKey, p)
   144  }
   145  
   146  func BufferPolicyFromContext(ctx context.Context) Buffer {
   147  	pPolicy := ctx.Value(bufferPolicyKey)
   148  	if pPolicy == nil {
   149  		return defaultBufferPolicy()
   150  	}
   151  	return pPolicy.(Buffer)
   152  }