diff --git a/.dockerignore b/.dockerignore index 815eb8e..92331fd 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,3 +2,5 @@ Dockerfile LICENSE README.md *.yaml +*_templ.go +regexp2_codegen.go \ No newline at end of file diff --git a/.gitignore b/.gitignore index 602e3bd..38b3eb5 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ fly.toml *.fiber.gz soundcloak.json compose.yaml +regexp2_codegen.go \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 13d0038..0387a5d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -ARG GO_VERSION=1.21.3 +ARG GO_VERSION=1.22.10 ARG NODE_VERSION=bookworm FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build @@ -7,10 +7,14 @@ ARG TARGETARCH WORKDIR /build COPY . . -RUN go install github.com/a-h/templ/cmd/templ@latest && \ - templ generate && \ - CGO_ENABLED=0 GOARCH=${TARGETARCH} GOOS=${TARGETOS} go build -ldflags "-s -w -extldflags '-static'" -o ./app && \ - echo "soundcloak:x:5000:5000:Soundcloak user:/:/sbin/nologin" > /etc/minimal-passwd && \ +RUN go install github.com/a-h/templ/cmd/templ@latest +RUN templ generate + +RUN go install github.com/dlclark/regexp2cg@main +RUN go generate ./lib/textparsing + +RUN CGO_ENABLED=0 GOARCH=${TARGETARCH} GOOS=${TARGETOS} go build -ldflags "-s -w -extldflags '-static' -X main.commit=`git rev-parse HEAD | head -c 7` -X main.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 FROM node:${NODE_VERSION} AS node diff --git a/README.md b/README.md index 14c524e..fbf8aa7 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ You can also [reach out to me privately](https://laptopc.at) ## 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) +2. [golang](https://go.dev) (I recommend version 1.22.10. Technically, you need 1.21.4 or higher) 3. [git](https://git-scm.com) ## Setup: @@ -88,35 +88,31 @@ npm i go install github.com/a-h/templ/cmd/templ@latest ``` +5. Download regexp2cg: + +Not really required, but helps speed up some parts of the code that use regular expressions. Keep in mind that the `build` script expects this to be installed. +```sh +go install github.com/dlclark/regexp2cg@main +``` + *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 ``` -5. Generate code from templates: - -```sh -templ generate -``` - -6. Download other required go modules: - -```sh -go get -``` - -7. *Optional.* Edit config: +6. *Optional.* Edit config: Refer to [Configuration guide](#configuration-guide) for configuration information. Can be configured from environment variables or JSON file. -8. Build binary: +7. Build binary: +This uses the `build` script, which generates code from templates, generates code for regular expiressions, and then builds the binary. ```sh -go build main.go +./build ``` -9. Run the binary: +8. Run the binary: ```sh ./main @@ -219,7 +215,7 @@ Some notes: ## 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`) +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`), build it (`docker compose build`) and start it again. (`docker compose up -d`)
Click to view @@ -246,31 +242,19 @@ rm *.fiber.gz Sometimes, new updates add new config values or change default ones. Refer to [Configuration guide](#configuration-guide) for configuration information. Can be configured from environment variables or JSON file. -4. Regenerate templates (if they changed): - -```sh -templ generate -``` - -5. Get latest Go modules: - -```sh -go get -``` - -6. Update hls.js: +5. Update hls.js: ```sh npm i ``` -7. Build binary: +6. Build binary: ```sh -go build main.go +./build ``` -8. Run it: +7. Run it: ```sh ./main diff --git a/build b/build new file mode 100755 index 0000000..5ffc6d5 --- /dev/null +++ b/build @@ -0,0 +1,3 @@ +templ generate +go generate ./lib/textparsing +go build -ldflags "-X main.commit=`git rev-parse HEAD | head -c 7` -X main.repo=`git remote get-url origin`" main.go \ No newline at end of file diff --git a/go.mod b/go.mod index c378a44..8d202ed 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,11 @@ module github.com/maid-zone/soundcloak -go 1.21.3 +go 1.22.10 require ( github.com/a-h/templ v0.2.793 github.com/bogem/id3v2/v2 v2.1.4 + github.com/dlclark/regexp2 v1.11.5-0.20240806004527-5bbbed8ea10b github.com/gofiber/fiber/v2 v2.52.5 github.com/segmentio/encoding v0.4.0 github.com/valyala/fasthttp v1.57.0 diff --git a/go.sum b/go.sum index aa6b21d..ccbed03 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7X github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA= github.com/bogem/id3v2/v2 v2.1.4 h1:CEwe+lS2p6dd9UZRlPc1zbFNIha2mb2qzT1cCEoNWoI= github.com/bogem/id3v2/v2 v2.1.4/go.mod h1:l+gR8MZ6rc9ryPTPkX77smS5Me/36gxkMgDayZ9G1vY= +github.com/dlclark/regexp2 v1.11.5-0.20240806004527-5bbbed8ea10b h1:AJKOdc+1fRSJ0/75Jty1npvxUUD0y7hQDg15LMAHhyU= +github.com/dlclark/regexp2 v1.11.5-0.20240806004527-5bbbed8ea10b/go.mod h1:YvCrhrh/qlds8EhFKPtJprdXn5fWBllSw1qo99dZyiQ= github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo= github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= diff --git a/lib/cfg/init.go b/lib/cfg/init.go index aed5fed..5f3a9a5 100644 --- a/lib/cfg/init.go +++ b/lib/cfg/init.go @@ -533,7 +533,7 @@ type ProxyReader struct { Resp *fasthttp.Response } -func (pr *ProxyReader) Read(p []byte) (n int, err error) { +func (pr *ProxyReader) Read(p []byte) (int, error) { return pr.Reader.Read(p) } diff --git a/lib/textparsing/init.go b/lib/textparsing/init.go index b3c1411..71c8c9e 100644 --- a/lib/textparsing/init.go +++ b/lib/textparsing/init.go @@ -4,23 +4,27 @@ import ( "fmt" "html" "net/url" - "regexp" "strings" + + "github.com/dlclark/regexp2" ) //var wordre = regexp.MustCompile(`\S+`) - -// var urlre = regexp.MustCompile(`https?:\/\/[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{1,6}[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*`) -var emailre = regexp.MustCompile(`^[-a-zA-Z0-9%._\+~#=]+@[-a-zA-Z0-9%._\+~=&]{2,256}\.[a-z]{1,6}$`) - // var usernamere = regexp.MustCompile(`@[a-zA-Z0-9\-]+`) -var theregex = regexp.MustCompile(`@[a-zA-Z0-9\-]+|(?:https?:\/\/[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{1,6}[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)|(?:[-a-zA-Z0-9%._\+~#=]+@[-a-zA-Z0-9%._\+~=&]{2,256}\.[a-z]{1,6})`) +// var urlre = regexp.MustCompile(`https?:\/\/[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{1,6}[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*`) + +//go:generate regexp2cg -package textparsing -o regexp2_codegen.go +var emailre = regexp2.MustCompile(`^[-a-zA-Z0-9%._\+~#=]+@[-a-zA-Z0-9%._\+~=&]{2,256}\.[a-z]{1,6}$`, 0) +var theregex = regexp2.MustCompile(`@[a-zA-Z0-9\-]+|(?:https?:\/\/[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{1,6}[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)|(?:[-a-zA-Z0-9%._\+~#=]+@[-a-zA-Z0-9%._\+~=&]{2,256}\.[a-z]{1,6})`, 0) func IsEmail(s string) bool { - return emailre.MatchString(s) + t, _ := emailre.MatchString(s) + return t } -func replacer(ent string) string { +func replacer(m regexp2.Match) string { + ent := m.Capture.String() + if strings.HasPrefix(ent, "@") { return fmt.Sprintf(`%s`, ent[1:], ent) } @@ -46,5 +50,11 @@ func replacer(ent string) string { } func Format(text string) string { - return theregex.ReplaceAllStringFunc(html.EscapeString(text), replacer) + text = html.EscapeString(text) + out, err := theregex.ReplaceFunc(text, replacer, -1, -1) + if err != nil { + return text + } + + return out } diff --git a/main.go b/main.go index 50c1f1e..1096401 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,10 @@ import ( "github.com/maid-zone/soundcloak/templates" ) +// see build script/dockerfile +var commit = "unknown" +var repo = "unknown" + func main() { app := fiber.New(fiber.Config{ Prefork: cfg.Prefork, @@ -220,6 +224,8 @@ func main() { if cfg.InstanceInfo { type info struct { + Commit string + Repo string ProxyImages bool ProxyStreams bool Restream bool @@ -229,6 +235,8 @@ func main() { app.Get("/_/info", func(c *fiber.Ctx) error { return c.JSON(info{ + Commit: commit, + Repo: repo, ProxyImages: cfg.ProxyImages, ProxyStreams: cfg.ProxyStreams, Restream: cfg.Restream,