fix: remember iso answers

This commit is contained in:
FUFSoB committed 2026-09-27 17:08:08 +05:00
1 parent 845291ee49
commit a24d2a976b
2 files changed
+202 -79

No files matched your search

+95 -63
View File
@@ -9,19 +9,33 @@
MNT=/mnt MNT=/mnt
SECRETS_DIR=/var/lib/proxy-suite-server SECRETS_DIR=/var/lib/proxy-suite-server
# On the live system: kept until the ISO reboots, so a rerun offers the same answers.
ANSWERS=/var/lib/proxy-suite-install/answers
if [[ $(id -u) != 0 ]]; then if [[ $(id -u) != 0 ]]; then
echo "proxy-suite-install: run it as root (sudo -i)" >&2 echo "proxy-suite-install: run it as root (sudo -i)" >&2
exit 1 exit 1
fi fi
confirm() { # yes installs; edit asks everything again, offering the current answers; anything
# else is asked again, so a typo does not throw the answers away.
confirm_install() {
local reply local reply
if [[ -n ${PSI_UNATTENDED-} ]]; then if [[ -n ${PSI_UNATTENDED-} ]]; then
return 0 return 0
fi fi
read -r -p "$1 Type yes to continue: " reply || true while true; do
[[ $reply == yes ]] read -r -p "Type yes to install, edit to change the answers, or quit: " reply || reply=quit
case ${reply,,} in
yes) return 0 ;;
edit) return 1 ;;
quit | q)
say "Aborted; nothing was written. sudo proxy-suite-install offers these answers again."
exit 1
;;
*) warn "Type yes, edit or quit." ;;
esac
done
} }
step() { printf '\n\033[1;36m== %s ==\033[0m\n' "$*"; } step() { printf '\n\033[1;36m== %s ==\033[0m\n' "$*"; }
@@ -52,68 +66,75 @@ clear
bold "proxy-suite server installer" bold "proxy-suite server installer"
say "Installs NixOS with VLESS REALITY, TLS and WS inbounds for one user, and SSH." say "Installs NixOS with VLESS REALITY, TLS and WS inbounds for one user, and SSH."
say "Every question has a default in [brackets]: press Enter to take it." say "Every question has a default in [brackets]: press Enter to take it."
say "Ctrl+C aborts; run sudo proxy-suite-install to start over." say "Ctrl+C aborts; sudo proxy-suite-install starts over, offering the answers given."
if [[ -z ${PSI_UNATTENDED-} ]] && load_answers "$ANSWERS"; then
# --- Network -------------------------------------------------------------------- say "The defaults are the answers from the last run."
step "Network"
setup_network || exit 1
step "Public address"
if [[ $NET_DHCP == false ]]; then
local_ip=${NET_V4%/*}
else
local_ip=$(ip -4 route get 1.1.1.1 | awk '{ for (i = 1; i < NF; i++) if ($i == "src") { print $(i + 1); exit } }')
fi fi
seen_ip=$(curl -4 -s --max-time 10 https://api.ipify.org || true)
default_ip=$local_ip # The questions, again from the top on edit: each offers the answer given before.
if [[ -n $seen_ip && $seen_ip != "$local_ip" ]] && is_ipv4 "$seen_ip"; then ask_all() {
local local_ip seen_ip default_ip disks
# --- Network ------------------------------------------------------------------
step "Network"
setup_network || exit 1
step "Public address"
if [[ $NET_DHCP == false ]]; then
local_ip=${NET_V4%/*}
else
local_ip=$(ip -4 route get 1.1.1.1 | awk '{ for (i = 1; i < NF; i++) if ($i == "src") { print $(i + 1); exit } }')
fi
seen_ip=$(curl -4 -s --max-time 10 https://api.ipify.org || true)
default_ip=$local_ip
if [[ -n $seen_ip && $seen_ip != "$local_ip" ]] && is_ipv4 "$seen_ip"; then
say "This server's address is $local_ip, but the internet sees it as $seen_ip (NAT)." say "This server's address is $local_ip, but the internet sees it as $seen_ip (NAT)."
default_ip=$seen_ip default_ip=$seen_ip
fi fi
while true; do while true; do
ask PUBLIC_IP "Public IPv4 clients connect to" "$default_ip" ask PUBLIC_IP "Public IPv4 clients connect to" "$default_ip"
is_ipv4 "$PUBLIC_IP" && break is_ipv4 "$PUBLIC_IP" && break
reject "Not an IPv4 address." PUBLIC_IP reject "Not an IPv4 address." PUBLIC_IP
done done
if ipv4_is_private "$PUBLIC_IP"; then if ipv4_is_private "$PUBLIC_IP"; then
warn "$PUBLIC_IP is a private address: clients outside cannot reach it, and no certificate" warn "$PUBLIC_IP is a private address: clients outside cannot reach it, and no certificate"
warn "can be issued for it. Use a domain, and forward the ports to this server." warn "can be issued for it. Use a domain, and forward the ports to this server."
fi fi
# --- Server --------------------------------------------------------------------- # --- Server -------------------------------------------------------------------
step "Server" step "Server"
while true; do while true; do
ask HOST_NAME "Hostname" "proxy-$(openssl rand -hex 2)" ask HOST_NAME "Hostname" "proxy-$(openssl rand -hex 2)"
[[ $HOST_NAME =~ ^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$ ]] && break [[ $HOST_NAME =~ ^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$ ]] && break
reject "Lowercase letters, digits and dashes only." HOST_NAME reject "Lowercase letters, digits and dashes only." HOST_NAME
done done
while true; do while true; do
ask ADMIN_USER "Admin user name (SSH login, sudo)" "admin" ask ADMIN_USER "Admin user name (SSH login, sudo)" "admin"
[[ $ADMIN_USER =~ ^[a-z_][a-z0-9_-]{0,31}$ && $ADMIN_USER != root ]] && break [[ $ADMIN_USER =~ ^[a-z_][a-z0-9_-]{0,31}$ && $ADMIN_USER != root ]] && break
reject "Lowercase letters, digits, - and _; not root." ADMIN_USER reject "Lowercase letters, digits, - and _; not root." ADMIN_USER
done done
while true; do while true; do
ask SSH_PORT "SSH port" "22" ask SSH_PORT "SSH port" "22"
if [[ $SSH_PORT =~ ^[0-9]+$ ]] && ((SSH_PORT >= 1 && SSH_PORT <= 65535)) && if [[ $SSH_PORT =~ ^[0-9]+$ ]] && ((SSH_PORT >= 1 && SSH_PORT <= 65535)) &&
[[ ! " 80 443 2053 8443 18533 18534 18535 " == *" $SSH_PORT "* ]]; then [[ ! " 80 443 2053 8443 18533 18534 18535 " == *" $SSH_PORT "* ]]; then
break break
fi fi
reject "A free port from 1 to 65535 (80, 443, 2053 and 8443 are taken)." SSH_PORT reject "A free port from 1 to 65535 (80, 443, 2053 and 8443 are taken)." SSH_PORT
done done
# --- Proxy ---------------------------------------------------------------------- # --- Proxy --------------------------------------------------------------------
step "Proxy" step "Proxy"
while true; do while true; do
ask PROXY_USER "Proxy user name (label of the share links)" "user" ask PROXY_USER "Proxy user name (label of the share links)" "user"
[[ $PROXY_USER =~ ^[A-Za-z0-9_.-]{1,32}$ ]] && break [[ $PROXY_USER =~ ^[A-Za-z0-9_.-]{1,32}$ ]] && break
reject "Letters, digits, dot, dash and underscore." PROXY_USER reject "Letters, digits, dot, dash and underscore." PROXY_USER
done done
say "" say ""
say "The TLS and WS inbounds need a certificate from Let's Encrypt. With a domain" say "The TLS and WS inbounds need a certificate from Let's Encrypt. With a domain"
say "(its A record pointing at $PUBLIC_IP) it is issued for the domain; without one," say "(its A record pointing at $PUBLIC_IP) it is issued for the domain; without one,"
say "for the IP itself (a six-day certificate, renewed automatically). Port 80 must be open." say "for the IP itself (a six-day certificate, renewed automatically). Port 80 must be open."
while true; do while true; do
ask DOMAIN "Domain (empty: certificate for the IP)" "" ask DOMAIN "Domain (empty: certificate for the IP)" ""
if [[ -z $DOMAIN ]]; then if [[ -z $DOMAIN ]]; then
break break
@@ -130,18 +151,19 @@ while true; do
ask_yes KEEP_DOMAIN "Keep $DOMAIN anyway" "y" ask_yes KEEP_DOMAIN "Keep $DOMAIN anyway" "y"
[[ $KEEP_DOMAIN == true ]] && break [[ $KEEP_DOMAIN == true ]] && break
unset PSI_DOMAIN PSI_KEEP_DOMAIN unset PSI_DOMAIN PSI_KEEP_DOMAIN
done forget KEEP_DOMAIN
done
while true; do while true; do
ask ACME_EMAIL "Email for Let's Encrypt (optional)" "" ask ACME_EMAIL "Email for Let's Encrypt (optional)" ""
[[ -z $ACME_EMAIL || $ACME_EMAIL =~ ^[^[:space:]\"@]+@[^[:space:]\"@]+$ ]] && break [[ -z $ACME_EMAIL || $ACME_EMAIL =~ ^[^[:space:]\"@]+@[^[:space:]\"@]+$ ]] && break
reject "Not an email address." ACME_EMAIL reject "Not an email address." ACME_EMAIL
done done
say "" say ""
say "REALITY impersonates another site's TLS. Pick a big HTTPS site, ideally hosted" say "REALITY impersonates another site's TLS. Pick a big HTTPS site, ideally hosted"
say "near this server and not blocked where your clients are." say "near this server and not blocked where your clients are."
while true; do while true; do
ask REALITY_SNI "REALITY site" "www.microsoft.com" ask REALITY_SNI "REALITY site" "www.microsoft.com"
if [[ ! $REALITY_SNI =~ ^([A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z]{2,}$ ]]; then if [[ ! $REALITY_SNI =~ ^([A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z]{2,}$ ]]; then
reject "Not a domain name." REALITY_SNI reject "Not a domain name." REALITY_SNI
@@ -154,30 +176,34 @@ while true; do
ask_yes KEEP_SNI "Keep $REALITY_SNI anyway" "n" ask_yes KEEP_SNI "Keep $REALITY_SNI anyway" "n"
[[ $KEEP_SNI == true ]] && break [[ $KEEP_SNI == true ]] && break
unset PSI_REALITY_SNI PSI_KEEP_SNI unset PSI_REALITY_SNI PSI_KEEP_SNI
done forget KEEP_SNI
done
# --- Disk ----------------------------------------------------------------------- # --- Disk ---------------------------------------------------------------------
step "Disk" step "Disk"
mapfile -t disks < <(lsblk -dnpo NAME,TYPE,RO | awk '$2 == "disk" && $3 == 0 && $1 !~ /\/(zram|loop|sr|fd)[0-9]/ { print $1 }') mapfile -t disks < <(lsblk -dnpo NAME,TYPE,RO | awk '$2 == "disk" && $3 == 0 && $1 !~ /\/(zram|loop|sr|fd)[0-9]/ { print $1 }')
if ((${#disks[@]} == 0)); then if ((${#disks[@]} == 0)); then
warn "No writable disk found." warn "No writable disk found."
exit 1 exit 1
fi fi
lsblk -dpo NAME,SIZE,MODEL "${disks[@]}" lsblk -dpo NAME,SIZE,MODEL "${disks[@]}"
while true; do while true; do
ask DISK "Disk to install on (ERASED)" "$(if ((${#disks[@]} == 1)); then printf '%s' "${disks[0]}"; fi)" ask DISK "Disk to install on (ERASED)" "$(if ((${#disks[@]} == 1)); then printf '%s' "${disks[0]}"; fi)"
[[ -b $DISK && " ${disks[*]} " == *" $DISK "* ]] && break [[ -b $DISK && " ${disks[*]} " == *" $DISK "* ]] && break
reject "Not one of the disks above." DISK reject "Not one of the disks above." DISK
done done
}
# --- Summary -------------------------------------------------------------------- # --- Summary --------------------------------------------------------------------
step "Summary" show_summary() {
if [[ $NET_DHCP == true ]]; then local net_summary
step "Summary"
if [[ $NET_DHCP == true ]]; then
net_summary="DHCP on $NET_IFACE ($NET_MAC)" net_summary="DHCP on $NET_IFACE ($NET_MAC)"
else else
net_summary="$NET_V4 via $NET_V4_GW${NET_V6:+, $NET_V6 via $NET_V6_GW} on $NET_IFACE ($NET_MAC)" net_summary="$NET_V4 via $NET_V4_GW${NET_V6:+, $NET_V6 via $NET_V6_GW} on $NET_IFACE ($NET_MAC)"
fi fi
cat <<SUMMARY cat <<SUMMARY
Disk $DISK (everything on it is erased) Disk $DISK (everything on it is erased)
Network $net_summary Network $net_summary
DNS $NET_DNS DNS $NET_DNS
@@ -189,11 +215,17 @@ cat <<SUMMARY
REALITY 443 as $REALITY_SNI REALITY 443 as $REALITY_SNI
TLS / WS 8443 / 2053 TLS / WS 8443 / 2053
SUMMARY SUMMARY
confirm "Install now?" || {
say "Aborted; nothing was written."
exit 1
} }
ask_all
while true; do
show_summary
if confirm_install; then
break
fi
ask_all
done
# --- Partition ------------------------------------------------------------------ # --- Partition ------------------------------------------------------------------
step "Partitioning $DISK" step "Partitioning $DISK"
swapoff -a || true swapoff -a || true
+107 -16
View File
@@ -2,8 +2,9 @@
# applied through a systemd-networkd file in /run, the same way the installed system # applied through a systemd-networkd file in /run, the same way the installed system
# applies them from /etc, so settings that work here work after the reboot. # applies them from /etc, so settings that work here work after the reboot.
# #
# Sets NET_IFACE NET_MAC NET_DHCP NET_V4 NET_V4_GW NET_V6 NET_V6_GW NET_DNS. Unattended # Sets NET_IFACE NET_MAC NET_DHCP NET_V4 NET_V4_GW NET_V6 NET_V6_GW NET_DNS. A wrong
# runs (PSI_UNATTENDED=1) take every answer from PSI_<NAME> and fail instead of re-asking. # answer asks that question again. Unattended runs (PSI_UNATTENDED=1) take every answer
# from PSI_<NAME> and fail instead of re-asking.
NET_RUNTIME_FILE=/run/systemd/network/05-proxy-suite-net.network NET_RUNTIME_FILE=/run/systemd/network/05-proxy-suite-net.network
@@ -11,33 +12,95 @@ say() { printf '%s\n' "$*"; }
warn() { printf '\033[1;33m%s\033[0m\n' "$*" >&2; } warn() { printf '\033[1;33m%s\033[0m\n' "$*" >&2; }
bold() { printf '\033[1m%s\033[0m\n' "$*"; } bold() { printf '\033[1m%s\033[0m\n' "$*"; }
# Answers given so far, by VAR: asking VAR again offers the last answer as the default.
# With ANSWERS_FILE set (load_answers), they also outlive the run.
declare -A REMEMBERED=()
ANSWERS_FILE=""
# load_answers FILE: keep answers in FILE, starting from those it already holds.
# Succeeds only if it held some.
load_answers() {
ANSWERS_FILE=$1
[[ -s $ANSWERS_FILE ]] || return 1
# shellcheck source=/dev/null
source "$ANSWERS_FILE"
}
save_answers() {
local name
[[ -n $ANSWERS_FILE ]] || return 0
install -d -m 0700 "$(dirname "$ANSWERS_FILE")"
for name in "${!REMEMBERED[@]}"; do
printf 'REMEMBERED[%s]=%q\n' "$name" "${REMEMBERED[$name]}"
done >"$ANSWERS_FILE.tmp"
mv "$ANSWERS_FILE.tmp" "$ANSWERS_FILE"
}
forget() {
local name
for name in "$@"; do unset "REMEMBERED[$name]"; done
save_answers
}
# ask VAR "Question" [default]. PSI_<VAR> in the environment answers it unattended. # ask VAR "Question" [default]. PSI_<VAR> in the environment answers it unattended.
# An earlier answer replaces the default. An empty default makes the question
# optional: "-" answers it with nothing even when an earlier answer is offered.
ask() { ask() {
local __preset="PSI_$1" __reply local __preset="PSI_$1" __reply __default=${3-} __hint __optional=false
if [[ -n ${!__preset+x} ]]; then if [[ -n ${!__preset+x} ]]; then
__reply=${!__preset} __reply=${!__preset}
elif [[ -n ${3-} ]]; then [[ -n $__reply ]] || __reply=$__default
read -r -p "$2 [$3]: " __reply || true printf -v "$1" '%s' "$__reply"
return 0
fi
if [[ -n ${3+x} && -z $3 ]]; then
__optional=true
fi
if [[ -n ${REMEMBERED[$1]-} ]]; then
__default=${REMEMBERED[$1]}
fi
__hint=$__default
if [[ $__optional == true && -n $__default ]]; then
__hint+=", - for none"
fi
if [[ -n $__hint ]]; then
read -r -p "$2 [$__hint]: " __reply || true
else else
read -r -p "$2: " __reply || true read -r -p "$2: " __reply || true
fi fi
[[ -n $__reply ]] || __reply=${3-} if [[ $__optional == true && $__reply == - ]]; then
__reply=""
elif [[ -z $__reply ]]; then
__reply=$__default
fi
REMEMBERED[$1]=$__reply
save_answers
printf -v "$1" '%s' "$__reply" printf -v "$1" '%s' "$__reply"
} }
# ask_yes VAR "Question" y|n; sets VAR to true or false. # ask_yes VAR "Question" y|n; sets VAR to true or false.
ask_yes() { ask_yes() {
local __yn local __yn
while true; do
ask "$1" "$2 (y/n)" "$3" ask "$1" "$2 (y/n)" "$3"
__yn=${!1} __yn=${!1}
case ${__yn,,} in case ${__yn,,} in
y | yes | true) printf -v "$1" '%s' true ;; y | yes | true)
*) printf -v "$1" '%s' false ;; printf -v "$1" '%s' true
return 0
;;
n | no | false)
printf -v "$1" '%s' false
return 0
;;
esac esac
reject "Answer y or n." "$1"
done
} }
# Drop a rejected answer so the next ask prompts; unattended, give up instead. # retry MESSAGE VAR...: ask VARs again, offering the answers just given; unattended,
reject() { # give up instead.
retry() {
warn "$1" warn "$1"
shift shift
if [[ -n ${PSI_UNATTENDED-} ]]; then if [[ -n ${PSI_UNATTENDED-} ]]; then
@@ -47,6 +110,13 @@ reject() {
for name in "$@"; do unset "PSI_$name"; done for name in "$@"; do unset "PSI_$name"; done
} }
# reject MESSAGE VAR...: like retry, but the answers are wrong, so they are not offered.
reject() {
retry "$@"
shift
forget "$@"
}
is_ipv4() { is_ipv4() {
local IFS=. octet local IFS=. octet
[[ $1 =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}$ ]] || return 1 [[ $1 =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}$ ]] || return 1
@@ -55,6 +125,15 @@ is_ipv4() {
is_ipv6() { [[ $1 == *:* && $1 =~ ^[0-9A-Fa-f:.]+$ ]]; } is_ipv6() { [[ $1 == *:* && $1 =~ ^[0-9A-Fa-f:.]+$ ]]; }
is_dns_list() {
local dns count=0
for dns in $1; do
is_ipv4 "$dns" || is_ipv6 "$dns" || return 1
count=$((count + 1))
done
((count > 0))
}
mask_to_prefix() { mask_to_prefix() {
local mask=$1 IFS=. octet bits=0 local mask=$1 IFS=. octet bits=0
if [[ $mask =~ ^[0-9]+$ ]]; then if [[ $mask =~ ^[0-9]+$ ]]; then
@@ -177,19 +256,31 @@ ask_network() {
say "Gateway $NET_V4_GW is outside $NET_V4: it is routed on-link, as providers with /32 addresses expect." say "Gateway $NET_V4_GW is outside $NET_V4: it is routed on-link, as providers with /32 addresses expect."
fi fi
while true; do
ask NET_V6_IN "IPv6 address with prefix (empty to skip)" "" ask NET_V6_IN "IPv6 address with prefix (empty to skip)" ""
if [[ -n $NET_V6_IN ]]; then [[ -n $NET_V6_IN ]] || break
[[ $NET_V6_IN == */* ]] || NET_V6_IN="$NET_V6_IN/64" [[ $NET_V6_IN == */* ]] || NET_V6_IN="$NET_V6_IN/64"
if is_ipv6 "${NET_V6_IN%/*}"; then prefix=${NET_V6_IN#*/}
if is_ipv6 "${NET_V6_IN%/*}" && [[ $prefix =~ ^[0-9]+$ ]] && ((prefix >= 1 && prefix <= 128)); then
NET_V6=$NET_V6_IN NET_V6=$NET_V6_IN
ask NET_V6_GW "IPv6 gateway" "fe80::1" break
else
warn "Not an IPv6 address; skipping IPv6."
fi fi
reject "Not an IPv6 address with a valid prefix." NET_V6_IN
done
if [[ -n $NET_V6 ]]; then
while true; do
ask NET_V6_GW "IPv6 gateway" "fe80::1"
is_ipv6 "$NET_V6_GW" && break
reject "Not an IPv6 address." NET_V6_GW
done
fi fi
fi fi
while true; do
ask NET_DNS "DNS servers (space-separated)" "1.1.1.1 8.8.8.8" ask NET_DNS "DNS servers (space-separated)" "1.1.1.1 8.8.8.8"
is_dns_list "$NET_DNS" && break
reject "One or more IPv4 or IPv6 addresses, separated by spaces." NET_DNS
done
} }
# Mirrors the systemd.network config of deploy/server-module.nix. # Mirrors the systemd.network config of deploy/server-module.nix.
@@ -261,7 +352,7 @@ setup_network() {
if apply_network && test_network; then if apply_network && test_network; then
return 0 return 0
fi fi
reject "Network settings did not work; enter them again." \ retry "Network settings did not work; enter them again." \
NET_IFACE NET_DHCP NET_ADDR NET_MASK NET_V4_GW NET_V6_IN NET_V6_GW NET_DNS NET_IFACE NET_DHCP NET_ADDR NET_MASK NET_V4_GW NET_V6_IN NET_V6_GW NET_DNS
done done
} }