diff --git a/pkg/server/handlers.go b/pkg/server/handlers.go index baf999a..e11e4df 100644 --- a/pkg/server/handlers.go +++ b/pkg/server/handlers.go @@ -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 { @@ -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 @@ -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) + } } } diff --git a/pkg/server/routes.go b/pkg/server/routes.go index b1ae27b..4440d27 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -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) } } } diff --git a/pkg/server/server.go b/pkg/server/server.go index c73f9d8..23d8087 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -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 { @@ -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 @@ -66,6 +69,7 @@ func NewServer(config *config.ProxyConfig) (*Config, error) { &p, nil, defaultProxyfiedM3UPath, + endpointAntiColision, }, nil } @@ -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()