Skip to content

Commit ba6da3b

Browse files
Add endpoint anti colision
Signed-off-by: Pierre-Emmanuel Jacquier <[email protected]>
1 parent d3d0f99 commit ba6da3b

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

pkg/server/handlers.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (c *Config) stream(ctx *gin.Context, oriURL *url.URL) {
7171
return
7272
}
7373

74-
req.Header.Set("User-Agent", ctx.Request.UserAgent())
74+
copyHttpHeader(req.Header, ctx.Request.Header)
7575

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

83-
copyHTTPHeader(ctx, resp.Header)
83+
copyHttpHeader(ctx.Writer.Header(), resp.Header)
8484
ctx.Status(resp.StatusCode)
8585
ctx.Stream(func(w io.Writer) bool {
8686
io.Copy(w, resp.Body) // nolint: errcheck
@@ -98,9 +98,11 @@ func (c *Config) xtreamStream(ctx *gin.Context, oriURL *url.URL) {
9898
c.stream(ctx, oriURL)
9999
}
100100

101-
func copyHTTPHeader(ctx *gin.Context, header http.Header) {
102-
for k, v := range header {
103-
ctx.Header(k, strings.Join(v, ", "))
101+
func copyHttpHeader(dst, src http.Header) {
102+
for k, vv := range src {
103+
for _, v := range vv {
104+
dst.Add(k, v)
105+
}
104106
}
105107
}
106108

pkg/server/routes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ func (c *Config) m3uRoutes(r *gin.RouterGroup) {
7373
}
7474

7575
if strings.HasSuffix(track.URI, ".m3u8") {
76-
r.GET(fmt.Sprintf("/%s/%s/%d/:id", c.User, c.Password, i), trackConfig.m3u8ReverseProxy)
76+
r.GET(fmt.Sprintf("/%s/%s/%s/%d/:id", c.endpointAntiColision, c.User, c.Password, i), trackConfig.m3u8ReverseProxy)
7777
} else {
78-
r.GET(fmt.Sprintf("/%s/%s/%d/%s", c.User, c.Password, i, path.Base(track.URI)), trackConfig.reverseProxy)
78+
r.GET(fmt.Sprintf("/%s/%s/%s/%d/%s", c.endpointAntiColision, c.User, c.Password, i, path.Base(track.URI)), trackConfig.reverseProxy)
7979
}
8080
}
8181
}

pkg/server/server.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
)
3838

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

4142
// Config represent the server configuration
4243
type Config struct {
@@ -48,6 +49,8 @@ type Config struct {
4849
track *m3u.Track
4950
// path to the proxyfied m3u file
5051
proxyfiedM3UPath string
52+
53+
endpointAntiColision string
5154
}
5255

5356
// NewServer initialize a new server configuration
@@ -66,6 +69,7 @@ func NewServer(config *config.ProxyConfig) (*Config, error) {
6669
&p,
6770
nil,
6871
defaultProxyfiedM3UPath,
72+
endpointAntiColision,
6973
}, nil
7074
}
7175

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

160164
basicAuth := oriURL.User.String()

0 commit comments

Comments
 (0)