#!/usr/bin/env bash set -euo pipefail if [[ "${EUID}" -ne 0 ]]; then echo "Run this test activator as root." >&2 exit 1 fi console_host="${1:-}" if [[ ! "${console_host}" =~ ^[A-Za-z0-9.-]+$ ]] || [[ "${console_host}" == .* ]] || [[ "${console_host}" == *..* ]]; then echo "Invalid console host." >&2 exit 1 fi env_file=/etc/sea-vhost/sea-vhost.env nginx_binary=/www/server/nginx/sbin/nginx nginx_config=/www/server/panel/vhost/nginx/sea-vhost-console.conf if [[ ! -f "${env_file}" || -L "${env_file}" ]]; then echo "Sea vHost environment file is missing or unsafe." >&2 exit 1 fi if [[ ! -x "${nginx_binary}" ]]; then echo "aaPanel Nginx binary is unavailable." >&2 exit 1 fi had_config=false if [[ -e "${nginx_config}" ]]; then if [[ ! -f "${nginx_config}" || -L "${nginx_config}" ]]; then echo "Existing console Nginx configuration is unsafe." >&2 exit 1 fi had_config=true fi env_backup="$(mktemp)" config_backup="$(mktemp)" temporary_config="" cp --preserve=mode,ownership,timestamps -- "${env_file}" "${env_backup}" if [[ "${had_config}" == true ]]; then cp --preserve=mode,ownership,timestamps -- "${nginx_config}" "${config_backup}" fi rollback() { cp --preserve=mode,ownership,timestamps -- "${env_backup}" "${env_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 } trap 'rollback; [[ -z "${temporary_config}" ]] || rm -f -- "${temporary_config}"; rm -f -- "${env_backup}" "${config_backup}"' ERR if grep -q '^SEA_COOKIE_SECURE=' "${env_file}"; then sed -i 's/^SEA_COOKIE_SECURE=.*/SEA_COOKIE_SECURE=false/' "${env_file}" else echo 'SEA_COOKIE_SECURE=false' >> "${env_file}" fi chown root:sea-vhost-control "${env_file}" chmod 0640 "${env_file}" temporary_config="$(mktemp /www/server/panel/vhost/nginx/.sea-vhost-console-XXXXXX.tmp)" cat > "${temporary_config}" </dev/null; then break fi sleep 1 done curl --fail --silent --show-error http://127.0.0.1:8080/healthz "${nginx_binary}" -s reload trap - ERR rm -f -- "${env_backup}" "${config_backup}" echo "Sea vHost test activation result: success"