mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-24 15:06:17 +01:00
Compare commits
2 Commits
402fc2c453
...
36dd26b014
| Author | SHA1 | Date | |
|---|---|---|---|
| 36dd26b014 | |||
| 20bc1aeb02 |
4
.gitignore
vendored
4
.gitignore
vendored
@ -3,6 +3,10 @@
|
||||
# Sensitive — must NEVER be committed (contains regex of home GPS coordinates etc.)
|
||||
.secret-patterns
|
||||
|
||||
# dufs cloud secret config — holds a plaintext web password; never commit anywhere.
|
||||
**/.dufs_cloud.conf
|
||||
.dufs_cloud.conf
|
||||
|
||||
# Compiled output
|
||||
/dist
|
||||
/tmp
|
||||
|
||||
@ -448,6 +448,13 @@ repos:
|
||||
language: script
|
||||
exclude: ^(\.secret-patterns|\.pre-commit-config\.yaml|.*\.geojson)$
|
||||
|
||||
- id: check-no-dufs-conf
|
||||
name: block dufs secret config (.dufs_cloud.conf)
|
||||
entry: meta/scripts/check_no_dufs_conf.sh
|
||||
language: script
|
||||
always_run: true
|
||||
pass_filenames: false
|
||||
|
||||
# ===========================================================================
|
||||
# COMMITIZEN - Conventional commits (optional)
|
||||
# ===========================================================================
|
||||
|
||||
402
docs/explanations/2026-07-12-explanation-dufs-cloud.html
Normal file
402
docs/explanations/2026-07-12-explanation-dufs-cloud.html
Normal file
@ -0,0 +1,402 @@
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Self-hosted dufs cloud + KeePass WebDAV sync — explained</title>
|
||||
<style>
|
||||
:root {
|
||||
--bg: #ffffff; --fg: #1c1f24; --muted: #5b6570; --card: #f6f8fa;
|
||||
--border: #d8dee4; --accent: #2f6feb; --accent-soft: #dbe7ff;
|
||||
--good: #1a7f37; --good-soft: #d7f5df; --bad: #cf222e; --bad-soft: #ffe3e3;
|
||||
--code: #0b1220; --codefg: #e6edf3; --kbd: #eaeef2;
|
||||
}
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--bg: #0d1117; --fg: #e6edf3; --muted: #9198a1; --card: #161b22;
|
||||
--border: #30363d; --accent: #589bff; --accent-soft: #17253f;
|
||||
--good: #3fb950; --good-soft: #10251a; --bad: #f85149; --bad-soft: #2a1416;
|
||||
--code: #010409; --codefg: #e6edf3; --kbd: #21262d;
|
||||
}
|
||||
}
|
||||
:root[data-theme="dark"] {
|
||||
--bg: #0d1117; --fg: #e6edf3; --muted: #9198a1; --card: #161b22;
|
||||
--border: #30363d; --accent: #589bff; --accent-soft: #17253f;
|
||||
--good: #3fb950; --good-soft: #10251a; --bad: #f85149; --bad-soft: #2a1416;
|
||||
--code: #010409; --codefg: #e6edf3; --kbd: #21262d;
|
||||
}
|
||||
:root[data-theme="light"] {
|
||||
--bg: #ffffff; --fg: #1c1f24; --muted: #5b6570; --card: #f6f8fa;
|
||||
--border: #d8dee4; --accent: #2f6feb; --accent-soft: #dbe7ff;
|
||||
--good: #1a7f37; --good-soft: #d7f5df; --bad: #cf222e; --bad-soft: #ffe3e3;
|
||||
--code: #0b1220; --codefg: #e6edf3; --kbd: #eaeef2;
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
body {
|
||||
margin: 0; background: var(--bg); color: var(--fg);
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
||||
line-height: 1.65; font-size: 17px;
|
||||
}
|
||||
.wrap { max-width: min(48rem, 92vw); margin: 0 auto; padding: 2.2rem 0 5rem; }
|
||||
h1 { font-size: 1.9rem; line-height: 1.2; margin: 0 0 .3rem; }
|
||||
h2 { font-size: 1.4rem; margin: 2.6rem 0 .8rem; padding-top: .6rem; border-top: 1px solid var(--border); }
|
||||
h3 { font-size: 1.08rem; margin: 1.6rem 0 .5rem; }
|
||||
p, li { color: var(--fg); }
|
||||
.sub { color: var(--muted); font-size: 1.02rem; margin-top: .2rem; }
|
||||
a { color: var(--accent); text-decoration: none; }
|
||||
a:hover { text-decoration: underline; }
|
||||
code.inl { background: var(--kbd); padding: .1em .38em; border-radius: 5px; font-size: .88em;
|
||||
font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace; }
|
||||
pre { background: var(--code); color: var(--codefg); padding: 1rem 1.1rem; border-radius: 10px;
|
||||
overflow-x: auto; white-space: pre-wrap; word-break: break-word; font-size: .84rem; line-height: 1.55;
|
||||
font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace; }
|
||||
pre .c { color: #8b98a5; }
|
||||
pre .k { color: #ff9d6b; }
|
||||
pre .g { color: #6fd08c; }
|
||||
pre .r { color: #ff8080; }
|
||||
.toc { background: var(--card); border: 1px solid var(--border); border-radius: 12px;
|
||||
padding: 1rem 1.2rem; margin: 1.6rem 0; }
|
||||
.toc ol { margin: .3rem 0 0; padding-left: 1.2rem; }
|
||||
.toc li { margin: .25rem 0; }
|
||||
.card { background: var(--card); border: 1px solid var(--border); border-radius: 12px;
|
||||
padding: 1.1rem 1.2rem; margin: 1.2rem 0; }
|
||||
.note { border-left: 4px solid var(--accent); background: var(--accent-soft); border-radius: 0 8px 8px 0;
|
||||
padding: .7rem 1rem; margin: 1.1rem 0; }
|
||||
.tag { display: inline-block; font-size: .72rem; font-weight: 600; letter-spacing: .02em;
|
||||
padding: .12em .5em; border-radius: 999px; background: var(--accent-soft); color: var(--accent);
|
||||
border: 1px solid var(--border); vertical-align: middle; }
|
||||
/* diagram */
|
||||
.flow { display: flex; flex-wrap: wrap; align-items: stretch; gap: .5rem; margin: 1rem 0; }
|
||||
.node { flex: 1 1 130px; min-width: 120px; background: var(--bg); border: 1px solid var(--border);
|
||||
border-radius: 10px; padding: .7rem .6rem; text-align: center; }
|
||||
.node b { display: block; font-size: .92rem; }
|
||||
.node span { display: block; color: var(--muted); font-size: .76rem; margin-top: .15rem; }
|
||||
.arrow { align-self: center; color: var(--accent); font-weight: 700; font-size: 1.3rem; }
|
||||
.lock { border-color: var(--good); }
|
||||
.pub { border-color: var(--accent); }
|
||||
.layers { display: grid; gap: .5rem; margin: 1rem 0; }
|
||||
.layer { display: flex; gap: .7rem; align-items: flex-start; background: var(--bg);
|
||||
border: 1px solid var(--border); border-left: 4px solid var(--good); border-radius: 8px; padding: .6rem .8rem; }
|
||||
.layer .n { flex: 0 0 1.6rem; height: 1.6rem; border-radius: 50%; background: var(--good-soft); color: var(--good);
|
||||
display: grid; place-items: center; font-weight: 700; font-size: .85rem; }
|
||||
.layer .t b { font-size: .95rem; }
|
||||
.layer .t span { display: block; color: var(--muted); font-size: .82rem; }
|
||||
/* micro world */
|
||||
.mw { border: 1px solid var(--border); border-radius: 12px; padding: 1.1rem 1.2rem; background: var(--card); }
|
||||
.mw input { width: 100%; padding: .6rem .7rem; border-radius: 8px; border: 1px solid var(--border);
|
||||
background: var(--bg); color: var(--fg); font-family: "SF Mono", ui-monospace, Menlo, monospace; font-size: .9rem; }
|
||||
.mw .verdict { margin-top: .8rem; padding: .7rem .9rem; border-radius: 8px; font-weight: 600; }
|
||||
.mw .ok { background: var(--good-soft); color: var(--good); border: 1px solid var(--good); }
|
||||
.mw .no { background: var(--bad-soft); color: var(--bad); border: 1px solid var(--bad); }
|
||||
.mw .why { font-weight: 400; color: var(--muted); font-size: .85rem; margin-top: .3rem; display:block; }
|
||||
.chips { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .7rem; }
|
||||
.chip { font-size: .78rem; padding: .28em .6em; border-radius: 999px; border: 1px solid var(--border);
|
||||
background: var(--bg); color: var(--fg); cursor: pointer; font-family: "SF Mono", ui-monospace, monospace; }
|
||||
.chip:hover { border-color: var(--accent); color: var(--accent); }
|
||||
/* quiz */
|
||||
.q { margin: 1.3rem 0; }
|
||||
.q .stem { font-weight: 600; margin-bottom: .5rem; }
|
||||
.opt { display: block; width: 100%; text-align: left; background: var(--bg); color: var(--fg);
|
||||
border: 1px solid var(--border); border-radius: 9px; padding: .6rem .8rem; margin: .4rem 0; cursor: pointer;
|
||||
font-size: .95rem; transition: background .12s, border-color .12s; }
|
||||
.opt:hover { border-color: var(--accent); }
|
||||
.opt.correct { background: var(--good-soft); border-color: var(--good); }
|
||||
.opt.wrong { background: var(--bad-soft); border-color: var(--bad); }
|
||||
.opt:disabled { cursor: default; }
|
||||
.rat { font-size: .85rem; color: var(--muted); margin: .3rem 0 0; padding-left: .2rem; display: none; }
|
||||
.rat.show { display: block; }
|
||||
.score { position: sticky; top: 0; background: var(--bg); border-bottom: 1px solid var(--border);
|
||||
padding: .5rem 0; font-size: .9rem; color: var(--muted); z-index: 5; }
|
||||
.toggle { float: right; font-size: .8rem; cursor: pointer; color: var(--accent); border: 1px solid var(--border);
|
||||
border-radius: 999px; padding: .1em .7em; background: var(--card); }
|
||||
footer { margin-top: 3rem; color: var(--muted); font-size: .84rem; border-top: 1px solid var(--border); padding-top: 1rem; }
|
||||
</style>
|
||||
|
||||
<div class="wrap">
|
||||
<span class="toggle" onclick="(function(){var r=document.documentElement;var d=(r.getAttribute('data-theme')|| (matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light'));r.setAttribute('data-theme', d==='dark'?'light':'dark');})()">◐ theme</span>
|
||||
<h1>A self-hosted “cloud drive” in one Rust binary</h1>
|
||||
<p class="sub">How commit <code class="inl">20bc1ae</code> stands up <b>dufs</b> behind the existing Caddy/DuckDNS stack, auto-syncs the KeePassXC vault over WebDAV, and wraps the one secret it needs in six layers of paranoia.</p>
|
||||
|
||||
<div class="toc">
|
||||
<b>Contents</b>
|
||||
<ol>
|
||||
<li><a href="#background">Background — the pieces already on the machine</a></li>
|
||||
<li><a href="#intuition">Intuition — what changed and why</a></li>
|
||||
<li><a href="#code">Code — a walkthrough by concern</a></li>
|
||||
<li><a href="#playground">Playground — the serve-dir safety guard</a></li>
|
||||
<li><a href="#quiz">Quiz — check your understanding</a></li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
<h2 id="background">1 · Background</h2>
|
||||
<p>This PC already runs a small fleet of self-hosted services, and they all share one exposure pattern. Understanding that pattern is 80% of understanding this change.</p>
|
||||
|
||||
<h3>The exposure stack (already in place)</h3>
|
||||
<ul>
|
||||
<li><b>Docker + Caddy</b> — one <code class="inl">caddy:2.8</code> container (<code class="inl">gitea-caddy</code>) is the <i>only</i> thing bound to public ports 80/443. It terminates TLS and gets Let’s Encrypt certificates automatically.</li>
|
||||
<li><b>DuckDNS</b> — a cron job every 5 minutes keeps <code class="inl">kuhy.duckdns.org</code> pointed at the home IP. One account/token, and you can add more subdomain labels to it.</li>
|
||||
<li><b>nftables</b> — default-drop firewall; ports 80/443 are opened by a flag inside <code class="inl">setup_wireguard_ssh.sh allow-web</code>.</li>
|
||||
<li>A subtle gotcha the stack already solved: the nftables <code class="inl">forward</code> chain is default-drop, which silently kills Docker <i>bridge</i> networking. Every public container therefore runs with <code class="inl">network_mode: host</code> and binds its app to <code class="inl">127.0.0.1</code>. Gitea is the reference implementation.</li>
|
||||
</ul>
|
||||
|
||||
<h3>The two things this change adds a home for</h3>
|
||||
<ul>
|
||||
<li><b>A file store</b> reachable from a phone on mobile data — the “Google Drive alternative”.</li>
|
||||
<li><b>The KeePassXC vault</b>: a single encrypted file, <code class="inl">~/Keepass/Passwords.kdbx</code>. KeePass has no built-in cloud sync by design — you choose where the file lives.</li>
|
||||
</ul>
|
||||
|
||||
<div class="note"><b>dufs</b> is the chosen tool: a single ~2.5 MB Rust binary that serves a <i>real filesystem folder</i> over HTTP with a web UI <b>and</b> built-in WebDAV. Because it serves the folder directly, an <code class="inl">rsync</code> (or any local write) into that folder shows up instantly — no database, no re-scan step. Contrast Seafile, which stores files in an internal block database that <code class="inl">rsync</code> cannot write into.</p></div>
|
||||
|
||||
<h2 id="intuition">2 · Intuition</h2>
|
||||
<p>Nothing here reinvents exposure. dufs slots into the existing Caddy pattern exactly like Gitea did — a new loopback app, a new Caddy block, a new DuckDNS label:</p>
|
||||
|
||||
<div class="card">
|
||||
<div class="flow">
|
||||
<div class="node pub"><b>📱 Phone</b><span>on cellular</span></div>
|
||||
<div class="arrow">→</div>
|
||||
<div class="node pub"><b>Caddy</b><span>:443 HTTPS<br>kuhy-cloud.duckdns.org</span></div>
|
||||
<div class="arrow">→</div>
|
||||
<div class="node lock"><b>dufs</b><span>127.0.0.1:5000<br>auth required</span></div>
|
||||
<div class="arrow">→</div>
|
||||
<div class="node lock"><b>~/cloud</b><span>real folder</span></div>
|
||||
</div>
|
||||
<p style="margin:.4rem 0 0; color:var(--muted); font-size:.85rem">Only Caddy is public. dufs is unreachable except through it, and it refuses every request without the right login.</p>
|
||||
</div>
|
||||
|
||||
<h3>The vault, kept fresh automatically</h3>
|
||||
<p>KeePassXC keeps writing <code class="inl">Passwords.kdbx</code> where it always has. A <b>systemd path unit</b> watches that one file; the instant it changes, a oneshot service <code class="inl">rsync</code>s it into the served folder. No polling loop, no cron — the kernel wakes the unit only on a real write.</p>
|
||||
|
||||
<div class="card">
|
||||
<div class="flow">
|
||||
<div class="node"><b>KeePassXC</b><span>saves the vault</span></div>
|
||||
<div class="arrow">→</div>
|
||||
<div class="node"><b>.path unit</b><span>PathModified fires</span></div>
|
||||
<div class="arrow">→</div>
|
||||
<div class="node"><b>rsync</b><span>only the .kdbx</span></div>
|
||||
<div class="arrow">→</div>
|
||||
<div class="node"><b>~/cloud/Keepass</b><span>phone pulls via WebDAV</span></div>
|
||||
</div>
|
||||
</div>
|
||||
<p>On the phone, <b>KeePassDX</b> opens the database straight from the WebDAV URL — so the “mobile app” requirement is met with zero custom code.</p>
|
||||
|
||||
<h3>The paranoid part: one secret, six fences around it</h3>
|
||||
<p>To keep re-runs non-interactive, the installer caches the web password in <code class="inl">.dufs_cloud.conf</code>. That plaintext file must <i>never</i> reach the repo or the served folder. The change surrounds it with defense-in-depth so that a single mistake can’t leak it:</p>
|
||||
|
||||
<div class="layers">
|
||||
<div class="layer"><div class="n">1</div><div class="t"><b>Gitignored three ways</b><span>repo root, <code class="inl">linux_configuration/</code>, and a <code class="inl">**/</code> glob</span></div></div>
|
||||
<div class="layer"><div class="n">2</div><div class="t"><b>Pre-commit backstop</b><span>a hook inspects the staged set and blocks the file even after <code class="inl">git add -f</code></span></div></div>
|
||||
<div class="layer"><div class="n">3</div><div class="t"><b>Runtime refusal</b><span>the installer runs <code class="inl">git check-ignore</code> first and aborts rather than write the secret to a non-ignored path</span></div></div>
|
||||
<div class="layer"><div class="n">4</div><div class="t"><b>No argv leak</b><span>the password is hashed via <code class="inl">openssl passwd -6 -stdin</code>, never passed as a command argument visible in <code class="inl">ps</code></span></div></div>
|
||||
<div class="layer"><div class="n">5</div><div class="t"><b>Serve-dir guard</b><span>dufs refuses to serve <code class="inl">$HOME</code>, <code class="inl">.ssh</code>, <code class="inl">.config</code>, <code class="inl">.gnupg</code>, the repo, or <code class="inl">/</code></span></div></div>
|
||||
<div class="layer"><div class="n">6</div><div class="t"><b>Minimal exposure</b><span>only <code class="inl">Passwords.kdbx</code> is mirrored — the local timestamped <code class="inl">.backup_*</code> vaults stay off the public server</span></div></div>
|
||||
</div>
|
||||
|
||||
<h2 id="code">3 · Code</h2>
|
||||
<p>Nine files, grouped by what they’re for.</p>
|
||||
|
||||
<h3><span class="tag">installer</span> The idempotent flow</h3>
|
||||
<p><code class="inl">setup_dufs_cloud.sh</code> runs as the normal user and uses <code class="inl">sudo</code> only where it must (binary install, systemd, firewall). Its <code class="inl">main</code> reads like the plan:</p>
|
||||
<pre><span class="c"># pinned + checksum-verified — no "curl | sh" trust</span>
|
||||
<span class="k">readonly</span> DUFS_VERSION="0.46.0"
|
||||
<span class="k">readonly</span> DUFS_SHA256="817769f7…410977"
|
||||
…
|
||||
main() {
|
||||
preflight <span class="c"># deps + require the Caddy/DuckDNS stack to already exist</span>
|
||||
load_config; prompt_config; save_config
|
||||
install_dufs <span class="c"># download, verify sha256, install -m0755</span>
|
||||
create_serve_dir
|
||||
write_dufs_config <span class="c"># hashed auth, bind 127.0.0.1, no anonymous</span>
|
||||
install_units <span class="c"># render placeholders → /etc/systemd/system</span>
|
||||
configure_caddy <span class="c"># append a block, reload gitea-caddy</span>
|
||||
configure_duckdns <span class="c"># extend the EXISTING updater, no new cron</span>
|
||||
ensure_firewall <span class="c"># allow-web only if 80/443 not already open</span>
|
||||
setup_autosync <span class="c"># initial mirror + enable the .path watcher</span>
|
||||
}</pre>
|
||||
|
||||
<h3><span class="tag">reuse</span> Slotting into the existing stack</h3>
|
||||
<p>The change deliberately edits shared infrastructure in place rather than duplicating it. The Caddy block is appended with an idempotency guard and the proxy is reloaded live:</p>
|
||||
<pre><span class="k">if</span> <span class="k">grep</span> -qE "^${DUFS_SUBDOMAIN} \{" "$CADDYFILE"; <span class="k">then</span> … <span class="c"># already present</span>
|
||||
<span class="k">else</span> printf '\n%s {\n\treverse_proxy 127.0.0.1:%s\n}\n' "$DUFS_SUBDOMAIN" "$DUFS_PORT" >> "$CADDYFILE"; <span class="k">fi</span>
|
||||
docker exec gitea-caddy caddy reload --config /etc/caddy/Caddyfile</pre>
|
||||
<p>DuckDNS gets the new label spliced into the one running updater’s <code class="inl">domains=</code> list (token untouched) — never a second cron:</p>
|
||||
<pre><span class="k">sed</span> -i -E "s|(domains=)([^&\"']*)|\1\2,${label}|" "$DUCKDNS_UPDATER"</pre>
|
||||
|
||||
<h3><span class="tag">systemd</span> Event-driven sync, not polling</h3>
|
||||
<p>Three tiny unit templates carry placeholders (<code class="inl">__USER__</code>, <code class="inl">__HOME__</code>, <code class="inl">__CLOUD_ROOT__</code>) that the installer substitutes. The watcher is the interesting one:</p>
|
||||
<pre><span class="c"># keepass-cloud-sync.path</span>
|
||||
[Path]
|
||||
PathModified=__HOME__/Keepass/Passwords.kdbx
|
||||
Unit=keepass-cloud-sync.service
|
||||
|
||||
<span class="c"># keepass-cloud-sync.service (oneshot) — only the vault file</span>
|
||||
ExecStart=/usr/bin/rsync -a __HOME__/Keepass/Passwords.kdbx __CLOUD_ROOT__/Keepass/</pre>
|
||||
|
||||
<h3><span class="tag">hardening</span> The runtime secret guards</h3>
|
||||
<p>Two functions make leaking the password structurally hard. First, refuse to persist it unless git is <i>certain</i> it’s ignored:</p>
|
||||
<pre>assert_config_gitignored() {
|
||||
[[ -n $REPO_ROOT ]] || die "Not in a git repo — refusing to write the secret."
|
||||
<span class="k">if</span> ! git -C "$REPO_ROOT" check-ignore -q "$CONFIG_FILE"; <span class="k">then</span>
|
||||
die "Refusing to write $CONFIG_FILE: it is NOT gitignored."
|
||||
<span class="k">fi</span>
|
||||
}</pre>
|
||||
<p>Second, never let dufs serve a directory that would expose secrets (this is the logic you can play with below):</p>
|
||||
<pre>case "$resolved" <span class="k">in</span>
|
||||
"$home_r" | / | "$home_r/.ssh"* | "$home_r/.config"* | "$home_r/.gnupg"*)
|
||||
die "Unsafe cloud folder — it would expose sensitive files." ;;
|
||||
<span class="k">esac</span>
|
||||
<span class="k">if</span> [[ $resolved == "$REPO_ROOT"* ]]; <span class="k">then</span> die "…inside the git repo"; <span class="k">fi</span></pre>
|
||||
|
||||
<h3><span class="tag">hook</span> Blocking the file even against <code class="inl">git add -f</code></h3>
|
||||
<p><code class="inl">check_no_dufs_conf.sh</code> reads the staged set directly, so a force-added conf is caught regardless of any file filter:</p>
|
||||
<pre>staged="$(git diff --cached --name-only --diff-filter=ACM)"
|
||||
bad="$(printf '%s\n' "$staged" | grep -E '(^|/)\.dufs_cloud\.conf$' || true)"
|
||||
[ -n "$bad" ] && { echo "ERROR: refusing to commit the dufs secret config" >&2; exit 1; }</pre>
|
||||
|
||||
<h2 id="playground">4 · Playground — the serve-dir guard</h2>
|
||||
<p>Type a candidate <code class="inl">CLOUD_ROOT</code> and see the exact verdict <code class="inl">assert_cloud_root_safe</code> would give. (Assumes <code class="inl">$HOME=/home/kuhy</code>, repo <code class="inl">/home/kuhy/testsAndMisc</code>. Paths are resolved like <code class="inl">readlink -m</code>, so <code class="inl">..</code> collapses.)</p>
|
||||
<div class="mw">
|
||||
<input id="mwIn" value="/home/kuhy/cloud" spellcheck="false" oninput="mwEval()">
|
||||
<div id="mwOut" class="verdict"></div>
|
||||
<div class="chips">
|
||||
<span class="chip" onclick="mwSet('/home/kuhy/cloud')">~/cloud</span>
|
||||
<span class="chip" onclick="mwSet('/home/kuhy')">~ (home)</span>
|
||||
<span class="chip" onclick="mwSet('/home/kuhy/.ssh')">~/.ssh</span>
|
||||
<span class="chip" onclick="mwSet('/home/kuhy/.config/dufs')">~/.config/dufs</span>
|
||||
<span class="chip" onclick="mwSet('/home/kuhy/testsAndMisc/x')">inside the repo</span>
|
||||
<span class="chip" onclick="mwSet('/srv/cloud/../../home/kuhy/.gnupg')">traversal → .gnupg</span>
|
||||
<span class="chip" onclick="mwSet('/srv/cloud')">/srv/cloud</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2 id="quiz">5 · Quiz</h2>
|
||||
<div class="score" id="score">Score: 0 / 0</div>
|
||||
<div id="quiz"></div>
|
||||
|
||||
<footer>
|
||||
Generated for commit <code class="inl">20bc1ae</code> · <code class="inl">linux_configuration: self-hosted dufs cloud storage + KeePass WebDAV auto-sync</code>.
|
||||
An <a href="https://www.geoffreylitt.com/2024/06/03/craft-durable-software.html">explain-diff</a> doc — understanding is the bottleneck.
|
||||
</footer>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// ---- Micro-world: mirror of assert_cloud_root_safe ----
|
||||
function resolve(p){ // approximate readlink -m: collapse . and ..
|
||||
var parts = p.split('/'), out = [];
|
||||
for (var i=0;i<parts.length;i++){ var s=parts[i];
|
||||
if(s===''||s==='.') continue;
|
||||
if(s==='..'){ out.pop(); continue; }
|
||||
out.push(s);
|
||||
}
|
||||
return '/' + out.join('/');
|
||||
}
|
||||
function guard(cloudRoot){
|
||||
// Mirrors assert_cloud_root_safe: HOME and / are blocked ONLY as an exact
|
||||
// match (subdirs of HOME like ~/cloud are fine); .ssh/.config/.gnupg and
|
||||
// the repo are blocked as exact-or-prefix.
|
||||
var HOME='/home/kuhy', REPO='/home/kuhy/testsAndMisc';
|
||||
var r = resolve(cloudRoot);
|
||||
if (r===HOME) return {ok:false, why:'Resolves to '+r+' — that is $HOME itself; serving it would expose everything.'};
|
||||
if (r==='/') return {ok:false, why:'Resolves to / — the whole filesystem. Refused.'};
|
||||
var pfx = [HOME+'/.ssh', HOME+'/.config', HOME+'/.gnupg'];
|
||||
for (var i=0;i<pfx.length;i++){
|
||||
var b = pfx[i];
|
||||
if (r===b || r.indexOf(b+'/')===0)
|
||||
return {ok:false, why:'Resolves to '+r+' — under '+b+' (keys/config/secrets). Refused.'};
|
||||
}
|
||||
if (r===REPO || r.indexOf(REPO+'/')===0)
|
||||
return {ok:false, why:'Resolves to '+r+' — inside the git repo, which holds the gitignored secret .conf. Refused.'};
|
||||
return {ok:true, why:'Resolves to '+r+' — a dedicated folder, safe to serve.'};
|
||||
}
|
||||
function mwEval(){
|
||||
var v = document.getElementById('mwIn').value.trim();
|
||||
var out = document.getElementById('mwOut');
|
||||
if(!v){ out.className='verdict'; out.textContent=''; return; }
|
||||
var g = guard(v);
|
||||
out.className = 'verdict ' + (g.ok?'ok':'no');
|
||||
out.innerHTML = (g.ok?'✅ ALLOW':'⛔ REJECT') + '<span class="why">'+g.why+'</span>';
|
||||
}
|
||||
function mwSet(v){ document.getElementById('mwIn').value=v; mwEval(); }
|
||||
mwEval();
|
||||
|
||||
// ---- Quiz ----
|
||||
// correctIndex chosen to span positions C,A,D,B,C and to never be the longest option.
|
||||
var QS = [
|
||||
{
|
||||
q: "Why does dufs listen on 127.0.0.1 instead of 0.0.0.0?",
|
||||
opts: [
|
||||
"Because dufs is technically unable to bind to any publicly routable network interface at all.",
|
||||
"Because DuckDNS only forwards traffic that originates from loopback addresses.",
|
||||
"So Caddy is the only public entry point; dufs can't be reached except through TLS + auth.",
|
||||
"Because the nftables firewall only permits services that bind to loopback ports."
|
||||
],
|
||||
correct: 2,
|
||||
rat: "Loopback binding + a host-network Caddy means the only way in is the reverse proxy, which enforces HTTPS and login."
|
||||
},
|
||||
{
|
||||
q: "Why does serving a real folder (dufs) matter for the rsync requirement, versus something like Seafile?",
|
||||
opts: [
|
||||
"dufs serves the filesystem directly, so an rsync write shows up immediately with no re-scan.",
|
||||
"dufs compresses uploads, which makes rsync transfers finish roughly two to three times faster overall.",
|
||||
"dufs keeps a separate index database that rsync updates automatically on each run.",
|
||||
"dufs disables WebDAV whenever rsync is active to avoid corrupting concurrent writes."
|
||||
],
|
||||
correct: 0,
|
||||
rat: "Seafile stores files in an internal block DB rsync can't write into; dufs serves the folder as-is."
|
||||
},
|
||||
{
|
||||
q: "What makes the KeePass mirror event-driven rather than a polling loop?",
|
||||
opts: [
|
||||
"A cron entry re-runs the rsync once every minute to catch recent edits.",
|
||||
"The installer leaves an inotifywait polling loop running in the background after it exits.",
|
||||
"dufs itself watches the vault file and copies it whenever a client connects.",
|
||||
"A systemd .path unit fires the sync service only when Passwords.kdbx is modified."
|
||||
],
|
||||
correct: 3,
|
||||
rat: "PathModified wakes the oneshot service on a real write — no per-second forks."
|
||||
},
|
||||
{
|
||||
q: "Which guard prevents the installer from ever writing the plaintext password to a non-ignored location?",
|
||||
opts: [
|
||||
"The 0600 file permissions set with umask 077 before the write.",
|
||||
"A git check-ignore call that aborts the install if the conf isn't ignored.",
|
||||
"The three .gitignore entries covering the conf across the repo.",
|
||||
"The pre-commit hook that scans the staged file set for the secret config's filename."
|
||||
],
|
||||
correct: 1,
|
||||
rat: "assert_config_gitignored refuses to persist the secret unless git confirms it's ignored — a runtime, fail-closed check."
|
||||
},
|
||||
{
|
||||
q: "Why does the sync copy only Passwords.kdbx instead of the whole ~/Keepass directory?",
|
||||
opts: [
|
||||
"Because rsync cannot recurse into directories that contain hidden dot-folders.",
|
||||
"Because copying the directory would exceed dufs's per-file upload size limit.",
|
||||
"To keep the local timestamped .backup_* vault copies off the public server.",
|
||||
"Because KeePassDX refuses to open a database served from a multi-file folder."
|
||||
],
|
||||
correct: 2,
|
||||
rat: "The vault is what the phone needs; the local .backup_* dirs shouldn't be exposed, encrypted or not."
|
||||
}
|
||||
];
|
||||
var answered = 0, correct = 0;
|
||||
var quiz = document.getElementById('quiz');
|
||||
QS.forEach(function(item, qi){
|
||||
var wrap = document.createElement('div'); wrap.className='q';
|
||||
var stem = document.createElement('div'); stem.className='stem';
|
||||
stem.textContent = (qi+1)+'. '+item.q; wrap.appendChild(stem);
|
||||
var rat = document.createElement('p'); rat.className='rat'; rat.textContent='→ '+item.rat;
|
||||
item.opts.forEach(function(text, oi){
|
||||
var b = document.createElement('button'); b.className='opt'; b.textContent=text;
|
||||
b.onclick = function(){
|
||||
if (wrap.dataset.done) return;
|
||||
wrap.dataset.done = '1'; answered++;
|
||||
var buttons = wrap.querySelectorAll('.opt');
|
||||
buttons.forEach(function(bb){ bb.disabled=true; });
|
||||
buttons[item.correct].classList.add('correct');
|
||||
if (oi === item.correct){ correct++; }
|
||||
else { b.classList.add('wrong'); }
|
||||
rat.classList.add('show');
|
||||
document.getElementById('score').textContent = 'Score: '+correct+' / '+answered;
|
||||
};
|
||||
wrap.appendChild(b);
|
||||
});
|
||||
wrap.appendChild(rat);
|
||||
quiz.appendChild(wrap);
|
||||
});
|
||||
</script>
|
||||
@ -30,8 +30,12 @@
|
||||
<div class="wrap">
|
||||
<div class="kicker">explanations</div>
|
||||
<h1>Change explanations</h1>
|
||||
<p class="lead">1 explanation · interactive walkthroughs of notable changes, newest first.</p>
|
||||
<p class="lead">2 explanations · interactive walkthroughs of notable changes, newest first.</p>
|
||||
<div class="grid">
|
||||
<a class="card" href="2026-07-12-explanation-dufs-cloud.html">
|
||||
<span class="date">2026-07-12</span>
|
||||
<span class="title">Self-hosted dufs cloud + KeePass WebDAV sync — explained</span>
|
||||
</a>
|
||||
<a class="card" href="2026-07-11-explanation-phone-focus-whitelist-foss.html">
|
||||
<span class="date">2026-07-11</span>
|
||||
<span class="title">Explaining: whitelist run-with-music apps + FOSS replacements</span>
|
||||
|
||||
57
docs/superpowers/evidence/setup-dufs-cloud-2026-07-12.json
Normal file
57
docs/superpowers/evidence/setup-dufs-cloud-2026-07-12.json
Normal file
@ -0,0 +1,57 @@
|
||||
{
|
||||
"intent": "Provide a self-hosted Google-Drive-style cloud on this Arch PC: a dufs file server (web UI + WebDAV) exposed publicly via the existing Caddy+DuckDNS stack, with the KeePassXC vault auto-mirrored into it so it is reachable and syncable from the phone on mobile network.",
|
||||
"scope": [
|
||||
"linux_configuration/scripts/single_use/features/setup_dufs_cloud.sh (new self-sustaining installer)",
|
||||
"linux_configuration/scripts/single_use/features/systemd/{dufs.service,keepass-cloud-sync.service,keepass-cloud-sync.path} (new units)",
|
||||
"linux_configuration/.gitignore (ignore .dufs_cloud.conf)",
|
||||
"Non-goals: no second Caddy/DuckDNS updater, no hand-edit of /etc/nftables.conf, no new mobile app (WebDAV covers KeePassDX)"
|
||||
],
|
||||
"changes": [
|
||||
"Installer runs as the normal user with targeted sudo: installs a pinned, sha256-verified dufs v0.46.0 binary, writes a 0600 dufs.yaml (SHA-512 hashed auth, no anonymous access, bound to 127.0.0.1:5000), and installs+enables a system dufs.service.",
|
||||
"Reuses the host exposure pattern from setup_gitea.sh: appends a marker-guarded reverse_proxy block for a new DuckDNS subdomain to the shared ~/gitea/Caddyfile and reloads gitea-caddy; extends the existing DuckDNS updater's domains= list (no new cron); opens 80/443 via setup_wireguard_ssh.sh allow-web only if not already open.",
|
||||
"KeePass auto-sync: an event-driven systemd .path unit watches ~/Keepass/Passwords.kdbx and rsyncs ONLY that vault file into the served folder on every change (local .backup_* vaults stay off the public server); an initial mirror runs at install time.",
|
||||
"Paranoid secret hardening: the plaintext password config is gitignored at 3 layers (repo root, linux_configuration/, **/ patterns); a new check-no-dufs-conf pre-commit hook blocks it even against `git add -f`; the installer refuses to write it unless `git check-ignore` confirms it is ignored; the password is hashed via openssl stdin (never in argv/ps); and a cloud-root guard forbids serving home/.ssh/.config/.gnupg/the repo/root."
|
||||
],
|
||||
"verification": [
|
||||
{
|
||||
"command": "bash -n setup_dufs_cloud.sh && shellcheck -x --severity=warning setup_dufs_cloud.sh",
|
||||
"result": "pass",
|
||||
"evidence": "syntax OK; shellcheck clean (no findings)."
|
||||
},
|
||||
{
|
||||
"command": "dufs v0.46.0 config + WebDAV smoke test (scratchpad, real binary)",
|
||||
"result": "pass",
|
||||
"evidence": "Flat-string auth 'user:$6$hash@/:rw' loads. no-auth->401, bad-pass->401, good->200; WebDAV PROPFIND->207, GET file->200 (returned content). Confirms schema, hashed auth, no anonymous access, WebDAV works."
|
||||
},
|
||||
{
|
||||
"command": "Isolated idempotency/render tests (scratchpad copies)",
|
||||
"result": "pass",
|
||||
"evidence": "DuckDNS domains sed adds label once and is skipped on re-run with token preserved; Caddy block appended once (1 cloud block) with tab indentation; unit rendering substitutes __USER__/__HOME__/__CLOUD_ROOT__ with no leftover placeholders."
|
||||
},
|
||||
{
|
||||
"command": "systemd-analyze verify rendered-*.service rendered-*.path",
|
||||
"result": "pass",
|
||||
"evidence": "No structural errors; only benign 'dufs not executable yet' (binary installed at deploy time) and sandbox 'unknown user' notes."
|
||||
},
|
||||
{
|
||||
"command": "Paranoid secret guards (functional tests)",
|
||||
"result": "pass",
|
||||
"evidence": "git check-ignore confirms .dufs_cloud.conf ignored at all 3 locations; check_no_dufs_conf.sh blocks a `git add -f`ed conf (exit 1) and cleanup leaves nothing staged; cloud-root guard rejects HOME/.ssh/.config/.gnupg/repo/`/`/`../` traversal and allows dedicated folders; openssl passwd -6 -stdin yields a $6$ hash without argv exposure."
|
||||
},
|
||||
{
|
||||
"command": "End-to-end deploy + phone-on-cellular acceptance",
|
||||
"result": "pending",
|
||||
"evidence": "Requires interactive password + sudo, so run by the user via '! ./setup_dufs_cloud.sh', plus a one-time manual DuckDNS subdomain registration. To be confirmed before commit."
|
||||
}
|
||||
],
|
||||
"risks": [
|
||||
"Public file server: mitigated by mandatory auth (SHA-512 hashed, no anonymous), HTTPS via Caddy, dufs bound to 127.0.0.1 only; the kdbx is itself master-password-encrypted.",
|
||||
"Caddy block is appended to the shared Gitea Caddyfile; re-running setup_gitea.sh regenerates it and drops the block (re-run setup_dufs_cloud.sh to restore).",
|
||||
"External reachability depends on a one-time manual DuckDNS subdomain creation and the existing router 80/443 forward."
|
||||
],
|
||||
"rollback": [
|
||||
"sudo systemctl disable --now dufs.service keepass-cloud-sync.path; sudo rm /etc/systemd/system/{dufs.service,keepass-cloud-sync.service,keepass-cloud-sync.path}; sudo systemctl daemon-reload",
|
||||
"Remove the cloud block from ~/gitea/Caddyfile and 'docker exec gitea-caddy caddy reload'; revert the domains= edit in ~/.joplin-server/duckdns-update.sh; sudo rm /usr/local/bin/dufs; rm -rf ~/.config/dufs ~/cloud",
|
||||
"Validate after rollback: gitea still reachable at https://kuhy.duckdns.org and dufs.service is gone."
|
||||
]
|
||||
}
|
||||
5
linux_configuration/.gitignore
vendored
5
linux_configuration/.gitignore
vendored
@ -8,6 +8,11 @@ scripts/features/.raspberry_pi.conf
|
||||
scripts/single_use/features/.wireguard_ssh.conf
|
||||
.wireguard_ssh.conf
|
||||
|
||||
# dufs cloud setup config (contains the plaintext web password) — never commit.
|
||||
scripts/single_use/features/.dufs_cloud.conf
|
||||
.dufs_cloud.conf
|
||||
**/.dufs_cloud.conf
|
||||
|
||||
# Generated study materials (repo_to_study.sh output)
|
||||
study_materials/
|
||||
**/study_materials/
|
||||
|
||||
328
linux_configuration/scripts/single_use/features/setup_dufs_cloud.sh
Executable file
328
linux_configuration/scripts/single_use/features/setup_dufs_cloud.sh
Executable file
@ -0,0 +1,328 @@
|
||||
#!/bin/bash
|
||||
# setup_dufs_cloud.sh — Self-hosted "cloud" storage (dufs) on this Arch PC.
|
||||
#
|
||||
# Stands up dufs (a single Rust binary: web UI + built-in WebDAV over a real
|
||||
# folder), bound to 127.0.0.1 and fronted by the EXISTING Caddy reverse proxy
|
||||
# (from setup_gitea.sh) for automatic public HTTPS at a new DuckDNS subdomain.
|
||||
# Also mirrors the KeePassXC vault into the served folder on every change so it
|
||||
# is always fetchable from the phone (KeePassDX syncs it natively over WebDAV).
|
||||
#
|
||||
# Reuses the host's existing exposure stack — it does NOT add a second Caddy,
|
||||
# a second DuckDNS updater, or hand-edit /etc/nftables.conf. See
|
||||
# .github/skills/self-hosted-service-exposure/SKILL.md.
|
||||
#
|
||||
# Run as your normal user (NOT root). It uses sudo only for pacman, the binary
|
||||
# install, systemd units and the firewall flag. Idempotent — safe to re-run.
|
||||
#
|
||||
# Usage:
|
||||
# ./setup_dufs_cloud.sh Install / update everything
|
||||
# ./setup_dufs_cloud.sh -h Show help
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# --- Pinned dufs release (verified by sha256) --------------------------------
|
||||
readonly DUFS_VERSION="0.46.0"
|
||||
readonly DUFS_SHA256="817769f726613194bcff9d0e3e481eaccc86ac11208857614f36a8c02f410977"
|
||||
readonly DUFS_ASSET="dufs-v${DUFS_VERSION}-x86_64-unknown-linux-musl.tar.gz"
|
||||
readonly DUFS_URL="https://github.com/sigoden/dufs/releases/download/v${DUFS_VERSION}/${DUFS_ASSET}"
|
||||
readonly DUFS_BIN="/usr/local/bin/dufs"
|
||||
readonly DUFS_PORT=5000
|
||||
|
||||
# --- Existing host infrastructure (from setup_gitea.sh / install_joplin.sh) --
|
||||
readonly CADDY_CONTAINER="gitea-caddy"
|
||||
readonly CADDYFILE="${HOME}/gitea/Caddyfile"
|
||||
readonly CADDYFILE_IN_CONTAINER="/etc/caddy/Caddyfile"
|
||||
readonly DUCKDNS_UPDATER="${HOME}/.joplin-server/duckdns-update.sh"
|
||||
|
||||
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
|
||||
readonly SCRIPT_DIR
|
||||
readonly SYSTEMD_SRC="${SCRIPT_DIR}/systemd"
|
||||
readonly WG_SCRIPT="${SCRIPT_DIR}/setup_wireguard_ssh.sh"
|
||||
readonly CONFIG_FILE="${SCRIPT_DIR}/.dufs_cloud.conf"
|
||||
|
||||
# Git repo root (used by the paranoid secret guards below).
|
||||
REPO_ROOT="$(git -C "${SCRIPT_DIR}" rev-parse --show-toplevel 2>/dev/null || true)"
|
||||
readonly REPO_ROOT
|
||||
|
||||
# shellcheck source=../../lib/common.sh
|
||||
# shellcheck disable=SC1091
|
||||
source "${SCRIPT_DIR}/../../lib/common.sh"
|
||||
|
||||
# OS user that will own and run the service (not the dufs web-login user).
|
||||
SYS_USER="$(get_actual_user)"
|
||||
readonly SYS_USER
|
||||
|
||||
# Config values (populated from CONFIG_FILE / prompts).
|
||||
DUFS_USER=""
|
||||
DUFS_PASSWORD=""
|
||||
DUFS_SUBDOMAIN=""
|
||||
CLOUD_ROOT=""
|
||||
|
||||
die() {
|
||||
log_error "$1"
|
||||
exit 1
|
||||
}
|
||||
|
||||
usage() {
|
||||
grep -E '^#( |$)' "$0" | sed -E 's/^# ?//'
|
||||
exit 0
|
||||
}
|
||||
|
||||
# --- Step 1: preflight -------------------------------------------------------
|
||||
preflight() {
|
||||
log_info "Preflight checks"
|
||||
[[ ${EUID} -ne 0 ]] || die "Run as your normal user, not root (it will sudo where needed)."
|
||||
|
||||
# Runtime deps; install any that are missing from the official repos.
|
||||
install_missing_pacman_packages curl rsync inotify-tools openssl
|
||||
|
||||
# The public-exposure stack must already exist (Caddy from setup_gitea.sh).
|
||||
has_cmd docker || die "docker not found — run setup_gitea.sh first."
|
||||
docker ps --format '{{.Names}}' | grep -qx "${CADDY_CONTAINER}" \
|
||||
|| die "Caddy container '${CADDY_CONTAINER}' not running — run setup_gitea.sh first."
|
||||
[[ -f ${CADDYFILE} ]] || die "Caddyfile ${CADDYFILE} missing — run setup_gitea.sh first."
|
||||
[[ -x ${DUCKDNS_UPDATER} ]] || die "DuckDNS updater ${DUCKDNS_UPDATER} missing — run install_joplin.sh first."
|
||||
log_ok "Host exposure stack present (docker + ${CADDY_CONTAINER} + DuckDNS updater)"
|
||||
}
|
||||
|
||||
# --- Step 2: configuration (gitignored .conf, prompt when empty) -------------
|
||||
load_config() {
|
||||
if [[ -f ${CONFIG_FILE} ]]; then
|
||||
# shellcheck source=/dev/null
|
||||
source "${CONFIG_FILE}"
|
||||
fi
|
||||
DUFS_USER="${DUFS_USER:-}"
|
||||
DUFS_PASSWORD="${DUFS_PASSWORD:-}"
|
||||
DUFS_SUBDOMAIN="${DUFS_SUBDOMAIN:-}"
|
||||
CLOUD_ROOT="${CLOUD_ROOT:-}"
|
||||
}
|
||||
|
||||
prompt_config() {
|
||||
if [[ -z ${DUFS_USER} ]]; then
|
||||
read -r -p "dufs web username [${SYS_USER}]: " DUFS_USER
|
||||
DUFS_USER="${DUFS_USER:-${SYS_USER}}"
|
||||
fi
|
||||
if [[ -z ${DUFS_PASSWORD} ]]; then
|
||||
read -r -s -p "dufs web password: " DUFS_PASSWORD
|
||||
echo
|
||||
[[ -n ${DUFS_PASSWORD} ]] || die "A password is required (this server is public)."
|
||||
fi
|
||||
if [[ -z ${DUFS_SUBDOMAIN} ]]; then
|
||||
read -r -p "public subdomain [kuhy-cloud.duckdns.org]: " DUFS_SUBDOMAIN
|
||||
DUFS_SUBDOMAIN="${DUFS_SUBDOMAIN:-kuhy-cloud.duckdns.org}"
|
||||
fi
|
||||
if [[ -z ${CLOUD_ROOT} ]]; then
|
||||
read -r -p "cloud storage folder [${HOME}/cloud]: " CLOUD_ROOT
|
||||
CLOUD_ROOT="${CLOUD_ROOT:-${HOME}/cloud}"
|
||||
fi
|
||||
}
|
||||
|
||||
# Paranoid guard: never persist the plaintext password unless git is certain
|
||||
# the file is ignored. This is checked against the live .gitignore, so a broken
|
||||
# or removed ignore rule stops the write instead of leaking the secret.
|
||||
assert_config_gitignored() {
|
||||
[[ -n ${REPO_ROOT} ]] || die "Not inside a git repo — refusing to write the secret config."
|
||||
if ! git -C "${REPO_ROOT}" check-ignore -q "${CONFIG_FILE}"; then
|
||||
die "Refusing to write ${CONFIG_FILE}: it is NOT gitignored. Restore the .gitignore rule first — will not risk leaking the password."
|
||||
fi
|
||||
}
|
||||
|
||||
save_config() {
|
||||
assert_config_gitignored
|
||||
# 0600, gitignored — holds the plaintext web password so re-runs stay
|
||||
# non-interactive. Never committed (see .gitignore + the check-no-dufs-conf
|
||||
# pre-commit hook, which blocks it even against `git add -f`).
|
||||
umask 077
|
||||
{
|
||||
printf 'DUFS_USER=%q\n' "${DUFS_USER}"
|
||||
printf 'DUFS_PASSWORD=%q\n' "${DUFS_PASSWORD}"
|
||||
printf 'DUFS_SUBDOMAIN=%q\n' "${DUFS_SUBDOMAIN}"
|
||||
printf 'CLOUD_ROOT=%q\n' "${CLOUD_ROOT}"
|
||||
} >"${CONFIG_FILE}"
|
||||
chmod 600 "${CONFIG_FILE}"
|
||||
log_ok "Saved config to ${CONFIG_FILE} (0600)"
|
||||
}
|
||||
|
||||
# --- Step 3: install the pinned, checksum-verified dufs binary ---------------
|
||||
install_dufs() {
|
||||
if [[ -x ${DUFS_BIN} ]] && "${DUFS_BIN}" --version 2>/dev/null | grep -q "${DUFS_VERSION}"; then
|
||||
log_ok "dufs ${DUFS_VERSION} already installed"
|
||||
return 0
|
||||
fi
|
||||
log_info "Installing dufs ${DUFS_VERSION}"
|
||||
local tmp
|
||||
tmp="$(mktemp -d)"
|
||||
# shellcheck disable=SC2064
|
||||
trap "rm -rf '${tmp}'" RETURN
|
||||
curl -fsSL -o "${tmp}/${DUFS_ASSET}" "${DUFS_URL}"
|
||||
echo "${DUFS_SHA256} ${tmp}/${DUFS_ASSET}" | sha256sum -c - \
|
||||
|| die "dufs checksum mismatch — refusing to install."
|
||||
tar -xzf "${tmp}/${DUFS_ASSET}" -C "${tmp}"
|
||||
sudo install -m 0755 "${tmp}/dufs" "${DUFS_BIN}"
|
||||
log_ok "Installed ${DUFS_BIN}"
|
||||
}
|
||||
|
||||
# Paranoid guard: never let dufs serve a directory that would expose secrets
|
||||
# (home root, ~/.ssh, ~/.config — which holds dufs.yaml — ~/.gnupg, / , or the
|
||||
# git repo which holds the gitignored .dufs_cloud.conf).
|
||||
assert_cloud_root_safe() {
|
||||
local resolved home_r
|
||||
resolved="$(readlink -m "${CLOUD_ROOT}")"
|
||||
home_r="$(readlink -m "${HOME}")"
|
||||
case "${resolved}" in
|
||||
"${home_r}" | / | "${home_r}/.ssh" | "${home_r}/.ssh/"* \
|
||||
| "${home_r}/.config" | "${home_r}/.config/"* \
|
||||
| "${home_r}/.gnupg" | "${home_r}/.gnupg/"*)
|
||||
die "Unsafe cloud folder '${CLOUD_ROOT}' — it would expose sensitive files. Use a dedicated folder like ${HOME}/cloud." ;;
|
||||
esac
|
||||
if [[ -n ${REPO_ROOT} && (${resolved} == "${REPO_ROOT}" || ${resolved} == "${REPO_ROOT}/"*) ]]; then
|
||||
die "Unsafe cloud folder '${CLOUD_ROOT}' — it is inside the git repo (which holds the secret config). Use a dedicated folder."
|
||||
fi
|
||||
}
|
||||
|
||||
# --- Step 4: served folder ---------------------------------------------------
|
||||
create_serve_dir() {
|
||||
assert_cloud_root_safe
|
||||
ensure_dir "${CLOUD_ROOT}"
|
||||
ensure_dir "${CLOUD_ROOT}/Keepass"
|
||||
chmod 700 "${CLOUD_ROOT}"
|
||||
log_ok "Cloud folder ${CLOUD_ROOT} ready"
|
||||
}
|
||||
|
||||
# --- Step 5: dufs config (SHA-512 hashed password, no anonymous access) -------
|
||||
write_dufs_config() {
|
||||
local cfg_dir="${HOME}/.config/dufs"
|
||||
local cfg="${cfg_dir}/dufs.yaml"
|
||||
local hash
|
||||
# Read the password from stdin so it never appears in the process argv (ps).
|
||||
hash="$(printf '%s' "${DUFS_PASSWORD}" | openssl passwd -6 -stdin)"
|
||||
ensure_dir "${cfg_dir}"
|
||||
umask 077
|
||||
cat >"${cfg}" <<EOF
|
||||
# Generated by setup_dufs_cloud.sh — do not edit by hand.
|
||||
serve-path: ${CLOUD_ROOT}
|
||||
bind: 127.0.0.1
|
||||
port: ${DUFS_PORT}
|
||||
allow-all: true
|
||||
auth:
|
||||
- "${DUFS_USER}:${hash}@/:rw"
|
||||
EOF
|
||||
chmod 600 "${cfg}"
|
||||
log_ok "Wrote ${cfg} (auth required, password hashed)"
|
||||
}
|
||||
|
||||
# --- Step 6: systemd units (render placeholders → /etc/systemd/system) -------
|
||||
render_unit() {
|
||||
local name="$1"
|
||||
sed -e "s|__USER__|${SYS_USER}|g" \
|
||||
-e "s|__HOME__|${HOME}|g" \
|
||||
-e "s|__CLOUD_ROOT__|${CLOUD_ROOT}|g" \
|
||||
"${SYSTEMD_SRC}/${name}" | sudo tee "/etc/systemd/system/${name}" >/dev/null
|
||||
}
|
||||
|
||||
install_units() {
|
||||
log_info "Installing systemd units"
|
||||
render_unit dufs.service
|
||||
render_unit keepass-cloud-sync.service
|
||||
render_unit keepass-cloud-sync.path
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable --now dufs.service
|
||||
log_ok "dufs.service enabled and started"
|
||||
}
|
||||
|
||||
# --- Step 7: Caddy reverse-proxy block (append + reload) ---------------------
|
||||
configure_caddy() {
|
||||
if grep -qE "^${DUFS_SUBDOMAIN} \{" "${CADDYFILE}"; then
|
||||
log_ok "Caddyfile already has a block for ${DUFS_SUBDOMAIN}"
|
||||
else
|
||||
log_info "Adding Caddy block for ${DUFS_SUBDOMAIN}"
|
||||
# NOTE: appended to the shared Gitea Caddyfile. Re-running setup_gitea.sh
|
||||
# regenerates that file and drops this block — re-run setup_dufs_cloud.sh
|
||||
# to restore it. Caddyfile uses tab indentation.
|
||||
printf '\n%s {\n\treverse_proxy 127.0.0.1:%s\n}\n' "${DUFS_SUBDOMAIN}" "${DUFS_PORT}" \
|
||||
>>"${CADDYFILE}"
|
||||
fi
|
||||
docker exec "${CADDY_CONTAINER}" caddy reload --config "${CADDYFILE_IN_CONTAINER}" \
|
||||
|| die "caddy reload failed — check 'docker logs ${CADDY_CONTAINER}'."
|
||||
log_ok "Caddy reloaded (auto-HTTPS will be issued once the subdomain resolves)"
|
||||
}
|
||||
|
||||
# --- Step 8: DuckDNS subdomain (extend existing updater; no new cron) --------
|
||||
configure_duckdns() {
|
||||
local label="${DUFS_SUBDOMAIN%.duckdns.org}"
|
||||
if grep -qE "domains=[^&\"']*${label}([,&\"']|$)" "${DUCKDNS_UPDATER}"; then
|
||||
log_ok "DuckDNS updater already includes '${label}'"
|
||||
else
|
||||
log_info "Adding '${label}' to the existing DuckDNS updater"
|
||||
sed -i -E "s|(domains=)([^&\"']*)|\1\2,${label}|" "${DUCKDNS_UPDATER}"
|
||||
fi
|
||||
# Refresh now (harmless if the subdomain isn't registered yet).
|
||||
bash "${DUCKDNS_UPDATER}" || log_warn "DuckDNS update returned non-zero (register the subdomain first)."
|
||||
if getent hosts "${DUFS_SUBDOMAIN}" >/dev/null 2>&1; then
|
||||
log_ok "${DUFS_SUBDOMAIN} resolves"
|
||||
else
|
||||
log_warn "${DUFS_SUBDOMAIN} does not resolve yet — create the '${label}' subdomain in the DuckDNS dashboard."
|
||||
fi
|
||||
}
|
||||
|
||||
# --- Step 9: firewall (open 80/443 via the owning script, idempotent) -------
|
||||
ensure_firewall() {
|
||||
if sudo nft list ruleset 2>/dev/null | grep -qE 'dport \{[^}]*80[^}]*443'; then
|
||||
log_ok "Firewall already allows tcp/80 + tcp/443"
|
||||
elif [[ -f ${WG_SCRIPT} ]]; then
|
||||
log_info "Opening 80/443 via setup_wireguard_ssh.sh allow-web"
|
||||
sudo bash "${WG_SCRIPT}" allow-web
|
||||
else
|
||||
log_warn "setup_wireguard_ssh.sh not found — ensure tcp/80+443 are open."
|
||||
fi
|
||||
}
|
||||
|
||||
# --- Step 10: KeePass auto-sync (initial mirror + enable the path watcher) ---
|
||||
setup_autosync() {
|
||||
log_info "Setting up KeePass auto-sync watcher"
|
||||
# Mirror only the vault file (not the whole dir with local .backup_* vaults).
|
||||
rsync -a "${HOME}/Keepass/Passwords.kdbx" "${CLOUD_ROOT}/Keepass/"
|
||||
sudo systemctl enable --now keepass-cloud-sync.path
|
||||
log_ok "Vault mirrored and watcher enabled"
|
||||
}
|
||||
|
||||
# --- Step 11: report ---------------------------------------------------------
|
||||
print_report() {
|
||||
local url="https://${DUFS_SUBDOMAIN}"
|
||||
cat <<EOF
|
||||
|
||||
============================================================================
|
||||
dufs cloud is up.
|
||||
Web UI / WebDAV : ${url}/
|
||||
Login : ${DUFS_USER} / (your password)
|
||||
Served folder : ${CLOUD_ROOT}
|
||||
KeePassDX target: ${url}/Keepass/Passwords.kdbx (WebDAV)
|
||||
|
||||
If the subdomain is new, create it once in the DuckDNS dashboard
|
||||
(label: ${DUFS_SUBDOMAIN%.duckdns.org}) so Caddy can issue TLS.
|
||||
|
||||
Acceptance test: on your phone with Wi-Fi OFF (cellular), open ${url}/,
|
||||
log in, and confirm you see Keepass/Passwords.kdbx.
|
||||
============================================================================
|
||||
EOF
|
||||
}
|
||||
|
||||
main() {
|
||||
[[ "${1:-}" == "-h" || "${1:-}" == "--help" ]] && usage
|
||||
print_setup_header "Self-hosted dufs cloud storage"
|
||||
preflight
|
||||
load_config
|
||||
prompt_config
|
||||
save_config
|
||||
install_dufs
|
||||
create_serve_dir
|
||||
write_dufs_config
|
||||
install_units
|
||||
configure_caddy
|
||||
configure_duckdns
|
||||
ensure_firewall
|
||||
setup_autosync
|
||||
print_report
|
||||
}
|
||||
|
||||
main "$@"
|
||||
@ -0,0 +1,22 @@
|
||||
[Unit]
|
||||
# dufs self-hosted cloud file server (web UI + WebDAV), bound to loopback and
|
||||
# fronted by the existing Caddy reverse proxy for public HTTPS.
|
||||
# Placeholders (__USER__ / __HOME__) are substituted by setup_dufs_cloud.sh at
|
||||
# install time; this file is the source of truth, not the installed copy.
|
||||
Description=dufs self-hosted cloud file server
|
||||
Documentation=https://github.com/sigoden/dufs
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=__USER__
|
||||
ExecStart=/usr/local/bin/dufs --config __HOME__/.config/dufs/dufs.yaml
|
||||
Restart=on-failure
|
||||
RestartSec=3
|
||||
# Minimal hardening (dufs needs $HOME/.config and the served dir under /home).
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@ -0,0 +1,12 @@
|
||||
[Unit]
|
||||
# Event-driven watch on the KeePassXC vault (no polling). On modification it
|
||||
# starts keepass-cloud-sync.service, which rsyncs the vault into the cloud folder.
|
||||
# Placeholder __HOME__ is substituted by setup_dufs_cloud.sh at install time.
|
||||
Description=Watch KeePass vault and sync to dufs cloud folder
|
||||
|
||||
[Path]
|
||||
PathModified=__HOME__/Keepass/Passwords.kdbx
|
||||
Unit=keepass-cloud-sync.service
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@ -0,0 +1,13 @@
|
||||
[Unit]
|
||||
# Mirror the live KeePassXC vault into the dufs-served cloud folder.
|
||||
# Triggered by keepass-cloud-sync.path on every change to Passwords.kdbx.
|
||||
# Placeholders (__USER__ / __HOME__ / __CLOUD_ROOT__) are substituted by
|
||||
# setup_dufs_cloud.sh at install time.
|
||||
Description=Mirror KeePass vault into dufs cloud folder
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
User=__USER__
|
||||
# Only the vault file — not the whole ~/Keepass dir (keeps local .backup_* vaults
|
||||
# off the public server). No --delete: the cloud folder may hold other files.
|
||||
ExecStart=/usr/bin/rsync -a __HOME__/Keepass/Passwords.kdbx __CLOUD_ROOT__/Keepass/
|
||||
25
meta/scripts/check_no_dufs_conf.sh
Executable file
25
meta/scripts/check_no_dufs_conf.sh
Executable file
@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
# ============================================================================
|
||||
# Pre-commit guard: never allow the dufs cloud secret config to be committed.
|
||||
#
|
||||
# .dufs_cloud.conf holds the plaintext dufs web password. It is gitignored, but
|
||||
# `git add -f` can bypass gitignore — this hook is the backstop. It inspects the
|
||||
# staged set directly (not just the files pre-commit passes) so it catches a
|
||||
# force-added file regardless of any `files:` filter.
|
||||
# ============================================================================
|
||||
set -euo pipefail
|
||||
|
||||
# Added/Copied/Modified staged paths.
|
||||
staged="$(git diff --cached --name-only --diff-filter=ACM 2>/dev/null || true)"
|
||||
|
||||
bad="$(printf '%s\n' "$staged" | grep -E '(^|/)\.dufs_cloud\.conf$' || true)"
|
||||
|
||||
if [ -n "$bad" ]; then
|
||||
echo "ERROR: refusing to commit the dufs secret config (plaintext password):" >&2
|
||||
printf ' %s\n' "$bad" >&2
|
||||
echo "" >&2
|
||||
echo "This file must stay local only. Unstage it: git rm --cached <file>" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exit 0
|
||||
Loading…
Reference in New Issue
Block a user