Files
iptv-proxy/pkg/server/handlers.go
Pierre-Emmanuel Jacquier 520fb7fd14 Big refactoring (#27)
Signed-off-by: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com>
2019-12-13 01:29:07 +01:00

153 lines
3.8 KiB
Go

package server
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
"github.com/gin-gonic/gin"
)
func (c *Config) getM3U(ctx *gin.Context) {
ctx.Header("Content-Disposition", fmt.Sprintf(`attachment; filename=%q`, c.M3UFileName))
ctx.Header("Content-Type", "application/octet-stream")
ctx.File(c.proxyfiedM3UPath)
}
func (c *Config) reverseProxy(ctx *gin.Context) {
rpURL, err := url.Parse(c.track.URI)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
c.stream(ctx, rpURL)
}
func (c *Config) stream(ctx *gin.Context, oriURL *url.URL) {
id := ctx.Param("id")
if strings.HasSuffix(id, ".m3u8") {
c.hlsStream(ctx, oriURL)
return
}
resp, err := http.Get(oriURL.String())
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
defer resp.Body.Close()
copyHTTPHeader(ctx, resp.Header)
ctx.Status(resp.StatusCode)
ctx.Stream(func(w io.Writer) bool {
io.Copy(w, resp.Body)
return false
})
}
func (c *Config) hlsStream(ctx *gin.Context, oriURL *url.URL) {
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Get(oriURL.String())
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusFound {
location, err := resp.Location()
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
id := ctx.Param("id")
if strings.Contains(location.String(), id) {
hlsChannelsRedirectURLLock.Lock()
hlsChannelsRedirectURL[id] = *location
hlsChannelsRedirectURLLock.Unlock()
hlsResp, err := http.Get(location.String())
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
defer hlsResp.Body.Close()
b, err := ioutil.ReadAll(hlsResp.Body)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
body := string(b)
body = strings.ReplaceAll(body, "/"+c.XtreamUser+"/"+c.XtreamPassword+"/", "/"+c.User+"/"+c.Password+"/")
ctx.Data(http.StatusOK, hlsResp.Header.Get("Content-Type"), []byte(body))
return
}
ctx.AbortWithError(http.StatusInternalServerError, errors.New("Unable to HLS stream"))
return
}
ctx.Status(resp.StatusCode)
}
func copyHTTPHeader(ctx *gin.Context, header http.Header) {
for k, v := range header {
ctx.Header(k, strings.Join(v, ", "))
}
}
// authRequest handle auth credentials
type authRequest struct {
Username string `form:"username" binding:"required"`
Password string `form:"password" binding:"required"`
}
func (c *Config) authenticate(ctx *gin.Context) {
var authReq authRequest
if err := ctx.Bind(&authReq); err != nil {
ctx.AbortWithError(http.StatusBadRequest, err)
return
}
if c.ProxyConfig.User != authReq.Username || c.ProxyConfig.Password != authReq.Password {
ctx.AbortWithStatus(http.StatusUnauthorized)
}
}
func (c *Config) appAuthenticate(ctx *gin.Context) {
contents, err := ioutil.ReadAll(ctx.Request.Body)
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
q, err := url.ParseQuery(string(contents))
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
if len(q["username"]) == 0 || len(q["password"]) == 0 {
ctx.AbortWithError(http.StatusBadRequest, fmt.Errorf("bad body url query parameters"))
return
}
log.Printf("[iptv-proxy] %v | %s |App Auth\n", time.Now().Format("2006/01/02 - 15:04:05"), ctx.ClientIP())
if c.ProxyConfig.User != q["username"][0] || c.ProxyConfig.Password != q["password"][0] {
ctx.AbortWithStatus(http.StatusUnauthorized)
}
ctx.Request.Body = ioutil.NopCloser(bytes.NewReader(contents))
}