Skip to content

Commit

Permalink
fix: 修复虎牙直播异常分段问题 (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaobiao131 authored Jul 30, 2023
1 parent d40f568 commit 346816a
Showing 1 changed file with 48 additions and 10 deletions.
58 changes: 48 additions & 10 deletions src/live/huya/huya.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package huya

import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"fmt"
"math/rand"
"net/http"
"net/url"
"strconv"
"strings"
"time"

"github.com/hr3lxphr6j/requests"
uuid "github.com/satori/go.uuid"

"github.com/hr3lxphr6j/bililive-go/src/live"
"github.com/hr3lxphr6j/bililive-go/src/live/internal"
Expand Down Expand Up @@ -82,6 +87,35 @@ func (l *Live) GetInfo() (info *live.Info, err error) {
return info, nil
}

func GetMD5Hash(text string) string {
hash := md5.Sum([]byte(text))
return hex.EncodeToString(hash[:])
}

func parseAntiCode(anticode string, uid int64, streamName string) (string, error) {
qr, err := url.ParseQuery(anticode)
if err != nil {
return "", err
}
qr.Set("ver", "1")
qr.Set("sv", "2110211124")
qr.Set("seqid", strconv.FormatInt(time.Now().Unix()*1000+uid, 10))
qr.Set("uid", strconv.FormatInt(uid, 10))
uuid, _ := uuid.NewV4()
qr.Set("uuid", uuid.String())
ss := GetMD5Hash(fmt.Sprintf("%s|%s|%s", qr.Get("seqid"), qr.Get("ctype"), qr.Get("t")))

decodeString, _ := base64.StdEncoding.DecodeString(qr.Get("fm"))
fm := string(decodeString)
fm = strings.ReplaceAll(fm, "$0", qr.Get("uid"))
fm = strings.ReplaceAll(fm, "$1", streamName)
fm = strings.ReplaceAll(fm, "$2", ss)
fm = strings.ReplaceAll(fm, "$3", qr.Get("wsTime"))

qr.Set("wsSecret", GetMD5Hash(fm))
return qr.Encode(), nil
}

func (l *Live) GetStreamUrls() (us []*url.URL, err error) {
resp, err := requests.Get(l.Url.String(), live.CommonUserAgent)
if err != nil {
Expand All @@ -102,20 +136,24 @@ func (l *Live) GetStreamUrls() (us []*url.URL, err error) {
sStreamName = utils.Match1(`"sStreamName":"([^"]*)"`, streamStr)
sFlvUrl = strings.ReplaceAll(utils.Match1(`"sFlvUrl":"([^"]*)"`, streamStr), `\/`, `/`)
sFlvAntiCode = utils.Match1(`"sFlvAntiCode":"([^"]*)"`, streamStr)
iLineIndex = utils.Match1(`"iLineIndex":(\d*),`, streamStr)
uid = (time.Now().Unix()%1e7*1e6 + int64(1e3*rand.Float64())) % 4294967295
// iLineIndex = utils.Match1(`"iLineIndex":(\d*),`, streamStr)
uid = (time.Now().Unix()%1e7*1e6 + int64(1e3*rand.Float64())) % 4294967295
)
u, err := url.Parse(fmt.Sprintf("%s/%s.flv", sFlvUrl, sStreamName))
query, err := parseAntiCode(sFlvAntiCode, uid, sStreamName)
if err != nil {
return nil, err
}
u, err := url.Parse(fmt.Sprintf("%s/%s.flv?%s", sFlvUrl, sStreamName, query))
if err != nil {
return nil, err
}
value := url.Values{}
value.Add("line", iLineIndex)
value.Add("p2p", "0")
value.Add("type", "web")
value.Add("ver", "1805071653")
value.Add("uid", fmt.Sprintf("%d", uid))
u.RawQuery = fmt.Sprintf("%s&%s", value.Encode(), utils.UnescapeHTMLEntity(sFlvAntiCode))
// value := url.Values{}
// value.Add("line", iLineIndex)
// value.Add("p2p", "0")
// value.Add("type", "web")
// value.Add("ver", "1805071653")
// value.Add("uid", fmt.Sprintf("%d", uid))
// u.RawQuery = fmt.Sprintf("%s&%s", value.Encode(), utils.UnescapeHTMLEntity(sFlvAntiCode))
return []*url.URL{u}, nil
}

Expand Down

0 comments on commit 346816a

Please sign in to comment.