add optional rsync server

This commit is contained in:
Jonas Leder 2026-09-16 23:45:18 +02:00
commit be9685feea
7 changed files with 150 additions and 1 deletions

View file

@ -27,3 +27,13 @@ LOG_FOLDER=
# Optional: DEBUG, INFO, WARN, ERROR (default INFO).
LOG_LEVEL=INFO
# If true, also starts a read-only rsync daemon that mirrors ROOT_DIR.
RSYNC_ENABLED=false
# Address the rsync daemon listens on. Port 873 is the rsync default but
# requires elevated privileges to bind on Linux; use a high port otherwise.
RSYNC_ADDR=:873
# Module name rsync clients use, e.g. rsync://host/<RSYNC_MODULE_NAME>.
RSYNC_MODULE_NAME=share

View file

@ -22,6 +22,8 @@ in-memory file index so it stays fast with thousands of files.
`go:embed` — the Docker image only needs the compiled binary.
- Optional virtual-host restriction so the listing only answers on a
specific hostname.
- Optional read-only rsync daemon that mirrors the same directory, so it
can be synced with a standard `rsync` client.
## Configuration
@ -39,6 +41,9 @@ automatically if present). See [.env.example](.env.example).
| `GIN_MODE` | `release` | Gin mode: `release`, `debug`, or `test`. |
| `LOG_FOLDER` | *(empty)* | If set, logs are also written to `<LOG_FOLDER>/application.log` (rotated). |
| `LOG_LEVEL` | `INFO` | `DEBUG`, `INFO`, `WARN`, or `ERROR`. |
| `RSYNC_ENABLED` | `false` | If `true`, also starts a read-only rsync daemon mirroring `ROOT_DIR`. |
| `RSYNC_ADDR` | `:873` | Address the rsync daemon listens on. Port 873 is the rsync default but requires binding as root (or `CAP_NET_BIND_SERVICE`) on Linux; use a high port otherwise. |
| `RSYNC_MODULE_NAME` | `share` | The module name clients use, e.g. `rsync://host/<RSYNC_MODULE_NAME>`. |
## Running locally
@ -65,6 +70,33 @@ docker run -p 8080:8080 \
pretty-directorylist
```
## Mirroring with rsync
Set `RSYNC_ENABLED=true` to start a read-only rsync daemon alongside the
HTTP server. It serves the same `ROOT_DIR` as a single module (named by
`RSYNC_MODULE_NAME`) and only allows pulling files out — clients cannot
push, modify, or delete anything on the server.
```sh
RSYNC_ENABLED=true RSYNC_ADDR=:8873 RSYNC_MODULE_NAME=share go run .
```
Then, from any machine with `rsync` installed:
```sh
rsync -av rsync://host:8873/share/ ./local-mirror/
```
With Docker, publish the rsync port too:
```sh
docker run -p 8080:8080 -p 8873:8873 \
-v /path/to/files:/data \
-e RSYNC_ENABLED=true \
-e RSYNC_ADDR=:8873 \
pretty-directorylist
```
## API
The frontend talks to the backend exclusively through this JSON API:

8
go.mod
View file

@ -5,7 +5,9 @@ go 1.26.5
require (
github.com/fsnotify/fsnotify v1.10.1
github.com/gin-gonic/gin v1.12.0
github.com/gokrazy/rsync v0.3.7
github.com/joho/godotenv v1.5.1
github.com/sirupsen/logrus v1.9.3
jonasled.dev/jonasled/go-libs v0.0.3
)
@ -21,24 +23,28 @@ require (
github.com/go-playground/validator/v10 v10.30.1 // indirect
github.com/goccy/go-json v0.10.5 // indirect
github.com/goccy/go-yaml v1.19.2 // indirect
github.com/google/renameio/v2 v2.0.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
github.com/landlock-lsm/go-landlock v0.0.0-20250303204525-1544bccde3a3 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mmcloughlin/md4 v0.1.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/quic-go/qpack v0.6.0 // indirect
github.com/quic-go/quic-go v0.59.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.3.1 // indirect
go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect
golang.org/x/arch v0.22.0 // indirect
golang.org/x/crypto v0.48.0 // indirect
golang.org/x/net v0.51.0 // indirect
golang.org/x/sync v0.19.0 // indirect
golang.org/x/sys v0.41.0 // indirect
golang.org/x/text v0.34.0 // indirect
google.golang.org/protobuf v1.36.10 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
kernel.org/pub/linux/libs/security/libcap/psx v1.2.70 // indirect
)

12
go.sum
View file

@ -29,19 +29,27 @@ github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM=
github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/gokrazy/rsync v0.3.7 h1:kWOgS3/6rhC8CKvwaJ6r2l/JoluwNZYKThsyO2MWpf0=
github.com/gokrazy/rsync v0.3.7/go.mod h1:1R8gV3DX/cCc3DHWuUOobD7QONQHD7xRZqJE/b332Mo=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/renameio/v2 v2.0.2 h1:qKZs+tfn+arruZZhQ7TKC/ergJunuJicWS6gLDt/dGw=
github.com/google/renameio/v2 v2.0.2/go.mod h1:OX+G6WHHpHq3NVj7cAOleLOwJfcQ1s3uUJQCrr78SWo=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
github.com/landlock-lsm/go-landlock v0.0.0-20250303204525-1544bccde3a3 h1:zcMi8R8vP0WrrXlFMNUBpDy/ydo3sTnCcUPowq1XmSc=
github.com/landlock-lsm/go-landlock v0.0.0-20250303204525-1544bccde3a3/go.mod h1:RSub3ourNF8Hf+swvw49Catm3s7HVf4hzdFxDUnEzdA=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mmcloughlin/md4 v0.1.2 h1:kGYl+iNbxhyz4u76ka9a+0TXP9KWt/LmnM0QhZwhcBo=
github.com/mmcloughlin/md4 v0.1.2/go.mod h1:AAxFX59fddW0IguqNzWlf1lazh1+rXeIt/Bj49cqDTQ=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@ -83,6 +91,8 @@ golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts=
golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos=
golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo=
golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y=
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
@ -99,3 +109,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
jonasled.dev/jonasled/go-libs v0.0.3 h1:2Tg1pYJMufBMPkyYVVAvsUJe/R+TRRtRVW9MCbc24bM=
jonasled.dev/jonasled/go-libs v0.0.3/go.mod h1:E8Sev4obR0x6y7x0HaIN1tImeLnVJgwJXt++jzTwiFM=
kernel.org/pub/linux/libs/security/libcap/psx v1.2.70 h1:HsB2G/rEQiYyo1bGoQqHZ/Bvd6x1rERQTNdPr1FyWjI=
kernel.org/pub/linux/libs/security/libcap/psx v1.2.70/go.mod h1:+l6Ee2F59XiJ2I6WR5ObpC1utCQJZ/VLsEbQCD8RG24=

View file

@ -24,6 +24,14 @@ type Config struct {
MaxSearchResults int
// GinMode is passed to gin.SetMode ("release", "debug", "test").
GinMode string
// RsyncEnabled turns on an optional, read-only rsync daemon that mirrors
// RootDir alongside the HTTP server.
RsyncEnabled bool
// RsyncAddr is the address the rsync daemon listens on, e.g. ":873".
RsyncAddr string
// RsyncModuleName is the module name clients use, e.g.
// "rsync://host::<RsyncModuleName>".
RsyncModuleName string
}
func getEnv(key, fallback string) string {
@ -42,6 +50,15 @@ func getEnvInt(key string, fallback int) int {
return fallback
}
func getEnvBool(key string, fallback bool) bool {
if v, ok := os.LookupEnv(key); ok && v != "" {
if b, err := strconv.ParseBool(v); err == nil {
return b
}
}
return fallback
}
// Load builds a Config from environment variables, applying sane defaults.
func Load() Config {
refreshSeconds := getEnvInt("CACHE_REFRESH_INTERVAL_SECONDS", 30)
@ -54,5 +71,8 @@ func Load() Config {
CacheRefreshInterval: time.Duration(refreshSeconds) * time.Second,
MaxSearchResults: getEnvInt("MAX_SEARCH_RESULTS", 500),
GinMode: getEnv("GIN_MODE", "release"),
RsyncEnabled: getEnvBool("RSYNC_ENABLED", false),
RsyncAddr: getEnv("RSYNC_ADDR", ":873"),
RsyncModuleName: getEnv("RSYNC_MODULE_NAME", "share"),
}
}

View file

@ -0,0 +1,57 @@
// Package rsyncmirror optionally exposes the served directory as a
// read-only rsync daemon module, so it can be mirrored with a standard
// rsync client (e.g. `rsync -av rsync://host::share ./local/`).
package rsyncmirror
import (
"context"
"fmt"
"net"
"github.com/gokrazy/rsync/rsyncd"
"github.com/sirupsen/logrus"
"jonasled.dev/jonasled/pretty-directorylist/internal/config"
)
// logAdapter satisfies the small logger interface rsyncd expects, without
// this package needing to import rsyncd's internal log package.
type logAdapter struct {
entry *logrus.Entry
}
func (l logAdapter) Printf(msg string, a ...any) {
l.entry.Printf(msg, a...)
}
func (l logAdapter) Output(_ int, s string) error {
l.entry.Info(s)
return nil
}
// Serve starts the rsync daemon and blocks until ctx is cancelled or the
// listener fails. The module is always read-only: clients can only pull
// files out, never push into RootDir.
func Serve(ctx context.Context, cfg config.Config, logger *logrus.Logger) error {
module := rsyncd.Module{
Name: cfg.RsyncModuleName,
Path: cfg.RootDir,
Writable: false,
}
srv, err := rsyncd.NewServer([]rsyncd.Module{module}, rsyncd.WithLogger(logAdapter{logger.WithField("component", "rsyncd")}))
if err != nil {
return fmt.Errorf("rsyncmirror: creating server: %w", err)
}
ln, err := net.Listen("tcp", cfg.RsyncAddr)
if err != nil {
return fmt.Errorf("rsyncmirror: listening on %s: %w", cfg.RsyncAddr, err)
}
logger.WithFields(logrus.Fields{
"addr": ln.Addr().String(),
"module": cfg.RsyncModuleName,
}).Infof("rsync mirror listening; try: rsync -av rsync://%s/%s/ ./local/", ln.Addr(), cfg.RsyncModuleName)
return srv.Serve(ctx, ln)
}

12
main.go
View file

@ -1,6 +1,7 @@
package main
import (
"context"
"io/fs"
"os"
"os/signal"
@ -10,6 +11,7 @@ import (
"jonasled.dev/jonasled/go-libs/log"
"jonasled.dev/jonasled/pretty-directorylist/internal/config"
"jonasled.dev/jonasled/pretty-directorylist/internal/index"
"jonasled.dev/jonasled/pretty-directorylist/internal/rsyncmirror"
"jonasled.dev/jonasled/pretty-directorylist/internal/server"
"jonasled.dev/jonasled/pretty-directorylist/web"
)
@ -43,10 +45,20 @@ func main() {
}
}()
rsyncCtx, cancelRsync := context.WithCancel(context.Background())
if cfg.RsyncEnabled {
go func() {
if err := rsyncmirror.Serve(rsyncCtx, cfg, log.Log); err != nil && rsyncCtx.Err() == nil {
log.Log.WithError(err).Error("rsync mirror stopped unexpectedly")
}
}()
}
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
close(stop)
cancelRsync()
log.Log.Info("shutting down")
}