added cors fix
This commit is contained in:
@ -35,25 +35,11 @@
|
||||
<body>
|
||||
<div id="video-container">
|
||||
<video id="videoPlayer" controls autoplay playsinline muted></video>
|
||||
<button id="fullscreen-button">Fullscreen</button>
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
|
||||
<script>
|
||||
const videoPlayer = document.getElementById('videoPlayer');
|
||||
const fullscreenButton = document.getElementById('fullscreen-button');
|
||||
|
||||
fullscreenButton.addEventListener('click', () => {
|
||||
if (videoPlayer.requestFullscreen) {
|
||||
videoPlayer.requestFullscreen();
|
||||
} else if (videoPlayer.mozRequestFullScreen) { /* Firefox */
|
||||
videoPlayer.mozRequestFullScreen();
|
||||
} else if (videoPlayer.webkitRequestFullscreen) { /* Chrome, Safari & Opera */
|
||||
videoPlayer.webkitRequestFullscreen();
|
||||
} else if (videoPlayer.msRequestFullscreen) { /* IE/Edge */
|
||||
videoPlayer.msRequestFullscreen();
|
||||
}
|
||||
});
|
||||
|
||||
// Add event listener when video ends to loop it
|
||||
videoPlayer.addEventListener('ended', function() {
|
||||
@ -64,11 +50,12 @@
|
||||
// Check if HLS.js is supported
|
||||
if (Hls.isSupported()) {
|
||||
const hls = new Hls();
|
||||
hls.loadSource('https://brevent.akamaized.net/hls/live/2028222-b/event_05/master_1280p_3328.m3u8');
|
||||
const proxyUrl = 'http://127.0.0.1:8090/cors?url=https://brevent.akamaized.net/hls/live/2028222/event_05/master1080p5000.m3u8';
|
||||
hls.loadSource(proxyUrl);
|
||||
hls.attachMedia(videoPlayer);
|
||||
} else {
|
||||
console.error('HLS not supported');
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
90
main.go
90
main.go
@ -1,17 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/apis"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := pocketbase.New()
|
||||
|
||||
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||
_, err := e.Router.AddRoute(echo.Route{
|
||||
Method: http.MethodGet,
|
||||
Path: "/cors",
|
||||
Handler: func(c echo.Context) error {
|
||||
return corsProxyHandler(c)
|
||||
},
|
||||
Middlewares: []echo.MiddlewareFunc{
|
||||
apis.ActivityLogger(app),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||
// add new "GET /hello" route to the app router (echo)
|
||||
e.Router.GET("/*", apis.StaticDirectoryHandler(os.DirFS("./pb_public"), false))
|
||||
@ -33,3 +55,65 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func corsProxyHandler(c echo.Context) error {
|
||||
// Get the target URL from the request query string
|
||||
url := c.QueryParam("url")
|
||||
if url == "" {
|
||||
return c.JSON(http.StatusBadRequest, "url param is required")
|
||||
}
|
||||
|
||||
// Fetch the resource from the target URL
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, "Failed to fetch the URL")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// If the content is a .m3u8 file, rewrite the URLs
|
||||
if strings.HasSuffix(url, ".m3u8") {
|
||||
bodyBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, "Failed to read response body")
|
||||
}
|
||||
|
||||
proxyBaseURL := c.Scheme() + "://" + c.Request().Host + c.Path() + "?url=" + url[:strings.LastIndex(url, "/")+1]
|
||||
|
||||
// Rewrite the URLs in the .m3u8 file
|
||||
lines := strings.Split(string(bodyBytes), "\n")
|
||||
for i, line := range lines {
|
||||
if strings.HasSuffix(line, ".ts") {
|
||||
lines[i] = proxyBaseURL + line
|
||||
}
|
||||
}
|
||||
modifiedContent := strings.Join(lines, "\n")
|
||||
|
||||
// Write the modified content to the response
|
||||
c.Response().Header().Set("Content-Type", resp.Header.Get("Content-Type"))
|
||||
c.Response().WriteHeader(resp.StatusCode)
|
||||
_, err = io.Copy(c.Response().Writer, bytes.NewReader([]byte(modifiedContent)))
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, "Failed to copy modified response body")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Copy the headers from the fetched response to the proxy response
|
||||
for key, values := range resp.Header {
|
||||
for _, value := range values {
|
||||
c.Response().Header().Add(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
// Copy the status code from the fetched response to the proxy response
|
||||
c.Response().WriteHeader(resp.StatusCode)
|
||||
|
||||
// Copy the body from the fetched response to the proxy response
|
||||
_, err = io.Copy(c.Response().Writer, resp.Body)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, "Failed to copy response body")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user