|
| 1 | +#!/bin/bash |
| 2 | +# info: adding ssl force to domain |
| 3 | +# options: USER DOMAIN |
| 4 | +# |
| 5 | +# The function forces SSL for the requested domain. |
| 6 | + |
| 7 | + |
| 8 | +#----------------------------------------------------------# |
| 9 | +# Variable&Function # |
| 10 | +#----------------------------------------------------------# |
| 11 | + |
| 12 | +# Argument definition |
| 13 | +user=$1 |
| 14 | +domain=$2 |
| 15 | + |
| 16 | +# Includes |
| 17 | +source $HESTIA/func/main.sh |
| 18 | + |
| 19 | + |
| 20 | +#----------------------------------------------------------# |
| 21 | +# Verifications # |
| 22 | +#----------------------------------------------------------# |
| 23 | + |
| 24 | +check_args '2' "$#" 'USER DOMAIN' |
| 25 | +is_format_valid 'user' 'domain' 'ssl_dir' |
| 26 | +is_object_valid 'user' 'USER' "$user" |
| 27 | +is_object_unsuspended 'user' 'USER' "$user" |
| 28 | +is_object_valid 'web' 'DOMAIN' "$domain" |
| 29 | +is_object_unsuspended 'web' 'DOMAIN' "$domain" |
| 30 | + |
| 31 | + |
| 32 | +#----------------------------------------------------------# |
| 33 | +# Action # |
| 34 | +#----------------------------------------------------------# |
| 35 | + |
| 36 | +# Load domain data |
| 37 | +eval $(grep "DOMAIN='$domain'" $USER_DATA/web.conf) |
| 38 | + |
| 39 | +# Check if SSL is enabled |
| 40 | +if [ "$SSL" != 'yes' ]; then |
| 41 | + echo "Error: ssl is not enabled" |
| 42 | + exit $E_NOTEXIST |
| 43 | +fi |
| 44 | + |
| 45 | +# Check if proxy is active |
| 46 | +if [ ! -z "$PROXY_SYSTEM" ] || [ ! -z "$PROXY" ]; then |
| 47 | + if ! grep --quiet "forcessl" $HESTIA/data/templates/web/nginx/default.tpl; then |
| 48 | + $BIN/v-update-web-templates |
| 49 | + fi |
| 50 | + forcessl="/home/$user/conf/web/forcessl.nginx.$domain.conf" |
| 51 | +else |
| 52 | + if ! grep --quiet "forcessl" $HESTIA/data/templates/web/nginx/default.tpl; then |
| 53 | + $BIN/v-update-web-templates |
| 54 | + fi |
| 55 | + forcessl="/home/$user/conf/web/forcessl.apache2.$domain.conf" |
| 56 | +fi |
| 57 | + |
| 58 | +# Check if force ssl already exists |
| 59 | +if [ -f "$forcessl" ]; then |
| 60 | + echo "Error: rewrite already enabled" |
| 61 | + exit $E_EXIST |
| 62 | +fi |
| 63 | + |
| 64 | +# Place rewrite commands |
| 65 | +if [ ! -z $PROXY ]; then |
| 66 | + echo 'return 301 https://$server_name$request_uri;' > $forcessl |
| 67 | +else |
| 68 | + echo 'RewriteEngine On' > $forcessl |
| 69 | + echo 'RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]' >> $forcessl |
| 70 | +fi |
| 71 | + |
| 72 | + |
| 73 | +#----------------------------------------------------------# |
| 74 | +# Hestia # |
| 75 | +#----------------------------------------------------------# |
| 76 | + |
| 77 | +# Restarting web server |
| 78 | +$BIN/v-restart-web |
| 79 | +check_result $? "Web restart failed" >/dev/null |
| 80 | + |
| 81 | +$BIN/v-restart-proxy |
| 82 | +check_result $? "Proxy restart failed" >/dev/null |
| 83 | + |
| 84 | +exit |
0 commit comments