github.com/eagleql/xray-core@v1.4.4/app/policy/config.go (about)

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