github.com/EagleQL/Xray-core@v1.4.3/infra/conf/policy.go (about)

     1  package conf
     2  
     3  import (
     4  	"github.com/xtls/xray-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  }
    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  }
    59  
    60  func (p *SystemPolicy) Build() (*policy.SystemPolicy, error) {
    61  	return &policy.SystemPolicy{
    62  		Stats: &policy.SystemPolicy_Stats{
    63  			InboundUplink:    p.StatsInboundUplink,
    64  			InboundDownlink:  p.StatsInboundDownlink,
    65  			OutboundUplink:   p.StatsOutboundUplink,
    66  			OutboundDownlink: p.StatsOutboundDownlink,
    67  		},
    68  	}, nil
    69  }
    70  
    71  type PolicyConfig struct {
    72  	Levels map[uint32]*Policy `json:"levels"`
    73  	System *SystemPolicy      `json:"system"`
    74  }
    75  
    76  func (c *PolicyConfig) Build() (*policy.Config, error) {
    77  	levels := make(map[uint32]*policy.Policy)
    78  	for l, p := range c.Levels {
    79  		if p != nil {
    80  			pp, err := p.Build()
    81  			if err != nil {
    82  				return nil, err
    83  			}
    84  			levels[l] = pp
    85  		}
    86  	}
    87  	config := &policy.Config{
    88  		Level: levels,
    89  	}
    90  
    91  	if c.System != nil {
    92  		sc, err := c.System.Build()
    93  		if err != nil {
    94  			return nil, err
    95  		}
    96  		config.System = sc
    97  	}
    98  
    99  	return config, nil
   100  }