Skip to content

Commit

Permalink
Add m3u cache expiration in hour
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Emmanuel Jacquier <pierre-emmanuel.jacquier@exoscale.ch>
  • Loading branch information
pierre-emmanuelJ committed Oct 1, 2019
1 parent 468ae7f commit 2618b46
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
16 changes: 9 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down
15 changes: 8 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
16 changes: 11 additions & 5 deletions pkg/routes/xtream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 2618b46

Please sign in to comment.