|
| 1 | +#!/bin/bash |
| 2 | +# info: change system web terminal backend port |
| 3 | +# options: PORT |
| 4 | +# |
| 5 | +# example: v-change-sys-web-terminal-port 5678 |
| 6 | +# |
| 7 | +# This function for changing the system's web terminal backend port in NGINX configuration. |
| 8 | + |
| 9 | +#----------------------------------------------------------# |
| 10 | +# Variables & Functions # |
| 11 | +#----------------------------------------------------------# |
| 12 | + |
| 13 | +# Argument definition |
| 14 | +PORT=$1 |
| 15 | +NGINX_CONFIG="$HESTIA/nginx/conf/nginx.conf" |
| 16 | + |
| 17 | +# Includes |
| 18 | +# shellcheck source=/etc/hestiacp/hestia.conf |
| 19 | +source /etc/hestiacp/hestia.conf |
| 20 | +# shellcheck source=/usr/local/hestia/func/main.sh |
| 21 | +source $HESTIA/func/main.sh |
| 22 | +# load config file |
| 23 | +source_conf "$HESTIA/conf/hestia.conf" |
| 24 | + |
| 25 | +# Functions |
| 26 | +is_port_valid() { |
| 27 | + # Check if PORT is numeric |
| 28 | + if [[ ! "$PORT" =~ ^[0-9]+$ ]]; then |
| 29 | + echo "Port should contains a numeric value only!" |
| 30 | + log_event "$E_INVALID" "$ARGUMENTS" |
| 31 | + exit "$E_INVALID" |
| 32 | + fi |
| 33 | + |
| 34 | + # Check if PORT is already used |
| 35 | + BUSY_PORT=$(lsof -i:"$PORT") |
| 36 | + if [ -n "$BUSY_PORT" ] && [ "$PORT" != "$BACKEND_PORT" ]; then |
| 37 | + echo "Port is already used by Hestia, please set another one!" |
| 38 | + log_event "$E_INUSE" "$ARGUMENTS" |
| 39 | + exit "$E_INUSE" |
| 40 | + fi |
| 41 | +} |
| 42 | + |
| 43 | +#----------------------------------------------------------# |
| 44 | +# Verifications # |
| 45 | +#----------------------------------------------------------# |
| 46 | + |
| 47 | +check_args '1' "$#" 'PORT' |
| 48 | +is_port_valid |
| 49 | + |
| 50 | +# Perform verification if read-only mode is enabled |
| 51 | +check_hestia_demo_mode |
| 52 | + |
| 53 | +#----------------------------------------------------------# |
| 54 | +# Action # |
| 55 | +#----------------------------------------------------------# |
| 56 | + |
| 57 | +# Get original port |
| 58 | +ORIGINAL_PORT=$(cat ${NGINX_CONFIG} | grep -m1 "proxy_pass http://localhost:" | sed 's/[^0-9]*//g') |
| 59 | + |
| 60 | +# Check if port is different to nginx.conf |
| 61 | +if [ "$ORIGINAL_PORT" = "$PORT" ]; then |
| 62 | + # Nothing to do, exit |
| 63 | + exit |
| 64 | +else |
| 65 | + # Set new port in config via v-change-sys-config-value |
| 66 | + $BIN/v-change-sys-config-value "WEB_TERMINAL_PORT" "$PORT" |
| 67 | + # Replace port in config files. |
| 68 | + sed -i "s/\(proxy_pass http:\/\/localhost:\)[0-9][0-9]*\([^0-9]*\;$\)/\1$PORT\2/" ${NGINX_CONFIG} |
| 69 | + |
| 70 | + # Check if the web terminal backend is running |
| 71 | + if [[ $(ps -eaf | grep -i hestia/web-terminal | sed '/^$/d' | wc -l) -gt 1 ]]; then |
| 72 | + $BIN/v-restart-service hestia-web-terminal |
| 73 | + fi |
| 74 | + |
| 75 | + # Check if Hestia is running |
| 76 | + if [[ $(ps -eaf | grep -i hestia | sed '/^$/d' | wc -l) -gt 1 ]]; then |
| 77 | + $BIN/v-restart-service hestia |
| 78 | + fi |
| 79 | +fi |
| 80 | + |
| 81 | +#----------------------------------------------------------# |
| 82 | +# Hestia # |
| 83 | +#----------------------------------------------------------# |
| 84 | + |
| 85 | +# Logging |
| 86 | +$BIN/v-log-action "system" "Warning" "System" "Hestia Control Panel web terminal port changed (New Value: $PORT, Old Value: $ORIGINAL_PORT)." |
| 87 | +log_event "$OK" "$ARGUMENTS" |
| 88 | + |
| 89 | +exit |
0 commit comments