From 014eb3467daf47030f42f123204de9c080c9364e Mon Sep 17 00:00:00 2001 From: Laptop Date: Thu, 19 Sep 2024 18:18:04 +0300 Subject: [PATCH] once again --- README.md | 18 ++++++++++++++++++ lib/cfg/init.go | 11 +++++++++++ main.go | 40 ++++++++++++++++++++++++++++++++++++++++ templates/base.templ | 2 +- templates/track.templ | 15 ++++++++------- 5 files changed, 78 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4641f7c..da83d99 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,13 @@ wip alternative frontend for soundcloud # [official public instance](https://sc.maid.zone) there is no image and audio proxy for now so beware +# Already implemented +- Searching for songs, users, playlists +- Basic user overview (songs, metadata) +- Basic song overview (author, metadata) & streaming (requires javascript if no [browser support for HLS](https://caniuse.com/http-live-streaming)) +- Basic playlist/set/album overview (songs list, author, metadata) +- Resolving shortened links (`https://on.soundcloud.com/boiKDP46fayYDoVK9` -> `https://sc.maid.zone/on/boiKDP46fayYDoVK9`) + # Setting it up ## Prerequisites: 1. [node.js + npm](https://nodejs.org) (any recent enough version should do, it's just used for getting hls.js builds) @@ -56,3 +63,14 @@ templ generate && go build main.go ``` This will run soundcloak on localhost, port 4664. (by default) + +# Built with +## Backend +- [Go programming language](https://github.com/golang/go) +- [Fiber (v2)](https://github.com/gofiber/fiber/tree/v2) +- [templ](https://github.com/a-h/templ) +- [fasthttp](https://github.com/valyala/fasthttp) + +## Frontend +- HTML, CSS and JavaScript +- [hls.js](https://github.com/video-dev/hls.js) \ No newline at end of file diff --git a/lib/cfg/init.go b/lib/cfg/init.go index f800143..2ae5f16 100644 --- a/lib/cfg/init.go +++ b/lib/cfg/init.go @@ -25,6 +25,9 @@ const UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 // time-to-live for dns cache const DNSCacheTTL = 10 * time.Minute +// // // some webserver configuration, put here to make it easier to configure what you need // // // +// more info can be found here: https://docs.gofiber.io/api/fiber#config + // run soundcloak on this address (localhost:4664 by default) const Addr = ":4664" @@ -32,4 +35,12 @@ const Addr = ":4664" // each one will be a separate process, so they will have separate cache const Prefork = false +// use X-Forwarded-* headers ONLY when ip is in TrustedProxies list +// when disabled, the X-Forwarded-* headers will be blindly used +const TrustedProxyCheck = false + +// ip or ip range of trusted proxies (check above) +var TrustedProxies = []string{} + +// what JSON library should be used var JSON = jsoniter.ConfigFastest diff --git a/main.go b/main.go index bb6df0c..2fc4e20 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "github.com/gofiber/fiber/v2/middleware/compress" "github.com/gofiber/fiber/v2/middleware/earlydata" "github.com/gofiber/fiber/v2/middleware/recover" + "github.com/valyala/fasthttp" "github.com/maid-zone/soundcloak/lib/cfg" "github.com/maid-zone/soundcloak/lib/sc" @@ -22,6 +23,9 @@ func main() { Prefork: cfg.Prefork, JSONEncoder: cfg.JSON.Marshal, JSONDecoder: cfg.JSON.Unmarshal, + + EnableTrustedProxyCheck: cfg.TrustedProxyCheck, + TrustedProxies: cfg.TrustedProxies, }) app.Use(compress.New()) app.Use(recover.New()) @@ -68,6 +72,42 @@ func main() { return c.SendStatus(404) }) + app.Get("/on/:id", func(c *fiber.Ctx) error { + id := c.Params("id") + if id == "" { + return fiber.ErrNotFound + } + + req := fasthttp.AcquireRequest() + defer fasthttp.ReleaseRequest(req) + + req.Header.SetMethod("HEAD") + req.SetRequestURI("https://on.soundcloud.com/" + id) + req.Header.Set("User-Agent", cfg.UserAgent) + + resp := fasthttp.AcquireResponse() + defer fasthttp.ReleaseResponse(resp) + + err := fasthttp.Do(req, resp) + if err != nil { + return err + } + + loc := resp.Header.Peek("location") + if len(loc) == 0 { + return fiber.ErrNotFound + } + + //fmt.Println(c.Hostname(), c.Protocol(), c.IPs()) + + u, err := url.Parse(string(loc)) + if err != nil { + return err + } + + return c.Redirect(u.Path) + }) + app.Get("/:user/:track", func(c *fiber.Ctx) error { track, err := sc.GetTrack(c.Params("user") + "/" + c.Params("track")) if err != nil { diff --git a/templates/base.templ b/templates/base.templ index 3866404..375bb67 100644 --- a/templates/base.templ +++ b/templates/base.templ @@ -8,7 +8,7 @@ templ Base(title string, content templ.Component, head templ.Component) { if title != "" { - { title } • soundcloak + { title } ~ soundcloak } else { soundcloak } diff --git a/templates/track.templ b/templates/track.templ index 0899fda..d4b3588 100644 --- a/templates/track.templ +++ b/templates/track.templ @@ -20,7 +20,11 @@ templ Track(t sc.Track, stream string) { }

{ t.Title }

- + + if t.Genre != "" {

{ t.Genre }

} else { @@ -51,15 +55,12 @@ templ Track(t sc.Track, stream string) { } }