Initial commit: web_hghl_server
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,258 @@
|
||||
package videoser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"videoweb/pub/common/crypt"
|
||||
"videoweb/pub/common/ipaddr"
|
||||
"videoweb/pub/common/log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
h5M3U8TicketTTLSeconds = int64(600)
|
||||
h5M3U8TicketKeyVersion = "v1"
|
||||
h5M3U8TicketBindIP = true
|
||||
h5M3U8RoutePrefix = "/h5/m3u8/"
|
||||
hardcodedH5M3U8TicketKey = "3d058714838683a97c04d7b6ab609fc196f29e55df36197341d87325dc63154a"
|
||||
H5M3U8FallbackPath = "/v3/av/w0/w6/db/7j/029e2f2df53d4741ad39b471e2ad1806.m3u8"
|
||||
)
|
||||
|
||||
// H5M3U8Ticket 记录业务接口已确认的正片/预览权限,供 m3u8 接口按 IP 可信计数。
|
||||
type H5M3U8Ticket struct {
|
||||
MediaID int64 `json:"mediaId"`
|
||||
Path string `json:"path"`
|
||||
ExpireAt int64 `json:"expireAt"`
|
||||
IPHash string `json:"ipHash,omitempty"`
|
||||
Playable bool `json:"playable"`
|
||||
Preview bool `json:"preview"`
|
||||
UseNewKey bool `json:"useNewKey,omitempty"` // 短剧原片使用 newm3u8.key
|
||||
KeyVersion string `json:"keyVersion,omitempty"`
|
||||
OfficialPlayletID int64 `json:"officialPlayletId,omitempty"` // 官网短剧ID 非官网播放为0
|
||||
}
|
||||
|
||||
func h5M3U8TicketKey() string {
|
||||
return hardcodedH5M3U8TicketKey
|
||||
}
|
||||
|
||||
// BuildH5M3U8LegacyURL 返回不带路由前缀的相对票据地址,兼容 H5 自行拼接 /h5/m3u8/。
|
||||
func BuildH5M3U8LegacyURL(mediaID int64, path, ip string, playable, preview bool) string {
|
||||
return buildH5M3U8URL(mediaID, path, ip, playable, preview, false, 0)
|
||||
}
|
||||
|
||||
// BuildH5M3U8PlayletURL 为短剧播放地址生成票据。原片 useNewKey 为 true。
|
||||
func BuildH5M3U8PlayletURL(chapterID int64, path, ip string, useNewKey bool) string {
|
||||
return buildH5M3U8URL(chapterID, path, ip, true, false, useNewKey, 0)
|
||||
}
|
||||
|
||||
// BuildH5M3U8OfficialURL 签发需要再次校验官网开放范围的剧集票据
|
||||
func BuildH5M3U8OfficialURL(chapterID, playletID int64, path, ip string, useNewKey bool) string {
|
||||
return buildH5M3U8URL(chapterID, path, ip, true, false, useNewKey, playletID)
|
||||
}
|
||||
|
||||
func buildH5M3U8URL(mediaID int64, path, ip string, playable, preview, useNewKey bool, officialPlayletID int64) string {
|
||||
key := h5M3U8TicketKey()
|
||||
if key == "" || mediaID <= 0 || strings.TrimSpace(ip) == "" {
|
||||
return ""
|
||||
}
|
||||
normalizedPath, ok := normalizeH5M3U8Path(path)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
expireAt := time.Now().Add(time.Duration(h5M3U8TicketTTLSeconds) * time.Second).Unix()
|
||||
ticket := H5M3U8Ticket{
|
||||
MediaID: mediaID,
|
||||
Path: normalizedPath,
|
||||
ExpireAt: expireAt,
|
||||
Playable: playable,
|
||||
Preview: preview,
|
||||
UseNewKey: useNewKey,
|
||||
KeyVersion: h5M3U8TicketKeyVersion,
|
||||
OfficialPlayletID: officialPlayletID,
|
||||
}
|
||||
if h5M3U8TicketBindIP {
|
||||
ticket.IPHash = h5M3U8Hash(ip)
|
||||
}
|
||||
encodedTicket, err := encodeH5M3U8Ticket(ticket, key)
|
||||
if err != nil {
|
||||
log.Error("【BuildH5M3U8LegacyURL】生成H5播放票据失败", log.E(err))
|
||||
return ""
|
||||
}
|
||||
query := url.Values{}
|
||||
query.Set("id", strconv.FormatInt(mediaID, 10))
|
||||
query.Set("t", strconv.FormatInt(expireAt, 10))
|
||||
query.Set("ticket", encodedTicket)
|
||||
return normalizedPath + "?" + query.Encode()
|
||||
}
|
||||
|
||||
// CheckH5M3U8Ticket 校验 IP、视频、路径、有效期,防止伪造正片计数语义。
|
||||
func CheckH5M3U8Ticket(mediaID, expireAt int64, path, ip, ticket string) (*H5M3U8Ticket, bool) {
|
||||
data, reason := validateH5M3U8Ticket(mediaID, expireAt, path, ip, ticket, true)
|
||||
if reason != "" {
|
||||
return nil, false
|
||||
}
|
||||
return data, true
|
||||
}
|
||||
|
||||
// CheckH5M3U8TicketForEnkey 校验 /enkey 请求,不校验 m3u8 路径。
|
||||
func CheckH5M3U8TicketForEnkey(mediaID, expireAt int64, ip, ticket string) (*H5M3U8Ticket, bool) {
|
||||
data, reason := validateH5M3U8Ticket(mediaID, expireAt, "", ip, ticket, false)
|
||||
if reason != "" {
|
||||
return nil, false
|
||||
}
|
||||
return data, true
|
||||
}
|
||||
|
||||
// CheckH5M3U8TicketReason 与 CheckH5M3U8Ticket 相同,额外返回失败原因便于排查保底视频。
|
||||
func CheckH5M3U8TicketReason(mediaID, expireAt int64, path, ip, ticket string) (*H5M3U8Ticket, bool, string) {
|
||||
data, reason := validateH5M3U8Ticket(mediaID, expireAt, path, ip, ticket, true)
|
||||
if reason != "" {
|
||||
return nil, false, reason
|
||||
}
|
||||
return data, true, ""
|
||||
}
|
||||
|
||||
func validateH5M3U8Ticket(mediaID, expireAt int64, path, ip, ticket string, checkPath bool) (*H5M3U8Ticket, string) {
|
||||
data, reason := decodeAndValidateH5M3U8TicketReason(mediaID, expireAt, ip, ticket)
|
||||
if reason != "" {
|
||||
return nil, reason
|
||||
}
|
||||
if !checkPath {
|
||||
return data, ""
|
||||
}
|
||||
normalizedPath, pathOK := normalizeH5M3U8Path(path)
|
||||
if !pathOK {
|
||||
return nil, "path_invalid"
|
||||
}
|
||||
payloadPath, valid := normalizeH5M3U8Path(data.Path)
|
||||
if !valid || payloadPath != normalizedPath {
|
||||
return nil, "path_mismatch"
|
||||
}
|
||||
return data, ""
|
||||
}
|
||||
|
||||
func decodeAndValidateH5M3U8Ticket(mediaID, expireAt int64, ip, ticket string) (*H5M3U8Ticket, bool) {
|
||||
data, reason := decodeAndValidateH5M3U8TicketReason(mediaID, expireAt, ip, ticket)
|
||||
if reason != "" {
|
||||
return nil, false
|
||||
}
|
||||
return data, true
|
||||
}
|
||||
|
||||
func decodeAndValidateH5M3U8TicketReason(mediaID, expireAt int64, ip, ticket string) (*H5M3U8Ticket, string) {
|
||||
key := h5M3U8TicketKey()
|
||||
if key == "" || mediaID <= 0 || expireAt <= 0 || strings.TrimSpace(ticket) == "" {
|
||||
return nil, "param_missing"
|
||||
}
|
||||
data, err := decodeH5M3U8Ticket(ticket, key)
|
||||
if err != nil {
|
||||
return nil, "ticket_decode_failed"
|
||||
}
|
||||
if data.MediaID != mediaID || data.ExpireAt != expireAt {
|
||||
return nil, "id_or_expire_mismatch"
|
||||
}
|
||||
if data.KeyVersion != "" && data.KeyVersion != h5M3U8TicketKeyVersion {
|
||||
return nil, "key_version_mismatch"
|
||||
}
|
||||
if time.Now().Unix() > data.ExpireAt {
|
||||
return nil, "ticket_expired"
|
||||
}
|
||||
if h5M3U8TicketBindIP && data.IPHash != h5M3U8Hash(ip) {
|
||||
return nil, "ip_mismatch"
|
||||
}
|
||||
return data, ""
|
||||
}
|
||||
|
||||
func H5M3U8RequestIP(ctx context.Context) string {
|
||||
if c, ok := ctx.(*gin.Context); ok {
|
||||
return strings.TrimSpace(ipaddr.ResolveClientIP(c))
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func normalizeH5M3U8TicketParam(ticket string) string {
|
||||
ticket = strings.TrimSpace(ticket)
|
||||
if idx := strings.Index(ticket, "?"); idx >= 0 {
|
||||
ticket = ticket[:idx]
|
||||
}
|
||||
return ticket
|
||||
}
|
||||
|
||||
func normalizeH5M3U8Path(path string) (string, bool) {
|
||||
path = strings.TrimSpace(path)
|
||||
if path == "" {
|
||||
return "", false
|
||||
}
|
||||
lowerPath := strings.ToLower(path)
|
||||
if strings.HasPrefix(lowerPath, "http://") || strings.HasPrefix(lowerPath, "https://") {
|
||||
return "", false
|
||||
}
|
||||
if idx := strings.Index(path, "?"); idx >= 0 {
|
||||
path = path[:idx]
|
||||
}
|
||||
path = strings.TrimLeft(path, "/")
|
||||
path = strings.TrimPrefix(path, strings.TrimLeft(h5M3U8RoutePrefix, "/"))
|
||||
lowerPath = strings.ToLower(path)
|
||||
if path == "" || strings.Contains(path, `\`) || strings.Contains(path, "..") || strings.Contains(lowerPath, "%2e") {
|
||||
return "", false
|
||||
}
|
||||
if !strings.HasSuffix(lowerPath, ".m3u8") {
|
||||
return "", false
|
||||
}
|
||||
return path, true
|
||||
}
|
||||
|
||||
func encodeH5M3U8Ticket(ticket H5M3U8Ticket, key string) (string, error) {
|
||||
data, err := json.Marshal(ticket)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
encrypted, err := crypt.CoreAesEncryptEx(data, 12, key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.URLEncoding.EncodeToString(encrypted), nil
|
||||
}
|
||||
|
||||
func decodeH5M3U8Ticket(ticket, key string) (*H5M3U8Ticket, error) {
|
||||
ticket = normalizeH5M3U8TicketParam(ticket)
|
||||
encrypted, err := base64.URLEncoding.DecodeString(ticket)
|
||||
if err != nil {
|
||||
encrypted, err = base64.RawURLEncoding.DecodeString(ticket)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
decrypted, err := safeCoreAesDecryptEx(encrypted, 12, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var data H5M3U8Ticket
|
||||
if err = json.Unmarshal(decrypted, &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &data, nil
|
||||
}
|
||||
|
||||
func safeCoreAesDecryptEx(encrypted []byte, nonceLen int, key string) (decrypted []byte, err error) {
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
err = fmt.Errorf("decrypt h5 m3u8 ticket panic: %v", recovered)
|
||||
}
|
||||
}()
|
||||
return crypt.CoreAesDecryptEx(encrypted, nonceLen, key)
|
||||
}
|
||||
|
||||
func h5M3U8Hash(value string) string {
|
||||
hash := sha256.Sum256([]byte(strings.TrimSpace(value)))
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package videoser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"videoweb/app/api/proto"
|
||||
"videoweb/app/service/configser"
|
||||
"videoweb/app/service/moreser"
|
||||
"videoweb/app/service/translationser"
|
||||
"videoweb/pub/common/response"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/models/videoinformationmod"
|
||||
"videoweb/pub/models/videolikemod"
|
||||
"videoweb/pub/pubdef"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
// QueryLikeVideoList 按点赞时间倒序查询当前用户点赞的上架视频
|
||||
func QueryLikeVideoList(ctx context.Context, userID int64, req *proto.QueryLikeVideoListReq) (*proto.QueryLikeVideoListResp, *srvdef.SrvErr) {
|
||||
if userID <= 0 || req == nil {
|
||||
return nil, srvdef.New(srvdef.ErrParamError, "", "")
|
||||
}
|
||||
pageNum, pageSize := moreser.NormalizePage(req.PageNum, req.PageSize, 20, 100)
|
||||
pipeline := []bson.M{
|
||||
{"$match": bson.M{videolikemod.FN.VideoLike.UserID: userID}},
|
||||
{"$lookup": bson.M{
|
||||
"from": videoinformationmod.TN.VideoInformation,
|
||||
"localField": videolikemod.FN.VideoLike.VideoID,
|
||||
"foreignField": videoinformationmod.FN.VideoInformation.Id,
|
||||
"as": "video",
|
||||
}},
|
||||
{"$unwind": "$video"},
|
||||
{"$match": bson.M{"video." + videoinformationmod.FN.VideoInformation.Status: pubdef.InformationStatusVerifyOK}},
|
||||
}
|
||||
countRows := make([]struct {
|
||||
Total int64 `bson:"total"`
|
||||
}, 0)
|
||||
if err := videolikemod.AggregateVideoLike(ctx, &countRows, append(append([]bson.M(nil), pipeline...), bson.M{"$count": "total"})); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
total := int64(0)
|
||||
if len(countRows) > 0 {
|
||||
total = countRows[0].Total
|
||||
}
|
||||
resp := &proto.QueryLikeVideoListResp{BasePageResp: response.BasePageResp{
|
||||
TotalCount: total, PageNum: int(pageNum), PageSize: int(pageSize), List: make([]*proto.FavoriteVideoListItem, 0),
|
||||
}, Translations: translationser.GetTranslationData(), BottonTranslations: translationser.GetTranslationBottomList()}
|
||||
if total == 0 || pageNum > (total-1)/pageSize+1 {
|
||||
return resp, nil
|
||||
}
|
||||
pagePipeline := append([]bson.M{pipeline[0], {"$sort": bson.D{{Key: videolikemod.FN.VideoLike.CreatedAt, Value: -1}, {Key: videolikemod.FN.VideoLike.Id, Value: -1}}}}, pipeline[1:]...)
|
||||
pagePipeline = append(pagePipeline,
|
||||
bson.M{"$skip": (pageNum - 1) * pageSize},
|
||||
bson.M{"$limit": pageSize},
|
||||
bson.M{"$replaceRoot": bson.M{"newRoot": "$video"}},
|
||||
bson.M{"$project": favoriteVideoProjection()},
|
||||
)
|
||||
list := make([]*videoinformationmod.VideoInformation, 0)
|
||||
if err := videolikemod.AggregateVideoLike(ctx, &list, pagePipeline); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cdn := configser.GetRequestConfig(ctx).ImgCdn
|
||||
items := make([]*proto.FavoriteVideoListItem, 0, len(list))
|
||||
for _, item := range list {
|
||||
items = append(items, favoriteVideoListItem(item, cdn))
|
||||
}
|
||||
resp.List = items
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,386 @@
|
||||
package videoser
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
"videoweb/app/appg"
|
||||
"videoweb/app/service/configser"
|
||||
"videoweb/pub/common"
|
||||
"videoweb/pub/common/enum/consts"
|
||||
"videoweb/pub/common/enum/redisconst"
|
||||
"videoweb/pub/common/ipaddr"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/m3u8"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/models/m3u8requestlogmod"
|
||||
"videoweb/pub/models/playletchaptermod"
|
||||
"videoweb/pub/models/playletmod"
|
||||
"videoweb/pub/redis"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const m3u8RateLimitFallback = H5M3U8FallbackPath
|
||||
|
||||
type fileHandler func(ctx *gin.Context) (contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string, err error)
|
||||
|
||||
func Enkey(ctx *gin.Context) {
|
||||
fn := func(c *gin.Context) (contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string, err error) {
|
||||
idStr := c.Query("id")
|
||||
tStr := c.Query("t")
|
||||
ticket := c.Query("ticket")
|
||||
|
||||
if idStr == "" || tStr == "" || ticket == "" {
|
||||
err = errors.New("unauthorized")
|
||||
return
|
||||
}
|
||||
videoId, parseErr := strconv.ParseInt(idStr, 10, 64)
|
||||
if parseErr != nil {
|
||||
err = errors.New("invalid id")
|
||||
return
|
||||
}
|
||||
tVal, parseErr := strconv.ParseInt(tStr, 10, 64)
|
||||
if parseErr != nil {
|
||||
err = errors.New("invalid t")
|
||||
return
|
||||
}
|
||||
|
||||
ip := ipaddr.ResolveClientIP(c)
|
||||
ticketInfo, ticketOK := CheckH5M3U8TicketForEnkey(videoId, tVal, ip, ticket)
|
||||
if !ticketOK || ticketInfo.UseNewKey {
|
||||
log.Warn("Enkey: ticket invalid", log.Any("ip", ip), log.Any("videoId", videoId))
|
||||
err = errors.New("ticket check failed")
|
||||
return
|
||||
}
|
||||
if ticketInfo.OfficialPlayletID > 0 && !officialChapterAllowed(c, ticketInfo.OfficialPlayletID, videoId) {
|
||||
err = errors.New("本集暂未在官网开放")
|
||||
return
|
||||
}
|
||||
|
||||
buffer, err := m3u8.Enkey(c)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
contentLength = int64(buffer.Len())
|
||||
contentType = "application/octet-stream"
|
||||
reader = buffer
|
||||
extraHeaders = map[string]string{"Cache-Control": "private, no-store"}
|
||||
return
|
||||
}
|
||||
handlerFile(ctx, fn)
|
||||
}
|
||||
|
||||
func handlerFile(ctx *gin.Context, h fileHandler) {
|
||||
contentLength, contentType, reader, extraHeaders, err := h(ctx)
|
||||
if err != nil {
|
||||
common.ServerJSON(ctx, srvdef.DownloadM3u8File, err)
|
||||
return
|
||||
}
|
||||
ctx.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)
|
||||
}
|
||||
|
||||
func GetM3U8Ex(ctx *gin.Context) {
|
||||
fn := func(c *gin.Context) (contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string, err error) {
|
||||
start := time.Now()
|
||||
m3u8Name := ctx.Param("key")
|
||||
origM3u8Name := m3u8Name
|
||||
status := "success"
|
||||
var mediaID int64
|
||||
var expireAt int64
|
||||
var ticketOK bool
|
||||
var isOverseas bool
|
||||
var cdnDomain string
|
||||
|
||||
ip := ipaddr.ResolveClientIP(c)
|
||||
detector := appg.DetectorV6
|
||||
if ipaddr.IsIPv4(ip) {
|
||||
detector = appg.DetectorV4
|
||||
}
|
||||
if detector != nil {
|
||||
isOverseas, _ = detector.IsOverseasIP(ip)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err != nil && status == "success" {
|
||||
status = "error"
|
||||
}
|
||||
recordM3U8RequestLog(c, m3u8Name, mediaID, expireAt, ticketOK, isOverseas, cdnDomain, status, err, start)
|
||||
}()
|
||||
|
||||
ticket := ctx.Query("ticket")
|
||||
mediaID = parseInt64(ctx.Query("id"))
|
||||
expireAt = parseInt64(ctx.Query("t"))
|
||||
ticketInfo, ticketOK, ticketReason := CheckH5M3U8TicketReason(
|
||||
mediaID, expireAt, origM3u8Name, ip, ticket,
|
||||
)
|
||||
|
||||
if !ticketOK {
|
||||
m3u8Name = H5M3U8FallbackPath
|
||||
log.Warn("【GetM3U8Ex】H5播放票据无效",
|
||||
log.Any("reason", ticketReason),
|
||||
log.Any("mediaId", mediaID),
|
||||
log.Any("expireAt", expireAt),
|
||||
log.Any("ip", ip),
|
||||
log.Any("clientIP", c.ClientIP()),
|
||||
log.Any("remoteIP", c.RemoteIP()),
|
||||
log.Any("xff", c.GetHeader("X-Forwarded-For")),
|
||||
log.Any("cfIP", c.GetHeader("CF-Connecting-IP")),
|
||||
log.Any("path", origM3u8Name),
|
||||
log.Any("hasTicket", ticket != ""))
|
||||
}
|
||||
if ticketOK && ticketInfo.OfficialPlayletID > 0 && !officialChapterAllowed(c, ticketInfo.OfficialPlayletID, mediaID) {
|
||||
status = "official_closed"
|
||||
err = errors.New("本集暂未在官网开放")
|
||||
return
|
||||
}
|
||||
|
||||
countTowardDailyLimit := ticketOK && ticketInfo.Playable && !ticketInfo.Preview
|
||||
if countTowardDailyLimit && appg.Conf.RunMod == string(consts.RunModeProd) {
|
||||
cnt, countErr := m3u8DailyMediaCountByIP(ip, mediaID)
|
||||
if countErr != nil {
|
||||
log.Error("【GetM3U8Ex】统计IP正片获取次数失败", log.Any("ip", ip), log.E(countErr))
|
||||
} else if cnt > configser.GetM3U8DayRateLimitMax() {
|
||||
m3u8Name = m3u8RateLimitFallback
|
||||
status = "rate_limited"
|
||||
log.Warn("GetM3U8Ex daily rate limit, use fallback m3u8",
|
||||
log.Any("ip", ip),
|
||||
log.Any("remoteIP", c.RemoteIP()),
|
||||
log.Any("clientIP", c.ClientIP()),
|
||||
log.Any("xff", c.GetHeader("X-Forwarded-For")),
|
||||
log.Any("cnt", cnt),
|
||||
log.Any("mediaId", mediaID),
|
||||
log.Any("orig", origM3u8Name),
|
||||
log.Any("fallback", m3u8Name))
|
||||
}
|
||||
}
|
||||
|
||||
var req m3u8.Req
|
||||
if err = ctx.BindQuery(&req); err != nil {
|
||||
status = "bind_query_failed"
|
||||
return
|
||||
}
|
||||
req.Name = m3u8Name
|
||||
cdnDomain = configser.GetRandCDN()
|
||||
originDomain := appg.Conf.OrigDomain
|
||||
if isOverseas && len(configser.GetConfigInfo().MediaAppUsCdn) > 0 {
|
||||
cdnDomain = configser.GetConfigInfo().MediaAppUsCdn
|
||||
}
|
||||
log.Info("cdn信息", log.Any("ip:", ip), log.Any("isOverseas", isOverseas), log.Any("cdnDomain", cdnDomain))
|
||||
|
||||
enkeyUri := ""
|
||||
playOriginal := ticketOK && ticketInfo.UseNewKey && m3u8Name != H5M3U8FallbackPath
|
||||
if ticketOK {
|
||||
enkeyPath := "/enkey"
|
||||
if playOriginal {
|
||||
enkeyPath = "/newenkey"
|
||||
}
|
||||
enkeyUri = fmt.Sprintf("%s?id=%d&t=%d&ticket=%s", enkeyPath, mediaID, expireAt, url.QueryEscape(ticket))
|
||||
}
|
||||
|
||||
var buff *bytes.Buffer
|
||||
if playOriginal {
|
||||
_, buff, err = m3u8.TransformExOriginalPath(req.Name, cdnDomain, originDomain, 2, appg.Conf.TsAuthKey, appg.Conf.LsjMediaConf.M3u8Yuan, appg.Conf.CdnAuthKey, enkeyUri)
|
||||
} else {
|
||||
_, buff, err = m3u8.TransformEx(req.Name, cdnDomain, originDomain, 2, appg.Conf.TsAuthKey, appg.Conf.LsjMediaConf.M3u8Yuan, appg.Conf.CdnAuthKey, enkeyUri)
|
||||
}
|
||||
if err != nil {
|
||||
status = "transform_failed"
|
||||
return
|
||||
}
|
||||
contentType = "application/vnd.apple.mpegurl"
|
||||
reader = bytes.NewReader(buff.Bytes())
|
||||
contentLength = int64(buff.Len())
|
||||
extraHeaders = map[string]string{"Cache-Control": "private, no-store"}
|
||||
return
|
||||
}
|
||||
handlerFile(ctx, fn)
|
||||
}
|
||||
|
||||
func parseInt64(value string) int64 {
|
||||
n, _ := strconv.ParseInt(value, 10, 64)
|
||||
return n
|
||||
}
|
||||
|
||||
func m3u8DailyMediaCountByIP(ip string, mediaID int64) (int64, error) {
|
||||
expire := redisconst.M3U8DayRateLimitExpire()
|
||||
first, err := redis.Setnx_NewOK(redisconst.M3U8DailyMediaIpKey(ip, mediaID), 1, expire)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
countKey := redisconst.M3U8DayRateLimitIpKey(ip)
|
||||
if first {
|
||||
return redis.Incr(countKey, expire)
|
||||
}
|
||||
value, err := redis.Get(countKey)
|
||||
if err != nil || value == nil {
|
||||
return 0, err
|
||||
}
|
||||
return strconv.ParseInt(*value, 10, 64)
|
||||
}
|
||||
|
||||
func GetM3U8AuthParam(videoId int64, urlPath, ip, ua string) string {
|
||||
_ = ua
|
||||
if signed := BuildH5M3U8LegacyURL(videoId, urlPath, ip, true, false); signed != "" {
|
||||
return signed
|
||||
}
|
||||
return urlPath
|
||||
}
|
||||
|
||||
// GetPlayletM3U8AuthParam 为短剧剧集地址签发票据。useNewKey 表示当前地址是原片。
|
||||
func GetPlayletM3U8AuthParam(chapterID int64, urlPath, ip string, useNewKey bool) string {
|
||||
if signed := BuildH5M3U8PlayletURL(chapterID, urlPath, ip, useNewKey); signed != "" {
|
||||
return signed
|
||||
}
|
||||
return urlPath
|
||||
}
|
||||
|
||||
// GetM3U8OfficialParam 返回带官网开放范围校验的剧集播放地址
|
||||
func GetM3U8OfficialParam(chapterID, playletID int64, urlPath, ip string, useNewKey bool) string {
|
||||
return BuildH5M3U8OfficialURL(chapterID, playletID, urlPath, ip, useNewKey)
|
||||
}
|
||||
|
||||
// NewEnkey 返回短剧原片解密密钥。
|
||||
func NewEnkey(ctx *gin.Context) {
|
||||
fn := func(c *gin.Context) (contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string, err error) {
|
||||
idStr := c.Query("id")
|
||||
tStr := c.Query("t")
|
||||
ticket := c.Query("ticket")
|
||||
if idStr == "" || tStr == "" || ticket == "" {
|
||||
err = errors.New("unauthorized")
|
||||
return
|
||||
}
|
||||
videoId, parseErr := strconv.ParseInt(idStr, 10, 64)
|
||||
if parseErr != nil {
|
||||
err = errors.New("invalid id")
|
||||
return
|
||||
}
|
||||
tVal, parseErr := strconv.ParseInt(tStr, 10, 64)
|
||||
if parseErr != nil {
|
||||
err = errors.New("invalid t")
|
||||
return
|
||||
}
|
||||
ip := ipaddr.ResolveClientIP(c)
|
||||
ticketInfo, ticketOK := CheckH5M3U8TicketForEnkey(videoId, tVal, ip, ticket)
|
||||
if !ticketOK || !ticketInfo.UseNewKey {
|
||||
err = errors.New("ticket check failed")
|
||||
return
|
||||
}
|
||||
if ticketInfo.OfficialPlayletID > 0 && !officialChapterAllowed(c, ticketInfo.OfficialPlayletID, videoId) {
|
||||
err = errors.New("本集暂未在官网开放")
|
||||
return
|
||||
}
|
||||
buffer, err := m3u8.NewEnkey(c)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
contentLength = int64(buffer.Len())
|
||||
contentType = "application/octet-stream"
|
||||
reader = buffer
|
||||
extraHeaders = map[string]string{"Cache-Control": "private, no-store"}
|
||||
return
|
||||
}
|
||||
handlerFile(ctx, fn)
|
||||
}
|
||||
|
||||
// officialChapterAllowed 每次获取官网播放清单或密钥时重新读取开放范围
|
||||
func officialChapterAllowed(ctx context.Context, playletID, chapterID int64) bool {
|
||||
if playletID <= 0 || chapterID <= 0 {
|
||||
return false
|
||||
}
|
||||
playlet, err := playletmod.FindOnePlaylet(ctx, bson.M{
|
||||
playletmod.FN.Playlet.Id: playletID,
|
||||
playletmod.FN.Playlet.Status: playletmod.StatusUp,
|
||||
})
|
||||
if err != nil || playlet == nil {
|
||||
return false
|
||||
}
|
||||
chapter, err := playletchaptermod.FindOnePlayletChapter(ctx, bson.M{
|
||||
playletchaptermod.FN.PlayletChapter.Id: chapterID,
|
||||
playletchaptermod.FN.PlayletChapter.PlayletId: playletID,
|
||||
})
|
||||
if err != nil || chapter == nil {
|
||||
return false
|
||||
}
|
||||
return playlet.IsOfficialEpisodeOpen(chapter.CurrentEpisode)
|
||||
}
|
||||
|
||||
func LsjEnkey(ctx *gin.Context) {
|
||||
fn := func(c *gin.Context) (contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string, err error) {
|
||||
buffer, err := m3u8.LsjEnkey(c)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
contentLength = int64(buffer.Len())
|
||||
contentType = "application/octet-stream"
|
||||
reader = buffer
|
||||
extraHeaders = map[string]string{}
|
||||
return
|
||||
}
|
||||
handlerFile(ctx, fn)
|
||||
}
|
||||
|
||||
func recordM3U8RequestLog(ctx *gin.Context, m3u8Name string, videoId int64, t int64, ticketOK bool, isOverseas bool, cdnDomain string, status string, reqErr error, start time.Time) {
|
||||
doc := &m3u8requestlogmod.M3U8RequestLog{
|
||||
CreatedAt: time.Now(),
|
||||
Host: ctx.Request.Host,
|
||||
RequestURI: ctx.Request.RequestURI,
|
||||
M3U8Path: m3u8Name,
|
||||
RawQuery: ctx.Request.URL.RawQuery,
|
||||
Query: cloneURLValues(ctx.Request.URL.Query()),
|
||||
Headers: cloneHeader(ctx.Request.Header),
|
||||
VideoId: videoId,
|
||||
T: t,
|
||||
Token: ctx.Query("ticket"),
|
||||
ClientIP: ipaddr.ResolveClientIP(ctx),
|
||||
RemoteAddr: ctx.Request.RemoteAddr,
|
||||
XForwardedFor: ctx.GetHeader("X-Forwarded-For"),
|
||||
XRealIP: ctx.GetHeader("X-Real-IP"),
|
||||
CFConnectingIP: ctx.GetHeader("CF-Connecting-IP"),
|
||||
UserAgent: ctx.Request.UserAgent(),
|
||||
Referer: ctx.GetHeader("Referer"),
|
||||
IsOverseas: isOverseas,
|
||||
CDNDomain: cdnDomain,
|
||||
TokenOK: ticketOK,
|
||||
PathOK: true,
|
||||
Status: status,
|
||||
CostMs: time.Since(start).Milliseconds(),
|
||||
}
|
||||
if reqErr != nil {
|
||||
doc.Error = reqErr.Error()
|
||||
}
|
||||
|
||||
go func(data *m3u8requestlogmod.M3U8RequestLog) {
|
||||
c, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
if err1 := m3u8requestlogmod.InsertOneM3U8RequestLog(c, data); err1 != nil {
|
||||
log.Warn("recordM3U8RequestLog insert failed", log.Any("status", data.Status), log.Any("path", data.M3U8Path))
|
||||
}
|
||||
}(doc)
|
||||
}
|
||||
|
||||
func cloneHeader(header http.Header) map[string][]string {
|
||||
return cloneStringValues(map[string][]string(header))
|
||||
}
|
||||
|
||||
func cloneURLValues(values url.Values) map[string][]string {
|
||||
return cloneStringValues(map[string][]string(values))
|
||||
}
|
||||
|
||||
func cloneStringValues(values map[string][]string) map[string][]string {
|
||||
result := make(map[string][]string, len(values))
|
||||
for key, vals := range values {
|
||||
copied := make([]string, len(vals))
|
||||
copy(copied, vals)
|
||||
result[key] = copied
|
||||
}
|
||||
return result
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,127 @@
|
||||
package videoser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"testing"
|
||||
"videoweb/app/api/proto"
|
||||
"videoweb/app/service/moreser"
|
||||
"videoweb/pub/models/videoinformationmod"
|
||||
"videoweb/pub/pubdef"
|
||||
"videoweb/pub/storage/proxy"
|
||||
)
|
||||
|
||||
func TestOfficialPlaybackTicket(t *testing.T) {
|
||||
const ip = "127.0.0.1"
|
||||
const path = "v3/official-test.m3u8"
|
||||
signed := BuildH5M3U8OfficialURL(321, 123, path, ip, false)
|
||||
u, err := url.Parse(signed)
|
||||
if err != nil || u.Path != path {
|
||||
t.Fatalf("官网票据地址无效 %q %v", signed, err)
|
||||
}
|
||||
expireAt, err := strconv.ParseInt(u.Query().Get("t"), 10, 64)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ticket, ok := CheckH5M3U8Ticket(321, expireAt, path, ip, u.Query().Get("ticket"))
|
||||
if !ok || ticket.OfficialPlayletID != 123 {
|
||||
t.Fatalf("官网短剧标识未保存在签名票据中 %+v", ticket)
|
||||
}
|
||||
if _, ok := CheckH5M3U8Ticket(321, expireAt, path, "127.0.0.2", u.Query().Get("ticket")); ok {
|
||||
t.Fatal("官网票据未校验请求IP")
|
||||
}
|
||||
if ticket.UseNewKey {
|
||||
t.Fatal("水印片不应使用原片密钥")
|
||||
}
|
||||
original := BuildH5M3U8OfficialURL(321, 123, "bktappupdata/sp/a.m3u8", ip, true)
|
||||
ou, err := url.Parse(original)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
originalExpire, err := strconv.ParseInt(ou.Query().Get("t"), 10, 64)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
originalTicket, ok := CheckH5M3U8Ticket(321, originalExpire, "bktappupdata/sp/a.m3u8", ip, ou.Query().Get("ticket"))
|
||||
if !ok || !originalTicket.UseNewKey {
|
||||
t.Fatalf("原片票据未标记新密钥 %+v", originalTicket)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoListFilterAndSort(t *testing.T) {
|
||||
filter := videoListFilter(12)
|
||||
if filter[videoinformationmod.FN.VideoInformation.Status] != pubdef.InformationStatusVerifyOK ||
|
||||
filter[videoinformationmod.FN.VideoInformation.CategoryId] != int64(12) {
|
||||
t.Fatalf("分类或上架状态筛选错误: %v", filter)
|
||||
}
|
||||
if _, ok := filter[videoinformationmod.FN.VideoInformation.UpdatedAt]; ok {
|
||||
t.Fatalf("最近更新不应限制更新时间: %v", filter)
|
||||
}
|
||||
if got := videoListSort(1); got[0].Key != videoinformationmod.FN.VideoInformation.Real.WatchTimes {
|
||||
t.Fatalf("热度排序错误: %v", got)
|
||||
}
|
||||
if got := videoListSort(2); got[0].Key != videoinformationmod.FN.VideoInformation.UpdatedAt || got[0].Value != -1 {
|
||||
t.Fatalf("最近更新排序错误: %v", got)
|
||||
}
|
||||
if _, err := VideoList(context.Background(), &proto.VideoListReq{SortType: 3}); err == nil {
|
||||
t.Fatal("未拒绝无效排序方式")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoListMixedPagination(t *testing.T) {
|
||||
const videoCount int64 = 17
|
||||
ads := []proxy.AdvitiseInfo{{Id: "ad-1"}, {Id: "ad-2"}}
|
||||
adCount := videoListInsertedAdsCount(videoCount, len(ads))
|
||||
if adCount != 3 {
|
||||
t.Fatalf("广告位数量 = %d, want 3", adCount)
|
||||
}
|
||||
all := make(videoinformationmod.VideoInformationList, 0, videoCount)
|
||||
for id := int64(1); id <= videoCount; id++ {
|
||||
all = append(all, &videoinformationmod.VideoInformation{Id: id})
|
||||
}
|
||||
var got []*proto.VideoRecommendListItem
|
||||
for start := int64(0); start < videoCount+adCount; start += 7 {
|
||||
end := start + 7
|
||||
if end > videoCount+adCount {
|
||||
end = videoCount + adCount
|
||||
}
|
||||
contentStart, contentLimit := moreser.ContentRange(start, end, videoCount, adCount, videoListAdInterval)
|
||||
page := buildVideoListMixedPage(all[contentStart:contentStart+contentLimit], ads, "",
|
||||
start, end, contentStart, adCount)
|
||||
if int64(len(page)) != end-start {
|
||||
t.Fatalf("分页 [%d,%d) 返回 %d 条", start, end, len(page))
|
||||
}
|
||||
got = append(got, page...)
|
||||
}
|
||||
var videoID int64 = 1
|
||||
for pos, item := range got {
|
||||
if pos%6 == 5 && pos < 18 {
|
||||
if item.Type != proto.VideoRecommendItemTypeAdvertise || item.AdvertiseId != ads[(pos/6)%len(ads)].Id {
|
||||
t.Fatalf("位置 %d 广告错误: %+v", pos, item)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if item.Type != proto.VideoRecommendItemTypeVideo || item.Id != videoID {
|
||||
t.Fatalf("位置 %d 视频错误: %+v, want ID %d", pos, item, videoID)
|
||||
}
|
||||
videoID++
|
||||
}
|
||||
if videoID != videoCount+1 {
|
||||
t.Fatalf("视频末尾 ID = %d, want %d", videoID, videoCount+1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoListAdSlots(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
videos int64
|
||||
ads int
|
||||
want int64
|
||||
}{
|
||||
{0, 2, 0}, {5, 2, 0}, {6, 2, 1}, {10, 2, 1}, {11, 2, 2}, {16, 1, 3}, {16, 0, 0},
|
||||
} {
|
||||
if got := videoListInsertedAdsCount(tc.videos, tc.ads); got != tc.want {
|
||||
t.Errorf("视频 %d 条、广告 %d 条得到 %d 个广告位,want %d", tc.videos, tc.ads, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package videoser
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"videoweb/app/appg"
|
||||
"videoweb/app/service/configser"
|
||||
"videoweb/pub/common/ipaddr"
|
||||
"videoweb/pub/common/m3u8"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GetVTT 获取雪碧图 vtt,缩略图路径替换为图片 cdn,兼容 /laosiji 路径资源。
|
||||
func GetVTT(ctx *gin.Context) {
|
||||
fn := func(c *gin.Context) (contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string, err error) {
|
||||
vttName := c.Param("key")
|
||||
|
||||
var req m3u8.Req
|
||||
if err = c.BindQuery(&req); err != nil {
|
||||
return
|
||||
}
|
||||
req.Name = vttName
|
||||
|
||||
imgCdn := configser.GetAppImageCdn(ipaddr.ResolveClientIP(c))
|
||||
originDomain := appg.Conf.OrigDomain
|
||||
lsjImageYuan := appg.Conf.LsjMediaConf.ImageYuan
|
||||
|
||||
buff, err := m3u8.TransformVtt(req.Name, imgCdn, originDomain, lsjImageYuan)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
contentType = "text/vtt; charset=utf-8"
|
||||
reader = bytes.NewReader(buff)
|
||||
contentLength = int64(len(buff))
|
||||
extraHeaders = map[string]string{}
|
||||
return
|
||||
}
|
||||
handlerFile(ctx, fn)
|
||||
}
|
||||
Reference in New Issue
Block a user