1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-27 06:16:09 -04:00

fix data race when caching session id

This commit is contained in:
Darien Raymond 2018-02-26 17:49:53 +01:00
parent fec56dda70
commit 6a3abf3147
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -55,21 +55,16 @@ func (h *SessionHistory) Close() error {
return h.task.Close()
}
func (h *SessionHistory) add(session sessionId) {
func (h *SessionHistory) addIfNotExits(session sessionId) bool {
h.Lock()
defer h.Unlock()
h.cache[session] = time.Now().Add(time.Minute * 3)
}
func (h *SessionHistory) has(session sessionId) bool {
h.RLock()
defer h.RUnlock()
if expire, found := h.cache[session]; found {
return expire.After(time.Now())
if expire, found := h.cache[session]; found && expire.After(time.Now()) {
return false
}
return false
h.cache[session] = time.Now().Add(time.Minute * 3)
return true
}
func (h *SessionHistory) removeExpiredEntries() {
@ -152,10 +147,9 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request
copy(sid.user[:], vmessAccount.ID.Bytes())
copy(sid.key[:], s.requestBodyKey)
copy(sid.nonce[:], s.requestBodyIV)
if s.sessionHistory.has(sid) {
if !s.sessionHistory.addIfNotExits(sid) {
return nil, newError("duplicated session id, possibly under replay attack")
}
s.sessionHistory.add(sid)
s.responseHeader = buffer.Byte(33) // 1 byte
request.Option = bitmask.Byte(buffer.Byte(34)) // 1 byte