improve unix socket support, always embed commit

This commit is contained in:
Laptop
2025-06-18 22:34:38 +03:00
parent 3d96470a05
commit a0551d742b
8 changed files with 66 additions and 13 deletions

View File

@@ -20,7 +20,7 @@ RUN go generate ./lib/*
RUN soundcloakctl config codegen RUN soundcloakctl config codegen
RUN soundcloakctl -nozstd precompress 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 && \ RUN echo "soundcloak:x:5000:5000:Soundcloak user:/:/sbin/nologin" > /etc/minimal-passwd && \
echo "soundcloak:x:5000:" > /etc/minimal-group echo "soundcloak:x:5000:" > /etc/minimal-group

2
build
View File

@@ -1,3 +1,3 @@
templ generate templ generate
go generate ./lib/* go generate ./lib/*
go build main.go 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

View File

@@ -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 | | 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 | | DNSCacheTTL | DNS_CACHE_TTL | 60 minutes | Time until DNS cache expires |
| Network | NETWORK | tcp4 | Network to listen on. Can be tcp4, tcp6 or unix | | 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. | | 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. | | 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 | | TrustedProxies | TRUSTED_PROXIES | [] | List of IPs or IP ranges of trusted proxies |

View File

@@ -79,6 +79,9 @@ var Network = "tcp4"
// run soundcloak on this address (localhost:4664 by default) // run soundcloak on this address (localhost:4664 by default)
var Addr = ":4664" 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 // 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 // each one will be a separate process, so they will have separate cache
var Prefork = false var Prefork = false
@@ -394,6 +397,16 @@ func fromEnv() error {
Addr = env 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") env = os.Getenv("PREFORK")
if env != "" { if env != "" {
Prefork = boolean(env) Prefork = boolean(env)
@@ -469,6 +482,7 @@ func init() {
DNSCacheTTL *time.Duration DNSCacheTTL *time.Duration
Network *string Network *string
Addr *string Addr *string
UnixSocketPerms *string
Prefork *bool Prefork *bool
TrustedProxyCheck *bool TrustedProxyCheck *bool
TrustedProxies *[]string TrustedProxies *[]string
@@ -539,6 +553,14 @@ func init() {
if config.Addr != nil { if config.Addr != nil {
Addr = *config.Addr 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 { if config.Prefork != nil {
Prefork = *config.Prefork Prefork = *config.Prefork
} }
@@ -563,6 +585,3 @@ func init() {
} }
const Debug = false const Debug = false
const Commit = "unknown"
const Repo = "unknown"
const CommitURL = "unknown"

View File

@@ -1,6 +1,7 @@
package cfg package cfg
import ( import (
"strings"
"time" "time"
"unsafe" "unsafe"
) )
@@ -17,6 +18,13 @@ const MaxIdleConnDuration = 4 * time.Hour
var True = true var True = true
var False = false var False = false
// embedded at buildtime
var Commit = "unknown"
var Repo = "unknown"
// generated at runtime
var CommitURL = "unknown"
const ( const (
// Downloads the HLS stream on the backend, and restreams it to frontend as a file. Requires no JS, but less stable client-side // 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" RestreamPlayer string = "restream"
@@ -91,3 +99,18 @@ func B2s(b []byte) string {
func S2b(s string) []byte { func S2b(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s)) 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
}
}

View File

@@ -1,8 +1,8 @@
package misc package misc
import ( import (
"fmt"
"io" "io"
"log"
"sync" "sync"
"git.maid.zone/stuff/soundcloak/lib/cfg" "git.maid.zone/stuff/soundcloak/lib/cfg"
@@ -41,7 +41,7 @@ func (pr *ProxyReader) Close() error {
func Log(what ...any) { func Log(what ...any) {
if cfg.Debug { if cfg.Debug {
fmt.Println(what...) log.Println(what...)
} }
} }

16
main.go
View File

@@ -930,7 +930,6 @@ Disallow: /`)
} }
if comm.Next != "" { if comm.Next != "" {
fmt.Println(comm.Next)
c.Set("next", "?pagination="+url.QueryEscape(strings.Split(comm.Next, "/comments")[1])) c.Set("next", "?pagination="+url.QueryEscape(strings.Split(comm.Next, "/comments")[1]))
} else { } else {
c.Set("next", "done") c.Set("next", "done")
@@ -1157,5 +1156,18 @@ Disallow: /`)
if cfg.CodegenConfig { if cfg.CodegenConfig {
log.Println("Warning: you have CodegenConfig enabled, but the config was loaded dynamically.") 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))
} }

View File

@@ -60,8 +60,6 @@ templ MainPage(p cfg.Preferences) {
<a class="btn" href="/_/static/notice.txt">Legal notice</a> <a class="btn" href="/_/static/notice.txt">Legal notice</a>
</div> </div>
if cfg.Commit != "unknown" { <p style="text-align: center;">Build <a class="link" href={cfg.CommitURL}>{cfg.Commit}</a></p>
<p style="text-align: center;">Build <a class="link" href={cfg.CommitURL}>{cfg.Commit}</a></p>
}
</footer> </footer>
} }