diff --git a/cred/manager.go b/cred/manager.go index 2a39b7b..90ac4ea 100644 --- a/cred/manager.go +++ b/cred/manager.go @@ -13,6 +13,7 @@ import ( "github.com/database64128/shadowsocks-go/maps" "github.com/database64128/shadowsocks-go/mmap" + "github.com/database64128/shadowsocks-go/slices" "github.com/database64128/shadowsocks-go/ss2022" "go.uber.org/zap" ) @@ -44,6 +45,11 @@ type UserCredential struct { UPSK []byte `json:"uPSK"` } +// Less is useful for sorting user credentials by username. +func (uc UserCredential) Less(other UserCredential) bool { + return uc.Name < other.Name +} + type cachedUserCredential struct { uPSK []byte uPSKHash [ss2022.IdentityHeaderLength]byte @@ -60,6 +66,7 @@ func (s *ManagedServer) Credentials() []UserCredential { }) } s.mu.RUnlock() + slices.SortFunc(ucs, UserCredential.Less) return ucs } diff --git a/stats/collector.go b/stats/collector.go index d8ae453..13b6982 100644 --- a/stats/collector.go +++ b/stats/collector.go @@ -3,6 +3,8 @@ package stats import ( "sync" "sync/atomic" + + "github.com/database64128/shadowsocks-go/slices" ) type trafficCollector struct { @@ -82,6 +84,11 @@ type User struct { Traffic } +// Less is useful for sorting users by name. +func (u User) Less(other User) bool { + return u.Name < other.Name +} + func (uc *userCollector) snapshot(username string) User { return User{ Name: username, @@ -164,6 +171,7 @@ func (sc *serverCollector) Snapshot() (s Server) { s.Users = append(s.Users, u) } sc.mu.RUnlock() + slices.SortFunc(s.Users, User.Less) return } @@ -178,6 +186,7 @@ func (sc *serverCollector) SnapshotAndReset() (s Server) { s.Users = append(s.Users, u) } sc.mu.RUnlock() + slices.SortFunc(s.Users, User.Less) return }