2024-10-26 08:51:52 +00:00
|
|
|
package proxystreams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/url"
|
|
|
|
|
2024-12-25 06:46:14 +00:00
|
|
|
"git.maid.zone/stuff/soundcloak/lib/cfg"
|
|
|
|
"git.maid.zone/stuff/soundcloak/lib/misc"
|
|
|
|
"git.maid.zone/stuff/soundcloak/lib/sc"
|
2024-10-26 08:51:52 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
2024-12-13 21:06:13 +00:00
|
|
|
var httpc *fasthttp.HostClient
|
|
|
|
var httpc_aac *fasthttp.HostClient
|
2024-10-26 08:51:52 +00:00
|
|
|
|
|
|
|
func Load(r fiber.Router) {
|
2024-12-13 21:06:13 +00:00
|
|
|
httpc = &fasthttp.HostClient{
|
|
|
|
Addr: cfg.HLSCDN + ":443",
|
|
|
|
IsTLS: true,
|
|
|
|
DialDualStack: true,
|
|
|
|
Dial: (&fasthttp.TCPDialer{DNSCacheDuration: cfg.DNSCacheTTL}).Dial,
|
|
|
|
MaxIdleConnDuration: 1<<63 - 1,
|
|
|
|
StreamResponseBody: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
httpc_aac = &fasthttp.HostClient{
|
|
|
|
Addr: cfg.HLSAACCDN + ":443",
|
|
|
|
IsTLS: true,
|
|
|
|
DialDualStack: true,
|
|
|
|
Dial: (&fasthttp.TCPDialer{DNSCacheDuration: cfg.DNSCacheTTL}).Dial,
|
|
|
|
MaxIdleConnDuration: 1<<63 - 1,
|
|
|
|
StreamResponseBody: true,
|
|
|
|
}
|
|
|
|
|
2024-10-26 08:51:52 +00:00
|
|
|
r.Get("/_/proxy/streams", func(c *fiber.Ctx) error {
|
|
|
|
ur := c.Query("url")
|
|
|
|
if ur == "" {
|
|
|
|
return fiber.ErrBadRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
parsed := fasthttp.AcquireURI()
|
|
|
|
defer fasthttp.ReleaseURI(parsed)
|
|
|
|
|
|
|
|
err := parsed.Parse(nil, []byte(ur))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-12-13 21:06:13 +00:00
|
|
|
if !bytes.HasSuffix(parsed.Host(), []byte(".sndcdn.com")) {
|
2024-10-26 08:51:52 +00:00
|
|
|
return fiber.ErrBadRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
req := fasthttp.AcquireRequest()
|
|
|
|
defer fasthttp.ReleaseRequest(req)
|
|
|
|
|
|
|
|
req.SetURI(parsed)
|
2024-12-21 17:10:31 +00:00
|
|
|
req.Header.SetUserAgent(cfg.UserAgent)
|
2024-10-26 08:51:52 +00:00
|
|
|
req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
|
|
|
|
|
|
|
|
resp := fasthttp.AcquireResponse()
|
2024-12-05 19:46:29 +00:00
|
|
|
//defer fasthttp.ReleaseResponse(resp)
|
2024-10-26 08:51:52 +00:00
|
|
|
|
|
|
|
err = sc.DoWithRetry(httpc, req, resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-12-05 19:46:29 +00:00
|
|
|
//return c.Send(resp.Body())
|
2024-12-23 16:57:32 +00:00
|
|
|
pr := misc.AcquireProxyReader()
|
2024-12-05 19:46:29 +00:00
|
|
|
pr.Reader = resp.BodyStream()
|
|
|
|
pr.Resp = resp
|
|
|
|
return c.SendStream(pr)
|
2024-10-26 08:51:52 +00:00
|
|
|
})
|
|
|
|
|
2024-12-13 21:06:13 +00:00
|
|
|
r.Get("/_/proxy/streams/aac", func(c *fiber.Ctx) error {
|
|
|
|
ur := c.Query("url")
|
|
|
|
if ur == "" {
|
|
|
|
return fiber.ErrBadRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
parsed := fasthttp.AcquireURI()
|
|
|
|
defer fasthttp.ReleaseURI(parsed)
|
|
|
|
|
|
|
|
err := parsed.Parse(nil, []byte(ur))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.HasSuffix(parsed.Host(), []byte(".soundcloud.cloud")) {
|
|
|
|
return fiber.ErrBadRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
req := fasthttp.AcquireRequest()
|
|
|
|
defer fasthttp.ReleaseRequest(req)
|
|
|
|
|
|
|
|
req.SetURI(parsed)
|
2024-12-21 17:10:31 +00:00
|
|
|
req.Header.SetUserAgent(cfg.UserAgent)
|
2024-12-13 21:06:13 +00:00
|
|
|
req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
|
|
|
|
|
|
|
|
resp := fasthttp.AcquireResponse()
|
|
|
|
|
|
|
|
err = sc.DoWithRetry(httpc_aac, req, resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-12-23 16:57:32 +00:00
|
|
|
pr := misc.AcquireProxyReader()
|
2024-12-13 21:06:13 +00:00
|
|
|
pr.Reader = resp.BodyStream()
|
|
|
|
pr.Resp = resp
|
|
|
|
return c.SendStream(pr)
|
|
|
|
})
|
|
|
|
|
2024-10-26 08:51:52 +00:00
|
|
|
r.Get("/_/proxy/streams/playlist", func(c *fiber.Ctx) error {
|
|
|
|
ur := c.Query("url")
|
|
|
|
if ur == "" {
|
|
|
|
return fiber.ErrBadRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
parsed := fasthttp.AcquireURI()
|
|
|
|
defer fasthttp.ReleaseURI(parsed)
|
|
|
|
|
|
|
|
err := parsed.Parse(nil, []byte(ur))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-12-13 21:06:13 +00:00
|
|
|
if !bytes.HasSuffix(parsed.Host(), []byte(".sndcdn.com")) {
|
2024-10-26 08:51:52 +00:00
|
|
|
return fiber.ErrBadRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
req := fasthttp.AcquireRequest()
|
|
|
|
defer fasthttp.ReleaseRequest(req)
|
|
|
|
|
|
|
|
req.SetURI(parsed)
|
2024-12-21 17:10:31 +00:00
|
|
|
req.Header.SetUserAgent(cfg.UserAgent)
|
2024-10-26 08:51:52 +00:00
|
|
|
req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
|
|
|
|
|
|
|
|
resp := fasthttp.AcquireResponse()
|
|
|
|
defer fasthttp.ReleaseResponse(resp)
|
|
|
|
|
|
|
|
err = sc.DoWithRetry(httpc, req, resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := resp.BodyUncompressed()
|
|
|
|
if err != nil {
|
|
|
|
data = resp.Body()
|
|
|
|
}
|
|
|
|
|
|
|
|
var sp = bytes.Split(data, []byte("\n"))
|
2024-10-26 08:53:01 +00:00
|
|
|
for i, l := range sp {
|
2024-10-26 08:51:52 +00:00
|
|
|
if len(l) == 0 || l[0] == '#' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
l = []byte("/_/proxy/streams?url=" + url.QueryEscape(string(l)))
|
|
|
|
sp[i] = l
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Send(bytes.Join(sp, []byte("\n")))
|
|
|
|
})
|
2024-12-13 21:06:13 +00:00
|
|
|
|
|
|
|
r.Get("/_/proxy/streams/playlist/aac", func(c *fiber.Ctx) error {
|
|
|
|
ur := c.Query("url")
|
|
|
|
if ur == "" {
|
|
|
|
return fiber.ErrBadRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
parsed := fasthttp.AcquireURI()
|
|
|
|
defer fasthttp.ReleaseURI(parsed)
|
|
|
|
|
|
|
|
err := parsed.Parse(nil, []byte(ur))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.HasSuffix(parsed.Host(), []byte(".soundcloud.cloud")) {
|
|
|
|
return fiber.ErrBadRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
req := fasthttp.AcquireRequest()
|
|
|
|
defer fasthttp.ReleaseRequest(req)
|
|
|
|
|
|
|
|
req.SetURI(parsed)
|
2024-12-21 17:10:31 +00:00
|
|
|
req.Header.SetUserAgent(cfg.UserAgent)
|
2024-12-13 21:06:13 +00:00
|
|
|
req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
|
|
|
|
|
|
|
|
resp := fasthttp.AcquireResponse()
|
|
|
|
defer fasthttp.ReleaseResponse(resp)
|
|
|
|
|
|
|
|
err = sc.DoWithRetry(httpc_aac, req, resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := resp.BodyUncompressed()
|
|
|
|
if err != nil {
|
|
|
|
data = resp.Body()
|
|
|
|
}
|
|
|
|
|
|
|
|
var sp = bytes.Split(data, []byte("\n"))
|
|
|
|
for i, l := range sp {
|
|
|
|
if len(l) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if l[0] == '#' {
|
|
|
|
if bytes.HasPrefix(l, []byte(`#EXT-X-MAP:URI="`)) {
|
|
|
|
l = []byte(`#EXT-X-MAP:URI="/_/proxy/streams/aac?url=` + url.QueryEscape(string(l[16:len(l)-1])) + `"`)
|
|
|
|
sp[i] = l
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
l = []byte("/_/proxy/streams/aac?url=" + url.QueryEscape(string(l)))
|
|
|
|
sp[i] = l
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Send(bytes.Join(sp, []byte("\n")))
|
|
|
|
})
|
2024-10-26 08:51:52 +00:00
|
|
|
}
|