diff --git a/pkg/base/t_http_an__.go b/pkg/base/t_http_an__.go index 4ac0022..b1496f6 100644 --- a/pkg/base/t_http_an__.go +++ b/pkg/base/t_http_an__.go @@ -33,6 +33,7 @@ type LalInfo struct { type StatGroup struct { StreamName string `json:"stream_name"` + AppName string `json:"app_name"` AudioCodec string `json:"audio_codec"` VideoCodec string `json:"video_codec"` VideoWidth int `json:"video_width"` diff --git a/pkg/logic/auth.go b/pkg/logic/auth.go new file mode 100644 index 0000000..7749883 --- /dev/null +++ b/pkg/logic/auth.go @@ -0,0 +1,11 @@ +package logic + +import ( + "github.com/q191201771/lal/pkg/base" +) + +type IAuthentication interface { + OnPubStart(info base.PubStartInfo) error + OnSubStart(info base.SubStartInfo) error + OnHls(streamName, urlParam string) error +} \ No newline at end of file diff --git a/pkg/logic/group__.go b/pkg/logic/group__.go index 37a27e4..9018d2a 100644 --- a/pkg/logic/group__.go +++ b/pkg/logic/group__.go @@ -136,6 +136,7 @@ func NewGroup(appName string, streamName string, config *Config, observer IGroup observer: observer, stat: base.StatGroup{ StreamName: streamName, + AppName: appName, }, exitChan: make(chan struct{}, 1), rtmpSubSessionSet: make(map[*rtmp.ServerSession]struct{}), diff --git a/pkg/logic/group_manager.go b/pkg/logic/group_manager.go index 95d6b46..29ed2ea 100644 --- a/pkg/logic/group_manager.go +++ b/pkg/logic/group_manager.go @@ -14,6 +14,8 @@ type IGroupCreator interface { CreateGroup(appName, streamName string) *Group } +type ModConfigGroupCreator func(appName, streamName string, baseConfig *Config) + // IGroupManager // // 封装管理Group的容器 diff --git a/pkg/logic/logic.go b/pkg/logic/logic.go index a805656..b83cd25 100644 --- a/pkg/logic/logic.go +++ b/pkg/logic/logic.go @@ -93,6 +93,16 @@ type Option struct { // 注意,如果业务方实现了自己的事件监听,则lal server内部不再走http notify的逻辑(也即二选一)。 // NotifyHandler INotifyHandler + + // ModConfigGroupCreator + // This func help us modify the group configuration base on appName or streamName + // so that group can have it own configuration (configuration can be in other source like db) + // It will help us reduce resource usage if we just want some specific group record flv or hls... + ModConfigGroupCreator ModConfigGroupCreator + + // Authentication + // This interface make authenticate customizable so that we can implement any authenticate strategy like jwt... + Authentication IAuthentication } var defaultOption = Option{ diff --git a/pkg/logic/server_manager__.go b/pkg/logic/server_manager__.go index 8a032b9..01685a2 100644 --- a/pkg/logic/server_manager__.go +++ b/pkg/logic/server_manager__.go @@ -54,7 +54,6 @@ type ServerManager struct { mutex sync.Mutex groupManager IGroupManager - simpleAuthCtx *SimpleAuthCtx } func NewServerManager(modOption ...ModOption) *ServerManager { @@ -136,7 +135,9 @@ Doc: %s sm.pprofServer = &http.Server{Addr: sm.config.PprofConfig.Addr, Handler: nil} } - sm.simpleAuthCtx = NewSimpleAuthCtx(sm.config.SimpleAuthConfig) + if sm.option.Authentication == nil { + sm.option.Authentication = NewSimpleAuthCtx(sm.config.SimpleAuthConfig) + } return sm } @@ -375,7 +376,7 @@ func (sm *ServerManager) OnNewRtmpPubSession(session *rtmp.ServerSession) error info := base.Session2PubStartInfo(session) // 先做simple auth鉴权 - if err := sm.simpleAuthCtx.OnPubStart(info); err != nil { + if err := sm.option.Authentication.OnPubStart(info); err != nil { return err } @@ -414,7 +415,7 @@ func (sm *ServerManager) OnNewRtmpSubSession(session *rtmp.ServerSession) error info := base.Session2SubStartInfo(session) - if err := sm.simpleAuthCtx.OnSubStart(info); err != nil { + if err := sm.option.Authentication.OnSubStart(info); err != nil { return err } @@ -453,7 +454,7 @@ func (sm *ServerManager) OnNewHttpflvSubSession(session *httpflv.SubSession) err info := base.Session2SubStartInfo(session) - if err := sm.simpleAuthCtx.OnSubStart(info); err != nil { + if err := sm.option.Authentication.OnSubStart(info); err != nil { return err } @@ -490,7 +491,7 @@ func (sm *ServerManager) OnNewHttptsSubSession(session *httpts.SubSession) error info := base.Session2SubStartInfo(session) - if err := sm.simpleAuthCtx.OnSubStart(info); err != nil { + if err := sm.option.Authentication.OnSubStart(info); err != nil { return err } @@ -538,7 +539,7 @@ func (sm *ServerManager) OnNewRtspPubSession(session *rtsp.PubSession) error { info := base.Session2PubStartInfo(session) - if err := sm.simpleAuthCtx.OnPubStart(info); err != nil { + if err := sm.option.Authentication.OnPubStart(info); err != nil { return err } @@ -576,7 +577,7 @@ func (sm *ServerManager) OnNewRtspSubSessionDescribe(session *rtsp.SubSession) ( info := base.Session2SubStartInfo(session) - if err := sm.simpleAuthCtx.OnSubStart(info); err != nil { + if err := sm.option.Authentication.OnSubStart(info); err != nil { return false, nil } @@ -621,7 +622,15 @@ func (sm *ServerManager) OnDelRtspSubSession(session *rtsp.SubSession) { // ----- implement IGroupCreator interface ----------------------------------------------------------------------------- func (sm *ServerManager) CreateGroup(appName string, streamName string) *Group { - return NewGroup(appName, streamName, sm.config, sm) + var config *Config + if sm.option.ModConfigGroupCreator != nil { + cloneConfig := *sm.config + sm.option.ModConfigGroupCreator(appName, streamName, &cloneConfig) + config = &cloneConfig + } else { + config = sm.config + } + return NewGroup(appName, streamName, config, sm) } // ----- implement IGroupObserver interface ----------------------------------------------------------------------------- @@ -703,7 +712,7 @@ func (sm *ServerManager) serveHls(writer http.ResponseWriter, req *http.Request) if urlCtx.GetFileType() == "m3u8" { // TODO(chef): [refactor] 需要整理,这里使用 hls.PathStrategy 不太好 202207 streamName := hls.PathStrategy.GetRequestInfo(urlCtx, sm.config.HlsConfig.OutPath).StreamName - if err = sm.simpleAuthCtx.OnHls(streamName, urlCtx.RawQuery); err != nil { + if err = sm.option.Authentication.OnHls(streamName, urlCtx.RawQuery); err != nil { Log.Errorf("simple auth failed. err=%+v", err) return }