Skip to content

Commit

Permalink
Add endpoint anti colision
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 d3d0f99 commit ba6da3b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
12 changes: 7 additions & 5 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
}

req.Header.Set("User-Agent", ctx.Request.UserAgent())
copyHttpHeader(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, resp.Header)
copyHttpHeader(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,11 @@ func (c *Config) xtreamStream(ctx *gin.Context, oriURL *url.URL) {
c.stream(ctx, oriURL)
}

func copyHTTPHeader(ctx *gin.Context, header http.Header) {
for k, v := range header {
ctx.Header(k, strings.Join(v, ", "))
func copyHttpHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func (c *Config) m3uRoutes(r *gin.RouterGroup) {
}

if strings.HasSuffix(track.URI, ".m3u8") {
r.GET(fmt.Sprintf("/%s/%s/%d/:id", c.User, c.Password, i), trackConfig.m3u8ReverseProxy)
r.GET(fmt.Sprintf("/%s/%s/%s/%d/:id", c.endpointAntiColision, c.User, c.Password, i), trackConfig.m3u8ReverseProxy)
} else {
r.GET(fmt.Sprintf("/%s/%s/%d/%s", c.User, c.Password, i, path.Base(track.URI)), trackConfig.reverseProxy)
r.GET(fmt.Sprintf("/%s/%s/%s/%d/%s", c.endpointAntiColision, c.User, c.Password, i, path.Base(track.URI)), trackConfig.reverseProxy)
}
}
}
6 changes: 5 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
)

var defaultProxyfiedM3UPath = filepath.Join(os.TempDir(), uuid.NewV4().String()+".iptv-proxy.m3u")
var endpointAntiColision = strings.Split(uuid.NewV4().String(), "-")[0]

// Config represent the server configuration
type Config struct {
Expand All @@ -48,6 +49,8 @@ type Config struct {
track *m3u.Track
// path to the proxyfied m3u file
proxyfiedM3UPath string

endpointAntiColision string
}

// NewServer initialize a new server configuration
Expand All @@ -66,6 +69,7 @@ func NewServer(config *config.ProxyConfig) (*Config, error) {
&p,
nil,
defaultProxyfiedM3UPath,
endpointAntiColision,
}, nil
}

Expand Down Expand Up @@ -154,7 +158,7 @@ func (c *Config) replaceURL(uri string, trackIndex int, xtream bool) (string, er
uriPath = strings.ReplaceAll(uriPath, c.XtreamUser.PathEscape(), c.User.PathEscape())
uriPath = strings.ReplaceAll(uriPath, c.XtreamPassword.PathEscape(), c.Password.PathEscape())
} else {
uriPath = path.Join("/", c.User.PathEscape(), c.Password.PathEscape(), fmt.Sprintf("%d", trackIndex), path.Base(uriPath))
uriPath = path.Join("/", c.endpointAntiColision, c.User.PathEscape(), c.Password.PathEscape(), fmt.Sprintf("%d", trackIndex), path.Base(uriPath))
}

basicAuth := oriURL.User.String()
Expand Down

0 comments on commit ba6da3b

Please sign in to comment.