Files
fips_setup/scripts/08-add-peer.sh
2026-04-11 14:20:42 -04:00

89 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# ==============================================================================
# 08-add-peer.sh
#
# Add a static UDP peer to /etc/fips/fips.yaml (single-node mode).
#
# Usage:
# sudo bash 08-add-peer.sh [npub] [alias] [host:port]
#
# If arguments are omitted, prompts interactively.
# ==============================================================================
set -euo pipefail
CONFIG_FILE="/etc/fips/fips.yaml"
echo "=== Add FIPS Peer ==="
echo ""
if [ "$(id -u)" -ne 0 ]; then
echo "✗ This script must be run as root (sudo)"
exit 1
fi
if [ ! -f "$CONFIG_FILE" ]; then
echo "$CONFIG_FILE not found"
echo " Run: sudo bash 02-install-fips.sh"
exit 1
fi
NPUB="${1:-}"
ALIAS="${2:-}"
ADDR="${3:-}"
if [ -z "$NPUB" ]; then
read -r -p "Peer npub: " NPUB
fi
if [ -z "$ALIAS" ]; then
read -r -p "Alias (default peer): " ALIAS
ALIAS="${ALIAS:-peer}"
fi
if [ -z "$ADDR" ]; then
read -r -p "UDP address (host:port): " ADDR
fi
if ! echo "$NPUB" | grep -q '^npub1'; then
echo "✗ npub must start with 'npub1'"
exit 1
fi
if ! echo "$ADDR" | grep -q ':'; then
echo "✗ address must be in host:port format"
exit 1
fi
tmp=$(mktemp)
if grep -q '^peers:[[:space:]]*\[\][[:space:]]*$' "$CONFIG_FILE"; then
sed 's/^peers:[[:space:]]*\[\][[:space:]]*$/peers:/' "$CONFIG_FILE" > "$tmp"
cat >> "$tmp" << EOF
- npub: "$NPUB"
alias: "$ALIAS"
addresses:
- transport: udp
addr: "$ADDR"
connect_policy: auto_connect
EOF
else
cp "$CONFIG_FILE" "$tmp"
cat >> "$tmp" << EOF
- npub: "$NPUB"
alias: "$ALIAS"
addresses:
- transport: udp
addr: "$ADDR"
connect_policy: auto_connect
EOF
fi
mv "$tmp" "$CONFIG_FILE"
chmod 600 "$CONFIG_FILE"
echo ""
echo "✓ Peer added"
echo " npub: $NPUB"
echo " alias: $ALIAS"
echo " addr: $ADDR"
echo ""
echo "Restart FIPS to apply:"
echo " sudo systemctl restart fips"