2018-01-10 06:22:37 -05:00
package core
import (
"sync"
"time"
2018-02-08 09:39:46 -05:00
"v2ray.com/core/common"
2018-01-10 06:22:37 -05:00
)
// 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.
2018-01-10 06:22:37 -05:00
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.
2018-01-10 06:22:37 -05:00
DownlinkOnly time . Duration
}
2018-03-30 17:39:54 -04:00
type StatsPolicy struct {
2018-03-31 14:30:49 -04:00
UserUplink bool
UserDownlink bool
2018-03-30 17:39:54 -04:00
}
2018-01-10 06:22:37 -05: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-01-10 06:22:37 -05:00
}
// 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
}
// DefaultPolicy returns the Policy when user is not specified.
func DefaultPolicy ( ) Policy {
return Policy {
Timeouts : TimeoutPolicy {
Handshake : time . Second * 4 ,
ConnectionIdle : time . Second * 300 ,
UplinkOnly : time . Second * 5 ,
DownlinkOnly : time . Second * 30 ,
} ,
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-01-10 06:22:37 -05:00
}
}
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 )
}
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 {
2018-01-10 06:22:37 -05:00
m . RLock ( )
defer m . RUnlock ( )
2018-02-08 09:39:46 -05:00
return common . Close ( m . PolicyManager )
2018-01-10 06:22:37 -05:00
}
func ( m * syncPolicyManager ) Set ( manager PolicyManager ) {
if manager == nil {
return
}
m . Lock ( )
defer m . Unlock ( )
2018-02-20 16:07:58 -05:00
common . Close ( m . PolicyManager )
2018-01-10 06:22:37 -05:00
m . PolicyManager = manager
}