add rsync instructions to page

This commit is contained in:
Jonas Leder 2026-09-17 00:10:58 +02:00
commit b49be81bf2
7 changed files with 94 additions and 0 deletions

View file

@ -40,3 +40,9 @@ RSYNC_ADDR=:873
# Module name rsync clients use, e.g. rsync://host/<RSYNC_MODULE_NAME>.
RSYNC_MODULE_NAME=share
# Hostname shown to users in the "mirror with rsync" instructions on the
# page. Leave empty to use the hostname the browser used to reach the page
# (from the HTTP Host header) - set this if the rsync daemon is reachable
# under a different DNS name than the web UI.
RSYNC_PUBLIC_HOST=

View file

@ -45,6 +45,7 @@ automatically if present). See [.env.example](.env.example).
| `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>`. |
| `RSYNC_PUBLIC_HOST` | *(empty)* | Overrides the hostname shown in the on-page rsync instructions. Defaults to the request's `Host` header. |
## Running locally
@ -88,6 +89,16 @@ Then, from any machine with `rsync` installed:
rsync -av rsync://host:8873/share/ ./local-mirror/
```
When enabled, the web UI also shows this command directly on the page (via
`GET /api/stats`), using the hostname from the incoming request's `Host`
header by default. If the rsync daemon is reachable under a different DNS
name than the web UI (e.g. a separate internal hostname or load balancer),
override it with `RSYNC_PUBLIC_HOST`:
```sh
RSYNC_ENABLED=true RSYNC_ADDR=:8873 RSYNC_PUBLIC_HOST=files.example.com go run .
```
With Docker, publish the rsync port too:
```sh

View file

@ -34,6 +34,11 @@ type Config struct {
// RsyncModuleName is the module name clients use, e.g.
// "rsync://host::<RsyncModuleName>".
RsyncModuleName string
// RsyncPublicHost, if set, overrides the hostname shown to users in the
// rsync instructions (e.g. when the daemon is reachable under a
// different DNS name than the HTTP request's Host header). If empty,
// the hostname from the incoming HTTP request is used instead.
RsyncPublicHost string
}
func getEnv(key, fallback string) string {
@ -77,5 +82,6 @@ func Load() Config {
RsyncEnabled: getEnvBool("RSYNC_ENABLED", false),
RsyncAddr: getEnv("RSYNC_ADDR", ":873"),
RsyncModuleName: getEnv("RSYNC_MODULE_NAME", "share"),
RsyncPublicHost: getEnv("RSYNC_PUBLIC_HOST", ""),
}
}

View file

@ -1,6 +1,7 @@
package server
import (
"net"
"net/http"
"os"
"path/filepath"
@ -73,9 +74,45 @@ func (s *Server) handleStats(c *gin.Context) {
"fileCount": fileCount,
"totalSize": totalSize,
"indexedAt": builtAt,
"rsync": s.rsyncInfo(c),
})
}
// rsyncInfo builds the data the frontend needs to show a "how to mirror
// this with rsync" note, if the rsync daemon is enabled.
func (s *Server) rsyncInfo(c *gin.Context) gin.H {
if !s.cfg.RsyncEnabled {
return gin.H{"enabled": false}
}
host := s.cfg.RsyncPublicHost
if host == "" {
host = requestHostname(c.Request.Host)
}
_, port, err := net.SplitHostPort(s.cfg.RsyncAddr)
if err != nil {
// RsyncAddr had no host part to split off, e.g. just "873".
port = strings.TrimPrefix(s.cfg.RsyncAddr, ":")
}
return gin.H{
"enabled": true,
"host": host,
"port": port,
"module": s.cfg.RsyncModuleName,
"command": "rsync -av rsync://" + host + ":" + port + "/" + s.cfg.RsyncModuleName + "/ ./local/",
}
}
// requestHostname strips a ":port" suffix from an HTTP Host header.
func requestHostname(host string) string {
if h, _, err := net.SplitHostPort(host); err == nil {
return h
}
return host
}
// handleServeFile streams a file straight from disk via http.ServeContent,
// which handles Range/If-Modified-Since and never buffers the whole file
// in memory - it reads directly from the *os.File.

View file

@ -16,6 +16,7 @@
emptyState: document.getElementById("empty-state"),
statFileCount: document.getElementById("stat-filecount"),
statTotalSize: document.getElementById("stat-totalsize"),
rsyncNote: document.getElementById("rsync-note"),
};
function humanSize(bytes, decimals) {
@ -136,10 +137,24 @@
renderRows(sortEntries(entries), { showFullPath: state.searchMode });
}
function renderRsyncNote(rsync) {
if (!rsync || !rsync.enabled) {
els.rsyncNote.hidden = true;
return;
}
els.rsyncNote.innerHTML = "";
els.rsyncNote.appendChild(document.createTextNode("Mirror this share with rsync: "));
var code = document.createElement("code");
code.textContent = rsync.command;
els.rsyncNote.appendChild(code);
els.rsyncNote.hidden = false;
}
function loadStats() {
fetch(apiUrl("/api/stats")).then(function (r) { return r.json(); }).then(function (data) {
els.statFileCount.textContent = data.fileCount;
els.statTotalSize.textContent = humanSize(data.totalSize);
renderRsyncNote(data.rsync);
}).catch(function () {});
}

View file

@ -119,6 +119,24 @@ p {
text-align: center;
}
.rsync-note {
background-color: #1e1e1e;
border: 1px solid #3a3a3a;
border-radius: 4px;
padding: 10px 14px;
margin-bottom: 10px;
color: #bbbbbb;
word-break: break-word;
}
.rsync-note code {
color: #ffffff;
background-color: #2b2b2b;
padding: 2px 6px;
border-radius: 3px;
font-family: 'Courier New', monospace;
}
tbody tr:hover {
background-color: #3a3a3a;
}

View file

@ -20,6 +20,7 @@
Total size: <span id="stat-totalsize">-</span>
<br>
</p>
<p id="rsync-note" class="rsync-note" hidden></p>
<nav id="breadcrumbs" class="breadcrumbs"></nav>
<div>
<input type="text" id="search" placeholder="Search across all folders..." aria-label="Search across all folders">