Hunter0x7c7
2022-08-11 b8230139fb40edea387617b6accd8371e37eda58
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
94
95
96
97
98
99
100
package v4
 
import (
    "github.com/v2fly/v2ray-core/v5/app/policy"
)
 
type Policy struct {
    Handshake         *uint32 `json:"handshake"`
    ConnectionIdle    *uint32 `json:"connIdle"`
    UplinkOnly        *uint32 `json:"uplinkOnly"`
    DownlinkOnly      *uint32 `json:"downlinkOnly"`
    StatsUserUplink   bool    `json:"statsUserUplink"`
    StatsUserDownlink bool    `json:"statsUserDownlink"`
    BufferSize        *int32  `json:"bufferSize"`
}
 
func (t *Policy) Build() (*policy.Policy, error) {
    config := new(policy.Policy_Timeout)
    if t.Handshake != nil {
        config.Handshake = &policy.Second{Value: *t.Handshake}
    }
    if t.ConnectionIdle != nil {
        config.ConnectionIdle = &policy.Second{Value: *t.ConnectionIdle}
    }
    if t.UplinkOnly != nil {
        config.UplinkOnly = &policy.Second{Value: *t.UplinkOnly}
    }
    if t.DownlinkOnly != nil {
        config.DownlinkOnly = &policy.Second{Value: *t.DownlinkOnly}
    }
 
    p := &policy.Policy{
        Timeout: config,
        Stats: &policy.Policy_Stats{
            UserUplink:   t.StatsUserUplink,
            UserDownlink: t.StatsUserDownlink,
        },
    }
 
    if t.BufferSize != nil {
        bs := int32(-1)
        if *t.BufferSize >= 0 {
            bs = (*t.BufferSize) * 1024
        }
        p.Buffer = &policy.Policy_Buffer{
            Connection: bs,
        }
    }
 
    return p, nil
}
 
type SystemPolicy struct {
    StatsInboundUplink    bool `json:"statsInboundUplink"`
    StatsInboundDownlink  bool `json:"statsInboundDownlink"`
    StatsOutboundUplink   bool `json:"statsOutboundUplink"`
    StatsOutboundDownlink bool `json:"statsOutboundDownlink"`
}
 
func (p *SystemPolicy) Build() (*policy.SystemPolicy, error) {
    return &policy.SystemPolicy{
        Stats: &policy.SystemPolicy_Stats{
            InboundUplink:    p.StatsInboundUplink,
            InboundDownlink:  p.StatsInboundDownlink,
            OutboundUplink:   p.StatsOutboundUplink,
            OutboundDownlink: p.StatsOutboundDownlink,
        },
    }, nil
}
 
type PolicyConfig struct {
    Levels map[uint32]*Policy `json:"levels"`
    System *SystemPolicy      `json:"system"`
}
 
func (c *PolicyConfig) Build() (*policy.Config, error) {
    levels := make(map[uint32]*policy.Policy)
    for l, p := range c.Levels {
        if p != nil {
            pp, err := p.Build()
            if err != nil {
                return nil, err
            }
            levels[l] = pp
        }
    }
    config := &policy.Config{
        Level: levels,
    }
 
    if c.System != nil {
        sc, err := c.System.Build()
        if err != nil {
            return nil, err
        }
        config.System = sc
    }
 
    return config, nil
}