1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/policy.go

186 lines
4.2 KiB
Go
Raw Normal View History

package core
import (
2018-05-25 06:08:28 -04:00
"context"
"sync"
"time"
2018-02-08 09:39:46 -05:00
"v2ray.com/core/common"
2018-05-25 06:08:28 -04:00
"v2ray.com/core/common/platform"
)
// TimeoutPolicy contains limits for connection timeout.
type TimeoutPolicy struct {
// Timeout for handshake phase in a connection.
Handshake time.Duration
// Timeout for connection being idle, i.e., there is no egress or ingress traffic in this connection.
ConnectionIdle time.Duration
2018-02-20 07:53:07 -05:00
// Timeout for an uplink only connection, i.e., the downlink of the connection has been closed.
UplinkOnly time.Duration
2018-02-20 07:53:07 -05:00
// Timeout for an downlink only connection, i.e., the uplink of the connection has been closed.
DownlinkOnly time.Duration
}
2018-04-03 18:57:44 -04:00
// StatsPolicy contains settings for stats counters.
2018-03-30 17:39:54 -04:00
type StatsPolicy struct {
2018-04-03 18:57:44 -04:00
// Whether or not to enable stat counter for user uplink traffic.
UserUplink bool
// Whether or not to enable stat counter for user downlink traffic.
2018-03-31 14:30:49 -04:00
UserDownlink bool
2018-03-30 17:39:54 -04:00
}
2018-05-25 06:08:28 -04:00
// BufferPolicy contains settings for internal buffer.
type BufferPolicy struct {
2018-05-25 07:12:00 -04:00
// Size of buffer per connection, in bytes. -1 for unlimited buffer.
PerConnection int32
2018-05-25 06:08:28 -04:00
}
2018-05-31 05:55:11 -04:00
// SystemStatsPolicy contains stat policy settings on system level.
2018-04-11 18:10:14 -04:00
type SystemStatsPolicy struct {
// Whether or not to enable stat counter for uplink traffic in inbound handlers.
InboundUplink bool
// Whether or not to enable stat counter for downlink traffic in inbound handlers.
InboundDownlink bool
}
2018-05-31 05:55:11 -04:00
// SystemPolicy contains policy settings at system level.
2018-04-11 18:10:14 -04:00
type SystemPolicy struct {
2018-05-25 06:08:28 -04:00
Stats SystemStatsPolicy
Buffer BufferPolicy
2018-04-11 18:10:14 -04:00
}
// Policy is session based settings for controlling V2Ray requests. It contains various settings (or limits) that may differ for different users in the context.
type Policy struct {
Timeouts TimeoutPolicy // Timeout settings
2018-03-30 17:39:54 -04:00
Stats StatsPolicy
2018-05-25 06:08:28 -04:00
Buffer BufferPolicy
}
// PolicyManager is a feature that provides Policy for the given user by its id or level.
type PolicyManager interface {
Feature
// ForLevel returns the Policy for the given user level.
ForLevel(level uint32) Policy
2018-04-11 18:10:14 -04:00
// ForSystem returns the Policy for V2Ray system.
ForSystem() SystemPolicy
}
2018-05-25 07:12:00 -04:00
var defaultBufferSize int32 = 10 * 1024 * 1024
2018-05-25 06:08:28 -04:00
func init() {
const key = "v2ray.ray.buffer.size"
size := platform.EnvFlag{
Name: key,
AltName: platform.NormalizeEnvName(key),
}.GetValueAsInt(10)
if size == 0 {
2018-05-25 07:12:00 -04:00
defaultBufferSize = -1
2018-05-25 06:08:28 -04:00
} else {
2018-05-25 07:12:00 -04:00
defaultBufferSize = int32(size) * 1024 * 1024
2018-05-25 06:08:28 -04:00
}
}
func defaultBufferPolicy() BufferPolicy {
return BufferPolicy{
2018-05-25 07:12:00 -04:00
PerConnection: defaultBufferSize,
2018-05-25 06:08:28 -04:00
}
}
// DefaultPolicy returns the Policy when user is not specified.
func DefaultPolicy() Policy {
return Policy{
Timeouts: TimeoutPolicy{
Handshake: time.Second * 4,
ConnectionIdle: time.Second * 300,
2018-04-11 18:10:14 -04:00
UplinkOnly: time.Second * 2,
DownlinkOnly: time.Second * 5,
},
2018-03-30 17:39:54 -04:00
Stats: StatsPolicy{
2018-03-31 14:30:49 -04:00
UserUplink: false,
UserDownlink: false,
2018-03-30 17:39:54 -04:00
},
2018-05-25 06:08:28 -04:00
Buffer: defaultBufferPolicy(),
}
}
type policyKey int
const (
bufferPolicyKey policyKey = 0
)
func ContextWithBufferPolicy(ctx context.Context, p BufferPolicy) context.Context {
return context.WithValue(ctx, bufferPolicyKey, p)
}
func BufferPolicyFromContext(ctx context.Context) BufferPolicy {
pPolicy := ctx.Value(bufferPolicyKey)
if pPolicy == nil {
return defaultBufferPolicy()
}
2018-05-25 06:08:28 -04:00
return pPolicy.(BufferPolicy)
}
type syncPolicyManager struct {
sync.RWMutex
PolicyManager
}
func (m *syncPolicyManager) ForLevel(level uint32) Policy {
m.RLock()
defer m.RUnlock()
if m.PolicyManager == nil {
p := DefaultPolicy()
if level == 1 {
p.Timeouts.ConnectionIdle = time.Second * 600
}
return p
}
return m.PolicyManager.ForLevel(level)
}
2018-04-12 03:57:41 -04:00
func (m *syncPolicyManager) ForSystem() SystemPolicy {
m.RLock()
defer m.RUnlock()
if m.PolicyManager == nil {
return SystemPolicy{}
}
return m.PolicyManager.ForSystem()
}
func (m *syncPolicyManager) Start() error {
m.RLock()
defer m.RUnlock()
if m.PolicyManager == nil {
return nil
}
return m.PolicyManager.Start()
}
2018-02-08 09:39:46 -05:00
func (m *syncPolicyManager) Close() error {
m.RLock()
defer m.RUnlock()
2018-02-08 09:39:46 -05:00
return common.Close(m.PolicyManager)
}
func (m *syncPolicyManager) Set(manager PolicyManager) {
if manager == nil {
return
}
m.Lock()
defer m.Unlock()
2018-05-31 05:55:11 -04:00
common.Close(m.PolicyManager) // nolint: errcheck
m.PolicyManager = manager
}