206 lines
7.0 KiB
Bash
Executable File
206 lines
7.0 KiB
Bash
Executable File
#!/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}" <<EOF
|
|
# Managed by Sea vHost. The certificate files are maintained by aaPanel.
|
|
# Local-only aaPanel API bridge. aaPanel itself uses a self-signed certificate
|
|
# on its loopback listener; the broker talks to this private HTTP endpoint and
|
|
# never exposes the panel API key to the Web process.
|
|
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 127.0.0.1:${panel_bridge_port};
|
|
server_name _;
|
|
access_log off;
|
|
|
|
location / {
|
|
proxy_pass https://127.0.0.1:${panel_local_port};
|
|
proxy_ssl_verify off;
|
|
proxy_set_header Host 127.0.0.1:${panel_local_port};
|
|
proxy_set_header X-Real-IP 127.0.0.1;
|
|
proxy_connect_timeout 3s;
|
|
proxy_read_timeout 300s;
|
|
}
|
|
}
|
|
|
|
server {
|
|
listen 80;
|
|
server_name ${management_host};
|
|
server_tokens off;
|
|
|
|
location ^~ /.well-known/acme-challenge/ {
|
|
root /opt/sea-vhost/web;
|
|
try_files \$uri =404;
|
|
}
|
|
location / {
|
|
return 301 https://\$host\$request_uri;
|
|
}
|
|
}
|
|
|
|
server {
|
|
listen 443 ssl;
|
|
http2 on;
|
|
server_name ${management_host};
|
|
server_tokens off;
|
|
|
|
ssl_certificate ${certificate_file};
|
|
ssl_certificate_key ${private_key_file};
|
|
ssl_protocols TLSv1.2 TLSv1.3;
|
|
ssl_session_cache shared:SeaVHostSSL:10m;
|
|
ssl_session_timeout 10m;
|
|
|
|
root /opt/sea-vhost/web;
|
|
index index.html;
|
|
client_max_body_size 21m;
|
|
|
|
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
|
|
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;
|
|
proxy_cache off;
|
|
proxy_set_header Range \$http_range;
|
|
proxy_set_header If-Range \$http_if_range;
|
|
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 https;
|
|
proxy_connect_timeout 3s;
|
|
proxy_read_timeout 300s;
|
|
}
|
|
|
|
location = /healthz {
|
|
proxy_pass http://127.0.0.1:8080;
|
|
proxy_set_header Host \$http_host;
|
|
proxy_set_header X-Forwarded-Proto https;
|
|
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}"
|
|
temporary_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..30}; do
|
|
if curl --fail --silent --show-error --max-time 3 --header "Host: ${management_host}" http://127.0.0.1:8080/healthz >/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"
|