fix: make keygen autodetect source ip

This commit is contained in:
Marek Miklewicz
2026-06-30 13:03:22 +02:00
parent d43c87486c
commit b27c5c774a
4 changed files with 52 additions and 6 deletions
+27 -3
View File
@@ -5,7 +5,6 @@ KEY_DIR="${MAIL_LOGIN_KEY_DIR:-/usr/local/hitme_plugins/mail-login/secrets}"
KEY_NAME="${MAIL_LOGIN_KEY_NAME:-mx1_ed25519}"
KEY_PATH="$KEY_DIR/$KEY_NAME"
MX_MODE="${MX_LOGIN_MODE:-1}"
SOURCE_IP="${SOURCE_IP:-SOURCE_SERVER_PUBLIC_IP}"
HELPER_COMMAND="${MAIL_LOGIN_HELPER_COMMAND:-/usr/local/hitme_plugins/mail-login-helper/bin/create-login-url}"
derive_servername() {
@@ -21,6 +20,31 @@ clean_label() {
printf '%s' "$1" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g; s/^-*//; s/-*$//'
}
detect_source_ip() {
if [ -n "${SOURCE_IP:-}" ]; then
printf '%s' "$SOURCE_IP"
return
fi
if command -v ip >/dev/null 2>&1; then
detected="$(ip route get 1.1.1.1 2>/dev/null | awk '{for (i=1;i<=NF;i++) if ($i=="src") {print $(i+1); exit}}')"
if [ -n "$detected" ]; then
printf '%s' "$detected"
return
fi
fi
if command -v hostname >/dev/null 2>&1; then
detected="$(hostname -I 2>/dev/null | awk '{print $1}')"
if [ -n "$detected" ]; then
printf '%s' "$detected"
return
fi
fi
return 1
}
SERVER_LABEL="$(clean_label "$(derive_servername)")"
if [ -z "$SERVER_LABEL" ]; then
echo "Cannot determine SERVERNAME" >&2
@@ -35,9 +59,10 @@ case "$MX_MODE" in
;;
esac
SOURCE_IP="$(detect_source_ip || true)"
case "$SOURCE_IP" in
*[!A-Za-z0-9:./_-]*|'')
echo "SOURCE_IP contains unsupported characters" >&2
echo "Cannot determine SOURCE_IP automatically. Set SOURCE_IP explicitly." >&2
exit 1
;;
esac
@@ -82,4 +107,3 @@ MAIL_LOGIN_SSH_KNOWN_HOSTS=/root/.ssh/known_hosts
Add this line to /root/.ssh/authorized_keys on mx1:
$AUTH_OPTIONS $PUB_KEY $COMMENT
EOF