Fix: bilibili mpd url no token

pull/156/head
zijiren233 10 months ago
parent 1529653da0
commit c3f2b271b5

@ -21,8 +21,8 @@ import (
)
type BilibiliMpdCache struct {
Mpd string
HevcMpd string
Mpd *mpd.MPD
HevcMpd *mpd.MPD
Urls []string
}
@ -116,21 +116,48 @@ func BilibiliSharedMpdCacheInitFunc(ctx context.Context, movie *model.Movie, arg
}
}
}
s, err := m.WriteToString()
if err != nil {
return nil, err
}
s2, err := hevcM.WriteToString()
if err != nil {
return nil, err
}
return &BilibiliMpdCache{
Urls: movies,
Mpd: s,
HevcMpd: s2,
Mpd: m,
HevcMpd: hevcM,
}, nil
}
func BilibiliMpdToString(mpdRaw *mpd.MPD, token string) (string, error) {
newMpdRaw := *mpdRaw
newPeriods := make([]*mpd.Period, len(mpdRaw.Periods))
for i, p := range mpdRaw.Periods {
n := *p
newPeriods[i] = &n
}
newMpdRaw.Periods = newPeriods
for _, p := range newMpdRaw.Periods {
newAdaptationSets := make([]*mpd.AdaptationSet, len(p.AdaptationSets))
for i, as := range p.AdaptationSets {
n := *as
newAdaptationSets[i] = &n
}
p.AdaptationSets = newAdaptationSets
for _, as := range p.AdaptationSets {
newRepresentations := make([]*mpd.Representation, len(as.Representations))
for i, r := range as.Representations {
n := *r
newRepresentations[i] = &n
}
as.Representations = newRepresentations
for _, r := range as.Representations {
newBaseURL := make([]string, len(r.BaseURL))
copy(newBaseURL, r.BaseURL)
r.BaseURL = newBaseURL
for i := range r.BaseURL {
r.BaseURL[i] = fmt.Sprintf("%s&token=%s", r.BaseURL[i], token)
}
}
}
}
return newMpdRaw.WriteToString()
}
func NewBilibiliNoSharedMovieCacheInitFunc(movie *model.Movie) func(ctx context.Context, key string, args ...*BilibiliUserCache) (string, error) {
return func(ctx context.Context, key string, args ...*BilibiliUserCache) (string, error) {
return BilibiliNoSharedMovieCacheInitFunc(ctx, movie, args...)

@ -30,6 +30,7 @@ import (
uhc "github.com/zijiren233/go-uhc"
"github.com/zijiren233/livelib/protocol/hls"
"github.com/zijiren233/livelib/protocol/httpflv"
"github.com/zijiren233/stream"
"golang.org/x/exp/maps"
)
@ -861,9 +862,21 @@ func proxyVendorMovie(ctx *gin.Context, movie *op.Movie) {
}
if id := ctx.Query("id"); id == "" {
if t == "hevc" {
ctx.Data(http.StatusOK, "application/dash+xml", []byte(mpdC.HevcMpd))
s, err := cache.BilibiliMpdToString(mpdC.HevcMpd, ctx.MustGet("token").(string))
if err != nil {
log.Errorf("proxy vendor movie error: %v", err)
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
return
}
ctx.Data(http.StatusOK, "application/dash+xml", stream.StringToBytes(s))
} else {
ctx.Data(http.StatusOK, "application/dash+xml", []byte(mpdC.Mpd))
s, err := cache.BilibiliMpdToString(mpdC.Mpd, ctx.MustGet("token").(string))
if err != nil {
log.Errorf("proxy vendor movie error: %v", err)
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
return
}
ctx.Data(http.StatusOK, "application/dash+xml", stream.StringToBytes(s))
}
return
} else {

@ -364,10 +364,12 @@ func AuthRootMiddleware(ctx *gin.Context) {
func GetAuthorizationTokenFromContext(ctx *gin.Context) (string, error) {
Authorization := ctx.GetHeader("Authorization")
if Authorization != "" {
ctx.Set("token", Authorization)
return Authorization, nil
}
Authorization = ctx.Query("token")
if Authorization != "" {
ctx.Set("token", Authorization)
return Authorization, nil
}
return "", errors.New("token is empty")

Loading…
Cancel
Save