Skip to content

Commit 63c8289

Browse files
committed
ADD --list-access , --host-acl-enable and --host-acl-disable.
1 parent b4044b9 commit 63c8289

File tree

1 file changed

+191
-34
lines changed

1 file changed

+191
-34
lines changed

nginx_proxy_manager_cli.sh

Lines changed: 191 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Github [ https://github.com/Erreur32/nginx-proxy-manager-Bash-API ]
55
# Erreur32 July 2024
66

7-
VERSION="2.5.5"
7+
VERSION="2.5.6"
88

99
#
1010
# This script allows you to manage Nginx Proxy Manager via the API. It provides
@@ -82,6 +82,9 @@ VERSION="2.5.5"
8282
# --list-ssl-certificates List All SSL certificates availables (JSON)
8383
# --generate-cert domain email Generate certificate for the given domain and email
8484
# --delete-cert domain Delete certificate for the given domain
85+
# --list-access List all available access lists (ID and name)
86+
# --host-acl-enable id,access_list_id Enable ACL for a proxy host by ID with an access list ID
87+
# --host-acl-disable id Disable ACL for a proxy host by ID
8588
# --help Display this help
8689

8790
################################
@@ -108,11 +111,11 @@ CONFIG_FILE="$SCRIPT_DIR/nginx_proxy_manager_cli.conf"
108111
################################
109112

110113
if [ -f "$CONFIG_FILE" ]; then
111-
echo -e " ✅ Loading variables from file $PWD/nginx_proxy_manager_cli.conf..."
114+
#echo -e "\n ✅ Loading variables from file $CONFIG_FILE"
112115
# configuration file loading
113116
source "$CONFIG_FILE"
114117
else
115-
echo -e " ⚠️ Configuration file $PWD/nginx_proxy_manager_cli.conf don't exists. Used Default Variables... "
118+
echo -e " ⚠️ Configuration file $CONFIG_FILE don't exists. Used Default Variables... "
116119
fi
117120

118121
################################
@@ -163,6 +166,7 @@ SEARCH_HOST=false
163166
ENABLE_HOST=false
164167
DISABLE_HOST=false
165168
CHECK_TOKEN=false
169+
BACKUP_LIST=false
166170
BACKUP=false
167171
BACKUP_HOST=false
168172
RESTORE=false
@@ -172,7 +176,9 @@ ENABLE_SSL=false
172176
DISABLE_SSL=false
173177
HOST_SHOW=false
174178
SHOW_DEFAULT=false
175-
179+
ENABLE_ACL=false
180+
DISABLE_ACL=false
181+
ACCESS_LIST=false
176182
# Colors Custom
177183
COLOR_GREEN="\033[32m"
178184
COLOR_RED="\033[41;1m"
@@ -202,7 +208,7 @@ check_dependencies
202208
check_nginx_access() {
203209
if ping -c 2 -W 2 $NGINX_IP &> /dev/null; then
204210
if curl --output /dev/null --silent --head --fail "$BASE_URL"; then
205-
echo -e "\n ✅ Nginx url: $BASE_URL"
211+
echo -e "\n ✅ Nginx url: $BASE_URL"
206212
else
207213
echo -e "\n ⛔ Nginx url ⛔ $BASE_URL is NOT accessible."
208214
exit 1
@@ -213,6 +219,7 @@ check_nginx_access() {
213219
fi
214220
}
215221

222+
# check_nginx_access
216223

217224
# !!! ne filtrer que les dossiers !
218225
# Function to list available backups
@@ -256,6 +263,9 @@ usage() {
256263
echo -e " --host-list-users 📋 ${COLOR_YELLOW}List${COLOR_RESET} All Users"
257264
echo -e " --host-enable id ✅ ${COLOR_GREEN}Enable${COLOR_RESET} Proxy host by ${COLOR_YELLOW}ID${COLOR_RESET}"
258265
echo -e " --host-disable id ❌ ${COLOR_ORANGE}Disable${COLOR_RESET} Proxy host by ${COLOR_YELLOW}ID${COLOR_RESET}"
266+
echo -e " --access-list 📋 ${COLOR_YELLOW}List${COLOR_RESET} All available Access Lists (ID and Name)"
267+
echo -e " --host-acl-enable id,access_list_id ✅ ${COLOR_GREEN}Enable ACL${COLOR_RESET} for Proxy host by ${COLOR_YELLOW}ID${COLOR_RESET} with Access List ID (e.g., --host-acl-enable 16,2)"
268+
echo -e " --host-acl-disable id ❌ ${COLOR_ORANGE}Disable ACL${COLOR_RESET} for Proxy host by ${COLOR_YELLOW}ID${COLOR_RESET}"
259269
echo -e " --host-ssl-enable id 🔒 ${COLOR_GREEN}Enable${COLOR_RESET} SSL, HTTP/2, and HSTS for a proxy host (Enabled only if exist, check ${COLOR_ORANGE}--generate-cert${COLOR_RESET} to creating one)"
260270
echo -e " --host-ssl-disable id 🔓 ${COLOR_ORANGE}Disable${COLOR_RESET} SSL, HTTP/2, and HSTS for a proxy host"
261271
echo -e " --list-ssl-certificates [domain] 📋 ${COLOR_YELLOW}List${COLOR_RESET} All SSL certificates availables or filtered by [domain name] (JSON)"
@@ -305,6 +315,8 @@ examples_cli() {
305315
# Display script variables info
306316
display_info() {
307317

318+
check_nginx_access
319+
308320
echo -e "\n${COLOR_YELLOW}Script Info: ${COLOR_GREEN}${VERSION}${COLOR_RESET}"
309321

310322
echo -e "\n${COLOR_YELLOW}Script Variables Information:${COLOR_RESET}"
@@ -325,8 +337,8 @@ display_info() {
325337
if [ -f "$TOKEN_FILE" ]; then
326338
echo -e " ${COLOR_GREEN}Token NPM ${COLOR_YELLOW} $TOKEN_FILE ${COLOR_RESET}"
327339
else
328-
#echo -e " ${COLOR_RED}Token file does not exist! ${COLOR_RESET}"
329-
echo -e " ${COLOR_GREEN} Generating new token... ${COLOR_RESET} \n 🔖 Check ./nginx_proxy_manager_cli.sh --check-token "
340+
echo -e " ${COLOR_RED}Token file does not exist! ${COLOR_RESET} \n 🔖 Check ./nginx_proxy_manager_cli.sh --check-token "
341+
echo -e " Generating new token..."
330342
generate_token
331343

332344
fi
@@ -392,14 +404,15 @@ colorize_booleanh() {
392404
################################
393405
# Generate a new API token
394406
generate_token() {
407+
395408
response=$(curl -s -X POST "$BASE_URL$API_ENDPOINT?expiry=$TOKEN_EXPIRY" \
396409
-H "Content-Type: application/json; charset=UTF-8" \
397410
--data-raw "{\"identity\":\"$API_USER\",\"secret\":\"$API_PASS\"}")
398411

412+
# Old way
399413
# response=$(curl -s -X POST "$BASE_URL$API_ENDPOINT" \
400414
# -H "Content-Type: application/json; charset=UTF-8" \
401-
# --data-raw "{\"identity\":\"$API_USER\",\"secret\":\"$API_PASS\"}")
402-
#--data-raw "{\"identity\":\"$API_USER\",\"secret\":\"$API_PASS\",\"expiry\":\"$TOKEN_EXPIRY\"}")
415+
# --data-raw "{\"identity\":\"$API_USER\",\"secret\":\"$API_PASS\",\"expiry\":\"$TOKEN_EXPIRY\"}")
403416

404417
token=$(echo "$response" | jq -r '.token')
405418
expires=$(echo "$response" | jq -r '.expires')
@@ -418,6 +431,9 @@ generate_token() {
418431

419432
# Validate the existing token
420433
validate_token() {
434+
435+
generate_token
436+
421437
if [ ! -f "$TOKEN_FILE" ] || [ ! -f "$EXPIRY_FILE" ]; then
422438
return 1
423439
fi
@@ -453,6 +469,7 @@ while getopts "d:i:p:f:c:b:w:a:l:-:" opt; do
453469
case "${OPTARG}" in
454470
show-default) SHOW_DEFAULT=true ;;
455471
backup) BACKUP=true ;;
472+
backup-list) BACKUP_LIST=true ;;
456473
backup-host)
457474
BACKUP_HOST=true
458475
HOST_ID="${!OPTIND}"; shift
@@ -503,6 +520,20 @@ while getopts "d:i:p:f:c:b:w:a:l:-:" opt; do
503520
DISABLE_HOST=true
504521
HOST_ID="${!OPTIND}"; shift
505522
;;
523+
host-acl-enable)
524+
ENABLE_ACL=true
525+
# Expecting "HOST_ID,ACCESS_LIST_ID"
526+
ACL_ARG="${!OPTIND}"; shift
527+
IFS=',' read -r HOST_ID ACCESS_LIST_ID <<< "$ACL_ARG"
528+
if [ -z "$HOST_ID" ] || [ -z "$ACCESS_LIST_ID" ]; then
529+
echo -e "\n ⛔ ${COLOR_RED}Erreur : --host-acl-enable need HOST_ID et ACCESS_LIST_ID separated by a comma (e.g., --host-acl-enable 16,2).${COLOR_RESET}"
530+
usage
531+
fi
532+
;;
533+
host-acl-disable)
534+
DISABLE_ACL=true
535+
HOST_ID="${!OPTIND}"; shift
536+
;;
506537
check-token) CHECK_TOKEN=true ;;
507538
generate-cert)
508539
GENERATE_CERT=true
@@ -521,8 +552,8 @@ while getopts "d:i:p:f:c:b:w:a:l:-:" opt; do
521552
echo -e " \n⛔ ${COLOR_RED}Error: Missing host ID for --host-ssl-enable.${COLOR_RESET}"
522553
echo -e " To find ID Check with ${COLOR_ORANGE}$0 --host-list${COLOR_RESET}\n"
523554
exit 1
524-
fi
525-
;;
555+
fi
556+
;;
526557
host-ssl-disable)
527558
DISABLE_SSL=true
528559
HOST_ID="${!OPTIND}"; shift
@@ -532,7 +563,8 @@ while getopts "d:i:p:f:c:b:w:a:l:-:" opt; do
532563
LIST_SSL_CERTIFICATES=true
533564
DOMAIN="$2"
534565
#DOMAIN="${!OPTIND}"; shift
535-
;;
566+
;;
567+
access-list) ACCESS_LIST=true ;;
536568
examples) examples_cli ;;
537569
info) display_info;echo; exit 0 ;;
538570
esac ;;
@@ -551,6 +583,83 @@ fi
551583

