Update nginx_proxy_manager_cli.sh

This commit is contained in:
Erreur32 2024-07-11 11:50:09 +02:00 committed by GitHub
parent 183465d33b
commit a213bfc046
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,7 +4,7 @@
# Nginx Proxy Manager CLI Script # Nginx Proxy Manager CLI Script
# Erreur32 - July 2024 # Erreur32 - July 2024
# #
# This script allows you to manage Nginx Proxy Manager via l'API. It provides # This script allows you to manage Nginx Proxy Manager via the API. It provides
# functionalities such as creating proxy hosts, managing users, and displaying # functionalities such as creating proxy hosts, managing users, and displaying
# configurations. # configurations.
# #
@ -26,18 +26,12 @@ API_PASS="pass nginx"
################################ ################################
# Colors # Colors
#COLOR_GREEN="\e[32m"
#COLOR_RED="\e[41;1m"
#COLOR_ORANGE="\e[38;5;202m"
#COLOR_YELLOW="\e[93m"
#COLOR_RESET="\e[0m"
COLOR_GREEN="\033[32m" COLOR_GREEN="\033[32m"
COLOR_RED="\033[41;1m" COLOR_RED="\033[41;1m"
COLOR_ORANGE="\033[38;5;202m" COLOR_ORANGE="\033[38;5;202m"
COLOR_YELLOW="\033[93m" COLOR_YELLOW="\033[93m"
COLOR_RESET="\033[0m" COLOR_RESET="\033[0m"
WHITE_ON_GREEN="\033[97m\033[42m"
# API Endpoints # API Endpoints
BASE_URL="http://$NGINX_IP:81/api" BASE_URL="http://$NGINX_IP:81/api"
@ -67,6 +61,8 @@ LIST_HOSTS_FULL=false
LIST_SSL_CERTIFICATES=false LIST_SSL_CERTIFICATES=false
LIST_USERS=false LIST_USERS=false
SEARCH_HOST=false SEARCH_HOST=false
ENABLE_HOST=false
DISABLE_HOST=false
# Check if necessary dependencies are installed # Check if necessary dependencies are installed
check_dependencies() { check_dependencies() {
@ -84,7 +80,7 @@ check_dependencies
# Display help # Display help
usage() { usage() {
echo -e "\n${COLOR_YELLOW}Usage: $0 -d domain -i ip -p port [-f forward_scheme] [-s ssl_forced] [-c caching_enabled] [-b block_exploits] [-w allow_websocket_upgrade] [-h http2_support] [-a advanced_config] [-e lets_encrypt_agree] [-m lets_encrypt_email] [-n dns_challenge] [-t token_expiry] [--create-user username password] [--delete-user username] [--delete-host id] [--list-hosts] [--list-hosts-full] [--list-ssl-certificates] [--list-users] [--search-host hostname] [--help]${COLOR_RESET}" echo -e "\n${COLOR_YELLOW}Usage: $0 -d domain -i ip -p port [-f forward_scheme] [-s ssl_forced] [-c caching_enabled] [-b block_exploits] [-w allow_websocket_upgrade] [-h http2_support] [-a advanced_config] [-e lets_encrypt_agree] [-m lets_encrypt_email] [-n dns_challenge] [-t token_expiry] [--create-user username password] [--delete-user username] [--delete-host id] [--list-hosts] [--list-hosts-full] [--list-ssl-certificates] [--list-users] [--search-host hostname] [--enable-host id] [--disable-host id] [--help]${COLOR_RESET}"
echo "" echo ""
echo -e "Examples:" echo -e "Examples:"
echo -e " ./nginx_proxy_manager_cli.sh -d example.com -i 192.168.1.10 -p 8080 -s true" echo -e " ./nginx_proxy_manager_cli.sh -d example.com -i 192.168.1.10 -p 8080 -s true"
@ -114,6 +110,8 @@ usage() {
echo -e " --list-ssl-certificates List all SSL certificates" echo -e " --list-ssl-certificates List all SSL certificates"
echo -e " --list-users List all users" echo -e " --list-users List all users"
echo -e " --search-host hostname Search for a proxy host by domain name" echo -e " --search-host hostname Search for a proxy host by domain name"
echo -e " --enable-host id Enable a proxy host by ID"
echo -e " --disable-host id Disable a proxy host by ID"
echo -e " --help Display this help" echo -e " --help Display this help"
echo echo
exit 0 exit 0
@ -170,6 +168,14 @@ while getopts "d:i:p:f:s:c:b:w:h:a:e:m:n:t:-:" opt; do
SEARCH_HOST=true SEARCH_HOST=true
SEARCH_HOSTNAME="${!OPTIND}"; shift SEARCH_HOSTNAME="${!OPTIND}"; shift
;; ;;
enable-host)
ENABLE_HOST=true
HOST_ID="${!OPTIND}"; shift
;;
disable-host)
DISABLE_HOST=true
HOST_ID="${!OPTIND}"; shift
;;
*) echo "Unknown option --${OPTARG}" ; usage ;; *) echo "Unknown option --${OPTARG}" ; usage ;;
esac ;; esac ;;
*) usage ;; *) usage ;;
@ -240,7 +246,7 @@ if ! validate_token; then
fi fi
# Check required parameters for displaying help # Check required parameters for displaying help
if [ -z "$DOMAIN_NAMES" ] && ! $CREATE_USER && ! $DELETE_USER && ! $DELETE_HOST && ! $LIST_HOSTS && ! $LIST_HOSTS_FULL && ! $LIST_SSL_CERTIFICATES && ! $LIST_USERS && ! $SEARCH_HOST; then if [ -z "$DOMAIN_NAMES" ] && ! $CREATE_USER && ! $DELETE_USER && ! $DELETE_HOST && ! $LIST_HOSTS && ! $LIST_HOSTS_FULL && ! $LIST_SSL_CERTIFICATES && ! $LIST_USERS && ! $SEARCH_HOST && ! $ENABLE_HOST && ! $DISABLE_HOST; then
usage usage
fi fi
@ -352,6 +358,7 @@ create_or_update_proxy_host() {
check_existing_proxy_host check_existing_proxy_host
} }
# Function to delete a proxy host # Function to delete a proxy host
delete_proxy_host() { delete_proxy_host() {
if [ -z "$HOST_ID" ]; then if [ -z "$HOST_ID" ]; then
@ -369,20 +376,70 @@ delete_proxy_host() {
} }
# Function to list all proxy hosts (simple) # Function to list all proxy hosts (simple)
list_proxy_hosts() { list_proxy_hosts_old() {
echo "List of proxy hosts..." echo "List of proxy hosts..."
RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts" \ RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)") -H "Authorization: Bearer $(cat $TOKEN_FILE)")
echo "$RESPONSE" | jq -r '.[] | "\(.id) \(.domain_names[])"' | awk -v yellow="$COLOR_YELLOW" -v green="$COLOR_GREEN" -v reset="$COLOR_RESET" '{ printf " id: " yellow "%-4s" reset " " green "%s" reset "\n", $1, $2 }' echo "$RESPONSE" | jq -r '.[] | "\(.id) \(.domain_names | join(", ")) \(.enabled)"' | while read -r id domain enabled; do
if [ "$enabled" = "true" ]; then
status="${COLOR_GREEN}enabled${COLOR_RESET}"
else
# status="${COLOR_RED}disabled${COLOR_RESET}"
status="disabled"
fi
printf " id: ${COLOR_YELLOW}%-4s${COLOR_RESET} ${COLOR_GREEN}%-40s${COLOR_RESET} %b\n" "$id" "$domain" "$status"
done
} }
# Function to list all proxy hosts (simple)
list_proxy_hosts() {
echo -e "\n${COLOR_ORANGE} List of proxy hosts (simple)${COLOR_RESET}"
RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)")
echo "$RESPONSE" | jq -r '.[] | "\(.id) \(.domain_names | join(", ")) \(.enabled)"' | while read -r id domain enabled; do
if [ "$enabled" -eq 1 ]; then
status="[${WHITE_ON_GREEN}enabled ${COLOR_RESET}]"
else
status="[${COLOR_RED}disabled${COLOR_RESET}]"
fi
printf " id: ${COLOR_YELLOW}%-4s${COLOR_RESET} ${COLOR_GREEN}%-20s${COLOR_RESET} %b\n" "$id" "$domain" "$status"
done
}
# Function to list all proxy hosts with full details # Function to list all proxy hosts with full details
list_proxy_hosts_full() { list_proxy_hosts_full() {
echo -e "\n${COLOR_ORANGE} List of proxy hosts with full details...${COLOR_RESET}\n" echo -e "\n${COLOR_ORANGE} List of proxy hosts with full details...${COLOR_RESET}\n"
RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts" \ RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)") -H "Authorization: Bearer $(cat $TOKEN_FILE)")
# Parcourt chaque élément du tableau JSON et l'affiche
echo "$RESPONSE" | jq -c '.[]' | while read -r proxy; do
echo "$proxy" | jq .
done
# echo "$RESPONSE" | jq -c '.[]' | while IFS= read -r line; do
# domain_names=$(echo "$line" | jq -r '.domain_names[]')
# advanced_config=$(echo "$line" | jq -r '.advanced_config')
#
# echo -e "${COLOR_GREEN}$domain_names${COLOR_RESET}"
# echo -e "$advanced_config" | awk '{
# gsub(/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/, "'${COLOR_ORANGE}'&'${COLOR_RESET}'");
# print
# }' | sed 's/^/ /'
# echo
# done
}
# todo
list_proxy_hosts_advanced() {
echo -e "\n${COLOR_ORANGE} List of proxy hosts with full details...${COLOR_RESET}\n"
RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)")
echo "$RESPONSE" | jq -c '.[]' | while IFS= read -r line; do echo "$RESPONSE" | jq -c '.[]' | while IFS= read -r line; do
domain_names=$(echo "$line" | jq -r '.domain_names[]') domain_names=$(echo "$line" | jq -r '.domain_names[]')
advanced_config=$(echo "$line" | jq -r '.advanced_config') advanced_config=$(echo "$line" | jq -r '.advanced_config')
@ -396,13 +453,15 @@ list_proxy_hosts_full() {
done done
} }
# Function to search for a proxy host and display details if found # Function to search for a proxy host and display details if found
search_proxy_host() { search_proxy_host() {
if [ -z "$SEARCH_HOSTNAME" ]; then if [ -z "$SEARCH_HOSTNAME" ]; then
echo "The --search-host option requires a domain name." echo "The --search-host option requires a domain name."
usage usage
fi fi
echo "Searching for proxy host for $SEARCH_HOSTNAME..." echo -e "\n Searching for proxy host for $SEARCH_HOSTNAME..."
RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts" \ RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)") -H "Authorization: Bearer $(cat $TOKEN_FILE)")
@ -414,7 +473,6 @@ search_proxy_host() {
done done
} }
# Function to list all SSL certificates # Function to list all SSL certificates
list_ssl_certificates() { list_ssl_certificates() {
echo "List of SSL certificates..." echo "List of SSL certificates..."
@ -476,6 +534,40 @@ delete_user() {
fi fi
} }
# Function to enable a proxy host
enable_proxy_host() {
if [ -z "$HOST_ID" ]; then
echo "The --enable-host option requires a host ID."
usage
fi
echo " Enabling proxy host ID: $HOST_ID..."
RESPONSE=$(curl -s -X PUT "$BASE_URL/nginx/proxy-hosts/$HOST_ID/enable" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)" \
-H "Content-Type: application/json; charset=UTF-8")
if [ $(echo "$RESPONSE" | jq -r '.error | length') -eq 0 ]; then
echo -e " ${COLOR_GREEN}Proxy host enabled successfully!${COLOR_RESET}"
else
echo -e " ${COLOR_RED}Failed to enable proxy host. Error: $(echo "$RESPONSE" | jq -r '.message')${COLOR_RESET}"
fi
}
# Function to disable a proxy host
disable_proxy_host() {
if [ -z "$HOST_ID" ]; then
echo "The --disable-host option requires a host ID."
usage
fi
echo " Disabling proxy host ID: $HOST_ID..."
RESPONSE=$(curl -s -X PUT "$BASE_URL/nginx/proxy-hosts/$HOST_ID/disable" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)" \
-H "Content-Type: application/json; charset=UTF-8")
if [ $(echo "$RESPONSE" | jq -r '.error | length') -eq 0 ]; then
echo -e " ${COLOR_GREEN}Proxy host disabled successfully!${COLOR_RESET}"
else
echo -e " ${COLOR_RED}Failed to disable proxy host. Error: $(echo "$RESPONSE" | jq -r '.message')${COLOR_RESET}"
fi
}
# Call functions based on options # Call functions based on options
if [ "$CREATE_USER" = true ]; then if [ "$CREATE_USER" = true ]; then
create_user create_user
@ -493,6 +585,10 @@ elif [ "$LIST_USERS" = true ]; then
list_users list_users
elif [ "$SEARCH_HOST" = true ]; then elif [ "$SEARCH_HOST" = true ]; then
search_proxy_host search_proxy_host
elif [ "$ENABLE_HOST" = true ]; then
enable_proxy_host
elif [ "$DISABLE_HOST" = true ]; then
disable_proxy_host
else else
create_or_update_proxy_host create_or_update_proxy_host
fi fi