mirror of
https://github.com/pierre-emmanuelJ/iptv-proxy.git
synced 2026-05-19 21:25:29 +02:00
Add UNSAFE auth
Signed-off-by: Pierre-Emmanuel Jacquier <pierre-emmanuel.jacquier@epitech.eu>
This commit is contained in:
@@ -34,6 +34,8 @@ var rootCmd = &cobra.Command{
|
||||
Hostname: viper.GetString("hostname"),
|
||||
Port: viper.GetInt64("port"),
|
||||
},
|
||||
User: viper.GetString("user"),
|
||||
Password: viper.GetString("password"),
|
||||
}
|
||||
|
||||
if e := routes.Serve(conf); e != nil {
|
||||
@@ -61,6 +63,8 @@ func init() {
|
||||
rootCmd.Flags().String("m3u-url", "http://example.com/iptv.m3u", "iptv m3u file")
|
||||
rootCmd.Flags().Int64("port", 8080, "Port to expose the IPTVs endpoints")
|
||||
rootCmd.Flags().String("hostname", "", "Hostname or IP to expose the IPTVs endpoints")
|
||||
rootCmd.Flags().String("user", "usertest", "user UNSAFE(temp auth to access proxy)")
|
||||
rootCmd.Flags().String("password", "passwordtest", "password UNSAFE(temp auth to access proxy)")
|
||||
|
||||
if e := viper.BindPFlags(rootCmd.Flags()); e != nil {
|
||||
log.Fatal("error binding PFlags to viper")
|
||||
|
||||
@@ -12,4 +12,6 @@ type HostConfiguration struct {
|
||||
type ProxyConfig struct {
|
||||
Playlist *m3u.Playlist
|
||||
HostConfig *HostConfiguration
|
||||
//XXX Very unsafe
|
||||
User, Password string
|
||||
}
|
||||
|
||||
@@ -28,15 +28,23 @@ func Marshall(p *m3u.Playlist) (string, error) {
|
||||
}
|
||||
|
||||
// ReplaceURL replace original playlist url by proxy url
|
||||
func ReplaceURL(playlist *m3u.Playlist, config *config.HostConfiguration) (*m3u.Playlist, error) {
|
||||
result := make([]m3u.Track, 0, len(playlist.Tracks))
|
||||
for _, track := range playlist.Tracks {
|
||||
func ReplaceURL(proxyConfig *config.ProxyConfig) (*m3u.Playlist, error) {
|
||||
result := make([]m3u.Track, 0, len(proxyConfig.Playlist.Tracks))
|
||||
for _, track := range proxyConfig.Playlist.Tracks {
|
||||
oriURL, err := url.Parse(track.URI)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
destURL, err := url.Parse(fmt.Sprintf("http://%s:%d%s", config.Hostname, config.Port, oriURL.RequestURI()))
|
||||
config := proxyConfig.HostConfig
|
||||
uri := fmt.Sprintf(
|
||||
"http://%s:%d%s?user=%s&password=%s",
|
||||
config.Hostname,
|
||||
config.Port,
|
||||
oriURL.RequestURI(),
|
||||
proxyConfig.User,
|
||||
proxyConfig.Password,
|
||||
)
|
||||
destURL, err := url.Parse(uri)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ func Routes(proxyConfig *config.ProxyConfig, r *gin.RouterGroup) {
|
||||
nil,
|
||||
}
|
||||
|
||||
r.GET("/iptv.m3u", p.getM3U)
|
||||
r.GET("/iptv.m3u", p.authenticate, p.getM3U)
|
||||
|
||||
for i, track := range proxyConfig.Playlist.Tracks {
|
||||
oriURL, err := url.Parse(track.URI)
|
||||
@@ -49,7 +49,7 @@ func Routes(proxyConfig *config.ProxyConfig, r *gin.RouterGroup) {
|
||||
nil,
|
||||
&proxyConfig.Playlist.Tracks[i],
|
||||
}
|
||||
r.GET(oriURL.RequestURI(), tmp.reverseProxy)
|
||||
r.GET(oriURL.RequestURI(), p.authenticate, tmp.reverseProxy)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ func (p *proxy) reverseProxy(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (p *proxy) getM3U(c *gin.Context) {
|
||||
playlist, err := proxyM3U.ReplaceURL(p.Playlist, p.HostConfig)
|
||||
playlist, err := proxyM3U.ReplaceURL(p.ProxyConfig)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
@@ -86,3 +86,21 @@ func (p *proxy) getM3U(c *gin.Context) {
|
||||
c.Header("Content-Disposition", "attachment; filename=\"iptv.m3u\"")
|
||||
c.Data(http.StatusOK, "application/octet-stream", []byte(result))
|
||||
}
|
||||
|
||||
// AuthRequest handle auth credentials
|
||||
type AuthRequest struct {
|
||||
User string `form:"user" binding:"required"`
|
||||
Password string `form:"password" binding:"required"`
|
||||
} // XXX very unsafe
|
||||
|
||||
func (p *proxy) authenticate(ctx *gin.Context) {
|
||||
var authReq AuthRequest
|
||||
if err := ctx.Bind(&authReq); err != nil {
|
||||
ctx.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
//XXX very unsafe
|
||||
if p.ProxyConfig.User != authReq.User || p.ProxyConfig.Password != authReq.Password {
|
||||
ctx.AbortWithStatus(http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user