Skip to content

Commit

Permalink
Forward HTTP headers on request
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com>
  • Loading branch information
pierre-emmanuelJ committed Mar 24, 2021
1 parent 66d0a5c commit c9f2c63
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 23 deletions.
21 changes: 18 additions & 3 deletions pkg/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *Config) stream(ctx *gin.Context, oriURL *url.URL) {
return
}

copyHttpHeader(req.Header, ctx.Request.Header)
mergeHttpHeader(req.Header, ctx.Request.Header)

resp, err := client.Do(req)
if err != nil {
Expand All @@ -80,7 +80,7 @@ func (c *Config) stream(ctx *gin.Context, oriURL *url.URL) {
}
defer resp.Body.Close()

copyHttpHeader(ctx.Writer.Header(), resp.Header)
mergeHttpHeader(ctx.Writer.Header(), resp.Header)
ctx.Status(resp.StatusCode)
ctx.Stream(func(w io.Writer) bool {
io.Copy(w, resp.Body) // nolint: errcheck
Expand All @@ -98,9 +98,24 @@ func (c *Config) xtreamStream(ctx *gin.Context, oriURL *url.URL) {
c.stream(ctx, oriURL)
}

func copyHttpHeader(dst, src http.Header) {
type values []string

func (vs values) contains(s string) bool {
for _, v := range vs {
if v == s {
return true
}
}

return false
}

func mergeHttpHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
if values(dst.Values(k)).contains(v) {
continue
}
dst.Add(k, v)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
"path/filepath"
"strings"

"github.com/gin-contrib/cors"
"github.com/jamesnetherton/m3u"
"github.com/pierre-emmanuelJ/iptv-proxy/pkg/config"
uuid "github.com/satori/go.uuid"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)

Expand Down
6 changes: 3 additions & 3 deletions pkg/server/xtreamHandles.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (c *Config) hlsXtreamStream(ctx *gin.Context, oriURL *url.URL) {
return
}

copyHttpHeader(req.Header, ctx.Request.Header)
mergeHttpHeader(req.Header, ctx.Request.Header)

resp, err := client.Do(req)
if err != nil {
Expand All @@ -361,7 +361,7 @@ func (c *Config) hlsXtreamStream(ctx *gin.Context, oriURL *url.URL) {
return
}

copyHttpHeader(hlsReq.Header, ctx.Request.Header)
mergeHttpHeader(hlsReq.Header, ctx.Request.Header)

hlsResp, err := client.Do(hlsReq)
if err != nil {
Expand All @@ -378,7 +378,7 @@ func (c *Config) hlsXtreamStream(ctx *gin.Context, oriURL *url.URL) {
body := string(b)
body = strings.ReplaceAll(body, "/"+c.XtreamUser.String()+"/"+c.XtreamPassword.String()+"/", "/"+c.User.String()+"/"+c.Password.String()+"/")

copyHttpHeader(ctx.Request.Header, hlsResp.Header)
mergeHttpHeader(ctx.Writer.Header(), hlsResp.Header)

ctx.Data(http.StatusOK, hlsResp.Header.Get("Content-Type"), []byte(body))
return
Expand Down
52 changes: 36 additions & 16 deletions pkg/xtream-proxy/xtream-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,36 +104,46 @@ func (c *Client) Action(config *config.ProxyConfig, action string, q url.Values)
case getLiveCategories:
respBody, err = c.GetLiveCategories()
case getLiveStreams:
respBody, err = c.GetLiveStreams("")
categoryID := ""
if len(q["category_id"]) > 0 {
categoryID = q["category_id"][0]
}
respBody, err = c.GetLiveStreams(categoryID)
case getVodCategories:
respBody, err = c.GetVideoOnDemandCategories()
case getVodStreams:
respBody, err = c.GetVideoOnDemandStreams("")
categoryID := ""
if len(q["category_id"]) > 0 {
categoryID = q["category_id"][0]
}
respBody, err = c.GetVideoOnDemandStreams(categoryID)
case getVodInfo:
if len(q["vod_id"]) < 1 {
err = fmt.Errorf(`bad body url query parameters: missing "vod_id"`)
httpcode = http.StatusBadRequest
httpcode, err = validateParams(q, "vod_id")
if err != nil {
return
}
respBody, err = c.GetVideoOnDemandInfo(q["vod_id"][0])
case getSeriesCategories:
respBody, err = c.GetSeriesCategories()
case getSeries:
respBody, err = c.GetSeries("")
categoryID := ""
if len(q["category_id"]) > 0 {
categoryID = q["category_id"][0]
}
respBody, err = c.GetSeries(categoryID)
case getSerieInfo:
if len(q["series_id"]) < 1 {
err = fmt.Errorf(`bad body url query parameters: missing "series_id"`)
httpcode = http.StatusBadRequest
httpcode, err = validateParams(q, "series_id")
if err != nil {
return
}
respBody, err = c.GetSeriesInfo(q["series_id"][0])
case getShortEPG:
if len(q["stream_id"]) < 1 {
err = fmt.Errorf(`bad body url query parameters: missing "stream_id"`)
httpcode = http.StatusBadRequest
limit := 0

httpcode, err = validateParams(q, "stream_id")
if err != nil {
return
}
limit := 0
if len(q["limit"]) > 0 {
limit, err = strconv.Atoi(q["limit"][0])
if err != nil {
Expand All @@ -143,9 +153,8 @@ func (c *Client) Action(config *config.ProxyConfig, action string, q url.Values)
}
respBody, err = c.GetShortEPG(q["stream_id"][0], limit)
case getSimpleDataTable:
if len(q["stream_id"]) < 1 {
err = fmt.Errorf(`bad body url query parameters: missing "stream_id"`)
httpcode = http.StatusBadRequest
httpcode, err = validateParams(q, "stream_id")
if err != nil {
return
}
respBody, err = c.GetEPG(q["stream_id"][0])
Expand All @@ -155,3 +164,14 @@ func (c *Client) Action(config *config.ProxyConfig, action string, q url.Values)

return
}

func validateParams(u url.Values, params ...string) (int, error) {
for _, p := range params {
if len(u[p]) < 1 {
return http.StatusBadRequest, fmt.Errorf("missing %q", p)
}

}

return 0, nil
}

0 comments on commit c9f2c63

Please sign in to comment.