Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
45 lines
751 B
Go
45 lines
751 B
Go
package m3u8
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"sync"
|
|
)
|
|
|
|
var (
|
|
cacheEnkey []byte
|
|
initOnce sync.Once
|
|
initErr error
|
|
)
|
|
|
|
// 安全初始化 m3u8 key(文件缺失时不再 panic,交由调用方降级)
|
|
func initEnkey() {
|
|
initOnce.Do(func() {
|
|
data, err := ioutil.ReadFile("conf/m3u8.key")
|
|
if err != nil {
|
|
initErr = err
|
|
return
|
|
}
|
|
if len(data) == 0 {
|
|
initErr = fmt.Errorf("conf/m3u8.key is empty")
|
|
return
|
|
}
|
|
cacheEnkey = data
|
|
})
|
|
}
|
|
|
|
// Enkey 密匙 key
|
|
func Enkey(ctx context.Context) (buffer *bytes.Buffer, err error) {
|
|
initEnkey()
|
|
if initErr != nil {
|
|
return nil, initErr
|
|
}
|
|
if len(cacheEnkey) == 0 {
|
|
return nil, fmt.Errorf("m3u8 key not loaded")
|
|
}
|
|
buffer = bytes.NewBuffer(cacheEnkey)
|
|
return
|
|
}
|