nixos/mods/vps-init.nix
2026-11-18 16:57:41 +09:00

99 lines
3.8 KiB
Nix

{ config, pkgs, ... }: {
sops = {
secrets.awg0-spec = { };
templates.awg0-conf.content = ''
${config.sops.placeholder.awg-obf}
${config.sops.placeholder.awg0-spec}
'';
};
systemd.services.vps-init = {
path = [ pkgs.openssh ];
script = ''
cd /root/run
date +%s > awg-start-time
. vps-init.env
scp -i /root/.ssh/vps ${config.sops.templates.awg0-conf.path} $SSHCONN:awg0.conf
until cat <<'EOF' | ssh -i /root/.ssh/vps $SSHCONN
set -e
if [ ! -e ~/vps-init-state ]; then
echo 0 > ~/vps-init-state
fi
case "$(cat ~/vps-init-state)" in
0)
cd /etc/ssh
FILENAME="$(mktemp)"
(echo PasswordAuthentication no; cat sshd_config) > "$FILENAME"
mv "$FILENAME" sshd_config
SIZE="$(awk '/MemTotal:/ { print $2 "KiB" }' /proc/meminfo)"
fallocate -l $SIZE /swapfile
chmod 600 /swapfile
mkswap /swapfile
echo "/swapfile none swap defaults 0 0" >> /etc/fstab
cd /etc/apt/apt.conf.d
cat <<' EOF' > 99policy
Dpkg::Options { "--force-confnew"; };
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
EOF
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y unattended-upgrades
cat <<' EOF' > 20auto-upgrades
APT::Periodic::Unattended-Upgrade "1";
APT::Periodic::Update-Package-Lists "1";
EOF
apt-get dist-upgrade -y;;
1)
cat <<' EOF' >> /etc/sysctl.conf
net.ipv4.conf.all.forwarding = 1
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
EOF
for v in 6 ""; do
ip''${v}tables -P FORWARD DROP
ip''${v}tables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
ip''${v}tables -A FORWARD -i awg0 -j ACCEPT
ip''${v}tables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
done
for val in dest6=fc00::2 dest4=10.0.0.2 proto=tcp 25 80 443 32831:22 proto=udp 443; do
if [[ "$val" == *=* ]]; then
eval "$val"
if [[ "$val" == dest6=* ]]; then
ip6tables -t nat -A POSTROUTING -s $dest6 -d $dest6 -j MASQUERADE
elif [[ "$val" == dest4=* ]]; then
iptables -t nat -A POSTROUTING -s $dest4 -d $dest4 -j MASQUERADE
fi
continue
fi
if [[ "$val" == *:* ]]; then
lport=''${val%:*}
rport=''${val#*:}
else
lport=$val
rport=$val
fi
ip6tables -t nat -A PREROUTING -m addrtype --dst-type LOCAL -p $proto --dport $lport -j DNAT --to-destination [$dest6]:$rport
ip6tables -A FORWARD -d $dest6 -p $proto --dport $rport -j ACCEPT
iptables -t nat -A PREROUTING -m addrtype --dst-type LOCAL -p $proto --dport $lport -j DNAT --to-destination $dest4:$rport
iptables -A FORWARD -d $dest4 -p $proto --dport $rport -j ACCEPT
done
export DEBIAN_FRONTEND=noninteractive
add-apt-repository -y ppa:amnezia/ppa
apt-get install -y gnupg2 amneziawg amneziawg-tools iptables-persistent python3-launchpadlib linux-headers-generic software-properties-common
mv ~/awg0.conf /etc/amnezia/amneziawg
systemctl enable awg-quick@awg0
apt-get autoremove --purge -y
apt-get clean;;
2)
rm -f awg0.conf
exit;;
esac
echo "$((1+$(cat ~/vps-init-state)))" > ~/vps-init-state
reboot
exit 1
EOF
do sleep 1; done
echo Installed in $(($(date +%s)-$(cat awg-start-time)))s
'';
};
}