From 020e718c664bbfe9dead14fe88268bb22a98ddd9 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Jacquier Date: Sun, 10 Nov 2019 21:26:40 +0000 Subject: [PATCH] fixup! Fix and add HLS support for Xtream provider service Signed-off-by: Pierre-Emmanuel Jacquier --- pkg/routes/routes.go | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index 59896a35..3dd5d0be 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -2,6 +2,7 @@ package routes import ( "bytes" + "errors" "fmt" "io" "io/ioutil" @@ -123,6 +124,28 @@ func (p *proxy) reverseProxy(c *gin.Context) { } func (p *proxy) stream(c *gin.Context, oriURL *url.URL) { + id := c.Param("id") + if strings.HasSuffix(id, ".m3u8") { + p.hlsStream(c, oriURL) + return + } + + resp, err := http.Get(oriURL.String()) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + defer resp.Body.Close() + + copyHTTPHeader(c, resp.Header) + c.Status(resp.StatusCode) + c.Stream(func(w io.Writer) bool { + io.Copy(w, resp.Body) + return false + }) +} + +func (p *proxy) hlsStream(c *gin.Context, oriURL *url.URL) { client := &http.Client{ CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse @@ -165,14 +188,11 @@ func (p *proxy) stream(c *gin.Context, oriURL *url.URL) { c.Data(http.StatusOK, hlsResp.Header.Get("Content-Type"), []byte(body)) return } + c.AbortWithError(http.StatusInternalServerError, errors.New("Unable to HLS stream")) + return } - copyHTTPHeader(c, resp.Header) c.Status(resp.StatusCode) - c.Stream(func(w io.Writer) bool { - io.Copy(w, resp.Body) - return false - }) } func copyHTTPHeader(c *gin.Context, header http.Header) {