Skip to content

Commit 1524af7

Browse files
committed
Expiry don't work with other value than 1d, it be recreate only for one day
1 parent d419aa0 commit 1524af7

File tree

1 file changed

+52
-71
lines changed

1 file changed

+52
-71
lines changed

nginx_proxy_manager_cli.sh

Lines changed: 52 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ EXPIRY_FILE="$TOKEN_DIR/expiry_${NGINX_IP}.txt"
138138
TOKEN_FILE="$TOKEN_DIR/token_${NGINX_IP}.txt"
139139

140140
# Set Token duration validity.
141-
TOKEN_EXPIRY="1y"
141+
TOKEN_EXPIRY="365d"
142142

143143
# Default variables (you can adapt)
144144
CACHING_ENABLED=false
@@ -367,17 +367,25 @@ display_info() {
367367
else
368368
echo -e " ${COLOR_RED}Backup directory does not exist.${COLOR_RESET}"
369369
fi
370-
371370
if [ -f "$TOKEN_FILE" ]; then
372371
echo -e " ${COLOR_GREEN}Token NPM ${COLOR_YELLOW} $TOKEN_FILE ${COLOR_RESET}"
373372
else
374-
echo -e " ${COLOR_RED}Token file does not exist! ${COLOR_RESET} \n 🔖 Check $0 --check-token "
375-
echo -e " Generating new token..."
376-
generate_token
373+
echo -e "\n ${COLOR_RED}Generating new token... ${COLOR_RESET}"
374+
# check if empty file
375+
if [ ! -s "$TOKEN_FILE" ]; then
376+
echo -e " Create $TOKEN_DIR"
377+
rm -rf "$TOKEN_DIR"
378+
mkdir "$TOKEN_DIR"
379+
else
380+
echo -e " File $TOKEN_FILE"
381+
fi
382+
echo -e "\n 🔖 Check token\n"
383+
384+
generate_token
385+
#validate_token
377386

378387
fi
379388
echo -e "\n --help (Show all commands)"
380-
381389
}
382390

383391

