diff --git a/start.sh b/start.sh index 03e725c..99438bb 100755 --- a/start.sh +++ b/start.sh @@ -69,10 +69,24 @@ if [[ ! -f "$ENV_FILE" ]]; then fi # Safe reader for existing values (does not fail under set -e -o pipefail) +# Returns only the value part after the first "=", empty string if key missing or value is empty. ini_val() { local key="$1" local val - val=$(awk -F'=' -v k="$key" 'BEGIN{found=0} $1==k{print substr($0, index($0,$2)); found=1} END{ if(!found){} }' "$ENV_FILE" 2>/dev/null || true) + val=$(awk -F'=' -v k="$key" ' + $1 == k { + if (NF == 1) { + # Key present but no "=", treat as empty value + print "" + } else { + # Reconstruct everything after the first "=" + $1 = "" + sub(/^=/, "", $0) + print $0 + } + exit + } + ' "$ENV_FILE" 2>/dev/null || true) echo -n "$val" } @@ -104,6 +118,16 @@ sanitize_assignment() { echo "$value" } +is_valid_port() { + local p="$1" + [[ "$p" =~ ^[0-9]+$ ]] && (( p > 0 && p <= 65535 )) +} + +is_positive_int() { + local n="$1" + [[ "$n" =~ ^[0-9]+$ ]] && (( n > 0 )) +} + # Helper to prompt only if missing in .env ask_or_keep() { local key="$1"; shift @@ -136,6 +160,88 @@ ask_or_keep() { eval "$key=\"$val\"" } +# Helper specifically for port values: validates and reprompts until a correct port is provided. +ask_port_or_keep() { + local key="$1"; shift + local prompt="$1"; shift + local def="${1:-}" + + local cur + cur=$(ini_val "$key") + cur=$(sanitize_assignment "$key" "$cur") + + if is_valid_port "$cur"; then + echo "$key is set in .env; keeping existing value ($cur)" + eval "$key=\"$cur\"" + return + elif [[ -n "$cur" ]]; then + echo "Existing value for $key in .env is invalid ('$cur'); you will be asked to enter a valid port." + fi + + local hint + hint=$(sanitize_assignment "$key" "$def") + if ! is_valid_port "$hint"; then + hint="" + fi + + while true; do + local val + if [[ -n "$hint" ]]; then + read -rp "$prompt (1-65535) [${hint}]: " val || true + val=${val:-$hint} + else + read -rp "$prompt (1-65535): " val || true + fi + val=$(sanitize_assignment "$key" "$val") + if is_valid_port "$val"; then + eval "$key=\"$val\"" + break + fi + echo "Invalid port '$val'. Please enter a number between 1 and 65535." + done +} + +# Helper for positive integer values (e.g. intervals) +ask_positive_int_or_keep() { + local key="$1"; shift + local prompt="$1"; shift + local def="${1:-}" + + local cur + cur=$(ini_val "$key") + cur=$(sanitize_assignment "$key" "$cur") + + if is_positive_int "$cur"; then + echo "$key is set in .env; keeping existing value ($cur)" + eval "$key=\"$cur\"" + return + elif [[ -n "$cur" ]]; then + echo "Existing value for $key in .env is invalid ('$cur'); you will be asked to enter a valid integer." + fi + + local hint + hint=$(sanitize_assignment "$key" "$def") + if ! is_positive_int "$hint"; then + hint="60" + fi + + while true; do + local val + if [[ -n "$hint" ]]; then + read -rp "$prompt (integer) [${hint}]: " val || true + val=${val:-$hint} + else + read -rp "$prompt (integer): " val || true + fi + val=$(sanitize_assignment "$key" "$val") + if is_positive_int "$val"; then + eval "$key=\"$val\"" + break + fi + echo "Invalid value '$val'. Please enter a positive integer." + done +} + if [[ "$NODE_PRIVACY" == "private" ]]; then # For private nodes, PUBLIC_HOST optional; only ask if missing ask_or_keep PUBLIC_HOST "Public host URL (leave empty for private)" "" @@ -143,10 +249,10 @@ else ask_or_keep PUBLIC_HOST "Public host URL (e.g., https://node.example.com)" "$(ini_val PUBLIC_HOST)" fi -ask_or_keep SANIC_PORT "Internal app port (SANIC_PORT)" "$(ini_val SANIC_PORT)" -ask_or_keep BACKEND_HTTP_PORT "Published backend port on host (BACKEND_HTTP_PORT)" "$(ini_val BACKEND_HTTP_PORT)" +ask_port_or_keep SANIC_PORT "Internal app port (SANIC_PORT)" "$(ini_val SANIC_PORT)" +ask_port_or_keep BACKEND_HTTP_PORT "Published backend port on host (BACKEND_HTTP_PORT)" "$(ini_val BACKEND_HTTP_PORT)" ask_or_keep BOOTSTRAP_SEEDS "Bootstrap seeds (comma-separated URLs)" "" "https://my-bootstrap-1.example.com,https://my-bootstrap-2.example.com" -ask_or_keep HANDSHAKE_INTERVAL_SEC "Handshake interval seconds" "$(ini_val HANDSHAKE_INTERVAL_SEC)" +ask_positive_int_or_keep HANDSHAKE_INTERVAL_SEC "Handshake interval seconds" "$(ini_val HANDSHAKE_INTERVAL_SEC)" ask_or_keep TELEGRAM_API_KEY "Telegram uploader bot token (TELEGRAM_API_KEY)" "" "YOUR_UPLOADER_BOT_TOKEN" ask_or_keep CLIENT_TELEGRAM_API_KEY "Telegram client bot token (CLIENT_TELEGRAM_API_KEY)" "" "YOUR_CLIENT_BOT_TOKEN"