mirror of https://github.com/v2ray/v2ray-core
fix data race when caching session id
parent
fec56dda70
commit
6a3abf3147
|
@ -55,21 +55,16 @@ func (h *SessionHistory) Close() error {
|
||||||
return h.task.Close()
|
return h.task.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *SessionHistory) add(session sessionId) {
|
func (h *SessionHistory) addIfNotExits(session sessionId) bool {
|
||||||
h.Lock()
|
h.Lock()
|
||||||
defer h.Unlock()
|
defer h.Unlock()
|
||||||
|
|
||||||
h.cache[session] = time.Now().Add(time.Minute * 3)
|
if expire, found := h.cache[session]; found && expire.After(time.Now()) {
|
||||||
}
|
|
||||||
|
|
||||||
func (h *SessionHistory) has(session sessionId) bool {
|
|
||||||
h.RLock()
|
|
||||||
defer h.RUnlock()
|
|
||||||
|
|
||||||
if expire, found := h.cache[session]; found {
|
|
||||||
return expire.After(time.Now())
|
|
||||||
}
|
|
||||||
return false
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
h.cache[session] = time.Now().Add(time.Minute * 3)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *SessionHistory) removeExpiredEntries() {
|
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.user[:], vmessAccount.ID.Bytes())
|
||||||
copy(sid.key[:], s.requestBodyKey)
|
copy(sid.key[:], s.requestBodyKey)
|
||||||
copy(sid.nonce[:], s.requestBodyIV)
|
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")
|
return nil, newError("duplicated session id, possibly under replay attack")
|
||||||
}
|
}
|
||||||
s.sessionHistory.add(sid)
|
|
||||||
|
|
||||||
s.responseHeader = buffer.Byte(33) // 1 byte
|
s.responseHeader = buffer.Byte(33) // 1 byte
|
||||||
request.Option = bitmask.Byte(buffer.Byte(34)) // 1 byte
|
request.Option = bitmask.Byte(buffer.Byte(34)) // 1 byte
|
||||||
|
|
Loading…
Reference in New Issue