1 [opt] http-api: kick接口支持踢出hls sub session, 2 [opt] hls: session功能开启时,无效的ts和m3u8请求直接拒绝

pull/358/head
q191201771 9 months ago
parent 67511de4a8
commit b820227d57

@ -80,14 +80,17 @@ func (s *ServerHandler) ServeHTTPWithUrlCtx(resp http.ResponseWriter, req *http.
if s.isSubSessionModeEnable() { if s.isSubSessionModeEnable() {
sessionIdHash = urlObj.Query().Get("session_id") sessionIdHash = urlObj.Query().Get("session_id")
if filetype == "ts" && sessionIdHash != "" { if filetype == "ts" && sessionIdHash != "" {
// 注意为了增强容错性不管是session_id字段无效还是session_id为空我们都依然返回ts文件内容给播放端
if sessionIdHash != "" { if sessionIdHash != "" {
err = s.keepSessionAlive(sessionIdHash) err = s.keepSessionAlive(sessionIdHash)
if err != nil { if err != nil {
Log.Warnf("keepSessionAlive failed. session=%s, err=%+v", sessionIdHash, err) Log.Warnf("keepSessionAlive failed. session=%s, err=%+v", sessionIdHash, err)
resp.WriteHeader(http.StatusNotFound)
return
} }
} else { } else {
// noop Log.Warnf("session_id not exist. session=%s, err=%+v", sessionIdHash, err)
resp.WriteHeader(http.StatusNotFound)
return
} }
} else if filetype == "m3u8" { } else if filetype == "m3u8" {
neededRedirect := false neededRedirect := false
@ -96,7 +99,8 @@ func (s *ServerHandler) ServeHTTPWithUrlCtx(resp http.ResponseWriter, req *http.
err = s.keepSessionAlive(sessionIdHash) err = s.keepSessionAlive(sessionIdHash)
if err != nil { if err != nil {
Log.Warnf("keepSessionAlive failed. session=%s, err=%+v", sessionIdHash, err) Log.Warnf("keepSessionAlive failed. session=%s, err=%+v", sessionIdHash, err)
neededRedirect = true resp.WriteHeader(http.StatusNotFound)
return
} }
} else { } else {
neededRedirect = true neededRedirect = true
@ -201,7 +205,7 @@ func (s *ServerHandler) clearExpireSession() {
s.mutex.Lock() s.mutex.Lock()
defer s.mutex.Unlock() defer s.mutex.Unlock()
for sessionIdHash, session := range s.sessionMap { for sessionIdHash, session := range s.sessionMap {
if session.IsExpired() { if session.IsExpired() || session.IsDisposed() {
delete(s.sessionMap, sessionIdHash) delete(s.sessionMap, sessionIdHash)
s.observer.OnDelHlsSubSession(session) s.observer.OnDelHlsSubSession(session)
} }

@ -9,6 +9,7 @@
package hls package hls
import ( import (
"github.com/q191201771/naza/pkg/nazaatomic"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@ -29,6 +30,8 @@ type SubSession struct {
stat base.StatSession stat base.StatSession
prevStat connection.Stat prevStat connection.Stat
currStat connection.StatAtomic currStat connection.StatAtomic
disposedFlag nazaatomic.Bool
} }
func (s *SubSession) UniqueKey() string { func (s *SubSession) UniqueKey() string {
@ -105,10 +108,18 @@ func (s *SubSession) IsExpired() bool {
return s.LastRequestTime.Add(s.timeout).Before(time.Now()) return s.LastRequestTime.Add(s.timeout).Before(time.Now())
} }
func (s *SubSession) IsDisposed() bool {
return s.disposedFlag.Load()
}
func (s *SubSession) KeepAlive() { func (s *SubSession) KeepAlive() {
s.LastRequestTime = time.Now() s.LastRequestTime = time.Now()
} }
func (s *SubSession) Dispose() {
s.disposedFlag.Store(true)
}
func GetAppNameFromUrlCtx(urlCtx base.UrlContext, hlsUrlPattern string) string { func GetAppNameFromUrlCtx(urlCtx base.UrlContext, hlsUrlPattern string) string {
if hlsUrlPattern == "" { if hlsUrlPattern == "" {
return urlCtx.PathWithoutLastItem return urlCtx.PathWithoutLastItem

@ -379,6 +379,13 @@ func (group *Group) KickSession(sessionId string) bool {
return true return true
} }
} }
} else if strings.HasPrefix(sessionId, base.UkPreHlsSubSession) {
for s := range group.hlsSubSessionSet {
if s.UniqueKey() == sessionId {
s.Dispose()
return true
}
}
} else { } else {
Log.Errorf("[%s] kick session while session id format invalid. %s", group.UniqueKey, sessionId) Log.Errorf("[%s] kick session while session id format invalid. %s", group.UniqueKey, sessionId)
} }

Loading…
Cancel
Save