AJ ONeal 271b22fa1f
refactor(formmailer): ergonomic cleanups after prod deploy learnings
- Collapse SuccessBody/SuccessBodyFunc (and Error*) into a single required
  func() []byte. Callers build closures with baked-in fallbacks; one field,
  one code path, no priority ordering.
- Split EHLO local name from peer hostname. Add LocalName field + default
  to os.Hostname(). smtp.NewClient gets the peer name (for AUTH + TLS
  binding); c.Hello(localName) overrides the EHLO greeting separately.
  Stdlib conflates these but they're semantically distinct.
- Add TLSConfig override field (supports custom CAs, cert pinning, and
  test-time InsecureSkipVerify). Default is still ServerName: <SMTP host>.
- Add TrustedProxies []netip.Prefix. X-Forwarded-For is now only honored
  when r.RemoteAddr is in the trusted set — otherwise any client can forge
  their IP to bypass rate limits, blacklist, and geo gating.
- Integration tests drive sendMail against a fake SMTP server that does
  STARTTLS + AUTH PLAIN. Regression test for StartTLS(nil) panic (Go 1.22+
  requires ServerName or InsecureSkipVerify).

cmd/form2mail:
- Extract hotReload() helper; replaces the inline closures.
- silentDropRU looks up the email FormName from Fields by Kind instead of
  hardcoding "input_3" (survives field renames).
- Set TrustedProxies to loopback (proxy runs on same host).
- scripts/deploy.sh automates the rename-first / scp / restart / verify /
  cleanup cycle — leaves .old in place on verification failure for rollback.
2026-04-20 23:20:13 -06:00

43 lines
1.4 KiB
Bash
Executable File

#!/bin/sh
# Cross-compile form2mail for linux-amd64 and deploy to <user>@<host>.
# Usage: scripts/deploy.sh <user>@<host> [service-name]
#
# Uses the rename-old-first pattern (avoids scp ETXTBSY on running ELFs),
# restarts via systemctl, verifies --version runs from the new inode, then
# removes the .old backup. Leaves .old in place if verification fails so
# rollback is `mv ~/bin/<bin>.old ~/bin/<bin>`.
set -eu
g_remote="${1:?usage: $0 <user>@<host> [service-name]}"
g_service="${2:-form2mail}"
g_bin="form2mail"
cd "$(dirname "$0")/.."
echo "Building ${g_bin} for linux-amd64..."
GOOS=linux GOARCH=amd64 go build -o "${g_bin}-linux" .
echo "Renaming old binary on ${g_remote}..."
ssh "${g_remote}" "mv ~/bin/${g_bin} ~/bin/${g_bin}.old"
echo "Copying new binary..."
scp "${g_bin}-linux" "${g_remote}:~/bin/${g_bin}"
echo "Restarting ${g_service}..."
ssh "${g_remote}" "sudo systemctl restart ${g_service}"
sleep 2
echo "Verifying..."
if ! ssh "${g_remote}" "systemctl is-active ${g_service}" > /dev/null; then
echo " FAILED: ${g_service} not active. Logs:"
ssh "${g_remote}" "sudo journalctl -u ${g_service} -n 20 --no-pager"
echo " Backup still at ~/bin/${g_bin}.old — roll back with:"
echo " ssh ${g_remote} 'mv ~/bin/${g_bin}.old ~/bin/${g_bin} && sudo systemctl restart ${g_service}'"
exit 1
fi
echo "Removing backup..."
ssh "${g_remote}" "rm ~/bin/${g_bin}.old"
echo "OK: ${g_service} deployed and active."