Add endpoint anti colision

Signed-off-by: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com>
This commit is contained in:
Pierre-Emmanuel Jacquier
2021-03-19 11:48:04 +01:00
parent d3d0f998e2
commit ba6da3be5c
3 changed files with 14 additions and 8 deletions

View File

@@ -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)
}
}
}

View File

@@ -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)
}
}
}

View File

@@ -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()