2017-11-27 16:09:30 -05:00
|
|
|
package policy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2018-01-10 06:22:37 -05:00
|
|
|
|
|
|
|
"v2ray.com/core"
|
2017-11-27 16:09:30 -05:00
|
|
|
)
|
|
|
|
|
2017-12-14 09:02:36 -05:00
|
|
|
// Duration converts Second to time.Duration.
|
2017-11-27 16:09:30 -05:00
|
|
|
func (s *Second) Duration() time.Duration {
|
2018-01-10 06:22:37 -05:00
|
|
|
if s == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2017-11-27 16:09:30 -05:00
|
|
|
return time.Second * time.Duration(s.Value)
|
|
|
|
}
|
|
|
|
|
2018-02-20 07:53:07 -05:00
|
|
|
func defaultPolicy() *Policy {
|
|
|
|
p := core.DefaultPolicy()
|
|
|
|
|
|
|
|
return &Policy{
|
|
|
|
Timeout: &Policy_Timeout{
|
|
|
|
Handshake: &Second{Value: uint32(p.Timeouts.Handshake / time.Second)},
|
|
|
|
ConnectionIdle: &Second{Value: uint32(p.Timeouts.ConnectionIdle / time.Second)},
|
|
|
|
UplinkOnly: &Second{Value: uint32(p.Timeouts.UplinkOnly / time.Second)},
|
|
|
|
DownlinkOnly: &Second{Value: uint32(p.Timeouts.DownlinkOnly / time.Second)},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Policy_Timeout) overrideWith(another *Policy_Timeout) {
|
|
|
|
if another.Handshake != nil {
|
|
|
|
p.Handshake = &Second{Value: another.Handshake.Value}
|
|
|
|
}
|
|
|
|
if another.ConnectionIdle != nil {
|
|
|
|
p.ConnectionIdle = &Second{Value: another.ConnectionIdle.Value}
|
|
|
|
}
|
|
|
|
if another.UplinkOnly != nil {
|
|
|
|
p.UplinkOnly = &Second{Value: another.UplinkOnly.Value}
|
|
|
|
}
|
|
|
|
if another.DownlinkOnly != nil {
|
|
|
|
p.DownlinkOnly = &Second{Value: another.DownlinkOnly.Value}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Policy) overrideWith(another *Policy) {
|
|
|
|
if another.Timeout != nil {
|
|
|
|
p.Timeout.overrideWith(another.Timeout)
|
|
|
|
}
|
2018-03-30 17:39:54 -04:00
|
|
|
if another.Stats != nil && p.Stats == nil {
|
|
|
|
p.Stats = new(Policy_Stats)
|
|
|
|
*p.Stats = *another.Stats
|
|
|
|
}
|
2018-02-20 07:53:07 -05:00
|
|
|
}
|
|
|
|
|
2018-04-03 18:57:44 -04:00
|
|
|
// ToCorePolicy converts this Policy to core.Policy.
|
2018-01-10 06:22:37 -05:00
|
|
|
func (p *Policy) ToCorePolicy() core.Policy {
|
|
|
|
var cp core.Policy
|
|
|
|
if p.Timeout != nil {
|
|
|
|
cp.Timeouts.ConnectionIdle = p.Timeout.ConnectionIdle.Duration()
|
|
|
|
cp.Timeouts.Handshake = p.Timeout.Handshake.Duration()
|
|
|
|
cp.Timeouts.DownlinkOnly = p.Timeout.DownlinkOnly.Duration()
|
|
|
|
cp.Timeouts.UplinkOnly = p.Timeout.UplinkOnly.Duration()
|
|
|
|
}
|
2018-03-30 17:39:54 -04:00
|
|
|
if p.Stats != nil {
|
2018-03-31 14:30:49 -04:00
|
|
|
cp.Stats.UserUplink = p.Stats.UserUplink
|
|
|
|
cp.Stats.UserDownlink = p.Stats.UserDownlink
|
2018-03-30 17:39:54 -04:00
|
|
|
}
|
2018-01-10 06:22:37 -05:00
|
|
|
return cp
|
|
|
|
}
|
2018-04-11 18:10:14 -04:00
|
|
|
|
|
|
|
func (p *SystemPolicy) ToCorePolicy() core.SystemPolicy {
|
|
|
|
return core.SystemPolicy{
|
|
|
|
Stats: core.SystemStatsPolicy{
|
|
|
|
InboundUplink: p.Stats.InboundUplink,
|
|
|
|
InboundDownlink: p.Stats.InboundDownlink,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|