Skip to content

Commit 8a612fa

Browse files
authored
Respect standard proxy header for hostname when generating M3U playlist (#611)
* server: get host for M3U similarly to scheme Rely on Gin's location middleware for retrieval of host/port combination, so that HTTP headers (such as ones passed from a reverse proxy) are also taken into account. Only `/stream` API seems to be relevant for #610 (see web's Hosts.js) but fix `/playlist*` APIs as well while we're at it. It seems that Media Station X stuff (`msx.go`) may benefit from this too but keep it intact for now as it's offtopic. * server: fix hostname in M3U Respect standard proxy host header (`X-Forwarded-Host`) when building playlist content. See gin-contrib/location#15 Closes #610
1 parent 53b86c0 commit 8a612fa

File tree

6 files changed

+17
-10
lines changed

6 files changed

+17
-10
lines changed

server/go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/anacrolix/torrent v1.59.1
1818
github.com/dustin/go-humanize v1.0.1
1919
github.com/gin-contrib/cors v1.7.6
20-
github.com/gin-contrib/location v1.0.3
20+
github.com/gin-contrib/location/v2 v2.0.0
2121
github.com/gin-gonic/gin v1.11.0
2222
github.com/hanwen/go-fuse/v2 v2.9.0
2323
github.com/kljensen/snowball v0.10.0
@@ -99,7 +99,6 @@ require (
9999
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 // indirect
100100
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
101101
github.com/ugorji/go/codec v1.3.1 // indirect
102-
go.uber.org/mock v0.6.0 // indirect
103102
go.yaml.in/yaml/v3 v3.0.4 // indirect
104103
golang.org/x/arch v0.23.0 // indirect
105104
golang.org/x/crypto v0.45.0 // indirect

server/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ github.com/gin-contrib/cors v1.7.6 h1:3gQ8GMzs1Ylpf70y8bMw4fVpycXIeX1ZemuSQIsnQQ
230230
github.com/gin-contrib/cors v1.7.6/go.mod h1:Ulcl+xN4jel9t1Ry8vqph23a60FwH9xVLd+3ykmTjOk=
231231
github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4=
232232
github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk=
233-
github.com/gin-contrib/location v1.0.3 h1:iy5FY2JsunZ73Lnq8YZsx7wkGFY1xcyRdKiRh/8Uptg=
234-
github.com/gin-contrib/location v1.0.3/go.mod h1:fMoqRQxX0d5ycvxzP7e5VtqfID00RPb4jMGDh3oT0pk=
233+
github.com/gin-contrib/location/v2 v2.0.0 h1:iLx5RatHQHSxgC0tm2AG0sIuQKecI7FhREessVd6RWY=
234+
github.com/gin-contrib/location/v2 v2.0.0/go.mod h1:276TDNr25NENBA/NQZUuEIlwxy/I5CYVFIr/d2TgOdU=
235235
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
236236
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
237237
github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk=

server/utils/location.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package utils
22

33
import (
4-
"github.com/gin-contrib/location"
4+
"github.com/gin-contrib/location/v2"
55
"github.com/gin-gonic/gin"
66
)
77

@@ -12,3 +12,11 @@ func GetScheme(c *gin.Context) string {
1212
}
1313
return url.Scheme
1414
}
15+
16+
func GetHost(c *gin.Context) string {
17+
url := location.Get(c)
18+
if url == nil {
19+
return c.Request.Host
20+
}
21+
return url.Host
22+
}

server/web/api/m3u.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
func allPlayList(c *gin.Context) {
3636
torrs := torr.ListTorrent()
3737

38-
host := utils.GetScheme(c) + "://" + c.Request.Host
38+
host := utils.GetScheme(c) + "://" + utils.GetHost(c)
3939
list := "#EXTM3U\n"
4040
hash := ""
4141
// fn=file.m3u fix forkplayer bug with end .m3u in link
@@ -87,7 +87,7 @@ func playList(c *gin.Context) {
8787
}
8888
}
8989

90-
host := utils.GetScheme(c) + "://" + c.Request.Host
90+
host := utils.GetScheme(c) + "://" + utils.GetHost(c)
9191
list := getM3uList(tor.Status(), host, fromlast)
9292
list = "#EXTM3U\n" + list
9393
name := strings.ReplaceAll(c.Param("fname"), `/`, "") // strip starting / from param

server/web/api/stream.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func stream(c *gin.Context) {
179179
} else if !strings.HasSuffix(strings.ToLower(name), ".m3u") && !strings.HasSuffix(strings.ToLower(name), ".m3u8") {
180180
name += ".m3u"
181181
}
182-
m3ulist := "#EXTM3U\n" + getM3uList(tor.Status(), utils2.GetScheme(c)+"://"+c.Request.Host, fromlast)
182+
m3ulist := "#EXTM3U\n" + getM3uList(tor.Status(), utils2.GetScheme(c)+"://"+utils2.GetHost(c), fromlast)
183183
sendM3U(c, name, tor.Hash().HexString(), m3ulist)
184184
return
185185
} else
@@ -299,7 +299,7 @@ func streamNoAuth(c *gin.Context) {
299299
} else if !strings.HasSuffix(strings.ToLower(name), ".m3u") && !strings.HasSuffix(strings.ToLower(name), ".m3u8") {
300300
name += ".m3u"
301301
}
302-
m3ulist := "#EXTM3U\n" + getM3uList(tor.Status(), utils2.GetScheme(c)+"://"+c.Request.Host, fromlast)
302+
m3ulist := "#EXTM3U\n" + getM3uList(tor.Status(), utils2.GetScheme(c)+"://"+utils2.GetHost(c), fromlast)
303303
sendM3U(c, name, tor.Hash().HexString(), m3ulist)
304304
return
305305
} else

server/web/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"server/rutor"
1212

1313
"github.com/gin-contrib/cors"
14-
"github.com/gin-contrib/location"
14+
"github.com/gin-contrib/location/v2"
1515
"github.com/gin-gonic/gin"
1616
"github.com/wlynxg/anet"
1717

0 commit comments

Comments
 (0)