Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package policy
 
import (
    "time"
 
    "github.com/v2fly/v2ray-core/v5/features/policy"
)
 
// Duration converts Second to time.Duration.
func (s *Second) Duration() time.Duration {
    if s == nil {
        return 0
    }
    return time.Second * time.Duration(s.Value)
}
 
func defaultPolicy() *Policy {
    p := policy.SessionDefault()
 
    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)},
        },
        Buffer: &Policy_Buffer{
            Connection: p.Buffer.PerConnection,
        },
    }
}
 
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)
    }
    if another.Stats != nil && p.Stats == nil {
        p.Stats = &Policy_Stats{}
        p.Stats = another.Stats
    }
    if another.Buffer != nil {
        p.Buffer = &Policy_Buffer{
            Connection: another.Buffer.Connection,
        }
    }
}
 
// ToCorePolicy converts this Policy to policy.Session.
func (p *Policy) ToCorePolicy() policy.Session {
    cp := policy.SessionDefault()
 
    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()
    }
    if p.Stats != nil {
        cp.Stats.UserUplink = p.Stats.UserUplink
        cp.Stats.UserDownlink = p.Stats.UserDownlink
    }
    if p.Buffer != nil {
        cp.Buffer.PerConnection = p.Buffer.Connection
    }
    return cp
}
 
// ToCorePolicy converts this SystemPolicy to policy.System.
func (p *SystemPolicy) ToCorePolicy() policy.System {
    return policy.System{
        Stats: policy.SystemStats{
            InboundUplink:    p.Stats.InboundUplink,
            InboundDownlink:  p.Stats.InboundDownlink,
            OutboundUplink:   p.Stats.OutboundUplink,
            OutboundDownlink: p.Stats.OutboundDownlink,
        },
    }
}