修复dtls链接的panic文件

This commit is contained in:
bjdgyc 2022-11-04 15:40:06 +08:00
parent 46721b1453
commit 87dcc63b6f
2 changed files with 34 additions and 11 deletions

View File

@ -66,9 +66,13 @@ func startDtls() {
go func() { go func() {
// time.Sleep(1 * time.Second) // time.Sleep(1 * time.Second)
cc := conn.(*dtls.Conn) cc := conn.(*dtls.Conn)
sessid := hex.EncodeToString(cc.ConnectionState().SessionID) did := hex.EncodeToString(cc.ConnectionState().SessionID)
sess := sessdata.Dtls2Sess(sessid) cSess := sessdata.Dtls2CSess(did)
LinkDtls(conn, sess.CSess) if cSess == nil {
conn.Close()
return
}
LinkDtls(conn, cSess)
}() }()
} }
} }

View File

@ -94,18 +94,24 @@ func checkSession() {
timeout := time.Duration(base.Cfg.SessionTimeout) * time.Second timeout := time.Duration(base.Cfg.SessionTimeout) * time.Second
tick := time.NewTicker(time.Second * 60) tick := time.NewTicker(time.Second * 60)
for range tick.C { for range tick.C {
sessMux.Lock() outToken := []string{}
sessMux.RLock()
t := time.Now() t := time.Now()
for k, v := range sessions { for k, v := range sessions {
v.mux.Lock() v.mux.RLock()
if !v.IsActive { if !v.IsActive {
if t.Sub(v.LastLogin) > timeout { if t.Sub(v.LastLogin) > timeout {
delete(sessions, k) outToken = append(outToken, k)
} }
} }
v.mux.Unlock() v.mux.RUnlock()
}
sessMux.RUnlock()
// 删除过期session
for _, v := range outToken {
CloseSess(v)
} }
sessMux.Unlock()
} }
}() }()
} }
@ -365,6 +371,20 @@ func Dtls2Sess(did string) *Session {
return sessions[token] return sessions[token]
} }
func Dtls2CSess(did string) *ConnSession {
sessMux.RLock()
defer sessMux.RUnlock()
token := dtlsIds[did]
sess := sessions[token]
if sess == nil {
return nil
}
sess.mux.RLock()
defer sess.mux.RUnlock()
return sess.CSess
}
func Dtls2MasterSecret(did string) string { func Dtls2MasterSecret(did string) string {
sessMux.RLock() sessMux.RLock()
token := dtlsIds[did] token := dtlsIds[did]
@ -396,6 +416,7 @@ func CloseSess(token string) {
} }
delete(sessions, token) delete(sessions, token)
delete(dtlsIds, sess.DtlsSid)
sess.CSess.Close() sess.CSess.Close()
} }
@ -414,7 +435,5 @@ func DelSessByStoken(stoken string) {
stoken = strings.TrimSpace(stoken) stoken = strings.TrimSpace(stoken)
sarr := strings.Split(stoken, "@") sarr := strings.Split(stoken, "@")
token := sarr[1] token := sarr[1]
sessMux.Lock() CloseSess(token)
delete(sessions, token)
sessMux.Unlock()
} }