@@ -414,7 +422,7 @@ colorize_booleanh() {
414422
################################
415423
# Generate a new API token
416424
generate_token() {
417-
425+
418426
response=$(curl -s -X POST "$BASE_URL$API_ENDPOINT?expiry=$TOKEN_EXPIRY" \
419427
-H "Content-Type: application/json; charset=UTF-8" \
420428
--data-raw "{\"identity\":\"$API_USER\",\"secret\":\"$API_PASS\"}")
@@ -427,21 +435,44 @@ generate_token() {
427435
token=$(echo "$response" | jq -r '.token')
428436
expires=$(echo "$response" | jq -r '.expires')
429437

438+
# Debug
439+
# echo -e "$BASE_URL$API_ENDPOINT?expiry=$TOKEN_EXPIRY"
440+
430441
if [ "$token" != "null" ]; then
431442
echo "$token" > $TOKEN_FILE
432443
echo "$expires" > $EXPIRY_FILE
433444
echo "Token: $token"
434445
echo "Expiry: $expires"
435446
else
436-
echo -e "${COLOR_RED}Error generating token.${COLOR_RESET}"
437-
echo -e "Check your [user] and [pass] and [IP]"
447+
echo -e " ${COLOR_RED}Error generating token.${COLOR_RESET}"
448+
echo -e " Check your [user] and [pass] and [IP]"
438449
exit 1
450+
fi
451+
452+
#CHECK_TOKEN=true
453+
if [ ! -f "$TOKEN_FILE" ] || [ ! -f "$EXPIRY_FILE" ]; then
454+
return 1
455+
fi
456+
457+
token=$(cat $TOKEN_FILE)
458+
expires=$(cat $EXPIRY_FILE)
459+
current_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
460+
461+
if [[ "$current_time" < "$expires" ]]; then
462+
echo -e "\n ✅ ${COLOR_GREEN}The token is valid. Expiry: $expires${COLOR_RESET}"
463+
return 0
464+
else
465+
echo -e "\n ⛔ ${COLOR_RED}The token is invalid. Expiry: $expires${COLOR_RESET}"
466+
generate_token
467+
return 1
439468
fi
440469
}
441470

471+
442472
# Validate the existing token
443473
validate_token() {
444474

475+
445476
generate_token
446477

447478
if [ ! -f "$TOKEN_FILE" ] || [ ! -f "$EXPIRY_FILE" ]; then
@@ -453,10 +484,11 @@ validate_token() {
453484
current_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
454485

455486
if [[ "$current_time" < "$expires" ]]; then
456-
echo -e "${COLOR_GREEN}The token is valid. Expiry: $expires${COLOR_RESET}"
487+
echo -e "\n${COLOR_GREEN}The token is valid. Expiry: $expires${COLOR_RESET}"
457488
return 0
458489
else
459-
echo -e "${COLOR_RED}The token is invalid. Expiry: $expires${COLOR_RESET}"
490+
echo -e "\n ⛔ ${COLOR_RED}The token is invalid. Expiry: $expires${COLOR_RESET}"
491+
generate_token
460492
return 1
461493
fi
462494
}
@@ -1182,6 +1214,7 @@ create_user() {
11821214
fi
11831215
}
11841216

1217+
###########################
11851218
# Delete a user by username
11861219
delete_user() {
11871220
if [ -z "$USERNAME" ]; then
@@ -1253,7 +1286,7 @@ enable_proxy_host() {
12531286
fi
12541287
}
12551288

1256-
1289+
############################
12571290
# Disable a proxy host by ID
12581291
disable_proxy_host() {
12591292
if [ -z "$HOST_ID" ]; then
@@ -1277,6 +1310,7 @@ disable_proxy_host() {
12771310
fi
12781311
}
12791312

1313+
#############################
12801314
# Delete a certificate in NPM
12811315
delete_certificate() {
12821316
if [ -z "$DOMAIN" ]; then
@@ -1327,7 +1361,7 @@ delete_certificate() {
13271361
}
13281362

13291363

1330-
1364+
##################################################
13311365
# Generate Let's Encrypt certificate if not exists
13321366
generate_certificate() {
13331367
if [ -z "$DOMAIN" ] || [ -z "$EMAIL" ]; then
@@ -1384,63 +1418,7 @@ generate_certificate() {
13841418
}
13851419

13861420

1387-
# enable_ssl function adel
1388-
enable_ssl_old() {
1389-
if [ -z "$HOST_ID" ]; then
1390-
echo -e "\n 🛡️ The --host-ssl-enable option requires a host ID."
1391-
usage
1392-
fi
1393-
echo -e "\n ✅ Enabling 🔒 SSL, HTTP/2, and HSTS for proxy host ID: $HOST_ID..."
1394-
1395-
# Check host details
1396-
CHECK_RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts/$HOST_ID" \
1397-
-H "Authorization: Bearer $(cat $TOKEN_FILE)")
1398-
1399-
CERTIFICATE_ID=$(echo "$CHECK_RESPONSE" | jq -r '.certificate_id')
1400-
DOMAIN_NAMES=$(echo "$CHECK_RESPONSE" | jq -r '.domain_names[]')
1401-
1402-
# Check if a Let's Encrypt certificate exists
1403-
CERT_EXISTS=$(curl -s -X GET "$BASE_URL/nginx/certificates" \
1404-
-H "Authorization: Bearer $(cat $TOKEN_FILE)" | jq -r --arg domain "$DOMAIN_NAMES" '.[] | select(.provider == "letsencrypt" and .domain_names[] == $domain) | .id')
1405-
1406-
if [ -z "$CERT_EXISTS" ]; then
1407-
echo " ⛔ No Let's Encrypt certificate associated with this host. Generating a new certificate..."
1408-
1409-
generate_certificate
1410-
CERTIFICATE_ID=$(curl -s -X GET "$BASE_URL/nginx/certificates" \
1411-
-H "Authorization: Bearer $(cat $TOKEN_FILE)" | jq -r --arg domain "$DOMAIN_NAMES" '.[] | select(.provider == "letsencrypt" and .domain_names[] == $domain) | .id')
1412-
else
1413-
echo " ✅ Existing Let's Encrypt certificate found. Using certificate ID: $CERT_EXISTS"
1414-
CERTIFICATE_ID="$CERT_EXISTS"
1415-
fi
1416-
1417-
# Update the host with SSL enabled
1418-
DATA=$(jq -n --arg cert_id "$CERTIFICATE_ID" '{
1419-
certificate_id: $cert_id,
1420-
ssl_forced: true,
1421-
http2_support: true,
1422-
hsts_enabled: true,
1423-
hsts_subdomains: false
1424-
}')
1425-
1426-
echo -e "\n Data being sent for SSL enablement: $DATA" # Log the data being sent
1427-
1428-
HTTP_RESPONSE=$(curl -s -w "HTTPSTATUS:%{http_code}" -X PUT "$BASE_URL/nginx/proxy-hosts/$HOST_ID" \
1429-
-H "Authorization: Bearer $(cat $TOKEN_FILE)" \
1430-
-H "Content-Type: application/json; charset=UTF-8" \
1431-
--data-raw "$DATA")
1432-
1433-
HTTP_BODY=$(echo "$HTTP_RESPONSE" | sed -e 's/HTTPSTATUS\:.*//g')
1434-
HTTP_STATUS=$(echo "$HTTP_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
1435-
1436-
if [ "$HTTP_STATUS" -eq 200 ]; then
1437-
echo -e "\n ✅ ${COLOR_GREEN}SSL, HTTP/2, and HSTS enabled successfully!${COLOR_RESET}\n"
1438-
else
1439-
echo -e "\n 👉Data sent: $DATA" # Log the data sent
1440-
echo -e "\n ⛔ ${COLOR_RED}Failed to enable SSL, HTTP/2, and HSTS. HTTP status: $HTTP_STATUS. Response: $HTTP_BODY${COLOR_RESET}\n"
1441-
fi
1442-
}
1443-
1421+
####################################
14441422
enable_ssl() {
14451423
if [ -z "$HOST_ID" ]; then
14461424
echo -e "\n 🛡️ The --host-ssl-enable option requires a host ID."
@@ -1584,6 +1562,7 @@ list_certificates() {
15841562
}
15851563

15861564

1565+
##########################################
15871566
# disable_ssl
15881567
# Function to disable SSL for a proxy host
15891568
disable_ssl() {
@@ -1626,6 +1605,8 @@ disable_ssl() {
16261605
fi
16271606
}
16281607

1608+
1609+
#########################################################
16291610
# host_show
16301611
# Function to show full details for a specific host by ID
16311612
host_show() {
@@ -2146,7 +2127,7 @@ elif [ "$DISABLE_ACL" = true ]; then
21462127
elif [ "$CHECK_TOKEN" = true ]; then
21472128
validate_token
21482129
elif [ "$BACKUP_LIST" = true ]; then
2149-
list_backups
2130+
list_backups
21502131
elif [ "$BACKUP" = true ]; then
21512132
full_backup
21522133
elif [ "$BACKUP_HOST" = true ]; then

0 commit comments

Comments
 (0)