github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/infra/conf/v4/policy.go (about)

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