552584
######################################
553585

586+
list_access() {
587+
588+
echo -e " \n Available Access Lists:"
589+
RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/access-lists" \
590+
-H "Authorization: Bearer $(cat $TOKEN_FILE)")
591+
592+
# Vérifiez si la réponse est une liste JSON valide
593+
if echo " $RESPONSE" | jq -e 'type == "array"' > /dev/null; then
594+
# Parcourez et affichez les éléments de la liste
595+
echo " $RESPONSE" | jq -r '.[] | "\(.id): \(.name)"'
596+
else
597+
# En cas d'erreur, vérifiez s'il y a un message d'erreur dans la réponse
598+
if echo " $RESPONSE" | jq -e '.error // empty' > /dev/null; then
599+
echo -e " ⛔ API Error: $(echo "$RESPONSE" | jq -r '.message')"
600+
else
601+
echo -e " ⛔ Unknown Error: $RESPONSE"
602+
fi
603+
fi
604+
}
605+
606+
607+
################################
608+
# ACL proxy host
609+
enable_acl() {
610+
if [ -z "$HOST_ID" ] || [ -z "$ACCESS_LIST_ID" ]; then
611+
echo -e "\n ⛔ ${COLOR_RED}Erreur : HOST_ID et ACCESS_LIST_ID sont requis pour activer l'ACL.${COLOR_RESET}"
612+
usage
613+
fi
614+
echo -e " 🔓 Activation de l'ACL pour l'hôte ID : $HOST_ID avec la liste d'accès ID : $ACCESS_LIST_ID..."
615+
616+
DATA=$(jq -n \
617+
--argjson access_list_id "$ACCESS_LIST_ID" \
618+
--argjson enabled true \
619+
'{
620+
access_list_id: $access_list_id,
621+
enabled: $enabled
622+
}')
623+
624+
RESPONSE=$(curl -s -X PUT "$BASE_URL/nginx/proxy-hosts/$HOST_ID" \
625+
-H "Authorization: Bearer $(cat $TOKEN_FILE)" \
626+
-H "Content-Type: application/json; charset=UTF-8" \
627+
--data-raw "$DATA")
628+
629+
if [ "$(echo "$RESPONSE" | jq -r '.error | length')" -eq 0 ]; then
630+
echo -e "${COLOR_GREEN}ACL activée avec succès pour l'hôte ID $HOST_ID!${COLOR_RESET}"
631+
else
632+
echo -e "${COLOR_RED}Échec de l'activation de l'ACL. Erreur : $(echo "$RESPONSE" | jq -r '.message')${COLOR_RESET}\n"
633+
fi
634+
}
635+
636+
# Désactiver l'ACL pour un proxy host donné
637+
disable_acl() {
638+
if [ -z "$HOST_ID" ]; then
639+
echo -e "\n ⛔ ${COLOR_RED}Erreur : HOST_ID est requis pour désactiver l'ACL.${COLOR_RESET}"
640+
usage
641+
fi
642+
echo -e " 🔒 Désactivation de l'ACL pour l'hôte ID : $HOST_ID..."
643+
644+
DATA=$(jq -n \
645+
--argjson access_list_id null \
646+
--argjson enabled false \
647+
'{
648+
access_list_id: $access_list_id,
649+
enabled: $enabled
650+
}')
651+
652+
RESPONSE=$(curl -s -X PUT "$BASE_URL/nginx/proxy-hosts/$HOST_ID" \
653+
-H "Authorization: Bearer $(cat $TOKEN_FILE)" \
654+
-H "Content-Type: application/json; charset=UTF-8" \
655+
--data-raw "$DATA")
656+
657+
if [ "$(echo "$RESPONSE" | jq -r '.error | length')" -eq 0 ]; then
658+
echo -e "${COLOR_GREEN}ACL désactivée avec succès pour l'hôte ID $HOST_ID!${COLOR_RESET}"
659+
else
660+
echo -e "${COLOR_RED}Échec de la désactivation de l'ACL. Erreur : $(echo "$RESPONSE" | jq -r '.message')${COLOR_RESET}\n"
661+
fi
662+
}
554663

