diff --git a/README.md b/README.md index ef9439a..dd5332a 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # soundcloak + wip alternative frontend for soundcloud # [Instance list](https://maid.zone/soundcloak/instances.html) # Already implemented + - Searching for songs, users, playlists - Basic user overview (songs, playlists, albums, reposts, metadata) - Basic song overview (author, metadata) & streaming (requires some JS if instance has `Restream` disabled) @@ -14,11 +16,13 @@ wip alternative frontend for soundcloud - Users can change their preferences (should proxying be enabled, what method of playing the song should be used etc) ## In the works + - Track player embeds (`https://w.soundcloud.com/player/` -> `https://sc.maid.zone/w/player/`) The UI isn't really done yet. All parameters other than url are unsupported. You can also specify track without the `soundcloud.com` part: `https://sc.maid.zone/w/player/?url=` or `https://sc.maid.zone/w/player/?url=/` # Viewing instance settings + If the instance isn't outdated and has `InstanceInfo` setting enabled, you can navigate to `/_/info` to view useful instance settings. ([sc.maid.zone/_/info](https://sc.maid.zone/_/info) for example) An easier way is to navigate to `/_/preferences`. @@ -26,6 +30,7 @@ An easier way is to navigate to `/_/preferences`. If some features are disabled by the instance, they won't show up there. Available features: + - Parse descriptions: Highlight `@username`, `https://example.com` and `email@example.com` in text as clickable links - Proxy images: Retrieve images through the instance, instead of going to soundcloud's servers for them - Player: In what way should the track be streamed. Can be Restream (does not require JS, better compatibility, can be a bit buggy client-side) or HLS (requires JS, more stable, less good compatibility (you'll be ok unless you are using a very outdated browser)) @@ -34,46 +39,56 @@ Available features: - - Fully preload track: Fully loads the track when you load the page instead of buffering a small part of it # Contributing + Contributions are appreciated! This includes feedback, feature requests, issues, pull requests and etc. Feedback and feature requests are especially needed, since I (laptopcat) don't really know what to prioritize You can contribute on: + - [GitHub](https://github.com/maid-zone/soundcloak) - [Codeberg (mirror)](https://codeberg.org/maid-zone/soundcloak) You can also [reach out to me privately](https://laptopc.at) # Setting it up +
1. Regular method ## Prerequisites: + 1. [node.js + npm](https://nodejs.org) (any recent enough version should do, it's just used for getting hls.js builds) 2. [golang](https://go.dev) (1.21 or higher was tested, others might work too) 3. [git](https://git-scm.com) ## Setup: + 1. Clone this repository: + ```sh git clone https://github.com/maid-zone/soundcloak ``` 2. Go into the cloned repository: + ```sh cd soundcloak ``` 3. Download hls.js: + ```sh npm i ``` 4. Download templ: + ```sh go install github.com/a-h/templ/cmd/templ@latest ``` *You might need to add go binaries to your PATH (add this line to your .bashrc / .zshrc / whatever)* + ```sh export PATH=${PATH}:`go env GOPATH`/bin ``` @@ -85,6 +100,7 @@ templ generate ``` 6. Download other required go modules: + ```sh go get ``` @@ -100,11 +116,13 @@ go build main.go ``` 9. Run the binary: + ```sh ./main ``` This will run soundcloak on localhost, port 4664. (by default) +
@@ -113,24 +131,30 @@ This will run soundcloak on localhost, port 4664. (by default) The docker image was made by [vlnst](https://github.com/vlnst) ## Prerequisites: + 1. [Docker](https://www.docker.com/) 2. [Git](https://git-scm.com) ## Setup: + 1. Clone this repository: + ```sh git clone https://github.com/maid-zone/soundcloak ``` 2. Go into the cloned repository: + ```sh cd soundcloak ``` 3. Make a copy of the example `compose.yaml` file: + ```sh cp compose.example.yaml compose.yaml ``` + Make adjustments as needed. 4. *Optional.* Edit config: @@ -142,14 +166,58 @@ You can make a `soundcloak.json` file in the folder if you want. Refer to `sound ```sh docker compose up -d ``` + (if you get `docker: 'compose' is not a docker command.`, use `docker-compose up -d`) This will run soundcloak as a daemon (remove the -d part of the command to just run it) on localhost, port 4664. (by default) + +
+ +# Configuration guide + +
+Click to view + +You can only configure in one of the two ways: +- Using config file (`soundcloak.json` in current directory // your own path and filename) +- Using environment variables (`SOUNDCLOAK_CONFIG` must be set to `FROM_ENV`!) + +Some notes: +- When specifying time, specify it in seconds. + +| JSON key | Environment variable | Default value | Description | +| :------------------------ | ---------------------------- | ----------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| None | SOUNDCLOAK_CONFIG | soundcloak.json | File to load soundcloak config from. If set to `FROM_ENV`, soundcloak loads the config from environment variables. | +| DefaultPreferences | DEFAULT_PREFERENCES | {"Player": "hls", "ProxyStreams": false, "FullyPreloadTrack": false, "ProxyImages": false, "ParseDescriptions": true} | see /_/preferences page, default values adapt to your config (Player: "restream" if Restream, else "hls", ProxyStreams and ProxyImages will be same as respective config values) | +| ProxyImages | PROXY_IMAGES | false | Enables proxying of images (user avatars, track covers etc) | +| ImageCacheControl | IMAGE_CACHE_CONTROL | max-age=600, public, immutable | [Cache-Control](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Contro) header value for proxied images. Cached for 10 minutes by default. | +| ProxyStreams | PROXY_STREAMS | false | Enables proxying of song parts and hls playlist files | +| Restream | RESTREAM | false | Enables Restream Player in settings and the /_/restream/:author/:track endpoint. This player can be used without JavaScript and also can be used for downloading songs. | +| ClientIDTTL | CLIENT_ID_TTL | 30 minutes | Time until ClientID cache expires. ClientID is used for authenticating with SoundCloud API | +| UserTTL | USER_TTL | 10 minutes | Time until User profile cache expires | +| UserCacheCleanDelay | USER_CACHE_CLEAN_DELAY | 2.5 minutes | Time between each cleanup of the cache (to remove expired users) | +| TrackTTL | TRACK_TTL | 10 minutes | Time until Track data cache expires | +| TrackCacheCleanDelay | TRACK_CACHE_CLEAN_DELAY | 2.5 minutes | Time between each cleanup of the cache (to remove expired tracks) | +| PlaylistTTL | PLAYLIST_TTL | 10 minutes | Time until Playlist data cache expires | +| PlaylistCacheCleanDelay | PLAYLIST_CACHE_CLEAN_DELAY | 2.5 minutes | Time between each cleanup of the cache (to remove expired playlists) | +| UserAgent | USER_AGENT | Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.3 | User-Agent header used for requests to SoundCloud | +| DNSCacheTTL | DNS_CACHE_TTL | 10 minutes | Time until DNS cache expires | +| Addr | ADDR | :4664 | Address and port for soundcloak to listen on | +| Prefork | PREFORK | false | Run multiple instances of soundcloak locally to be able to handle more requests. Each one will be a separate process, so they will have separate cache. | +| TrustedProxyCheck | TRUSTED_PROXY_CHECK | true | Use X-Forwarded-* headers if IP is in TrustedProxies list. When disabled, those headers will blindly be used. | +| TrustedProxies | TRUSTED_PROXIES | [] | List of IPs or IP ranges of trusted proxies | +
# Maintenance-related stuffs + ## Updating + Note: this guide works only if you install from source. If you used docker, you could probably do the 1st step (pulling the code), stop the container (`docker container stop soundcloak`) and start it again. (`docker compose up -d`) + +
+Click to view + 1. Retrieve the latest code: ```sh @@ -162,6 +230,7 @@ git pull The webserver is configured to locally cache compressed versions of files. They have `.fiber.gz` extension and can be found in `assets` folder and `node_modules/hls.js/dist`. If any static files have been changed, you should purge these compressed files so the new versions can be served. Static files are also cached in user's browser, so you will need to clean your cache to get the new files (Ctrl + F5) For example, you can clean these files from `assets` folder like so: + ```sh cd assets rm *.fiber.gz @@ -171,7 +240,6 @@ rm *.fiber.gz Sometimes, new updates add new config values or change default ones. You can make a `soundcloak.json` file in the folder with the binary if you want, or an environment variable `SOUNDCLOAK_CONFIG` with path to the config. Refer to `soundcloak.json.example` and `lib/cfg/init.go` for configuration values and their meaning. - 4. Regenerate templates (if they changed): ```sh @@ -179,34 +247,43 @@ templ generate ``` 5. Get latest Go modules: + ```sh go get ``` 6. Update hls.js: + ```sh npm i ``` 7. Build binary: + ```sh go build main.go ``` 8. Run it: + ```sh ./main ``` Congratulations! You have succesfully updated your soundcloak. +
+ # 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) diff --git a/lib/cfg/init.go b/lib/cfg/init.go index 4630267..2ea8e90 100644 --- a/lib/cfg/init.go +++ b/lib/cfg/init.go @@ -3,6 +3,8 @@ package cfg import ( "log" "os" + "strconv" + "strings" "time" "github.com/segmentio/encoding/json" @@ -93,15 +95,10 @@ var DNSCacheTTL = 10 * time.Minute // run soundcloak on this address (localhost:4664 by default) var Addr = ":4664" -// run multiple instances of soundcloud locally to be able to handle more requests +// run multiple instances of soundcloak locally to be able to handle more requests // each one will be a separate process, so they will have separate cache var Prefork = false -// Enables TLS Early Data (0-RTT / zero round trip time) -// This can reduce latency, but also makes requests replayable (not that much of a concern for soundcloak, since there are no authenticated operations) -// There might be breakage when used together with TrustedProxyCheck and the proxy is untrusted -var EarlyData = false - // use X-Forwarded-* headers ONLY when ip is in TrustedProxies list // when disabled, the X-Forwarded-* headers will be blindly used var TrustedProxyCheck = true @@ -111,6 +108,12 @@ var TrustedProxies = []string{} // // end of config // // +// defaults are: +// Player: RestreamPlayer if Restream is enabled, otherwise - HLSPlayer +// ProxyStreams: same as ProxyStreams in your config (false by default) +// FullyPreloadTrack: false +// ProxyImages: same as ProxyImages in your config (false by default) +// ParseDescriptions: true func defaultPreferences() { var p string if Restream { @@ -131,9 +134,218 @@ func defaultPreferences() { DefaultPreferences.ParseDescriptions = &t } +func loadDefaultPreferences(loaded Preferences) { + var f bool + var t = true + if loaded.Player != nil { + DefaultPreferences.Player = loaded.Player + } else { + var p string + if Restream { + p = RestreamPlayer + } else { + p = HLSPlayer + } + DefaultPreferences.Player = &p + } + + if loaded.ProxyStreams != nil { + DefaultPreferences.ProxyStreams = loaded.ProxyStreams + } else { + DefaultPreferences.ProxyStreams = &ProxyStreams + } + + if loaded.FullyPreloadTrack != nil { + DefaultPreferences.FullyPreloadTrack = loaded.FullyPreloadTrack + } else { + DefaultPreferences.FullyPreloadTrack = &f + } + + if loaded.ProxyImages != nil { + DefaultPreferences.ProxyImages = loaded.ProxyImages + } else { + DefaultPreferences.ProxyImages = &ProxyImages + } + + if loaded.ParseDescriptions != nil { + DefaultPreferences.ParseDescriptions = loaded.ParseDescriptions + } else { + DefaultPreferences.ParseDescriptions = &t + } +} + +func boolean(in string) bool { + return strings.Trim(strings.ToLower(in), " ") == "true" +} + +func fromEnv() error { + env := os.Getenv("DEFAULT_PREFERENCES") + if env != "" { + var p Preferences + err := json.Unmarshal([]byte(env), &p) + if err != nil { + return err + } + + loadDefaultPreferences(p) + } else { + defaultPreferences() + } + + env = os.Getenv("PROXY_IMAGES") + if env != "" { + ProxyImages = boolean(env) + } + + env = os.Getenv("IMAGE_CACHE_CONTROL") + if env != "" { + ImageCacheControl = env + } + + env = os.Getenv("PROXY_STREAMS") + if env != "" { + ProxyStreams = boolean(env) + } + + env = os.Getenv("RESTREAM") + if env != "" { + Restream = boolean(env) + } + + env = os.Getenv("RESTREAM_CACHE_CONTROL") + if env != "" { + RestreamCacheControl = env + } + + env = os.Getenv("INSTANCE_INFO") + if env != "" { + InstanceInfo = boolean(env) + } + + env = os.Getenv("CLIENT_ID_TTL") + if env != "" { + num, err := strconv.ParseInt(env, 10, 64) + if err != nil { + return err + } + + ClientIDTTL = time.Duration(num) * time.Second + } + + env = os.Getenv("USER_TTL") + if env != "" { + num, err := strconv.ParseInt(env, 10, 64) + if err != nil { + return err + } + + UserTTL = time.Duration(num) * time.Second + } + + env = os.Getenv("USER_CACHE_CLEAN_DELAY") + if env != "" { + num, err := strconv.ParseInt(env, 10, 64) + if err != nil { + return err + } + + UserCacheCleanDelay = time.Duration(num) * time.Second + } + + env = os.Getenv("TRACK_TTL") + if env != "" { + num, err := strconv.ParseInt(env, 10, 64) + if err != nil { + return err + } + + TrackTTL = time.Duration(num) * time.Second + } + + env = os.Getenv("TRACK_CACHE_CLEAN_DELAY") + if env != "" { + num, err := strconv.ParseInt(env, 10, 64) + if err != nil { + return err + } + + TrackCacheCleanDelay = time.Duration(num) * time.Second + } + + env = os.Getenv("PLAYLIST_TTL") + if env != "" { + num, err := strconv.ParseInt(env, 10, 64) + if err != nil { + return err + } + + PlaylistTTL = time.Duration(num) * time.Second + } + + env = os.Getenv("PLAYLIST_CACHE_CLEAN_DELAY") + if env != "" { + num, err := strconv.ParseInt(env, 10, 64) + if err != nil { + return err + } + + PlaylistCacheCleanDelay = time.Duration(num) * time.Second + } + + env = os.Getenv("USER_AGENT") + if env != "" { + UserAgent = env + } + + env = os.Getenv("DNS_CACHE_TTL") + if env != "" { + num, err := strconv.ParseInt(env, 10, 64) + if err != nil { + return err + } + + DNSCacheTTL = time.Duration(num) * time.Second + } + + env = os.Getenv("ADDR") + if env != "" { + Addr = env + } + + env = os.Getenv("PREFORK") + if env != "" { + Prefork = boolean(env) + } + + env = os.Getenv("TRUSTED_PROXY_CHECK") + if env != "" { + TrustedProxyCheck = boolean(env) + } + + env = os.Getenv("TRUSTED_PROXIES") + if env != "" { + var p []string + err := json.Unmarshal([]byte(env), &p) + if err != nil { + return err + } + + TrustedProxies = p + } + + return nil +} + func init() { filename := "soundcloak.json" - if env := os.Getenv("SOUNDCLOAK_CONFIG"); env != "" { + if env := os.Getenv("SOUNDCLOAK_CONFIG"); env == "FROM_ENV" { + err := fromEnv() + if err != nil { + panic(err) + } + + return + } else { filename = env } @@ -163,7 +375,6 @@ func init() { DNSCacheTTL *time.Duration Addr *string Prefork *bool - EarlyData *bool TrustedProxyCheck *bool TrustedProxies *[]string } @@ -228,9 +439,6 @@ func init() { if config.Prefork != nil { Prefork = *config.Prefork } - if config.EarlyData != nil { - EarlyData = *config.EarlyData - } if config.TrustedProxyCheck != nil { TrustedProxyCheck = *config.TrustedProxyCheck } @@ -238,50 +446,8 @@ func init() { TrustedProxies = *config.TrustedProxies } - // defaults are: - // Player: RestreamPlayer if Restream is enabled, otherwise - HLSPlayer - // ProxyStreams: same as ProxyStreams in your config (false by default) - // FullyPreloadTrack: false - // ProxyImages: same as ProxyImages in your config (false by default) - // ParseDescriptions: true if config.DefaultPreferences != nil { - var f bool - var t = true - if config.DefaultPreferences.Player != nil { - DefaultPreferences.Player = config.DefaultPreferences.Player - } else { - var p string - if Restream { - p = RestreamPlayer - } else { - p = HLSPlayer - } - DefaultPreferences.Player = &p - } - - if config.DefaultPreferences.ProxyStreams != nil { - DefaultPreferences.ProxyStreams = config.DefaultPreferences.ProxyStreams - } else { - DefaultPreferences.ProxyStreams = &ProxyStreams - } - - if config.DefaultPreferences.FullyPreloadTrack != nil { - DefaultPreferences.FullyPreloadTrack = config.DefaultPreferences.FullyPreloadTrack - } else { - DefaultPreferences.FullyPreloadTrack = &f - } - - if config.DefaultPreferences.ProxyImages != nil { - DefaultPreferences.ProxyImages = config.DefaultPreferences.ProxyImages - } else { - DefaultPreferences.ProxyImages = &ProxyImages - } - - if config.DefaultPreferences.ParseDescriptions != nil { - DefaultPreferences.ParseDescriptions = config.DefaultPreferences.ParseDescriptions - } else { - DefaultPreferences.ParseDescriptions = &t - } + loadDefaultPreferences(*config.DefaultPreferences) } else { defaultPreferences() } diff --git a/main.go b/main.go index b6eac5d..c8137a2 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,6 @@ import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/compress" - "github.com/gofiber/fiber/v2/middleware/earlydata" "github.com/gofiber/fiber/v2/middleware/recover" "github.com/segmentio/encoding/json" "github.com/valyala/fasthttp" @@ -33,9 +32,6 @@ func main() { }) app.Use(recover.New()) - if cfg.EarlyData { - app.Use(earlydata.New()) - } app.Use(compress.New(compress.Config{Level: compress.LevelBestSpeed})) app.Static("/", "assets", fiber.Static{Compress: true, MaxAge: 3600}) // 1hour