From 4a4ced1a28a0d487d606c43141132a10b5d563a9 Mon Sep 17 00:00:00 2001 From: Anh Duc Le Date: Wed, 5 Nov 2025 10:39:08 +0700 Subject: [PATCH] Add domain verification and local SSL configuration to mkcert.sh --- bin/mkcert.sh | 354 +++++++++++++++++++++++++++++++------------------- 1 file changed, 219 insertions(+), 135 deletions(-) diff --git a/bin/mkcert.sh b/bin/mkcert.sh index 706f37f..45f072b 100644 --- a/bin/mkcert.sh +++ b/bin/mkcert.sh @@ -2,7 +2,6 @@ DOMAIN='' INSTALL='' REMOVE='' -TEST='' CONT_NAME='litespeed' CERT_DIR='./certs' EPACE=' ' @@ -137,10 +136,27 @@ create_cert_dir(){ fi } +# Function to verify if domain has been added (by checking document root existence) +domain_verify(){ + local domain="${1}" + local doc_path="/var/www/vhosts/${domain}/html" + + echo "[!] Checking if domain '${domain}' has been added..." + + if docker compose exec -T ${CONT_NAME} bash -c "[ -d ${doc_path} ]" 2>/dev/null; then + echo -e "[O] Domain \033[32m${domain}\033[0m exists (document root found)" + return 0 + else + echo -e "[X] Domain \033[31m${domain}\033[0m has NOT been added yet!" + echo "[!] Document root not found: ${doc_path}" + echo "[!] Please add this domain first using: bash bin/domain.sh -a ${domain}" + exit 1 + fi +} + # Function to generate SSL certificate using mkcert generate_cert(){ echo '[Start] Generating SSL certificate' - domain_filter "${DOMAIN}" www_domain "${DOMAIN}" create_cert_dir @@ -170,8 +186,78 @@ generate_cert(){ echo '[End] Generating SSL certificate' } +# Function to create docker-local.conf template for local development +create_local_template(){ + echo '[Start] Creating docker-local.conf template' + + local source_file="/usr/local/lsws/conf/templates/docker.conf" + local dest_file="/usr/local/lsws/conf/templates/docker-local.conf" + + # Check if template file already exists + if docker compose exec -T ${CONT_NAME} bash -c "[ -f ${dest_file} ]" 2>/dev/null; then + echo "[i] Template file already exists: ${dest_file}" + echo '[End] Creating docker-local.conf template' + return 0 + fi + + # Copy and modify template file in a single command + docker compose exec -T ${CONT_NAME} bash -c " + # Copy template file + cp ${source_file} ${dest_file} + + # Remove old vhssl block and last closing brace + sed -i '/^ vhssl {/,/^ }/d; \$d' ${dest_file} + + # Append new vhssl configuration + cat >> ${dest_file} <<'VHSSL_EOF' + vhssl { + keyFile /usr/local/lsws/conf/cert/\$VH_NAME/key.pem + certFile /usr/local/lsws/conf/cert/\$VH_NAME/cert.pem + certChain 1 + } +} +VHSSL_EOF + + # Fix ownership and permissions + chown nobody:nogroup ${dest_file} 2>/dev/null || chown lsadm:lsadm ${dest_file} + chmod 644 ${dest_file} + " + + echo -e "[O] Template \033[32mdocker-local.conf\033[0m created successfully!" + echo -e " SSL certificates path: /usr/local/lsws/conf/cert/\$VH_NAME/" + echo '[End] Creating docker-local.conf template' +} + +# Function to register dockerLocal vhTemplate in httpd_config.conf +register_local_template() { + echo '[Start] Registering vhTemplate: dockerLocal' + + local config_file="/usr/local/lsws/conf/httpd_config.conf" + local template_name="dockerLocal" + local template_path="conf/templates/docker-local.conf" + + docker compose exec -T ${CONT_NAME} bash -c " + if ! grep -q 'vhTemplate ${template_name} {' ${config_file}; then + cat >> ${config_file} <> ${vhconf_path} </dev/null' @@ -259,112 +436,22 @@ lsws_restart() { fi } -remove_cert(){ - echo '[Start] Removing SSL certificate' - domain_filter "${DOMAIN}" - - CERT_FILE="${DOMAIN}+1.pem" - KEY_FILE="${DOMAIN}+1-key.pem" - LSWS_CONF_DIR="/usr/local/lsws/conf" - HTTPD_CONF="${LSWS_CONF_DIR}/httpd_config.conf" - - # 1. Xóa chứng chỉ trên host - if [ -f "${CERT_DIR}/${CERT_FILE}" ]; then - rm "${CERT_DIR}/${CERT_FILE}" - echo -e "[O] Removed: ${CERT_DIR}/${CERT_FILE}" - else - echo "[!] Certificate file not found: ${CERT_DIR}/${CERT_FILE}" - fi - - if [ -f "${CERT_DIR}/${KEY_FILE}" ]; then - rm "${CERT_DIR}/${KEY_FILE}" - echo -e "[O] Removed: ${CERT_DIR}/${KEY_FILE}" - else - echo "[!] Key file not found: ${CERT_DIR}/${KEY_FILE}" - fi - - # 2. Xóa chứng chỉ trong container - docker compose exec -T ${CONT_NAME} bash -c " - if [ -f ${LSWS_CONF_DIR}/cert/${CERT_FILE} ]; then - rm ${LSWS_CONF_DIR}/cert/${CERT_FILE} - echo '[O] Removed certificate from container' - fi - - if [ -f ${LSWS_CONF_DIR}/cert/${KEY_FILE} ]; then - rm ${LSWS_CONF_DIR}/cert/${KEY_FILE} - echo '[O] Removed key from container' - fi - " - - # 3. Xóa domain mapping khỏi SSL Listener - echo "[!] Removing domain mapping from SSL Listener..." - - HAS_MAPPING=$(docker compose exec -T ${CONT_NAME} bash -c "grep -c 'map.*${DOMAIN}' ${HTTPD_CONF}" | tr -d '\r') - - if [ "${HAS_MAPPING}" != "0" ]; then - # Backup trước khi xóa - docker compose exec -T ${CONT_NAME} bash -c "cp ${HTTPD_CONF} ${HTTPD_CONF}.backup.\$(date +%Y%m%d_%H%M%S)" - - # Xóa dòng map của domain - docker compose exec -T ${CONT_NAME} bash -c " - sed -i '/listener Default HTTPS/,/^}/ { - /map.*${DOMAIN}/d - }' ${HTTPD_CONF} - " - echo -e "[O] Removed domain mapping for: \033[32m${DOMAIN}\033[0m" - - # Kiểm tra xem còn domain nào được map không - REMAINING_MAPS=$(docker compose exec -T ${CONT_NAME} bash -c "grep -A 15 'listener Default HTTPS' ${HTTPD_CONF} | grep -c 'map'" | tr -d '\r') - - if [ "${REMAINING_MAPS}" = "0" ]; then - echo "[!] No more domains mapped to SSL Listener" - echo "[?] Do you want to remove the entire SSL Listener? (y/N)" - read -r REMOVE_LISTENER - - if [[ "${REMOVE_LISTENER}" =~ ^[Yy]$ ]]; then - docker compose exec -T ${CONT_NAME} bash -c " - sed -i '/listener Default HTTPS {/,/^}/d' ${HTTPD_CONF} - " - echo "[O] SSL Listener removed" - fi - fi - else - echo "[!] Domain mapping not found in SSL Listener" - fi - - # 4. Hiển thị cấu hình hiện tại - echo "" - echo "[!] Current SSL Listener configuration:" - docker compose exec -T ${CONT_NAME} bash -c "grep -A 15 'listener Default HTTPS' ${HTTPD_CONF}" || echo "[!] No SSL Listener found" - echo "" - - # 5. Restart LiteSpeed - echo "[!] Restarting OpenLiteSpeed..." - lsws_restart - - echo "" - echo -e "\033[1m[SUCCESS] Certificate removed for domain: ${DOMAIN}\033[0m" - echo "" - echo '[End] Removing SSL certificate' -} - +# Main function to orchestrate the script operations main(){ if [ "${INSTALL}" = 'true' ]; then install_mkcert exit 0 fi - + + domain_filter "${DOMAIN}" + if [ "${REMOVE}" = 'true' ]; then remove_cert exit 0 fi - if [ "${TEST}" = 'true' ]; then - check_mkcert - exit 0 - fi - check_mkcert + domain_verify "${DOMAIN}" generate_cert configure_litespeed } @@ -387,9 +474,6 @@ while [ ! -z "${1}" ]; do -[rR] | --remove) REMOVE=true ;; - -[tT] | --test) - TEST=true - ;; *) help_message ;;