github.com/eagleql/xray-core@v1.4.4/features/policy/policy.go (about)

     1  package policy
     2  
     3  import (
     4  	"context"
     5  	"runtime"
     6  	"time"
     7  
     8  	"github.com/eagleql/xray-core/common/platform"
     9  	"github.com/eagleql/xray-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  }
    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  	Buffer Buffer
    54  }
    55  
    56  // Session is session based settings for controlling Xray requests. It contains various settings (or limits) that may differ for different users in the context.
    57  type Session struct {
    58  	Timeouts Timeout // Timeout settings
    59  	Stats    Stats
    60  	Buffer   Buffer
    61  }
    62  
    63  // Manager is a feature that provides Policy for the given user by its id or level.
    64  //
    65  // xray:api:stable
    66  type Manager interface {
    67  	features.Feature
    68  
    69  	// ForLevel returns the Session policy for the given user level.
    70  	ForLevel(level uint32) Session
    71  
    72  	// ForSystem returns the System policy for Xray system.
    73  	ForSystem() System
    74  }
    75  
    76  // ManagerType returns the type of Manager interface. Can be used to implement common.HasType.
    77  //
    78  // xray:api:stable
    79  func ManagerType() interface{} {
    80  	return (*Manager)(nil)
    81  }
    82  
    83  var defaultBufferSize int32
    84  
    85  func init() {
    86  	const key = "xray.ray.buffer.size"
    87  	const defaultValue = -17
    88  	size := platform.EnvFlag{
    89  		Name:    key,
    90  		AltName: platform.NormalizeEnvName(key),
    91  	}.GetValueAsInt(defaultValue)
    92  
    93  	switch size {
    94  	case 0:
    95  		defaultBufferSize = -1 // For pipe to use unlimited size
    96  	case defaultValue: // Env flag not defined. Use default values per CPU-arch.
    97  		switch runtime.GOARCH {
    98  		case "arm", "mips", "mipsle":
    99  			defaultBufferSize = 0
   100  		case "arm64", "mips64", "mips64le":
   101  			defaultBufferSize = 4 * 1024 // 4k cache for low-end devices
   102  		default:
   103  			defaultBufferSize = 512 * 1024
   104  		}
   105  	default:
   106  		defaultBufferSize = int32(size) * 1024 * 1024
   107  	}
   108  }
   109  
   110  func defaultBufferPolicy() Buffer {
   111  	return Buffer{
   112  		PerConnection: defaultBufferSize,
   113  	}
   114  }
   115  
   116  // SessionDefault returns the Policy when user is not specified.
   117  func SessionDefault() Session {
   118  	return Session{
   119  		Timeouts: Timeout{
   120  			// Align Handshake timeout with nginx client_header_timeout
   121  			// So that this value will not indicate server identity
   122  			Handshake:      time.Second * 60,
   123  			ConnectionIdle: time.Second * 300,
   124  			UplinkOnly:     time.Second * 1,
   125  			DownlinkOnly:   time.Second * 1,
   126  		},
   127  		Stats: Stats{
   128  			UserUplink:   false,
   129  			UserDownlink: false,
   130  		},
   131  		Buffer: defaultBufferPolicy(),
   132  	}
   133  }
   134  
   135  type policyKey int32
   136  
   137  const (
   138  	bufferPolicyKey policyKey = 0
   139  )
   140  
   141  func ContextWithBufferPolicy(ctx context.Context, p Buffer) context.Context {
   142  	return context.WithValue(ctx, bufferPolicyKey, p)
   143  }
   144  
   145  func BufferPolicyFromContext(ctx context.Context) Buffer {
   146  	pPolicy := ctx.Value(bufferPolicyKey)
   147  	if pPolicy == nil {
   148  		return defaultBufferPolicy()
   149  	}
   150  	return pPolicy.(Buffer)
   151  }