1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-12 02:40:42 +00:00
v2fly/app/policy/config.go

95 lines
2.5 KiB
Go
Raw Permalink Normal View History

2017-11-27 21:09:30 +00:00
package policy
import (
"time"
"github.com/v2fly/v2ray-core/v5/features/policy"
2017-11-27 21:09:30 +00:00
)
2017-12-14 14:02:36 +00:00
// Duration converts Second to time.Duration.
2017-11-27 21:09:30 +00:00
func (s *Second) Duration() time.Duration {
if s == nil {
return 0
}
2017-11-27 21:09:30 +00:00
return time.Second * time.Duration(s.Value)
}
2018-02-20 12:53:07 +00:00
func defaultPolicy() *Policy {
2018-10-11 20:34:31 +00:00
p := policy.SessionDefault()
2018-02-20 12:53:07 +00:00
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)},
},
2018-05-25 10:08:28 +00:00
Buffer: &Policy_Buffer{
2018-05-25 11:12:00 +00:00
Connection: p.Buffer.PerConnection,
2018-05-25 10:08:28 +00:00
},
2018-02-20 12:53:07 +00:00
}
}
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 21:39:54 +00:00
if another.Stats != nil && p.Stats == nil {
2020-08-26 11:35:33 +00:00
p.Stats = &Policy_Stats{}
p.Stats = another.Stats
2018-03-30 21:39:54 +00:00
}
2018-07-31 14:46:37 +00:00
if another.Buffer != nil {
p.Buffer = &Policy_Buffer{
Connection: another.Buffer.Connection,
}
}
2018-02-20 12:53:07 +00:00
}
2018-10-11 20:34:31 +00:00
// ToCorePolicy converts this Policy to policy.Session.
func (p *Policy) ToCorePolicy() policy.Session {
cp := policy.SessionDefault()
2018-05-25 10:08:28 +00:00
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 21:39:54 +00:00
if p.Stats != nil {
2018-03-31 18:30:49 +00:00
cp.Stats.UserUplink = p.Stats.UserUplink
cp.Stats.UserDownlink = p.Stats.UserDownlink
2018-03-30 21:39:54 +00:00
}
2018-05-25 10:08:28 +00:00
if p.Buffer != nil {
2018-05-25 11:12:00 +00:00
cp.Buffer.PerConnection = p.Buffer.Connection
2018-05-25 10:08:28 +00:00
}
return cp
}
2018-04-11 22:10:14 +00:00
2018-10-11 20:34:31 +00:00
// ToCorePolicy converts this SystemPolicy to policy.System.
func (p *SystemPolicy) ToCorePolicy() policy.System {
return policy.System{
Stats: policy.SystemStats{
2020-06-23 03:55:00 +00:00
InboundUplink: p.Stats.InboundUplink,
InboundDownlink: p.Stats.InboundDownlink,
OutboundUplink: p.Stats.OutboundUplink,
OutboundDownlink: p.Stats.OutboundDownlink,
2018-04-11 22:10:14 +00:00
},
OverrideAccessLogDest: p.OverrideAccessLogDest,
2018-04-11 22:10:14 +00:00
}
}