diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index 9d9f293..4f7cc4f 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -15,7 +15,6 @@ import ( "github.com/pierre-emmanuelJ/iptv-proxy/pkg/config" proxyM3U "github.com/pierre-emmanuelJ/iptv-proxy/pkg/m3u" - xtreamapi "github.com/pierre-emmanuelJ/iptv-proxy/pkg/xtream-proxy" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" @@ -53,7 +52,7 @@ func Routes(proxyConfig *config.ProxyConfig, r *gin.RouterGroup, newM3U []byte) // XXX Private need for external Android app r.POST("/iptv.m3u", p.authenticate, p.getM3U) - //Xtr Smarter android app compatibility + //Xtream, iptv Smarter android app compatibility r.POST("/player_api.php", p.appAuthenticate, p.xtreamPlayerAPI) r.GET("/xmltv.php", p.authenticate, p.xtreamXMLTV) r.GET(fmt.Sprintf("/%s/%s/:id", proxyConfig.User, proxyConfig.Password), p.xtreamStream) @@ -74,124 +73,9 @@ func Routes(proxyConfig *config.ProxyConfig, r *gin.RouterGroup, newM3U []byte) } } -func (p *proxy) xtreamPlayerAPI(c *gin.Context) { - contents, err := ioutil.ReadAll(c.Request.Body) - if err != nil { - c.AbortWithError(http.StatusInternalServerError, err) - return - } - - log.Println(string(contents)) - - q, err := url.ParseQuery(string(contents)) - if err != nil { - c.AbortWithError(http.StatusInternalServerError, err) - return - } - if len(q["username"]) == 0 || len(q["password"]) == 0 { - c.AbortWithError(http.StatusBadRequest, fmt.Errorf(`bad body url query parameters: missing "username" and "password"`)) - return - } - - var action string - if len(q["action"]) > 0 { - action = q["action"][0] - } - - client, err := xtreamapi.New(p.XtreamUser, p.XtreamPassword, p.XtreamBaseURL) - if err != nil { - c.AbortWithError(http.StatusInternalServerError, err) - return - } - - var respBody interface{} - - switch action { - case xtreamapi.GetLiveCategories: - respBody, err = client.GetLiveCategories() - case xtreamapi.GetLiveStreams: - respBody, err = client.GetLiveStreams("") - case xtreamapi.GetVodCategories: - respBody, err = client.GetVideoOnDemandCategories() - case xtreamapi.GetVodStreams: - respBody, err = client.GetVideoOnDemandStreams("") - case xtreamapi.GetVodInfo: - if len(q["vod_id"]) < 1 { - c.AbortWithError(http.StatusBadRequest, fmt.Errorf(`bad body url query parameters: missing "vod_id"`)) - return - } - respBody, err = client.GetVideoOnDemandInfo(q["vod_id"][0]) - case xtreamapi.GetSeriesCategories: - respBody, err = client.GetSeriesCategories() - case xtreamapi.GetSeries: - respBody, err = client.GetSeries("") - case xtreamapi.GetSerieInfo: - if len(q["series_id"]) < 1 { - c.AbortWithError(http.StatusBadRequest, fmt.Errorf(`bad body url query parameters: missing "series_id"`)) - return - } - respBody, err = client.GetSeriesInfo(q["series_id"][0]) - default: - respBody, err = client.Login(p.User, p.Password, "http://"+p.HostConfig.Hostname, int(p.HostConfig.Port)) - } - - log.Printf("[iptv-proxy] %v | %s |Action\t%s\n", time.Now().Format("2006/01/02 - 15:04:05"), c.ClientIP(), action) - - if err != nil { - c.AbortWithError(http.StatusInternalServerError, err) - return - } - - c.JSON(http.StatusOK, respBody) -} - -func (p *proxy) xtreamXMLTV(c *gin.Context) { - client, err := xtreamapi.New(p.XtreamUser, p.XtreamPassword, p.XtreamBaseURL) - if err != nil { - c.AbortWithError(http.StatusInternalServerError, err) - return - } - - resp, err := client.GetXMLTV() - if err != nil { - c.AbortWithError(http.StatusInternalServerError, err) - return - } - - c.Data(http.StatusOK, "application/xml", resp) -} - -func (p *proxy) xtreamStream(c *gin.Context) { - id := c.Param("id") - rpURL, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", p.XtreamBaseURL, p.XtreamUser, p.XtreamPassword, id)) - if err != nil { - c.AbortWithError(http.StatusInternalServerError, err) - return - } - - stream(c, rpURL) -} - -func (p *proxy) xtreamStreamMovie(c *gin.Context) { - id := c.Param("id") - rpURL, err := url.Parse(fmt.Sprintf("%s/movie/%s/%s/%s", p.XtreamBaseURL, p.XtreamUser, p.XtreamPassword, id)) - if err != nil { - c.AbortWithError(http.StatusInternalServerError, err) - return - } - - stream(c, rpURL) -} - -func (p *proxy) xtreamStreamSeries(c *gin.Context) { - id := c.Param("id") - rpURL, err := url.Parse(fmt.Sprintf("%s/series/%s/%s/%s", p.XtreamBaseURL, p.XtreamUser, p.XtreamPassword, id)) - if err != nil { - c.AbortWithError(http.StatusInternalServerError, err) - return - } - - stream(c, rpURL) +func (p *proxy) getM3U(c *gin.Context) { + c.Header("Content-Disposition", "attachment; filename=\"iptv.m3u\"") + c.Data(http.StatusOK, "application/octet-stream", p.newM3U) } func (p *proxy) reverseProxy(c *gin.Context) { @@ -225,11 +109,6 @@ func copyHTTPHeader(c *gin.Context, header http.Header) { } } -func (p *proxy) getM3U(c *gin.Context) { - c.Header("Content-Disposition", "attachment; filename=\"iptv.m3u\"") - c.Data(http.StatusOK, "application/octet-stream", p.newM3U) -} - // AuthRequest handle auth credentials type AuthRequest struct { User string `form:"username" binding:"required"` diff --git a/pkg/routes/xtream.go b/pkg/routes/xtream.go new file mode 100644 index 0000000..f798b48 --- /dev/null +++ b/pkg/routes/xtream.go @@ -0,0 +1,133 @@ +package routes + +import ( + "fmt" + "io/ioutil" + "log" + "net/http" + "net/url" + "time" + + "github.com/gin-gonic/gin" + xtreamapi "github.com/pierre-emmanuelJ/iptv-proxy/pkg/xtream-proxy" +) + +func (p *proxy) xtreamPlayerAPI(c *gin.Context) { + contents, err := ioutil.ReadAll(c.Request.Body) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + log.Println(string(contents)) + + q, err := url.ParseQuery(string(contents)) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + if len(q["username"]) == 0 || len(q["password"]) == 0 { + c.AbortWithError(http.StatusBadRequest, fmt.Errorf(`bad body url query parameters: missing "username" and "password"`)) + return + } + + var action string + if len(q["action"]) > 0 { + action = q["action"][0] + } + + client, err := xtreamapi.New(p.XtreamUser, p.XtreamPassword, p.XtreamBaseURL) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + var respBody interface{} + + switch action { + case xtreamapi.GetLiveCategories: + respBody, err = client.GetLiveCategories() + case xtreamapi.GetLiveStreams: + respBody, err = client.GetLiveStreams("") + case xtreamapi.GetVodCategories: + respBody, err = client.GetVideoOnDemandCategories() + case xtreamapi.GetVodStreams: + respBody, err = client.GetVideoOnDemandStreams("") + case xtreamapi.GetVodInfo: + if len(q["vod_id"]) < 1 { + c.AbortWithError(http.StatusBadRequest, fmt.Errorf(`bad body url query parameters: missing "vod_id"`)) + return + } + respBody, err = client.GetVideoOnDemandInfo(q["vod_id"][0]) + case xtreamapi.GetSeriesCategories: + respBody, err = client.GetSeriesCategories() + case xtreamapi.GetSeries: + respBody, err = client.GetSeries("") + case xtreamapi.GetSerieInfo: + if len(q["series_id"]) < 1 { + c.AbortWithError(http.StatusBadRequest, fmt.Errorf(`bad body url query parameters: missing "series_id"`)) + return + } + respBody, err = client.GetSeriesInfo(q["series_id"][0]) + default: + respBody, err = client.Login(p.User, p.Password, "http://"+p.HostConfig.Hostname, int(p.HostConfig.Port)) + } + + log.Printf("[iptv-proxy] %v | %s |Action\t%s\n", time.Now().Format("2006/01/02 - 15:04:05"), c.ClientIP(), action) + + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + c.JSON(http.StatusOK, respBody) +} + +func (p *proxy) xtreamXMLTV(c *gin.Context) { + client, err := xtreamapi.New(p.XtreamUser, p.XtreamPassword, p.XtreamBaseURL) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + resp, err := client.GetXMLTV() + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + c.Data(http.StatusOK, "application/xml", resp) +} + +func (p *proxy) xtreamStream(c *gin.Context) { + id := c.Param("id") + rpURL, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", p.XtreamBaseURL, p.XtreamUser, p.XtreamPassword, id)) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + stream(c, rpURL) +} + +func (p *proxy) xtreamStreamMovie(c *gin.Context) { + id := c.Param("id") + rpURL, err := url.Parse(fmt.Sprintf("%s/movie/%s/%s/%s", p.XtreamBaseURL, p.XtreamUser, p.XtreamPassword, id)) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + stream(c, rpURL) +} + +func (p *proxy) xtreamStreamSeries(c *gin.Context) { + id := c.Param("id") + rpURL, err := url.Parse(fmt.Sprintf("%s/series/%s/%s/%s", p.XtreamBaseURL, p.XtreamUser, p.XtreamPassword, id)) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + stream(c, rpURL) +}