555664
# Function to check if the host ID exists
556665
host-check-id() {
@@ -689,8 +798,8 @@ delete_all_proxy_hosts() {
689798
return 0
690799
}
691800

692-
693-
# Function to restore from a backup file
801+
#######################################################
802+
# Function to restore a backup file
694803
restore_backup() {
695804
echo -e "\n 🩹 ${COLOR_ORANGE}Restoring all configurations from backup...${COLOR_RESET}"
696805

@@ -795,7 +904,7 @@ restore_backup() {
795904

796905

797906
######################################################
798-
## en test
907+
## test BACKUP RESTORE
799908
######################################################
800909
# Function to list backup versions for a given host ID
801910
list_backup_versions_t() {
@@ -831,6 +940,7 @@ show_backup_differences() {
831940
diff <(echo "$CURRENT_HOST" | jq .) <(echo "$BACKUP_HOST" | jq .) | less
832941
}
833942

943+
##############################################################
834944

835945
# Delete a proxy host by ID
836946
delete_proxy_host() {
@@ -944,25 +1054,64 @@ create_new_proxy_host() {
9441054
CUSTOM_LOCATIONS_ESCAPED="[]"
9451055
fi
9461056

947-
DATA=$(printf '{
948-
"domain_names": ["%s"],
949-
"forward_host": "%s",
950-
"forward_port": %s,
951-
"access_list_id": null,
952-
"certificate_id": null,
953-
"ssl_forced": false,
954-
"caching_enabled": %s,
955-
"block_exploits": %s,
956-
"advanced_config": "%s",
957-
"meta": {
958-
"dns_challenge": null
959-
},
960-
"allow_websocket_upgrade": %s,
961-
"http2_support": %s,
962-
"forward_scheme": "%s",
963-
"enabled": true,
964-
"locations": %s
965-
}' "$DOMAIN_NAMES" "$FORWARD_HOST" "$FORWARD_PORT" "$CACHING_ENABLED" "$BLOCK_EXPLOITS" "$ADVANCED_CONFIG" "$ALLOW_WEBSOCKET_UPGRADE" "$HTTP2_SUPPORT" "$FORWARD_SCHEME" "$CUSTOM_LOCATIONS_ESCAPED")
1057+
# DATA=$(printf '{
1058+
# "domain_names": ["%s"],
1059+
# "forward_host": "%s",
1060+
# "forward_port": %s,
1061+
# "access_list_id": null,
1062+
# "certificate_id": null,
1063+
# "ssl_forced": false,
1064+
# "caching_enabled": %s,
1065+
# "block_exploits": %s,
1066+
# "advanced_config": "%s",
1067+
# "meta": {
1068+
# "dns_challenge": null
1069+
# },
1070+
# "allow_websocket_upgrade": %s,
1071+
# "http2_support": %s,
1072+
# "forward_scheme": "%s",
1073+
# "enabled": true,
1074+
# "locations": %s
1075+
# }' "$DOMAIN_NAMES" "$FORWARD_HOST" "$FORWARD_PORT" "$CACHING_ENABLED" "$BLOCK_EXPLOITS" "$ADVANCED_CONFIG" "$ALLOW_WEBSOCKET_UPGRADE" "$HTTP2_SUPPORT" "$FORWARD_SCHEME" "$CUSTOM_LOCATIONS_ESCAPED")
1076+
1077+
DATA=$(jq -n \
1078+
--arg domain "$DOMAIN_NAMES" \
1079+
--arg host "$FORWARD_HOST" \
1080+
--argjson port "$FORWARD_PORT" \
1081+
--argjson caching "$CACHING_ENABLED" \
1082+
--argjson block_exploits "$BLOCK_EXPLOITS" \
1083+
--arg advanced_config "$ADVANCED_CONFIG" \
1084+
--argjson websocket_upgrade "$ALLOW_WEBSOCKET_UPGRADE" \
1085+
--argjson http2_support "$HTTP2_SUPPORT" \
1086+
--arg scheme "$FORWARD_SCHEME" \
1087+
--argjson enabled true \
1088+
--argjson locations "$CUSTOM_LOCATIONS_ESCAPED" \
1089+
'{
1090+
domain_names: [$domain],
1091+
forward_host: $host,
1092+
forward_port: $port,
1093+
access_list_id: null,
1094+
certificate_id: null,
1095+
ssl_forced: false,
1096+
caching_enabled: $caching,
1097+
block_exploits: $block_exploits,
1098+
advanced_config: $advanced_config,
1099+
meta: { dns_challenge: null },
1100+
allow_websocket_upgrade: $websocket_upgrade,
1101+
http2_support: $http2_support,
1102+
forward_scheme: $scheme,
1103+
enabled: $enabled,
1104+
locations: $locations
1105+
}'
1106+
)
1107+
1108+
if $HOST_ACL_ENABLE; then
1109+
DATA=$(echo "$DATA" | jq --arg acl_id "$ACL_ID" '. + {access_list_id: ($acl_id | tonumber)}')
1110+
elif $HOST_ACL_DISABLE; then
1111+
DATA=$(echo "$DATA" | jq '. + {access_list_id: null}')
1112+
fi
1113+
1114+
9661115
# add dns_challenge
9671116
echo -e "\n Request Data: $DATA"
9681117

@@ -1871,8 +2020,16 @@ elif [ "$ENABLE_HOST" = true ]; then
18712020
enable_proxy_host
18722021
elif [ "$DISABLE_HOST" = true ]; then
18732022
disable_proxy_host
2023+
elif [ "$ACCESS_LIST" = true ]; then
2024+
list_access
2025+
elif [ "$ENABLE_ACL" = true ]; then
2026+
enable_acl
2027+
elif [ "$DISABLE_ACL" = true ]; then
2028+
disable_acl
18742029
elif [ "$CHECK_TOKEN" = true ]; then
18752030
validate_token
2031+
elif [ "$BACKUP_LIST" = true ]; then
2032+
list_backups
18762033
elif [ "$BACKUP" = true ]; then
18772034
full_backup
18782035
elif [ "$BACKUP_HOST" = true ]; then

0 commit comments

Comments
 (0)