mirror of https://github.com/cbeuw/Cloak
Refactor usedrandom
This commit is contained in:
parent
08cb5d8462
commit
64b39d728f
|
|
@ -15,20 +15,13 @@ var ErrCiphertextLength = errors.New("ciphertext has the wrong length")
|
|||
var ErrTimestampOutOfWindow = errors.New("timestamp is outside of the accepting window")
|
||||
|
||||
func TouchStone(ch *ClientHello, sta *State) (UID []byte, sessionID uint32, proxyMethod string, encryptionMethod byte, sharedSecret []byte, err error) {
|
||||
var random [32]byte
|
||||
copy(random[:], ch.random)
|
||||
|
||||
sta.usedRandomM.Lock()
|
||||
used := sta.usedRandom[random]
|
||||
sta.usedRandom[random] = int(sta.Now().Unix())
|
||||
sta.usedRandomM.Unlock()
|
||||
|
||||
if used != 0 {
|
||||
if sta.registerRandom(ch.random) {
|
||||
err = ErrReplay
|
||||
return
|
||||
}
|
||||
|
||||
ephPub, ok := ecdh.Unmarshal(random[:])
|
||||
ephPub, ok := ecdh.Unmarshal(ch.random)
|
||||
if !ok {
|
||||
err = ErrInvalidPubKey
|
||||
return
|
||||
|
|
@ -48,7 +41,7 @@ func TouchStone(ch *ClientHello, sta *State) (UID []byte, sessionID uint32, prox
|
|||
}
|
||||
|
||||
var plaintext []byte
|
||||
plaintext, err = util.AESGCMDecrypt(random[0:12], sharedSecret, ciphertext)
|
||||
plaintext, err = util.AESGCMDecrypt(ch.random[0:12], sharedSecret, ciphertext)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ type State struct {
|
|||
RedirAddr string
|
||||
|
||||
usedRandomM sync.RWMutex
|
||||
usedRandom map[[32]byte]int
|
||||
usedRandom map[[32]byte]int64
|
||||
|
||||
Panel *userPanel
|
||||
LocalAPIRouter *gmux.Router
|
||||
|
|
@ -48,7 +48,7 @@ func InitState(bindHost, bindPort string, nowFunc func() time.Time) (*State, err
|
|||
BindPort: bindPort,
|
||||
Now: nowFunc,
|
||||
}
|
||||
ret.usedRandom = make(map[[32]byte]int)
|
||||
ret.usedRandom = make(map[[32]byte]int64)
|
||||
go ret.UsedRandomCleaner()
|
||||
return ret, nil
|
||||
}
|
||||
|
|
@ -112,13 +112,23 @@ const TIMESTAMP_WINDOW = 12 * time.Hour
|
|||
func (sta *State) UsedRandomCleaner() {
|
||||
for {
|
||||
time.Sleep(TIMESTAMP_WINDOW)
|
||||
now := int(sta.Now().Unix())
|
||||
now := sta.Now().Unix()
|
||||
sta.usedRandomM.Lock()
|
||||
for key, t := range sta.usedRandom {
|
||||
if now-t > int(TIMESTAMP_WINDOW.Seconds()) {
|
||||
if now-t > int64(TIMESTAMP_WINDOW.Seconds()) {
|
||||
delete(sta.usedRandom, key)
|
||||
}
|
||||
}
|
||||
sta.usedRandomM.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func (sta *State) registerRandom(r []byte) bool {
|
||||
var random [32]byte
|
||||
copy(random[:], r)
|
||||
sta.usedRandomM.Lock()
|
||||
_, used := sta.usedRandom[random]
|
||||
sta.usedRandom[random] = sta.Now().Unix()
|
||||
sta.usedRandomM.Unlock()
|
||||
return used
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue