#!/usr/bin/env bash set -euo pipefail management_host="${SEA_MANAGEMENT_DOMAIN:-}" panel_local_port="${SEA_PANEL_LOCAL_PORT:-}" panel_bridge_port="${SEA_PANEL_BRIDGE_PORT:-7813}" if [[ ! "${management_host}" =~ ^[A-Za-z0-9.-]+$ ]] || [[ "${management_host}" == .* ]] || [[ "${management_host}" == *..* ]]; then echo "SEA_MANAGEMENT_DOMAIN is invalid." >&2 exit 1 fi for port in "${panel_local_port}" "${panel_bridge_port}"; do if [[ ! "${port}" =~ ^[0-9]+$ ]] || (( port < 1 || port > 65535 )); then echo "The panel and bridge ports must be valid TCP ports." >&2 exit 1 fi done if [[ "${panel_local_port}" == "${panel_bridge_port}" ]]; then echo "The local aaPanel and bridge ports must differ." >&2 exit 1 fi nginx_binary=/www/server/nginx/sbin/nginx environment_file=/etc/sea-vhost/sea-vhost.env nginx_config="/www/server/panel/vhost/nginx/${management_host}.conf" certificate_directory="/www/server/panel/vhost/cert/${management_host}" certificate_file="${certificate_directory}/fullchain.pem" private_key_file="${certificate_directory}/privkey.pem" for regular_file in "${environment_file}" "${certificate_file}" "${private_key_file}"; do if [[ ! -f "${regular_file}" || -L "${regular_file}" ]]; then printf 'Required regular file is missing or unsafe: %s\n' "${regular_file}" >&2 exit 1 fi done if [[ ! -x "${nginx_binary}" ]]; then echo "aaPanel Nginx is unavailable." >&2 exit 1 fi if ! openssl x509 -in "${certificate_file}" -noout -checkhost "${management_host}" >/dev/null; then echo "The installed certificate does not cover the management domain." >&2 exit 1 fi if ! openssl pkey -in "${private_key_file}" -noout -check >/dev/null 2>&1; then echo "The installed certificate private key is invalid." >&2 exit 1 fi had_config=false config_backup="$(mktemp)" environment_backup="$(mktemp)" temporary_config="" if [[ -e "${nginx_config}" ]]; then if [[ ! -f "${nginx_config}" || -L "${nginx_config}" ]]; then echo "The existing Nginx vhost is not a safe regular file." >&2 exit 1 fi had_config=true cp --preserve=mode,ownership,timestamps -- "${nginx_config}" "${config_backup}" fi cp --preserve=mode,ownership,timestamps -- "${environment_file}" "${environment_backup}" rollback() { cp --preserve=mode,ownership,timestamps -- "${environment_backup}" "${environment_file}" || true if [[ "${had_config}" == true ]]; then cp --preserve=mode,ownership,timestamps -- "${config_backup}" "${nginx_config}" || true else rm -f -- "${nginx_config}" fi "${nginx_binary}" -t >/dev/null 2>&1 && "${nginx_binary}" -s reload >/dev/null 2>&1 || true } cleanup() { [[ -z "${temporary_config}" ]] || rm -f -- "${temporary_config}" rm -f -- "${config_backup}" "${environment_backup}" } trap 'rollback; cleanup' ERR if grep -q '^SEA_COOKIE_SECURE=' "${environment_file}"; then sed -i 's/^SEA_COOKIE_SECURE=.*/SEA_COOKIE_SECURE=true/' "${environment_file}" else echo 'SEA_COOKIE_SECURE=true' >>"${environment_file}" fi chown root:sea-vhost-control "${environment_file}" chmod 0640 "${environment_file}" temporary_config="$(mktemp /www/server/panel/vhost/nginx/.sea-vhost-production-XXXXXX.tmp)" cat >"${temporary_config}" </dev/null; then break fi sleep 1 done curl --fail --silent --show-error --max-time 3 --header "Host: ${management_host}" http://127.0.0.1:8080/healthz >/dev/null "${nginx_binary}" -s reload for _ in {1..20}; do if curl --fail --silent --show-error --max-time 5 --resolve "${management_host}:443:127.0.0.1" "https://${management_host}/healthz" >/dev/null; then break fi sleep 1 done curl --fail --silent --show-error --max-time 5 --resolve "${management_host}:443:127.0.0.1" "https://${management_host}/healthz" >/dev/null trap - ERR cleanup echo "Sea vHost production activation result: success"