mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-12-18 21:24:37 +03:00
Some checks failed
Scheduled assets update / geodat (push) Has been cancelled
Build and Release for Windows 7 / check-assets (push) Has been cancelled
Build and Release / check-assets (push) Has been cancelled
Test / check-assets (push) Has been cancelled
Build and Release for Windows 7 / build (win7-64, amd64, windows) (push) Has been cancelled
Build and Release / build (mips, linux) (push) Has been cancelled
Build and Release / build (mips64, linux) (push) Has been cancelled
Build and Release / build (mips64le, linux) (push) Has been cancelled
Build and Release / build (mipsle, linux) (push) Has been cancelled
Build and Release / build (ppc64, linux) (push) Has been cancelled
Build and Release / build (ppc64le, linux) (push) Has been cancelled
Build and Release / build (riscv64, linux) (push) Has been cancelled
Build and Release / build (s390x, linux) (push) Has been cancelled
Build and Release for Windows 7 / build (win7-32, 386, windows) (push) Has been cancelled
Build and Release / build (386, freebsd, ) (push) Has been cancelled
Build and Release / build (386, linux, ) (push) Has been cancelled
Build and Release / build (386, openbsd, ) (push) Has been cancelled
Build and Release / build (386, windows, ) (push) Has been cancelled
Build and Release / build (amd64, android, android-amd64) (push) Has been cancelled
Build and Release / build (amd64, darwin, ) (push) Has been cancelled
Build and Release / build (amd64, freebsd, ) (push) Has been cancelled
Build and Release / build (amd64, linux, ) (push) Has been cancelled
Build and Release / build (amd64, openbsd, ) (push) Has been cancelled
Build and Release / build (amd64, windows, ) (push) Has been cancelled
Build and Release / build (arm, 5, linux) (push) Has been cancelled
Build and Release / build (arm, 6, linux) (push) Has been cancelled
Build and Release / build (arm, 7, freebsd) (push) Has been cancelled
Build and Release / build (arm, 7, linux) (push) Has been cancelled
Build and Release / build (arm, 7, openbsd) (push) Has been cancelled
Build and Release / build (arm, 7, windows) (push) Has been cancelled
Build and Release / build (arm64, android) (push) Has been cancelled
Build and Release / build (arm64, darwin) (push) Has been cancelled
Build and Release / build (arm64, freebsd) (push) Has been cancelled
Build and Release / build (arm64, linux) (push) Has been cancelled
Build and Release / build (arm64, openbsd) (push) Has been cancelled
Build and Release / build (arm64, windows) (push) Has been cancelled
Build and Release / build (loong64, linux) (push) Has been cancelled
Test / test (macos-latest) (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
Test / test (windows-latest) (push) Has been cancelled
Co-authored-by: null <null>
86 lines
1.5 KiB
Go
86 lines
1.5 KiB
Go
package stats
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// OnlineMap is an implementation of stats.OnlineMap.
|
|
type OnlineMap struct {
|
|
ipList map[string]time.Time
|
|
access sync.RWMutex
|
|
lastCleanup time.Time
|
|
cleanupPeriod time.Duration
|
|
}
|
|
|
|
// NewOnlineMap creates a new instance of OnlineMap.
|
|
func NewOnlineMap() *OnlineMap {
|
|
return &OnlineMap{
|
|
ipList: make(map[string]time.Time),
|
|
lastCleanup: time.Now(),
|
|
cleanupPeriod: 10 * time.Second,
|
|
}
|
|
}
|
|
|
|
// Count implements stats.OnlineMap.
|
|
func (c *OnlineMap) Count() int {
|
|
c.access.RLock()
|
|
defer c.access.RUnlock()
|
|
|
|
return len(c.ipList)
|
|
}
|
|
|
|
// List implements stats.OnlineMap.
|
|
func (c *OnlineMap) List() []string {
|
|
return c.GetKeys()
|
|
}
|
|
|
|
// AddIP implements stats.OnlineMap.
|
|
func (c *OnlineMap) AddIP(ip string) {
|
|
if ip == "127.0.0.1" {
|
|
return
|
|
}
|
|
|
|
c.access.Lock()
|
|
c.ipList[ip] = time.Now()
|
|
c.access.Unlock()
|
|
|
|
if time.Since(c.lastCleanup) > c.cleanupPeriod {
|
|
c.RemoveExpiredIPs()
|
|
c.lastCleanup = time.Now()
|
|
}
|
|
}
|
|
|
|
func (c *OnlineMap) GetKeys() []string {
|
|
c.access.RLock()
|
|
defer c.access.RUnlock()
|
|
|
|
keys := []string{}
|
|
for k := range c.ipList {
|
|
keys = append(keys, k)
|
|
}
|
|
return keys
|
|
}
|
|
|
|
func (c *OnlineMap) RemoveExpiredIPs() {
|
|
c.access.Lock()
|
|
defer c.access.Unlock()
|
|
|
|
now := time.Now()
|
|
for k, t := range c.ipList {
|
|
diff := now.Sub(t)
|
|
if diff.Seconds() > 20 {
|
|
delete(c.ipList, k)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *OnlineMap) IpTimeMap() map[string]time.Time {
|
|
if time.Since(c.lastCleanup) > c.cleanupPeriod {
|
|
c.RemoveExpiredIPs()
|
|
c.lastCleanup = time.Now()
|
|
}
|
|
|
|
return c.ipList
|
|
}
|