diff --git a/lib/misc/init.go b/lib/misc/init.go index ba8f847..090b812 100644 --- a/lib/misc/init.go +++ b/lib/misc/init.go @@ -44,3 +44,40 @@ func Log(what ...any) { fmt.Println(what...) } } + +var ImageClient *fasthttp.HostClient +var HlsClient *fasthttp.HostClient +var HlsAacClient *fasthttp.HostClient + +func init() { + if cfg.Restream || cfg.ProxyImages { + ImageClient = &fasthttp.HostClient{ + Addr: cfg.ImageCDN + ":443", + IsTLS: true, + DialDualStack: true, + Dial: (&fasthttp.TCPDialer{DNSCacheDuration: cfg.DNSCacheTTL}).Dial, + MaxIdleConnDuration: 1<<63 - 1, + StreamResponseBody: true, + } + } + + if cfg.Restream || cfg.ProxyStreams { + HlsClient = &fasthttp.HostClient{ + Addr: cfg.HLSCDN + ":443", + IsTLS: true, + DialDualStack: true, + Dial: (&fasthttp.TCPDialer{DNSCacheDuration: cfg.DNSCacheTTL}).Dial, + MaxIdleConnDuration: 1<<63 - 1, + StreamResponseBody: true, + } + + HlsAacClient = &fasthttp.HostClient{ + Addr: cfg.HLSAACCDN + ":443", + IsTLS: true, + DialDualStack: true, + Dial: (&fasthttp.TCPDialer{DNSCacheDuration: cfg.DNSCacheTTL}).Dial, + MaxIdleConnDuration: 1<<63 - 1, + StreamResponseBody: true, + } + } +} diff --git a/lib/proxy_images/init.go b/lib/proxy_images/init.go index 235c723..bc3f705 100644 --- a/lib/proxy_images/init.go +++ b/lib/proxy_images/init.go @@ -10,18 +10,9 @@ import ( "github.com/valyala/fasthttp" ) -var httpc *fasthttp.HostClient var al_httpc *fasthttp.HostClient func Load(r *fiber.App) { - httpc = &fasthttp.HostClient{ - Addr: cfg.ImageCDN + ":443", - IsTLS: true, - DialDualStack: true, - Dial: (&fasthttp.TCPDialer{DNSCacheDuration: cfg.DNSCacheTTL}).Dial, - MaxIdleConnDuration: 1<<63 - 1, - StreamResponseBody: true, - } al_httpc = &fasthttp.HostClient{ Addr: "al.sndcdn.com:443", @@ -53,7 +44,7 @@ func Load(r *fiber.App) { var cl *fasthttp.HostClient if parsed.Host()[0] == 'i' { parsed.SetHost(cfg.ImageCDN) - cl = httpc + cl = misc.ImageClient } else if string(parsed.Host()[:2]) == "al" { cl = al_httpc } diff --git a/lib/proxy_streams/init.go b/lib/proxy_streams/init.go index 60b8277..1671227 100644 --- a/lib/proxy_streams/init.go +++ b/lib/proxy_streams/init.go @@ -11,28 +11,7 @@ import ( "github.com/valyala/fasthttp" ) -var httpc *fasthttp.HostClient -var httpc_aac *fasthttp.HostClient - func Load(r *fiber.App) { - 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, - } - r.Get("/_/proxy/streams", func(c fiber.Ctx) error { ur := c.Query("url") if ur == "" { @@ -61,7 +40,7 @@ func Load(r *fiber.App) { resp := fasthttp.AcquireResponse() //defer fasthttp.ReleaseResponse(resp) - err = sc.DoWithRetry(httpc, req, resp) + err = sc.DoWithRetry(misc.HlsClient, req, resp) if err != nil { return err } @@ -99,7 +78,7 @@ func Load(r *fiber.App) { resp := fasthttp.AcquireResponse() - err = sc.DoWithRetry(httpc_aac, req, resp) + err = sc.DoWithRetry(misc.HlsAacClient, req, resp) if err != nil { return err } @@ -138,7 +117,7 @@ func Load(r *fiber.App) { resp := fasthttp.AcquireResponse() defer fasthttp.ReleaseResponse(resp) - err = sc.DoWithRetry(httpc, req, resp) + err = sc.DoWithRetry(misc.HlsClient, req, resp) if err != nil { return err } @@ -189,7 +168,7 @@ func Load(r *fiber.App) { resp := fasthttp.AcquireResponse() defer fasthttp.ReleaseResponse(resp) - err = sc.DoWithRetry(httpc_aac, req, resp) + err = sc.DoWithRetry(misc.HlsAacClient, req, resp) if err != nil { return err } diff --git a/lib/restream/init.go b/lib/restream/init.go index 46d219f..6c87514 100644 --- a/lib/restream/init.go +++ b/lib/restream/init.go @@ -17,10 +17,6 @@ import ( "github.com/valyala/fasthttp" ) -var httpc *fasthttp.HostClient -var httpc_aac *fasthttp.HostClient -var httpc_image *fasthttp.HostClient - const defaultPartsCapacity = 24 type reader struct { @@ -58,9 +54,9 @@ func (r *reader) Setup(url string, aac bool) error { r.req.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd") if aac { - r.client = httpc_aac + r.client = misc.HlsAacClient } else { - r.client = httpc + r.client = misc.HlsClient } err := sc.DoWithRetry(r.client, r.req, r.resp) @@ -195,31 +191,6 @@ func (c *collector) Write(data []byte) (n int, err error) { } func Load(r *fiber.App) { - httpc = &fasthttp.HostClient{ - Addr: cfg.HLSCDN + ":443", - IsTLS: true, - DialDualStack: true, - Dial: (&fasthttp.TCPDialer{DNSCacheDuration: cfg.DNSCacheTTL}).Dial, - MaxIdleConnDuration: 1<<63 - 1, - } - - httpc_aac = &fasthttp.HostClient{ - Addr: cfg.HLSAACCDN + ":443", - IsTLS: true, - DialDualStack: true, - Dial: (&fasthttp.TCPDialer{DNSCacheDuration: cfg.DNSCacheTTL}).Dial, - MaxIdleConnDuration: 1<<63 - 1, - } - - httpc_image = &fasthttp.HostClient{ - Addr: cfg.ImageCDN + ":443", - IsTLS: true, - DialDualStack: true, - Dial: (&fasthttp.TCPDialer{DNSCacheDuration: cfg.DNSCacheTTL}).Dial, - MaxIdleConnDuration: 1<<63 - 1, - StreamResponseBody: true, - } - r.Get("/_/restream/:author/:track", func(c fiber.Ctx) error { p, err := preferences.Get(c) if err != nil { @@ -303,7 +274,7 @@ func Load(r *fiber.App) { resp := fasthttp.AcquireResponse() defer fasthttp.ReleaseResponse(resp) - err = sc.DoWithRetry(httpc, req, resp) + err = sc.DoWithRetry(misc.HlsClient, req, resp) if err != nil { return err } @@ -327,7 +298,7 @@ func Load(r *fiber.App) { for _, part := range parts { req.SetRequestURIBytes(part) - err = sc.DoWithRetry(httpc, req, resp) + err = sc.DoWithRetry(misc.HlsClient, req, resp) if err != nil { return err } @@ -356,7 +327,7 @@ func Load(r *fiber.App) { req.SetRequestURI(t.Artwork) req.Header.Del("Accept-Encoding") - err := sc.DoWithRetry(httpc_image, req, resp) + err := sc.DoWithRetry(misc.ImageClient, req, resp) if err != nil { return err } @@ -382,7 +353,7 @@ func Load(r *fiber.App) { resp := fasthttp.AcquireResponse() defer fasthttp.ReleaseResponse(resp) - err = sc.DoWithRetry(httpc_aac, req, resp) + err = sc.DoWithRetry(misc.HlsAacClient, req, resp) if err != nil { return err } @@ -413,7 +384,7 @@ func Load(r *fiber.App) { for _, part := range parts { req.SetRequestURIBytes(part) - err = sc.DoWithRetry(httpc_aac, req, resp) + err = sc.DoWithRetry(misc.HlsAacClient, req, resp) if err != nil { return err } @@ -442,7 +413,7 @@ func Load(r *fiber.App) { req.SetRequestURI(t.Artwork) req.Header.Del("Accept-Encoding") - err := sc.DoWithRetry(httpc_image, req, resp) + err := sc.DoWithRetry(misc.ImageClient, req, resp) if err != nil { return err } diff --git a/lib/sc/init.go b/lib/sc/init.go index fe0210b..cc013ca 100644 --- a/lib/sc/init.go +++ b/lib/sc/init.go @@ -34,15 +34,6 @@ var httpc = &fasthttp.HostClient{ MaxIdleConnDuration: 1<<63 - 1, } -var httpc_image = &fasthttp.HostClient{ - Addr: cfg.ImageCDN + ":443", - IsTLS: true, - DialDualStack: true, - Dial: (&fasthttp.TCPDialer{DNSCacheDuration: cfg.DNSCacheTTL}).Dial, - MaxIdleConnDuration: 1<<63 - 1, - StreamResponseBody: true, -} - var genericClient = &fasthttp.Client{ Dial: (&fasthttp.TCPDialer{DNSCacheDuration: cfg.DNSCacheTTL}).Dial, } diff --git a/lib/sc/track.go b/lib/sc/track.go index 43ed9f5..c9a8c3d 100644 --- a/lib/sc/track.go +++ b/lib/sc/track.go @@ -452,7 +452,7 @@ func (t Track) DownloadImage() ([]byte, string, error) { resp := fasthttp.AcquireResponse() defer fasthttp.ReleaseResponse(resp) - err := DoWithRetry(httpc_image, req, resp) + err := DoWithRetry(misc.ImageClient, req, resp) if err != nil { return nil, "", err } diff --git a/main.go b/main.go index 500f393..f589fd3 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io/fs" "log" "math/rand" @@ -26,6 +27,13 @@ import ( "git.maid.zone/stuff/soundcloak/templates" ) +func boolean(b bool) string { + if b { + return "Enabled" + } + return "Disabled" +} + type osfs struct{} func (osfs) Open(name string) (fs.File, error) { @@ -934,8 +942,48 @@ func main() { return templates.Base(track.Title+" by "+track.Author.Username, templates.TrackInAlbums(track, p), templates.TrackHeader(prefs, track, false)).Render(c.RequestCtx(), c) }) + // cute + const art = ` + ⠀⠀⠀⠀⢀⡴⣆⠀⠀⠀⠀⠀⣠⡀⠀⠀⠀⠀⠀⠀⣼⣿⡗⠀⠀⠀⠀ + ⠀⠀⠀⣠⠟⠀⠘⠷⠶⠶⠶⠾⠉⢳⡄⠀⠀⠀⠀⠀⣧⣿⠀⠀⠀⠀⠀ + ⠀⠀⣰⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣤⣤⣤⣤⣤⣿⢿⣄⠀⠀⠀⠀ + ___ ___ ⠀⠀⡇⠀⢀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣧⠀⠀⠀⠀⠀⠀⠙⣷⡴⠶⣦ + / __|/ __| ⠀⠀⢱⡀⠀⠉⠉⠀⠀⠀⠀⠛⠃⠀⢠⡟⠂⠀⠀⢀⣀⣠⣤⠿⠞⠛⠋ + \__ \ (__ ⣠⠾⠋⠙⣶⣤⣤⣤⣤⣤⣀⣠⣤⣾⣿⠴⠶⠚⠋⠉⠁⠀⠀⠀⠀⠀⠀ + |___/\___| ⠛⠒⠛⠉⠉⠀⠀⠀⣴⠟⣣⡴⠛⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ +~~~~~~~~~~~~~~~~~~~~⠛⠛~~~~~~~~~~~~~~~~~~~~~~~~` + fmt.Println(art) + + keys := [...]string{"Built from", "ProxyStreams", "ProxyImages", "Restream", "GetWebProfiles", "Listening on"} + // maps in go are unordered.. + table := map[string]string{ + "Built from": fmt.Sprintf("%s (%s)", cfg.Commit, cfg.CommitURL), + "ProxyStreams": boolean(cfg.ProxyStreams), + "ProxyImages": boolean(cfg.ProxyStreams), + "Restream": boolean(cfg.Restream), + "GetWebProfiles": boolean(cfg.GetWebProfiles), + "Listening on": cfg.Addr, + } + if cfg.Addr[0] == ':' { + table["Listening on"] = "127.0.0.1" + cfg.Addr + } + longest := "" + for key := range table { + if len(key) > len(longest) { + longest = key + } + } + longest += " :: " + + for _, key := range keys { + fmt.Print(key) + fmt.Print(strings.Repeat(" ", len(longest)-len(key)-len(" :: ")) + " :: ") + fmt.Println(table[key]) + } + + fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") if cfg.CodegenConfig { log.Println("Warning: you have CodegenConfig enabled, but the config was loaded dynamically.") } - log.Fatal(app.Listen(cfg.Addr, fiber.ListenConfig{EnablePrefork: cfg.Prefork})) + log.Fatal(app.Listen(cfg.Addr, fiber.ListenConfig{EnablePrefork: cfg.Prefork, DisableStartupMessage: true})) }