Compare commits
5
Commits
ed2d2731d6
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9100eca4c8 | ||
|
|
1715876ea7 | ||
|
|
f41867b5bf | ||
|
|
781ac510fb | ||
|
|
36dc96d86f |
No files matched your search
Generated
+4
-4
@@ -176,11 +176,11 @@
|
||||
},
|
||||
"secrets": {
|
||||
"locked": {
|
||||
"lastModified": 1795000895,
|
||||
"narHash": "sha256-DzTRUpiuhSmPJm/MjcUwNLxVbhWxjC1TGLyj41kXnr4=",
|
||||
"lastModified": 1795002280,
|
||||
"narHash": "sha256-PpeywbmyLrlliMSLvNCLT3L6Mmu5xHCGERrYuFIEM2s=",
|
||||
"ref": "refs/heads/main",
|
||||
"rev": "557ab40690eb4320e36c5e57b73bfba1ecb73393",
|
||||
"revCount": 23,
|
||||
"rev": "0b61e4b871c3bb33d25fdb5493a995dfef14f8e5",
|
||||
"revCount": 24,
|
||||
"type": "git",
|
||||
"url": "ssh://gitea@git.bulthuis.dev/Jan/nixos-secrets"
|
||||
},
|
||||
|
||||
+67
-45
@@ -6,23 +6,17 @@
|
||||
...
|
||||
}:
|
||||
|
||||
# Always-on torrent client, confined to a VPN-only network namespace, plus a
|
||||
# read-only, password-protected WebDAV share of its downloads for the local network.
|
||||
#
|
||||
# Everything provider-specific lives in secrets:
|
||||
# seedbox/wireguard wg-quick style config (keys, peer, Address, DNS, MTU)
|
||||
# seedbox/port the incoming port forwarded by the VPN
|
||||
# seedbox/share-password password for the share (user "seedbox")
|
||||
#
|
||||
# Web interface: only on this host's localhost, e.g. `ssh -L 8081:127.0.0.1:8081 ws-home`
|
||||
# Torrent client confined to a VPN-only network namespace, plus a WebDAV share.
|
||||
# See seedbox.md.
|
||||
let
|
||||
netns = "seedbox";
|
||||
wgIf = "wg-seedbox";
|
||||
dataDir = "/var/lib/seedbox";
|
||||
downloadsDir = "${dataDir}/downloads";
|
||||
webuiProxyPort = 8081; # localhost of this host only
|
||||
webuiPort = 8080; # inside the namespace
|
||||
certDir = "${dataDir}/webui-tls";
|
||||
webuiPort = 8081;
|
||||
sharePort = 8091;
|
||||
inherit (config.networking) hostName;
|
||||
|
||||
secret = name: config.sops.secrets."seedbox/${name}".path;
|
||||
secretFile = "${inputs.secrets}/secrets/ws-home.enc.yaml";
|
||||
@@ -34,6 +28,7 @@ let
|
||||
};
|
||||
netnsDeps = {
|
||||
bindsTo = [ "seedbox-netns.service" ];
|
||||
partOf = [ "seedbox-netns.service" ];
|
||||
after = [ "seedbox-netns.service" ];
|
||||
};
|
||||
in
|
||||
@@ -59,15 +54,15 @@ in
|
||||
];
|
||||
systemd.tmpfiles.rules = [ "d ${downloadsDir} 0750 ${user} ${group} -" ];
|
||||
|
||||
# Network namespace whose only route out is the WireGuard interface. The interface
|
||||
# is created here and moved into the namespace, so its encrypted traffic uses the
|
||||
# host's network, but nothing inside the namespace can reach anything else.
|
||||
boot.kernelModules = [ "tcp_bbr" ];
|
||||
|
||||
systemd.services.seedbox-netns = {
|
||||
description = "Network namespace for the seedbox";
|
||||
wants = [ "network-online.target" ];
|
||||
after = [ "network-online.target" ];
|
||||
path = with pkgs; [
|
||||
iproute2
|
||||
procps
|
||||
wireguard-tools
|
||||
gnugrep
|
||||
gnused
|
||||
@@ -90,8 +85,19 @@ in
|
||||
ip netns add ${netns}
|
||||
ip -n ${netns} link set lo up
|
||||
|
||||
# wg-quick strip requires a .conf file name
|
||||
stripped=$(grep -viE '^\s*(Address|DNS|MTU|Table|SaveConfig|(Pre|Post)(Up|Down))\s*=' "$conf")
|
||||
if ! grep -qi '^\s*PrivateKey' <<<"$stripped" || ! grep -qi '^\s*\[Peer\]' <<<"$stripped"; then
|
||||
echo "seedbox/wireguard has no PrivateKey or [Peer]" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
ip link add ${wgIf} type wireguard
|
||||
wg setconf ${wgIf} <(wg-quick strip "$conf")
|
||||
wg setconf ${wgIf} <(printf '%s\n' "$stripped")
|
||||
if ! wg show ${wgIf} peers | grep -q .; then
|
||||
echo "WireGuard peer was not configured" >&2
|
||||
exit 1
|
||||
fi
|
||||
ip link set ${wgIf} netns ${netns}
|
||||
for addr in $(field Address); do
|
||||
ip -n ${netns} addr add "$addr" dev ${wgIf}
|
||||
@@ -103,7 +109,13 @@ in
|
||||
ip -n ${netns} -6 route add default dev ${wgIf}
|
||||
fi
|
||||
|
||||
# DNS only through the tunnel
|
||||
# Faster TCP to distant peers
|
||||
ip netns exec ${netns} sysctl -q -w \
|
||||
net.ipv4.tcp_congestion_control=bbr \
|
||||
net.ipv4.tcp_wmem="4096 65536 16777216" \
|
||||
net.ipv4.tcp_rmem="4096 131072 16777216" \
|
||||
net.ipv4.tcp_slow_start_after_idle=0
|
||||
|
||||
field DNS | grep -v ':' | sed 's/^/nameserver /' > /run/seedbox/resolv.conf
|
||||
echo 'hosts: files dns' > /run/seedbox/nsswitch.conf
|
||||
'';
|
||||
@@ -121,8 +133,7 @@ in
|
||||
|
||||
systemd.services.qbittorrent = netnsDeps // {
|
||||
serviceConfig = inNetns // {
|
||||
# Resolve names only with the tunnel's DNS. The host's nscd would resolve them
|
||||
# outside the namespace, so it is hidden.
|
||||
# nscd would bypass the tunnel's DNS
|
||||
BindReadOnlyPaths = [
|
||||
"/run/seedbox/resolv.conf:/etc/resolv.conf"
|
||||
"/run/seedbox/nsswitch.conf:/etc/nsswitch.conf"
|
||||
@@ -130,8 +141,6 @@ in
|
||||
InaccessiblePaths = [ "-/run/nscd" ];
|
||||
ReadWritePaths = [ dataDir ];
|
||||
|
||||
# Initial settings, and the incoming port from the secret on every start. Other
|
||||
# settings are left to the web interface.
|
||||
ExecStartPre = pkgs.writeShellScript "seedbox-config" ''
|
||||
set -euo pipefail
|
||||
PATH=${
|
||||
@@ -139,41 +148,53 @@ in
|
||||
with pkgs;
|
||||
[
|
||||
coreutils
|
||||
gnused
|
||||
crudini
|
||||
openssl
|
||||
]
|
||||
)
|
||||
}
|
||||
port=$(tr -dc 0-9 < ${secret "port"})
|
||||
cfg=${dataDir}/qBittorrent/config/qBittorrent.conf
|
||||
mkdir -p ${downloadsDir} "$(dirname "$cfg")"
|
||||
if [ ! -f "$cfg" ]; then
|
||||
cat > "$cfg" <<EOF
|
||||
[BitTorrent]
|
||||
Session\DefaultSavePath=${downloadsDir}
|
||||
Session\Interface=${wgIf}
|
||||
Session\InterfaceName=${wgIf}
|
||||
Session\LSDEnabled=false
|
||||
Session\QueueingSystemEnabled=false
|
||||
Session\Port=$port
|
||||
set_() { crudini --ini-options=nospace --set "$cfg" "$@"; }
|
||||
|
||||
[Network]
|
||||
PortForwardingEnabled=false
|
||||
|
||||
[Preferences]
|
||||
WebUI\Address=127.0.0.1
|
||||
WebUI\Port=${toString webuiPort}
|
||||
EOF
|
||||
else
|
||||
sed -i -E "s|^Session\\\\Port=.*|Session\\\\Port=$port|" "$cfg"
|
||||
if [ ! -f ${certDir}/cert.pem ]; then
|
||||
mkdir -p ${certDir}
|
||||
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \
|
||||
-days 3650 -subj /CN=${hostName} -addext subjectAltName=DNS:${hostName},DNS:seedbox.bulthuis.dev \
|
||||
-keyout ${certDir}/key.pem -out ${certDir}/cert.pem
|
||||
chmod 600 ${certDir}/key.pem
|
||||
fi
|
||||
|
||||
if [ ! -f "$cfg" ]; then
|
||||
touch "$cfg"
|
||||
set_ BitTorrent 'Session\DefaultSavePath' ${downloadsDir}
|
||||
set_ BitTorrent 'Session\LSDEnabled' false
|
||||
set_ BitTorrent 'Session\QueueingSystemEnabled' false
|
||||
fi
|
||||
|
||||
set_ BitTorrent 'Session\Port' "$(tr -dc 0-9 < ${secret "port"})"
|
||||
set_ BitTorrent 'Session\Interface' ${wgIf}
|
||||
set_ BitTorrent 'Session\InterfaceName' ${wgIf}
|
||||
set_ Network PortForwardingEnabled false
|
||||
# qBittorrent 5 reads these from [WebUI], drop the old keys
|
||||
for k in Address Port HostHeaderValidation LocalHostAuth 'HTTPS\Enabled' 'HTTPS\CertificatePath' 'HTTPS\KeyPath'; do
|
||||
crudini --del "$cfg" Preferences "WebUI\\$k"
|
||||
done
|
||||
set_ WebUI 'Address' 127.0.0.1
|
||||
set_ WebUI 'Port' ${toString webuiPort}
|
||||
set_ WebUI 'HostHeaderValidation' false
|
||||
# Proxied clients appear as localhost, so they must not skip the login
|
||||
set_ WebUI 'LocalHostAuth' true
|
||||
set_ WebUI 'HTTPS\Enabled' true
|
||||
set_ WebUI 'HTTPS\CertificatePath' ${certDir}/cert.pem
|
||||
set_ WebUI 'HTTPS\KeyPath' ${certDir}/key.pem
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
# Web interface on this host's localhost, proxied into the namespace
|
||||
systemd.sockets.seedbox-webui = {
|
||||
wantedBy = [ "sockets.target" ];
|
||||
listenStreams = [ "127.0.0.1:${toString webuiProxyPort}" ];
|
||||
listenStreams = [ (toString webuiPort) ];
|
||||
};
|
||||
systemd.services.seedbox-webui = netnsDeps // {
|
||||
requires = [ "qbittorrent.service" ];
|
||||
@@ -185,8 +206,6 @@ in
|
||||
};
|
||||
};
|
||||
|
||||
# Read-only share of the downloads on the local network, password protected and
|
||||
# not announced anywhere
|
||||
systemd.services.seedbox-share = {
|
||||
description = "Seedbox downloads share";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
@@ -209,5 +228,8 @@ in
|
||||
--addr :${toString sharePort} --read-only --dir-cache-time 15s
|
||||
'';
|
||||
};
|
||||
networking.firewall.interfaces.eno1.allowedTCPPorts = [ sharePort ];
|
||||
networking.firewall.interfaces.eno1.allowedTCPPorts = [
|
||||
webuiPort
|
||||
sharePort
|
||||
];
|
||||
}
|
||||
Reference in new issue
Block a user