diff --git a/Dockerfile b/Dockerfile index a399eaf..f7034d3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,7 +20,7 @@ RUN go generate ./lib/* RUN soundcloakctl config codegen RUN soundcloakctl -nozstd precompress -RUN CGO_ENABLED=0 GOARCH=${TARGETARCH} GOOS=${TARGETOS} go build -v -ldflags "-s -w -extldflags '-static'" -o ./app +RUN CGO_ENABLED=0 GOARCH=${TARGETARCH} GOOS=${TARGETOS} go build -v -ldflags "-s -w -extldflags '-static' -X git.maid.zone/stuff/soundcloak/lib/cfg.Commit=`git rev-parse HEAD | head -c 7` -X git.maid.zone/stuff/soundcloak/lib/cfg.Repo=`git remote get-url origin`" -o ./app RUN echo "soundcloak:x:5000:5000:Soundcloak user:/:/sbin/nologin" > /etc/minimal-passwd && \ echo "soundcloak:x:5000:" > /etc/minimal-group diff --git a/build b/build index 3541284..8336234 100755 --- a/build +++ b/build @@ -1,3 +1,3 @@ templ generate go generate ./lib/* -go build main.go \ No newline at end of file +go build -ldflags "-X git.maid.zone/stuff/soundcloak/lib/cfg.Commit=`git rev-parse HEAD | head -c 7` -X git.maid.zone/stuff/soundcloak/lib/cfg.Repo=`git remote get-url origin`" main.go \ No newline at end of file diff --git a/docs/INSTANCE_GUIDE.md b/docs/INSTANCE_GUIDE.md index c641510..aec9f88 100644 --- a/docs/INSTANCE_GUIDE.md +++ b/docs/INSTANCE_GUIDE.md @@ -118,7 +118,8 @@ Some notes: | 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 | 60 minutes | Time until DNS cache expires | | Network | NETWORK | tcp4 | Network to listen on. Can be tcp4, tcp6 or unix | -| Addr | ADDR | :4664 | Address and port for soundcloak to listen on | +| Addr | ADDR | :4664 | Address and port (or socket path) for soundcloak to listen on | +| UnixSocketPerms | UNIX_SOCKET_PERMS | 0775 | Permissions for unix socket (Network must be set to unix) 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 | diff --git a/lib/cfg/init.go b/lib/cfg/init.go index 913abfc..5bbbe11 100644 --- a/lib/cfg/init.go +++ b/lib/cfg/init.go @@ -79,6 +79,9 @@ var Network = "tcp4" // run soundcloak on this address (localhost:4664 by default) var Addr = ":4664" +// unix socket perms wow +var UnixSocketPerms os.FileMode = 0775 + // 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 @@ -394,6 +397,16 @@ func fromEnv() error { Addr = env } + env = os.Getenv("UNIX_SOCKET_PERMS") + if env != "" { + p, err := strconv.ParseUint(env, 0, 32) + if err != nil { + return err + } + + UnixSocketPerms = os.FileMode(p) + } + env = os.Getenv("PREFORK") if env != "" { Prefork = boolean(env) @@ -469,6 +482,7 @@ func init() { DNSCacheTTL *time.Duration Network *string Addr *string + UnixSocketPerms *string Prefork *bool TrustedProxyCheck *bool TrustedProxies *[]string @@ -539,6 +553,14 @@ func init() { if config.Addr != nil { Addr = *config.Addr } + if config.UnixSocketPerms != nil { + p, err := strconv.ParseUint(*config.UnixSocketPerms, 0, 32) + if err != nil { + log.Println("failed to parse UnixSocketPerms:", err) + } else { + UnixSocketPerms = os.FileMode(p) + } + } if config.Prefork != nil { Prefork = *config.Prefork } @@ -563,6 +585,3 @@ func init() { } const Debug = false -const Commit = "unknown" -const Repo = "unknown" -const CommitURL = "unknown" diff --git a/lib/cfg/misc.go b/lib/cfg/misc.go index 21986ea..936ab76 100644 --- a/lib/cfg/misc.go +++ b/lib/cfg/misc.go @@ -1,6 +1,7 @@ package cfg import ( + "strings" "time" "unsafe" ) @@ -17,6 +18,13 @@ const MaxIdleConnDuration = 4 * time.Hour var True = true var False = false +// embedded at buildtime +var Commit = "unknown" +var Repo = "unknown" + +// generated at runtime +var CommitURL = "unknown" + const ( // Downloads the HLS stream on the backend, and restreams it to frontend as a file. Requires no JS, but less stable client-side RestreamPlayer string = "restream" @@ -91,3 +99,18 @@ func B2s(b []byte) string { func S2b(s string) []byte { return unsafe.Slice(unsafe.StringData(s), len(s)) } + +func init() { + if Repo != "unknown" { + if strings.HasPrefix(Repo, "http") { + CommitURL = strings.TrimSuffix(Repo, "/") + CommitURL = strings.TrimSuffix(CommitURL, ".git") + } else { + s := strings.Split(Repo, "@") + s = strings.Split(s[1], ":") + CommitURL = "https://" + s[0] + "/" + s[1] + } + + CommitURL += "/commit/" + Commit + } +} diff --git a/lib/misc/init.go b/lib/misc/init.go index 110ee01..1ace6ae 100644 --- a/lib/misc/init.go +++ b/lib/misc/init.go @@ -1,8 +1,8 @@ package misc import ( - "fmt" "io" + "log" "sync" "git.maid.zone/stuff/soundcloak/lib/cfg" @@ -41,7 +41,7 @@ func (pr *ProxyReader) Close() error { func Log(what ...any) { if cfg.Debug { - fmt.Println(what...) + log.Println(what...) } } diff --git a/main.go b/main.go index 483ff14..b62146b 100644 --- a/main.go +++ b/main.go @@ -930,7 +930,6 @@ Disallow: /`) } if comm.Next != "" { - fmt.Println(comm.Next) c.Set("next", "?pagination="+url.QueryEscape(strings.Split(comm.Next, "/comments")[1])) } else { c.Set("next", "done") @@ -1157,5 +1156,18 @@ Disallow: /`) 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, DisableStartupMessage: true, ListenerNetwork: cfg.Network})) + + lc := fiber.ListenConfig{EnablePrefork: cfg.Prefork, DisableStartupMessage: true, ListenerNetwork: cfg.Network} + if cfg.Network == "unix" { + os.Remove(cfg.Addr) + lc.BeforeServeFunc = func(*fiber.App) error { + err := os.Chmod(cfg.Addr, cfg.UnixSocketPerms) + if err != nil { + log.Println("failed to chmod socket:", err) + } + + return nil + } + } + log.Fatal(app.Listen(cfg.Addr, lc)) } diff --git a/templates/base.templ b/templates/base.templ index 05337e5..d5e64b3 100644 --- a/templates/base.templ +++ b/templates/base.templ @@ -60,8 +60,6 @@ templ MainPage(p cfg.Preferences) { Legal notice - if cfg.Commit != "unknown" { -
Build {cfg.Commit}
- } +Build {cfg.Commit}
}