mirror of https://github.com/XTLS/Xray-core
feat(api): update timestamp for existing IPs in AddIP instead of skipping (#4989)
Co-authored-by: null <null>pull/4998/head
parent
8222f43eea
commit
9359844149
|
@ -40,13 +40,13 @@ func (c *OnlineMap) AddIP(ip string) {
|
||||||
if ip == "127.0.0.1" {
|
if ip == "127.0.0.1" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.access.Lock()
|
c.access.Lock()
|
||||||
if _, ok := c.ipList[ip]; !ok {
|
|
||||||
c.ipList[ip] = time.Now()
|
c.ipList[ip] = time.Now()
|
||||||
}
|
|
||||||
c.access.Unlock()
|
c.access.Unlock()
|
||||||
|
|
||||||
if time.Since(c.lastCleanup) > c.cleanupPeriod {
|
if time.Since(c.lastCleanup) > c.cleanupPeriod {
|
||||||
c.RemoveExpiredIPs(c.ipList)
|
c.RemoveExpiredIPs()
|
||||||
c.lastCleanup = time.Now()
|
c.lastCleanup = time.Now()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,23 +62,22 @@ func (c *OnlineMap) GetKeys() []string {
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *OnlineMap) RemoveExpiredIPs(list map[string]time.Time) map[string]time.Time {
|
func (c *OnlineMap) RemoveExpiredIPs() {
|
||||||
c.access.Lock()
|
c.access.Lock()
|
||||||
defer c.access.Unlock()
|
defer c.access.Unlock()
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
for k, t := range list {
|
for k, t := range c.ipList {
|
||||||
diff := now.Sub(t)
|
diff := now.Sub(t)
|
||||||
if diff.Seconds() > 20 {
|
if diff.Seconds() > 20 {
|
||||||
delete(list, k)
|
delete(c.ipList, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return list
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *OnlineMap) IpTimeMap() map[string]time.Time {
|
func (c *OnlineMap) IpTimeMap() map[string]time.Time {
|
||||||
if time.Since(c.lastCleanup) > c.cleanupPeriod {
|
if time.Since(c.lastCleanup) > c.cleanupPeriod {
|
||||||
c.RemoveExpiredIPs(c.ipList)
|
c.RemoveExpiredIPs()
|
||||||
c.lastCleanup = time.Now()
|
c.lastCleanup = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue