0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2024-12-22 21:23:01 -05:00
safetwitch-backend/routes/proxy/proxy.go
2023-07-17 13:16:33 -04:00

129 lines
3.1 KiB
Go

package proxy
import (
b64 "encoding/base64"
"io"
"net/http"
"safetwitch-backend/extractor/twitch"
"strings"
"github.com/gin-gonic/gin"
)
func Routes(route *gin.Engine) {
auth := route.Group("/proxy")
auth.GET("/img/:url", func(context *gin.Context) {
decodedUrl, err := b64.StdEncoding.DecodeString(context.Param("url"))
if err != nil {
context.Error(err)
}
imageResp, err := http.Get(string(decodedUrl))
if err != nil {
context.Error(err)
}
body, err := io.ReadAll(imageResp.Body)
if err != nil {
context.Error(err)
}
contentType := imageResp.Header.Get("Content-Type")
context.Data(200, contentType, body)
context.JSON(imageResp.StatusCode, imageResp.Body)
})
auth.GET("/stream/:username/hls.m3u8", func(context *gin.Context) {
streamer := context.Param("username")
playlistFile, err := twitch.GetStream(streamer)
if err != nil {
context.Error(err)
}
context.Data(200, "application/vnd.apple.mpegurl", []byte(playlistFile))
})
auth.GET("/stream/sub/:encodedUrl", func(context *gin.Context) {
decodedUrl, err := b64.StdEncoding.DecodeString(context.Param("encodedUrl"))
if err != nil {
context.Error(err)
}
playlistFile, err := twitch.GetSubPlaylist(string(decodedUrl), false)
if err != nil {
context.Error(err)
}
context.Data(200, "application/vnd.apple.mpegurl", []byte(playlistFile))
})
auth.GET("/stream/segment/:encodedUrl", func(context *gin.Context) {
decodedUrl, err := b64.StdEncoding.DecodeString(context.Param("encodedUrl"))
if err != nil {
context.Error(err)
}
segmentData, err := http.Get(string(decodedUrl))
if err != nil {
context.Error(err)
return
}
segment, err := io.ReadAll(segmentData.Body)
if err != nil {
context.Error(err)
}
context.Data(200, "application/text", segment)
})
// vod
auth.GET("/vod/:vodID/video.m3u8", func(context *gin.Context) {
vodID := context.Param("vodID")
data, err := twitch.GetVODPlaylist(vodID)
if err != nil {
context.Error(err)
return
}
context.Data(200, "application/vnd.apple.mpegurl", data)
})
auth.GET("/vod/sub/:encodedUrl/video.m3u8", func(context *gin.Context) {
decodedUrl, err := b64.StdEncoding.DecodeString(context.Param("encodedUrl"))
if err != nil {
context.Error(err)
}
playlistFile, err := twitch.GetSubPlaylist(string(decodedUrl), true)
if err != nil {
context.Error(err)
}
context.Data(200, "application/vnd.apple.mpegurl", []byte(playlistFile))
})
auth.GET("/vod/sub/:encodedUrl/:segment", func(context *gin.Context) {
decodedUrl, err := b64.StdEncoding.DecodeString(context.Param("encodedUrl"))
if err != nil {
context.Error(err)
}
// remove the last path of url and replace with segment
tempurl := strings.Split(string(decodedUrl), "/")
newurl := strings.Join(tempurl[:len(tempurl)-1], "/") + "/" + context.Param("segment")
segmentData, err := http.Get(string(newurl))
if err != nil {
context.Error(err)
return
}
segment, err := io.ReadAll(segmentData.Body)
if err != nil {
context.Error(err)
}
context.Data(200, "application/text", segment)
})
}