Rewrite HLS manifest URLs.
This commit is contained in:
parent
f3ef98fbfe
commit
e615be63e4
1 changed files with 34 additions and 3 deletions
37
main.go
37
main.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
@ -51,10 +52,21 @@ var allowed_hosts = []string{
|
||||||
"lbryplayer.xyz",
|
"lbryplayer.xyz",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var manifest_re = regexp.MustCompile(`(?m)URI="([^"]+)"`)
|
||||||
|
|
||||||
type requesthandler struct{}
|
type requesthandler struct{}
|
||||||
|
|
||||||
func (*requesthandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
func (*requesthandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "*")
|
||||||
|
w.Header().Set("Access-Control-Max-Age", "1728000")
|
||||||
|
|
||||||
|
if req.Method == "OPTIONS" {
|
||||||
|
w.WriteHeader(200)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
q := req.URL.Query()
|
q := req.URL.Query()
|
||||||
host := q.Get("host")
|
host := q.Get("host")
|
||||||
q.Del("host")
|
q.Del("host")
|
||||||
|
@ -143,7 +155,7 @@ func (*requesthandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
w.WriteHeader(resp.StatusCode)
|
w.WriteHeader(resp.StatusCode)
|
||||||
|
|
||||||
if req.Method == "GET" && resp.Header.Get("Content-Type") == "application/x-mpegurl" {
|
if req.Method == "GET" && (resp.Header.Get("Content-Type") == "application/x-mpegurl" || resp.Header.Get("Content-Type") == "application/vnd.apple.mpegurl") {
|
||||||
bytes, err := io.ReadAll(resp.Body)
|
bytes, err := io.ReadAll(resp.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -157,7 +169,15 @@ func (*requesthandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
if !strings.HasPrefix(line, "https://") && (strings.HasSuffix(line, ".m3u8") || strings.HasSuffix(line, ".ts")) {
|
if !strings.HasPrefix(line, "https://") && (strings.HasSuffix(line, ".m3u8") || strings.HasSuffix(line, ".ts")) {
|
||||||
path := reqUrl.EscapedPath()
|
path := reqUrl.EscapedPath()
|
||||||
path = path[0 : strings.LastIndex(path, "/")+1]
|
path = path[0 : strings.LastIndex(path, "/")+1]
|
||||||
lines[i] = "https://" + reqUrl.Hostname() + path + line
|
line = "https://" + reqUrl.Hostname() + path + line
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(line, "https://") {
|
||||||
|
lines[i] = RelativeUrl(line)
|
||||||
|
}
|
||||||
|
|
||||||
|
if manifest_re.MatchString(line) {
|
||||||
|
url := manifest_re.FindStringSubmatch(line)[1]
|
||||||
|
lines[i] = strings.Replace(line, url, RelativeUrl(url), 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,7 +190,7 @@ func (*requesthandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
func copyHeaders(from http.Header, to http.Header) {
|
func copyHeaders(from http.Header, to http.Header) {
|
||||||
// Loop over header names
|
// Loop over header names
|
||||||
for name, values := range from {
|
for name, values := range from {
|
||||||
if name != "Content-Length" {
|
if name != "Content-Length" && name != "Accept-Encoding" {
|
||||||
// Loop over all values for the name.
|
// Loop over all values for the name.
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
to.Set(name, value)
|
to.Set(name, value)
|
||||||
|
@ -219,6 +239,17 @@ func getBestThumbnail(path string) (newpath string) {
|
||||||
return strings.Replace(path, "maxres.jpg", "mqdefault.jpg", 1)
|
return strings.Replace(path, "maxres.jpg", "mqdefault.jpg", 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RelativeUrl(in string) (newurl string) {
|
||||||
|
segment_url, err := url.Parse(in)
|
||||||
|
if err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
segment_query := segment_url.Query()
|
||||||
|
segment_query.Set("host", segment_url.Hostname())
|
||||||
|
segment_url.RawQuery = segment_query.Encode()
|
||||||
|
return segment_url.RequestURI()
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
socket := "socket" + string(os.PathSeparator) + "http-proxy.sock"
|
socket := "socket" + string(os.PathSeparator) + "http-proxy.sock"
|
||||||
syscall.Unlink(socket)
|
syscall.Unlink(socket)
|
||||||
|
|
Loading…
Reference in a new issue