github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/app/policy/config.go (about)

     1  package policy
     2  
     3  import (
     4  	"time"
     5  
     6  	"v2ray.com/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  			Rate:       p.Buffer.Rate,
    30  		},
    31  	}
    32  }
    33  
    34  func (p *Policy_Timeout) overrideWith(another *Policy_Timeout) {
    35  	if another.Handshake != nil {
    36  		p.Handshake = &Second{Value: another.Handshake.Value}
    37  	}
    38  	if another.ConnectionIdle != nil {
    39  		p.ConnectionIdle = &Second{Value: another.ConnectionIdle.Value}
    40  	}
    41  	if another.UplinkOnly != nil {
    42  		p.UplinkOnly = &Second{Value: another.UplinkOnly.Value}
    43  	}
    44  	if another.DownlinkOnly != nil {
    45  		p.DownlinkOnly = &Second{Value: another.DownlinkOnly.Value}
    46  	}
    47  }
    48  
    49  func (p *Policy) overrideWith(another *Policy) {
    50  	if another.Timeout != nil {
    51  		p.Timeout.overrideWith(another.Timeout)
    52  	}
    53  	if another.Stats != nil && p.Stats == nil {
    54  		p.Stats = &Policy_Stats{}
    55  		p.Stats = another.Stats
    56  	}
    57  	if another.Buffer != nil {
    58  		p.Buffer = &Policy_Buffer{
    59  			Connection: another.Buffer.Connection,
    60  			Rate:       another.Buffer.Rate,
    61  		}
    62  	}
    63  }
    64  
    65  // ToCorePolicy converts this Policy to policy.Session.
    66  func (p *Policy) ToCorePolicy() policy.Session {
    67  	cp := policy.SessionDefault()
    68  
    69  	if p.Timeout != nil {
    70  		cp.Timeouts.ConnectionIdle = p.Timeout.ConnectionIdle.Duration()
    71  		cp.Timeouts.Handshake = p.Timeout.Handshake.Duration()
    72  		cp.Timeouts.DownlinkOnly = p.Timeout.DownlinkOnly.Duration()
    73  		cp.Timeouts.UplinkOnly = p.Timeout.UplinkOnly.Duration()
    74  	}
    75  	if p.Stats != nil {
    76  		cp.Stats.UserUplink = p.Stats.UserUplink
    77  		cp.Stats.UserDownlink = p.Stats.UserDownlink
    78  	}
    79  	if p.Buffer != nil {
    80  		cp.Buffer.PerConnection = p.Buffer.Connection
    81  		cp.Buffer.Rate = p.Buffer.Rate
    82  	}
    83  	return cp
    84  }
    85  
    86  // ToCorePolicy converts this SystemPolicy to policy.System.
    87  func (p *SystemPolicy) ToCorePolicy() policy.System {
    88  	return policy.System{
    89  		Stats: policy.SystemStats{
    90  			InboundUplink:    p.Stats.InboundUplink,
    91  			InboundDownlink:  p.Stats.InboundDownlink,
    92  			OutboundUplink:   p.Stats.OutboundUplink,
    93  			OutboundDownlink: p.Stats.OutboundDownlink,
    94  		},
    95  	}
    96  }