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

     1  // +build !confonly
     2  
     3  package stats
     4  
     5  //go:generate go run v2ray.com/core/common/errors/errorgen
     6  
     7  import (
     8  	
     9  	"sync"
    10  	"bytes"
    11      "net"
    12  )
    13  
    14  type IPStorager struct {
    15  	access sync.RWMutex
    16  	ips []net.IP
    17  }
    18  
    19  func (s *IPStorager) Add(ip net.IP) bool {
    20  	s.access.Lock()
    21  	defer s.access.Unlock()
    22  
    23  	for _, _ip := range s.ips {
    24  		if bytes.Equal(_ip, ip) {
    25  			return false
    26  		}
    27  	}
    28  
    29  	s.ips = append(s.ips, ip)
    30  
    31  	return true
    32  }
    33  
    34  func (s *IPStorager) Empty() {
    35  	s.access.Lock()
    36  	defer s.access.Unlock()
    37  
    38  	s.ips = s.ips[:0]
    39  }
    40  
    41  func (s *IPStorager) Remove(removeIP net.IP) bool {
    42  	s.access.Lock()
    43  	defer s.access.Unlock()
    44  
    45  	for i, ip := range s.ips {
    46  		if bytes.Equal(ip, removeIP) {
    47  			s.ips = append(s.ips[:i], s.ips[i+1:]...)
    48  			return true
    49  		}
    50  	}
    51  
    52  	return false
    53  }
    54  
    55  func (s *IPStorager) All() []net.IP {
    56  	s.access.RLock()
    57  	defer s.access.RUnlock()
    58  
    59  	newIPs := make([]net.IP, len(s.ips))
    60  	copy(newIPs, s.ips)
    61  
    62  	return newIPs
    63  }