133 lines
4.3 KiB
Bash
Executable File
133 lines
4.3 KiB
Bash
Executable File
#!/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}" <<EOF
|
|
# Managed by Sea vHost test activation. Replace with TLS for production.
|
|
map \$upstream_http_content_security_policy \$sea_vhost_csp {
|
|
"" "default-src 'self'; base-uri 'self'; object-src 'none'; frame-ancestors 'none'; form-action 'self'; connect-src 'self'; img-src 'self' data:; font-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self'";
|
|
default \$upstream_http_content_security_policy;
|
|
}
|
|
server {
|
|
listen 80;
|
|
server_name ${console_host};
|
|
server_tokens off;
|
|
|
|
root /opt/sea-vhost/web;
|
|
index index.html;
|
|
client_max_body_size 21m;
|
|
|
|
add_header X-Content-Type-Options "nosniff" always;
|
|
add_header X-Frame-Options "DENY" always;
|
|
add_header Referrer-Policy "same-origin" always;
|
|
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
|
|
add_header Content-Security-Policy \$sea_vhost_csp always;
|
|
|
|
proxy_hide_header Content-Security-Policy;
|
|
|
|
location /api/ {
|
|
proxy_pass http://127.0.0.1:8080;
|
|
proxy_http_version 1.1;
|
|
# Preserve an externally forwarded non-standard port so the API can
|
|
# compare the browser Origin to the original Host without weakening
|
|
# its CSRF same-origin policy.
|
|
proxy_set_header Host \$http_host;
|
|
proxy_set_header X-Real-IP \$remote_addr;
|
|
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto http;
|
|
proxy_connect_timeout 3s;
|
|
proxy_read_timeout 300s;
|
|
}
|
|
|
|
location = /healthz {
|
|
proxy_pass http://127.0.0.1:8080;
|
|
access_log off;
|
|
}
|
|
|
|
location / {
|
|
try_files \$uri \$uri/ /index.html;
|
|
}
|
|
|
|
location ~ /\. {
|
|
deny all;
|
|
}
|
|
}
|
|
EOF
|
|
chown root:root "${temporary_config}"
|
|
chmod 0644 "${temporary_config}"
|
|
mv -f -- "${temporary_config}" "${nginx_config}"
|
|
|
|
"${nginx_binary}" -t
|
|
systemctl enable sea-vhost-agent sea-vhost-panel-broker sea-vhost-api
|
|
systemctl restart sea-vhost-agent sea-vhost-panel-broker sea-vhost-api
|
|
for _ in {1..20}; do
|
|
if curl --fail --silent --show-error http://127.0.0.1:8080/healthz >/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"
|