Fix possible infinite loop with HLS session rewriter
This commit is contained in:
parent
e45f80ed42
commit
74110dae54
1 changed files with 21 additions and 2 deletions
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"path"
|
||||||
urlpath "path"
|
urlpath "path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
@ -298,7 +299,7 @@ func (r *bodyReader) Close() error {
|
||||||
func (r *bodyReader) getSegments(dir string) []string {
|
func (r *bodyReader) getSegments(dir string) []string {
|
||||||
segments := []string{}
|
segments := []string{}
|
||||||
|
|
||||||
// Find all segments URLS in the .m3u8
|
// Find all segment URLs in the .m3u8
|
||||||
scanner := bufio.NewScanner(&r.buffer)
|
scanner := bufio.NewScanner(&r.buffer)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
|
|
@ -405,9 +406,23 @@ func (g *sessionRewriter) rewriteHLS(sessionID string, requestURL *url.URL) {
|
||||||
|
|
||||||
q := u.Query()
|
q := u.Query()
|
||||||
|
|
||||||
|
loop := false
|
||||||
|
|
||||||
// If this is a master manifest (i.e. an m3u8 which contains references to other m3u8), then
|
// If this is a master manifest (i.e. an m3u8 which contains references to other m3u8), then
|
||||||
// we give each substream an own session ID if they don't have already.
|
// we give each substream an own session ID if they don't have already.
|
||||||
if strings.HasSuffix(u.Path, ".m3u8") {
|
if strings.HasSuffix(u.Path, ".m3u8") {
|
||||||
|
// Check if we're referring to ourselves. This will cause an infinite loop
|
||||||
|
// and has to be stopped.
|
||||||
|
file := u.Path
|
||||||
|
if !strings.HasPrefix(file, "/") {
|
||||||
|
dir := path.Dir(requestURL.Path)
|
||||||
|
file = filepath.Join(dir, file)
|
||||||
|
}
|
||||||
|
|
||||||
|
if requestURL.Path == file {
|
||||||
|
loop = true
|
||||||
|
}
|
||||||
|
|
||||||
q.Set("session", shortuuid.New())
|
q.Set("session", shortuuid.New())
|
||||||
|
|
||||||
isMaster = true
|
isMaster = true
|
||||||
|
|
@ -417,8 +432,12 @@ func (g *sessionRewriter) rewriteHLS(sessionID string, requestURL *url.URL) {
|
||||||
|
|
||||||
u.RawQuery = q.Encode()
|
u.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
if loop {
|
||||||
|
buffer.WriteString("# m3u8 is referencing itself: " + u.String() + "\n")
|
||||||
|
} else {
|
||||||
buffer.WriteString(u.String() + "\n")
|
buffer.WriteString(u.String() + "\n")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue