mirror of https://github.com/q191201771/lal.git
[feat] 新增simple auth鉴权功能,rtmp,httpflv支持md5鉴权
parent
7f79c71356
commit
57ad766d34
@ -0,0 +1,74 @@
|
||||
// Copyright 2022, Chef. All rights reserved.
|
||||
// https://github.com/q191201771/lal
|
||||
//
|
||||
// Use of this source code is governed by a MIT-style license
|
||||
// that can be found in the License file.
|
||||
//
|
||||
// Author: Chef (191201771@qq.com)
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/q191201771/lal/pkg/base"
|
||||
"github.com/q191201771/naza/pkg/nazamd5"
|
||||
)
|
||||
|
||||
func SimpleAuthCalcSecret(key string, streamName string) string {
|
||||
return nazamd5.Md5([]byte(key + streamName))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
// TODO(chef): [refactor] 结合 NotifyHandler 整理
|
||||
|
||||
const secretName = "lal_secret"
|
||||
|
||||
type SimpleAuthCtx struct {
|
||||
key string
|
||||
config SimpleAuthConfig
|
||||
}
|
||||
|
||||
// NewSimpleAuthCtx
|
||||
//
|
||||
// @param key: 如果为空,则所有鉴权接口都返回成功
|
||||
//
|
||||
func NewSimpleAuthCtx(config SimpleAuthConfig) *SimpleAuthCtx {
|
||||
return &SimpleAuthCtx{
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SimpleAuthCtx) OnPubStart(info base.PubStartInfo) error {
|
||||
if s.config.PubRtmpEnable && info.Protocol == base.ProtocolRtmp {
|
||||
return s.check(info.StreamName, info.UrlParam)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SimpleAuthCtx) OnSubStart(info base.SubStartInfo) error {
|
||||
if (s.config.SubRtmpEnable && info.Protocol == base.ProtocolRtmp) ||
|
||||
(s.config.SubHttpflvEnable && info.Protocol == base.ProtocolHttpflv) {
|
||||
return s.check(info.StreamName, info.UrlParam)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SimpleAuthCtx) check(streamName string, urlParam string) error {
|
||||
q, err := url.ParseQuery(urlParam)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v := q.Get(secretName)
|
||||
if v == "" {
|
||||
return base.ErrSimpleAuthParamNotFound
|
||||
}
|
||||
se := SimpleAuthCalcSecret(s.config.Key, streamName)
|
||||
if se != v {
|
||||
return base.ErrSimpleAuthFailed
|
||||
}
|
||||
return nil
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
// Copyright 2022, Chef. All rights reserved.
|
||||
// https://github.com/q191201771/lal
|
||||
//
|
||||
// Use of this source code is governed by a MIT-style license
|
||||
// that can be found in the License file.
|
||||
//
|
||||
// Author: Chef (191201771@qq.com)
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/q191201771/lal/pkg/base"
|
||||
"github.com/q191201771/naza/pkg/assert"
|
||||
)
|
||||
|
||||
func TestSimpleAuthCalcSecret(t *testing.T) {
|
||||
res := SimpleAuthCalcSecret("q191201771", "test110")
|
||||
assert.Equal(t, "700997e1595a06c9ffa60ebef79105b0", res)
|
||||
}
|
||||
|
||||
func TestSimpleAuthCtx(t *testing.T) {
|
||||
ctx := NewSimpleAuthCtx(SimpleAuthConfig{
|
||||
Key: "q191201771",
|
||||
PubRtmpEnable: true,
|
||||
})
|
||||
var info base.PubStartInfo
|
||||
info.Protocol = base.ProtocolRtmp
|
||||
info.StreamName = "test110"
|
||||
info.UrlParam = "lal_secret=700997e1595a06c9ffa60ebef79105b0"
|
||||
res := ctx.OnPubStart(info)
|
||||
assert.Equal(t, nil, res)
|
||||
}
|
Loading…
Reference in New Issue