From 2618b46cfe4bcf75de7809f2f539c7809e50f942 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Jacquier Date: Tue, 1 Oct 2019 22:03:56 +0000 Subject: [PATCH] Add m3u cache expiration in hour Signed-off-by: Pierre-Emmanuel Jacquier --- cmd/root.go | 16 +++++++++------- pkg/config/config.go | 15 ++++++++------- pkg/routes/xtream.go | 16 +++++++++++----- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index d9c4d7f..6d89727 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -45,13 +45,14 @@ var rootCmd = &cobra.Command{ Hostname: viper.GetString("hostname"), Port: viper.GetInt64("port"), }, - RemoteURL: remoteHostURL, - XtreamUser: viper.GetString("xtream-user"), - XtreamPassword: viper.GetString("xtream-password"), - XtreamBaseURL: viper.GetString("xtream-base-url"), - User: viper.GetString("user"), - Password: viper.GetString("password"), - HTTPS: viper.GetBool("https"), + RemoteURL: remoteHostURL, + XtreamUser: viper.GetString("xtream-user"), + XtreamPassword: viper.GetString("xtream-password"), + XtreamBaseURL: viper.GetString("xtream-base-url"), + M3UCacheExpiration: viper.GetInt("m3u-cache-expiration"), + User: viper.GetString("user"), + Password: viper.GetString("password"), + HTTPS: viper.GetBool("https"), } if e := routes.Serve(conf); e != nil { @@ -85,6 +86,7 @@ func init() { rootCmd.Flags().String("xtream-user", "xtream_user", "Xtream-code user login") rootCmd.Flags().String("xtream-password", "xtream_password", "Xtream-code password login") rootCmd.Flags().String("xtream-base-url", "http://expample.tv:8080", "Xtream-code base url") + rootCmd.Flags().Int("m3u-cache-expiration", 24, "M3U cache expiration in hour") if e := viper.BindPFlags(rootCmd.Flags()); e != nil { log.Fatal("error binding PFlags to viper") diff --git a/pkg/config/config.go b/pkg/config/config.go index dd3d6a0..7e85f6d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -14,13 +14,14 @@ type HostConfiguration struct { // ProxyConfig Contain original m3u playlist and HostConfiguration type ProxyConfig struct { - Playlist *m3u.Playlist - HostConfig *HostConfiguration - XtreamUser string - XtreamPassword string - XtreamBaseURL string - RemoteURL *url.URL - HTTPS bool + Playlist *m3u.Playlist + HostConfig *HostConfiguration + XtreamUser string + XtreamPassword string + XtreamBaseURL string + M3UCacheExpiration int + RemoteURL *url.URL + HTTPS bool //XXX Very unsafe User, Password string } diff --git a/pkg/routes/xtream.go b/pkg/routes/xtream.go index 4b57ec4..65ea1ba 100644 --- a/pkg/routes/xtream.go +++ b/pkg/routes/xtream.go @@ -18,9 +18,14 @@ import ( xtreamapi "github.com/pierre-emmanuelJ/iptv-proxy/pkg/xtream-proxy" ) +type cacheMeta struct { + string + time.Time +} + // XXX Add one cache per url and store it on the local storage or key/value storage e.g: etcd, redis... // and remove that dirty globals -var xtreamM3uCache map[string]string = map[string]string{} +var xtreamM3uCache map[string]cacheMeta = map[string]cacheMeta{} var lock = sync.RWMutex{} func (p *proxy) cacheXtreamM3u(m3uURL *url.URL) error { @@ -45,7 +50,7 @@ func (p *proxy) cacheXtreamM3u(m3uURL *url.URL) error { return err } - xtreamM3uCache[m3uURL.String()] = path + xtreamM3uCache[m3uURL.String()] = cacheMeta{path, time.Now()} lock.Unlock() return nil @@ -82,8 +87,9 @@ func (p *proxy) xtreamGet(c *gin.Context) { // XXX Add cache per url and store it on the local storage or key/value storage e.g: etcd, redis... lock.RLock() - _, ok := xtreamM3uCache[m3uURL.String()] - if !ok { + meta, ok := xtreamM3uCache[m3uURL.String()] + d := time.Now().Sub(meta.Time) + if !ok || d.Hours() >= float64(p.M3UCacheExpiration) { log.Printf("[iptv-proxy] %v | %s | xtream cache m3u file\n", time.Now().Format("2006/01/02 - 15:04:05"), c.ClientIP()) lock.RUnlock() if err := p.cacheXtreamM3u(m3uURL); err != nil { @@ -96,7 +102,7 @@ func (p *proxy) xtreamGet(c *gin.Context) { c.Header("Content-Disposition", "attachment; filename=\"iptv.m3u\"") lock.RLock() - path := xtreamM3uCache[m3uURL.String()] + path := xtreamM3uCache[m3uURL.String()].string lock.RUnlock() data, err := ioutil.ReadFile(path) if err != nil {