github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/infra/conf/policy.go (about) 1 package conf 2 3 import ( 4 "v2ray.com/core/app/policy" 5 ) 6 7 type Policy struct { 8 Handshake *uint32 `json:"handshake"` 9 ConnectionIdle *uint32 `json:"connIdle"` 10 UplinkOnly *uint32 `json:"uplinkOnly"` 11 DownlinkOnly *uint32 `json:"downlinkOnly"` 12 StatsUserUplink bool `json:"statsUserUplink"` 13 StatsUserDownlink bool `json:"statsUserDownlink"` 14 BufferSize *int32 `json:"bufferSize"` 15 Rate *uint64 `json:"rate"` 16 } 17 18 func (t *Policy) Build() (*policy.Policy, error) { 19 config := new(policy.Policy_Timeout) 20 if t.Handshake != nil { 21 config.Handshake = &policy.Second{Value: *t.Handshake} 22 } 23 if t.ConnectionIdle != nil { 24 config.ConnectionIdle = &policy.Second{Value: *t.ConnectionIdle} 25 } 26 if t.UplinkOnly != nil { 27 config.UplinkOnly = &policy.Second{Value: *t.UplinkOnly} 28 } 29 if t.DownlinkOnly != nil { 30 config.DownlinkOnly = &policy.Second{Value: *t.DownlinkOnly} 31 } 32 33 p := &policy.Policy{ 34 Timeout: config, 35 Stats: &policy.Policy_Stats{ 36 UserUplink: t.StatsUserUplink, 37 UserDownlink: t.StatsUserDownlink, 38 }, 39 } 40 41 var bs int32 42 var rate uint64 43 44 if t.BufferSize != nil { 45 bs = int32(-1) 46 if *t.BufferSize >= 0 { 47 bs = (*t.BufferSize) * 1024 48 } 49 if t.Rate != nil { 50 rate = *t.Rate 51 } 52 53 p.Buffer = &policy.Policy_Buffer{ 54 Connection: bs, 55 Rate: rate, 56 } 57 } 58 return p, nil 59 } 60 61 type SystemPolicy struct { 62 StatsInboundUplink bool `json:"statsInboundUplink"` 63 StatsInboundDownlink bool `json:"statsInboundDownlink"` 64 StatsOutboundUplink bool `json:"statsOutboundUplink"` 65 StatsOutboundDownlink bool `json:"statsOutboundDownlink"` 66 } 67 68 func (p *SystemPolicy) Build() (*policy.SystemPolicy, error) { 69 return &policy.SystemPolicy{ 70 Stats: &policy.SystemPolicy_Stats{ 71 InboundUplink: p.StatsInboundUplink, 72 InboundDownlink: p.StatsInboundDownlink, 73 OutboundUplink: p.StatsOutboundUplink, 74 OutboundDownlink: p.StatsOutboundDownlink, 75 }, 76 }, nil 77 } 78 79 type PolicyConfig struct { 80 Levels map[uint32]*Policy `json:"levels"` 81 System *SystemPolicy `json:"system"` 82 } 83 84 func (c *PolicyConfig) Build() (*policy.Config, error) { 85 levels := make(map[uint32]*policy.Policy) 86 for l, p := range c.Levels { 87 if p != nil { 88 pp, err := p.Build() 89 if err != nil { 90 return nil, err 91 } 92 levels[l] = pp 93 } 94 } 95 config := &policy.Config{ 96 Level: levels, 97 } 98 99 if c.System != nil { 100 sc, err := c.System.Build() 101 if err != nil { 102 return nil, err 103 } 104 config.System = sc 105 } 106 107 return config, nil 108 }