Skip to content

Commit 9c6716c

Browse files
committed
Put check validating on all commands
1 parent 1524af7 commit 9c6716c

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

nginx_proxy_manager_cli.sh

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,11 @@ display_info() {
367367
else
368368
echo -e " ${COLOR_RED}Backup directory does not exist.${COLOR_RESET}"
369369
fi
370+
370371
if [ -f "$TOKEN_FILE" ]; then
371372
echo -e " ${COLOR_GREEN}Token NPM ${COLOR_YELLOW} $TOKEN_FILE ${COLOR_RESET}"
372373
else
373-
echo -e "\n ${COLOR_RED}Generating new token... ${COLOR_RESET}"
374+
echo -e "\n ${COLOR_RED}Generating new token... ${COLOR_RESET}"
374375
# check if empty file
375376
if [ ! -s "$TOKEN_FILE" ]; then
376377
echo -e " Create $TOKEN_DIR"
@@ -381,14 +382,14 @@ display_info() {
381382
fi
382383
echo -e "\n 🔖 Check token\n"
383384

384-
generate_token
385-
#validate_token
386-
385+
generate_token
386+
#validate_token
387387
fi
388+
389+
388390
echo -e "\n --help (Show all commands)"
389391
}
390392

391-
392393
# shellcheck disable=SC2120
393394
# check_no_arguments() {
394395
# if [ $# -eq 0 ]; then
@@ -399,7 +400,6 @@ display_info() {
399400
# fi
400401
# }
401402

402-
403403
# Colorize boolean values for display
404404
colorize_boolean() {
405405
local value=$1
@@ -423,7 +423,9 @@ colorize_booleanh() {
423423
# Generate a new API token
424424
generate_token() {
425425

426-
response=$(curl -s -X POST "$BASE_URL$API_ENDPOINT?expiry=$TOKEN_EXPIRY" \
426+
# response=$(curl -s -X POST "$BASE_URL$API_ENDPOINT?expiry=$TOKEN_EXPIRY" \
427+
428+
response=$(curl -s -X POST "$BASE_URL$API_ENDPOINT?expiresIn=$TOKEN_EXPIRY" \
427429
-H "Content-Type: application/json; charset=UTF-8" \
428430
--data-raw "{\"identity\":\"$API_USER\",\"secret\":\"$API_PASS\"}")
429431

@@ -436,7 +438,7 @@ generate_token() {
436438
expires=$(echo "$response" | jq -r '.expires')
437439

438440
# Debug
439-
# echo -e "$BASE_URL$API_ENDPOINT?expiry=$TOKEN_EXPIRY"
441+
# echo -e "\n $BASE_URL$API_ENDPOINT?expiry=$TOKEN_EXPIRY \n"
440442

441443
if [ "$token" != "null" ]; then
442444
echo "$token" > $TOKEN_FILE
@@ -472,25 +474,24 @@ generate_token() {
472474
# Validate the existing token
473475
validate_token() {
474476

475-
476-
generate_token
477-
478477
if [ ! -f "$TOKEN_FILE" ] || [ ! -f "$EXPIRY_FILE" ]; then
479-
return 1
478+
echo -e "\n ⛔ ${COLOR_RED}No valid token found. Generating a new token...${COLOR_RESET}"
479+
generate_token
480480
fi
481481

482482
token=$(cat $TOKEN_FILE)
483483
expires=$(cat $EXPIRY_FILE)
484484
current_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
485485

486486
if [[ "$current_time" < "$expires" ]]; then
487-
echo -e "\n${COLOR_GREEN}The token is valid. Expiry: $expires${COLOR_RESET}"
488-
return 0
487+
echo -e "${COLOR_GREEN}The token is valid. Expiry: $expires${COLOR_RESET}"
488+
# return 0
489489
else
490-
echo -e "\n${COLOR_RED}The token is invalid. Expiry: $expires${COLOR_RESET}"
490+
echo -e "${COLOR_RED}The token is invalid. Expiry: $expires${COLOR_RESET}"
491491
generate_token
492-
return 1
492+
# return 1
493493
fi
494+
494495
}
495496

496497
#################################
@@ -512,6 +513,7 @@ while getopts "d:i:p:f:c:b:w:a:l:-:" opt; do
512513
show-default) SHOW_DEFAULT=true ;;
513514
backup) BACKUP=true ;;
514515
backup-host)
516+
validate_token
515517
BACKUP_HOST=true
516518
HOST_ID="${!OPTIND}"; shift
517519
;;
@@ -527,42 +529,50 @@ while getopts "d:i:p:f:c:b:w:a:l:-:" opt; do
527529
RESTORE_HOST=true
528530
fi
529531
;;
530-
ssl-regenerate) SSL_REGENERATE=true ;;
531-
ssl-restore) SSL_RESTORE=true ;;
532+
ssl-regenerate) validate_token; SSL_REGENERATE=true ;;
533+
ssl-restore) validate_token; SSL_RESTORE=true ;;
532534
create-user)
535+
validate_token
533536
CREATE_USER=true
534537
USERNAME="${!OPTIND}"; shift
535538
PASSWORD="${!OPTIND}"; shift
536539
EMAIL="${!OPTIND}"; shift
537540
;;
538541
delete-user)
542+
validate_token
539543
DELETE_USER=true
540544
USERNAME="${!OPTIND}"; shift
541545
;;
542546
host-delete)
547+
validate_token
543548
DELETE_HOST=true
544549
HOST_ID="${!OPTIND}"; shift
545550
;;
546551
host-show)
552+
validate_token
547553
HOST_SHOW=true
548554
HOST_ID="${!OPTIND}"; shift
549-
;;
550-
host-list) LIST_HOSTS=true ;;
551-
host-list-full) LIST_HOSTS_FULL=true ;;
552-
host-list-users) LIST_USERS=true ;;
555+
;;
556+
host-list) validate_token; LIST_HOSTS=true ;;
557+
host-list-full) validate_token; LIST_HOSTS_FULL=true ;;
558+
host-list-users) validate_token; LIST_USERS=true ;;
553559
host-search)
560+
validate_token
554561
SEARCH_HOST=true
555562
SEARCH_HOSTNAME="${!OPTIND}"; shift
556563
;;
557564
host-enable)
565+
validate_token
558566
ENABLE_HOST=true
559567
HOST_ID="${!OPTIND}"; shift
560568
;;
561569
host-disable)
570+
validate_token
562571
DISABLE_HOST=true
563572
HOST_ID="${!OPTIND}"; shift
564573
;;
565574
host-acl-enable)
575+
validate_token
566576
ENABLE_ACL=true
567577
# Expecting "HOST_ID,ACCESS_LIST_ID"
568578
ACL_ARG="${!OPTIND}"; shift
@@ -573,20 +583,24 @@ while getopts "d:i:p:f:c:b:w:a:l:-:" opt; do
573583
fi
574584
;;
575585
host-acl-disable)
586+
validate_token
576587
DISABLE_ACL=true
577588
HOST_ID="${!OPTIND}"; shift
578589
;;
579590
check-token) CHECK_TOKEN=true ;;
580591
generate-cert)
592+
validate_token
581593
GENERATE_CERT=true
582594
DOMAIN="${!OPTIND}"; shift
583595
EMAIL="${!OPTIND}"; shift
584596
;;
585597
delete-cert)
598+
validate_token
586599
DELETE_CERT=true
587600
DOMAIN="${!OPTIND}"; shift
588601
;;
589602
host-ssl-enable)
603+
validate_token
590604
ENABLE_SSL=true
591605
HOST_ID="${!OPTIND}"; shift
592606
# Check if HOST_ID is provided
@@ -597,21 +611,24 @@ while getopts "d:i:p:f:c:b:w:a:l:-:" opt; do
597611
fi
598612
;;
599613
host-ssl-disable)
614+
validate_token
600615
DISABLE_SSL=true
601616
HOST_ID="${!OPTIND}"; shift
602617
;;
603-
force-cert-creation) FORCE_CERT_CREATION=true ;;
618+
force-cert-creation)
619+
validate_token
620+
FORCE_CERT_CREATION=true ;;
604621
list-ssl-certificates)
622+
validate_token
605623
LIST_SSL_CERTIFICATES=true
606-
DOMAIN="$2"
624+
DOMAIN="$2"
607625
#DOMAIN="${!OPTIND}"; shift
608-
;;
609-
access-list) ACCESS_LIST=true ;;
626+
;;
627+
access-list) validate_token; ACCESS_LIST=true ;;
610628
examples) EXAMPLES=true ;;
611629
info) INFO=true ;;
612630
esac ;;
613-
*) INFO=true
614-
# display_info; exit 0
631+
*) INFO=true # display_info; exit 0
615632
;;
616633
esac
617634
done
@@ -811,15 +828,15 @@ restore_ssl_certificates() {
811828
# Function to delete all existing proxy hosts
812829
delete_all_proxy_hosts() {
813830
echo -e "\n 🗑️ ${COLOR_ORANGE}Deleting all existing proxy hosts...${COLOR_RESET}"
814-
831+
815832
existing_hosts=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts" \
816833
-H "Authorization: Bearer $(cat $TOKEN_FILE)" | jq -r '.[].id')
817834

818835
for host_id in $existing_hosts; do
819836
echo -e " 💣 Deleting host ID $host_id..."
820837
response=$(curl -s -w "HTTPSTATUS:%{http_code}" -X DELETE "$BASE_URL/nginx/proxy-hosts/$host_id" \
821838
-H "Authorization: Bearer $(cat $TOKEN_FILE)")
822-
839+
823840
http_body=$(echo "$response" | sed -e 's/HTTPSTATUS\:.*//g')
824841
http_status=$(echo "$response" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
825842

@@ -2125,6 +2142,7 @@ elif [ "$ENABLE_ACL" = true ]; then
21252142
elif [ "$DISABLE_ACL" = true ]; then
21262143
disable_acl
21272144
elif [ "$CHECK_TOKEN" = true ]; then
2145+
#generate_token
21282146
validate_token
21292147
elif [ "$BACKUP_LIST" = true ]; then
21302148
list_backups

0 commit comments

Comments
 (0)