Skip to content

Commit

Permalink
fix new xtream hls Support
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Emmanuel Jacquier <[email protected]>
  • Loading branch information
pierre-emmanuelJ committed Mar 19, 2021
1 parent ed79269 commit 66d0a5c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions pkg/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (c *Config) xtreamRoutes(r *gin.RouterGroup) {
r.GET(fmt.Sprintf("/movie/%s/%s/:id", c.User, c.Password), c.xtreamStreamMovie)
r.GET(fmt.Sprintf("/series/%s/%s/:id", c.User, c.Password), c.xtreamStreamSeries)
r.GET(fmt.Sprintf("/hlsr/:token/%s/%s/:channel/:hash/:chunk", c.User, c.Password), c.xtreamHlsrStream)
r.GET("/hls/:token/:chunk", c.xtreamHlsStream)
}

func (c *Config) m3uRoutes(r *gin.RouterGroup) {
Expand Down
59 changes: 53 additions & 6 deletions pkg/server/xtreamHandles.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,50 @@ func (c *Config) xtreamStreamSeries(ctx *gin.Context) {
c.xtreamStream(ctx, rpURL)
}

func (c *Config) xtreamHlsStream(ctx *gin.Context) {
chunk := ctx.Param("chunk")
s := strings.Split(chunk, "_")
if len(s) != 2 {
ctx.AbortWithError( // nolint: errcheck
http.StatusInternalServerError,
errors.New("HSL malformed chunk"),
)
return
}
channel := s[0]

url, err := getHlsRedirectURL(channel)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err) // nolint: errcheck
return
}

req, err := url.Parse(
fmt.Sprintf(
"%s://%s/hls/%s/%s",
url.Scheme,
url.Host,
ctx.Param("token"),
ctx.Param("chunk"),
),
)

if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err) // nolint: errcheck
return
}

c.xtreamStream(ctx, req)
}

func (c *Config) xtreamHlsrStream(ctx *gin.Context) {
hlsChannelsRedirectURLLock.RLock()
url, ok := hlsChannelsRedirectURL[ctx.Param("channel")+".m3u8"]
if !ok {
ctx.AbortWithError(http.StatusNotFound, errors.New("HSL redirect url not found")) // nolint: errcheck
hlsChannelsRedirectURLLock.RUnlock()
channel := ctx.Param("channel")

url, err := getHlsRedirectURL(channel)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err) // nolint: errcheck
return
}
hlsChannelsRedirectURLLock.RUnlock()

req, err := url.Parse(
fmt.Sprintf(
Expand All @@ -274,6 +309,18 @@ func (c *Config) xtreamHlsrStream(ctx *gin.Context) {
c.xtreamStream(ctx, req)
}

func getHlsRedirectURL(channel string) (*url.URL, error) {
hlsChannelsRedirectURLLock.RLock()
defer hlsChannelsRedirectURLLock.RUnlock()

url, ok := hlsChannelsRedirectURL[channel+".m3u8"]
if !ok {
return nil, errors.New("HSL redirect url not found")
}

return &url, nil
}

func (c *Config) hlsXtreamStream(ctx *gin.Context, oriURL *url.URL) {
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
Expand Down

0 comments on commit 66d0a5c

Please sign in to comment.