Browse Source

Merge pull request #1350 from comwrg/refactor-validator

more readable for func `generateNewHashes` in `proxy/vmess/validator.go`
pull/1352/head
Victoria Raymond 6 years ago committed by GitHub
parent
commit
59fa890332
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      proxy/vmess/validator.go

15
proxy/vmess/validator.go

@ -56,13 +56,14 @@ func NewTimedUserValidator(hasher protocol.IDHash) *TimedUserValidator {
func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, user *user) {
var hashValue [16]byte
genEndSec := nowSec + cacheDurationSec
genHashForID := func(id *protocol.ID) {
idHash := v.hasher(id.Bytes())
lastSec := user.lastSec
if lastSec < nowSec-cacheDurationSec*2 {
lastSec = nowSec - cacheDurationSec*2
genBeginSec := user.lastSec
if genBeginSec < nowSec-cacheDurationSec {
genBeginSec = nowSec - cacheDurationSec
}
for ts := lastSec; ts <= nowSec; ts++ {
for ts := genBeginSec; ts <= genEndSec; ts++ {
common.Must2(idHash.Write(ts.Bytes(nil)))
idHash.Sum(hashValue[:0])
idHash.Reset()
@ -80,7 +81,7 @@ func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, user *
for _, id := range account.AlterIDs {
genHashForID(id)
}
user.lastSec = nowSec
user.lastSec = genEndSec
}
func (v *TimedUserValidator) removeExpiredHashes(expire uint32) {
@ -93,7 +94,7 @@ func (v *TimedUserValidator) removeExpiredHashes(expire uint32) {
func (v *TimedUserValidator) updateUserHash() {
now := time.Now()
nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec)
nowSec := protocol.Timestamp(now.Unix())
v.Lock()
defer v.Unlock()
@ -118,7 +119,7 @@ func (v *TimedUserValidator) Add(u *protocol.MemoryUser) error {
lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
}
v.users = append(v.users, uu)
v.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), uu)
v.generateNewHashes(protocol.Timestamp(nowSec), uu)
return nil
}

Loading…
Cancel
Save