Update nginx_proxy_manager_cli.sh

This commit is contained in:
Erreur32 2024-07-10 16:41:36 +02:00 committed by GitHub
parent cfcf171e4c
commit 2f5408c829
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,6 +14,7 @@
# -d DOMAIN_NAMES Domain name (required) # -d DOMAIN_NAMES Domain name (required)
# -i FORWARD_HOST IP address or domain name of the target server (required) # -i FORWARD_HOST IP address or domain name of the target server (required)
# -p FORWARD_PORT Port of the target server (required) # -p FORWARD_PORT Port of the target server (required)
# -f FORWARD_SCHEME Scheme for forwarding (http/https, default: http)
# -s SSL_FORCED Force SSL (true/false, default: false) # -s SSL_FORCED Force SSL (true/false, default: false)
# -c CACHING_ENABLED Enable caching (true/false, default: false) # -c CACHING_ENABLED Enable caching (true/false, default: false)
# -b BLOCK_EXPLOITS Block exploits (true/false, default: true) # -b BLOCK_EXPLOITS Block exploits (true/false, default: true)
@ -21,7 +22,9 @@
# -h HTTP2_SUPPORT Support HTTP/2 (true/false, default: true) # -h HTTP2_SUPPORT Support HTTP/2 (true/false, default: true)
# -a ADVANCED_CONFIG Advanced configuration (string) # -a ADVANCED_CONFIG Advanced configuration (string)
# -e LETS_ENCRYPT_AGREE Accept Let's Encrypt (true/false, default: false) # -e LETS_ENCRYPT_AGREE Accept Let's Encrypt (true/false, default: false)
# -m LETS_ENCRYPT_EMAIL Email for Let's Encrypt (required if LETS_ENCRYPT_AGREE is true)
# -n DNS_CHALLENGE DNS challenge (true/false, default: false) # -n DNS_CHALLENGE DNS challenge (true/false, default: false)
# -t TOKEN_EXPIRY Token expiry duration (default: 1y)
# --create-user username password Create a user with a username and password # --create-user username password Create a user with a username and password
# --delete-user username Delete a user by username # --delete-user username Delete a user by username
# --delete-host id Delete a proxy host by ID # --delete-host id Delete a proxy host by ID
@ -46,69 +49,78 @@ NGINX_IP="192.168.1.1"
# Token creation (user pass) with valid user on npm. # Token creation (user pass) with valid user on npm.
API_USER="your@email.com" API_USER="your@email.com"
API_PASS="password" API_PASS="password"
# Default token expiry duration
TOKEN_EXPIRY="1y"
# Colors # Colors
COLOR_TRUE="\e[42;1mtrue\e[0m" # Vert clair pour true COLOR_TRUE="\e[42;1mtrue\e[0m" # Light green for true
COLOR_FALSE="\e[93mfalse\e[0m" # Rouge pour false COLOR_FALSE="\e[93mfalse\e[0m" # Red for false
COLOR_ERROR="\e[41;1m" # Red for errors
############################ ############################
# Don't need to touch bellow # Don't need to touch below
############################ ############################
# Definition variables TOKEN # Definition variables TOKEN
BASE_URL="http://$NGINX_IP:81/api" BASE_URL="http://$NGINX_IP:81/api"
API_ENDPOINT="/tokens" API_ENDPOINT="/tokens"
EXPIRY_FILE="expiry.txt"
# File storage token # File storage token
TOKEN_FILE="token.txt" TOKEN_FILE="token.txt"
# Check if Nginx IP and port are accessible
check_nginx_access() {
if ! curl -s --head --request GET "$BASE_URL" | grep "200 OK" > /dev/null; then
echo -e "${COLOR_ERROR}Error: Nginx is not accessible at $NGINX_IP:81${COLOR_FALSE}"
exit 1
fi
}
# Fonction pour générer le token # Function to generate the token
generate_token() { generate_token() {
response=$(curl -s -X POST "$BASE_URL$API_ENDPOINT" \ response=$(curl -s -X POST "$BASE_URL$API_ENDPOINT" \
-H "Content-Type: application/json; charset=UTF-8" \ -H "Content-Type: application/json; charset=UTF-8" \
--data-raw "{\"identity\":\"$API_USER\",\"secret\":\"$API_PASS\",\"expiry\":\"1y\"}") --data-raw "{\"identity\":\"$API_USER\",\"secret\":\"$API_PASS\",\"expiry\":\"$TOKEN_EXPIRY\"}")
token=$(echo $response | jq -r '.token') token=$(echo $response | jq -r '.token')
expires=$(echo $response | jq -r '.expires') expires=$(echo $response | jq -r '.expires')
if [ "$token" != "null" ]; then if [ "$token" != "null" ]; then
echo $token > $TOKEN_FILE echo $token > $TOKEN_FILE
echo $expires > $EXPIRY_FILE
echo "Token: $token" echo "Token: $token"
echo "Expiry: $expires" echo "Expiry: $expires"
else else
echo "Erreur lors de la génération du token." echo -e "${COLOR_ERROR}Error generating token.${COLOR_FALSE}"
exit 1 exit 1
fi fi
} }
# Fonction pour valider le token # Function to validate the token
validate_token() { validate_token() {
if [ ! -f "$TOKEN_FILE" ] || [ ! -f "$EXPIRY_FILE" ]; then if [ ! -f "$TOKEN_FILE" ]; then
return 1 return 1
fi fi
token=$(cat $TOKEN_FILE) token=$(cat $TOKEN_FILE)
expires=$(cat $EXPIRY_FILE)
current_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") current_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
if [[ "$current_time" < "$expires" ]]; then if [[ "$current_time" < "$expires" ]]; then
echo -e "\e[42;1mLe token est valide. Expiration: $expires\033[0m" echo -e "\e[42;1mThe token is valid. Expiry: $expires\033[0m"
return 0 return 0
else else
echo -e "\e[41;1mLe token est invalide. Expiration: $expires\033[0m" echo -e "\e[41;1mThe token is invalid. Expiry: $expires\033[0m"
return 1 return 1
fi fi
} }
# Vérifier si le fichier de token existe # Check if Nginx is accessible
check_nginx_access
# Check if the token file exists
if ! validate_token; then if ! validate_token; then
echo "Aucun token valide trouvé. Génération d'un nouveau token..." echo "No valid token found. Generating a new token..."
generate_token generate_token
fi fi
# Fonction pour coloriser les valeurs true et false # Function to colorize true and false values
colorize_boolean() { colorize_boolean() {
local value=$1 local value=$1
if [ "$value" = true ]; then if [ "$value" = true ]; then
@ -118,37 +130,40 @@ colorize_boolean() {
fi fi
} }
# Afficher l'aide # Display help
usage() { usage() {
echo -e "\n\e[33mUsage: $0 -d domain -i ip -p port [-s ssl_forced] [-c caching_enabled] [-b block_exploits] [-w allow_websocket_upgrade] [-h http2_support] [-a advanced_config] [-e lets_encrypt_agree] [-n dns_challenge] [--create-user username password] [--delete-user username] [--delete-host id] [--list-hosts] [--list-hosts-full] [--list-ssl-certificates] [--list-users] [--search-host hostname] [--help]\e[0m" echo -e "\n\e[33mUsage: $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]\e[0m"
echo -e "" echo -e ""
echo -e "Options:" echo -e "Options:"
echo -e " -d \e[33mDOMAIN_NAMES\e[0m Nom de domaine (\e[41;1m obligatoire \e[0m)" echo -e " -d \e[33mDOMAIN_NAMES\e[0m Domain name (\e[41;1m required \e[0m)"
echo -e " -i \e[33mFORWARD_HOST\e[0m Adresse IP ou nom de domaine du serveur cible (\e[41;1m obligatoire \e[0m)" echo -e " -i \e[33mFORWARD_HOST\e[0m IP address or domain name of the target server (\e[41;1m required \e[0m)"
echo -e " -p \e[33mFORWARD_PORT\e[0m Port du serveur cible (\e[41;1m obligatoire \e[0m)" echo -e " -p \e[33mFORWARD_PORT\e[0m Port of the target server (\e[41;1m required \e[0m)"
echo -e " -s SSL_FORCED Forcer SSL (true/false, par défaut: $(colorize_boolean $SSL_FORCED))" echo -e " -f \e[33mFORWARD_SCHEME\e[0m Scheme for forwarding (http/https, default: http)"
echo -e " -c CACHING_ENABLED Activer le caching (true/false, par défaut: $(colorize_boolean $CACHING_ENABLED))" echo -e " -s SSL_FORCED Force SSL (true/false, default: $(colorize_boolean $SSL_FORCED))"
echo -e " -b BLOCK_EXPLOITS Bloquer les exploits (true/false, par défaut: $(colorize_boolean $BLOCK_EXPLOITS))" echo -e " -c CACHING_ENABLED Enable caching (true/false, default: $(colorize_boolean $CACHING_ENABLED))"
echo -e " -w ALLOW_WEBSOCKET_UPGRADE Autoriser l'upgrade WebSocket (true/false, par défaut: $(colorize_boolean $ALLOW_WEBSOCKET_UPGRADE))" echo -e " -b BLOCK_EXPLOITS Block exploits (true/false, default: $(colorize_boolean $BLOCK_EXPLOITS))"
echo -e " -h HTTP2_SUPPORT Support HTTP/2 (true/false, par défaut: $(colorize_boolean $HTTP2_SUPPORT))" echo -e " -w ALLOW_WEBSOCKET_UPGRADE Allow WebSocket upgrade (true/false, default: $(colorize_boolean $ALLOW_WEBSOCKET_UPGRADE))"
echo -e " -a ADVANCED_CONFIG Configuration avancée (chaîne de caractères)" echo -e " -h HTTP2_SUPPORT Support HTTP/2 (true/false, default: $(colorize_boolean $HTTP2_SUPPORT))"
echo -e " -e LETS_ENCRYPT_AGREE Accepter Let's Encrypt (true/false, par défaut: $(colorize_boolean $LETS_ENCRYPT_AGREE))" echo -e " -a ADVANCED_CONFIG Advanced configuration (string)"
echo -e " -n DNS_CHALLENGE Défi DNS (true/false, par défaut: $(colorize_boolean $DNS_CHALLENGE))" echo -e " -e LETS_ENCRYPT_AGREE Accept Let's Encrypt (true/false, default: $(colorize_boolean $LETS_ENCRYPT_AGREE))"
echo -e " --create-user username password Créer un utilisateur avec un nom d'utilisateur et un mot de passe" echo -e " -m LETS_ENCRYPT_EMAIL Email for Let's Encrypt (required if LETS_ENCRYPT_AGREE is true)"
echo -e " --delete-user username Supprimer un utilisateur par son nom d'utilisateur" echo -e " -n DNS_CHALLENGE DNS challenge (true/false, default: $(colorize_boolean $DNS_CHALLENGE))"
echo -e " --delete-host id Supprimer un proxy host par son ID" echo -e " -t TOKEN_EXPIRY Token expiry duration (default: 1y)"
echo -e " --list-hosts Lister les noms de tous les proxy hosts" echo -e " --create-user username password Create a user with a username and password"
echo -e " --list-hosts-full Lister tous les proxy hosts avec les détails complets" echo -e " --delete-user username Delete a user by username"
echo -e " --list-ssl-certificates Lister tous les certificats SSL" echo -e " --delete-host id Delete a proxy host by ID"
echo -e " --list-users Lister tous les utilisateurs" echo -e " --list-hosts List the names of all proxy hosts"
echo -e " --search-host hostname Rechercher un proxy host par nom de domaine" echo -e " --list-hosts-full List all proxy hosts with full details"
echo -e " --help Afficher cette aide" echo -e " --list-ssl-certificates List all SSL certificates"
echo -e " --list-users List all users"
echo -e " --search-host hostname Search for a proxy host by domain name"
echo -e " --help Display this help"
echo echo
exit 1 exit 1
} }
# Variables par défaut # Default variables
SSL_FORCED=false SSL_FORCED=false
CACHING_ENABLED=false CACHING_ENABLED=false
BLOCK_EXPLOITS=true BLOCK_EXPLOITS=true
@ -156,9 +171,11 @@ ALLOW_WEBSOCKET_UPGRADE=false
HTTP2_SUPPORT=true HTTP2_SUPPORT=true
ADVANCED_CONFIG="" ADVANCED_CONFIG=""
LETS_ENCRYPT_AGREE=false LETS_ENCRYPT_AGREE=false
LETS_ENCRYPT_EMAIL=""
DNS_CHALLENGE=false DNS_CHALLENGE=false
FORWARD_SCHEME="http"
# Variables de contrôle # Control variables
CREATE_USER=false CREATE_USER=false
DELETE_USER=false DELETE_USER=false
DELETE_HOST=false DELETE_HOST=false
@ -168,12 +185,13 @@ LIST_SSL_CERTIFICATES=false
LIST_USERS=false LIST_USERS=false
SEARCH_HOST=false SEARCH_HOST=false
# Parse les options en ligne de commande # Parse command-line options
while getopts "d:i:p:s:c:b:w:h:a:e:n:-:" opt; do while getopts "d:i:p:f:s:c:b:w:h:a:e:m:n:t:-:" opt; do
case $opt in case $opt in
d) DOMAIN_NAMES="$OPTARG" ;; d) DOMAIN_NAMES="$OPTARG" ;;
i) FORWARD_HOST="$OPTARG" ;; i) FORWARD_HOST="$OPTARG" ;;
p) FORWARD_PORT="$OPTARG" ;; p) FORWARD_PORT="$OPTARG" ;;
f) FORWARD_SCHEME="$OPTARG" ;;
s) SSL_FORCED="$OPTARG" ;; s) SSL_FORCED="$OPTARG" ;;
c) CACHING_ENABLED="$OPTARG" ;; c) CACHING_ENABLED="$OPTARG" ;;
b) BLOCK_EXPLOITS="$OPTARG" ;; b) BLOCK_EXPLOITS="$OPTARG" ;;
@ -181,7 +199,9 @@ while getopts "d:i:p:s:c:b:w:h:a:e:n:-:" opt; do
h) HTTP2_SUPPORT="$OPTARG" ;; h) HTTP2_SUPPORT="$OPTARG" ;;
a) ADVANCED_CONFIG="$OPTARG" ;; a) ADVANCED_CONFIG="$OPTARG" ;;
e) LETS_ENCRYPT_AGREE="$OPTARG" ;; e) LETS_ENCRYPT_AGREE="$OPTARG" ;;
m) LETS_ENCRYPT_EMAIL="$OPTARG" ;;
n) DNS_CHALLENGE="$OPTARG" ;; n) DNS_CHALLENGE="$OPTARG" ;;
t) TOKEN_EXPIRY="$OPTARG" ;;
-) -)
case "${OPTARG}" in case "${OPTARG}" in
help) usage ;; help) usage ;;
@ -206,26 +226,42 @@ while getopts "d:i:p:s:c:b:w:h:a:e:n:-:" opt; do
SEARCH_HOST=true SEARCH_HOST=true
SEARCH_HOSTNAME="${!OPTIND}"; shift SEARCH_HOSTNAME="${!OPTIND}"; shift
;; ;;
*) echo "Option inconnue --${OPTARG}" ; usage ;; *) echo "Unknown option --${OPTARG}" ; usage ;;
esac ;; esac ;;
*) usage ;; *) usage ;;
esac esac
done done
# Vérifie les paramètres obligatoires pour afficher l'aide # 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; then
usage usage
fi fi
################################################### ###################################################
# Fonction pour créer un proxy host # Function to check if proxy host exists
create_proxy_host() { check_existing_proxy_host() {
if [ -z "$DOMAIN_NAMES" ] || [ -z "$FORWARD_HOST" ] || [ -z "$FORWARD_PORT" ]; then RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts" \
echo "Les paramètres -d, -i et -p sont obligatoires pour créer un proxy host." -H "Authorization: Bearer $(cat $TOKEN_FILE)")
usage EXISTING_HOST=$(echo "$RESPONSE" | jq -r --arg DOMAIN "$DOMAIN_NAMES" '.[] | select(.domain_names[] == $DOMAIN)')
if [ -n "$EXISTING_HOST" ]; then
echo "Proxy host for $DOMAIN_NAMES already exists."
read -p "Do you want to update it with the new configuration? (y/n): " -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
HOST_ID=$(echo "$EXISTING_HOST" | jq -r '.id')
update_proxy_host $HOST_ID
else
echo "No changes made."
exit 0
fi fi
echo "Création du proxy host pour $DOMAIN_NAMES..." fi
}
# Function to update a proxy host
update_proxy_host() {
HOST_ID=$1
echo "Updating proxy host for $DOMAIN_NAMES..."
DATA='{ DATA='{
"domain_names": ["'"$DOMAIN_NAMES"'"], "domain_names": ["'"$DOMAIN_NAMES"'"],
"forward_host": "'"$FORWARD_HOST"'", "forward_host": "'"$FORWARD_HOST"'",
@ -238,59 +274,97 @@ create_proxy_host() {
"advanced_config": "'"$ADVANCED_CONFIG"'", "advanced_config": "'"$ADVANCED_CONFIG"'",
"meta": { "meta": {
"letsencrypt_agree": '"$LETS_ENCRYPT_AGREE"', "letsencrypt_agree": '"$LETS_ENCRYPT_AGREE"',
"dns_challenge": '"$DNS_CHALLENGE"' "dns_challenge": '"$DNS_CHALLENGE"',
"letsencrypt_email": "'"$LETS_ENCRYPT_EMAIL"'"
}, },
"allow_websocket_upgrade": '"$ALLOW_WEBSOCKET_UPGRADE"', "allow_websocket_upgrade": '"$ALLOW_WEBSOCKET_UPGRADE"',
"http2_support": '"$HTTP2_SUPPORT"', "http2_support": '"$HTTP2_SUPPORT"',
"forward_scheme": "http", "forward_scheme": "'"$FORWARD_SCHEME"'",
"enabled": true, "enabled": true,
"locations": [] "locations": []
}' }'
echo "Données envoyées: $DATA" echo "Data sent: $DATA"
RESPONSE=$(curl -s -X PUT "$BASE_URL/nginx/proxy-hosts/$HOST_ID" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)" \
-H "Content-Type: application/json; charset=UTF-8" \
--data-raw "$DATA")
echo "Response from updating proxy host: $RESPONSE"
echo "$RESPONSE" | jq
}
# Function to create a proxy host
create_proxy_host() {
if [ -z "$DOMAIN_NAMES" ] || [ -z "$FORWARD_HOST" ] || [ -z "$FORWARD_PORT" ]; then
echo "The -d, -i, and -p options are required to create a proxy host."
usage
fi
if $LETS_ENCRYPT_AGREE && [ -z "$LETS_ENCRYPT_EMAIL" ]; then
echo "The -m option is required when -e is true."
usage
fi
check_existing_proxy_host
echo "Creating proxy host for $DOMAIN_NAMES..."
DATA='{
"domain_names": ["'"$DOMAIN_NAMES"'"],
"forward_host": "'"$FORWARD_HOST"'",
"forward_port": '"$FORWARD_PORT"',
"access_list_id": null,
"certificate_id": null,
"ssl_forced": '"$SSL_FORCED"',
"caching_enabled": '"$CACHING_ENABLED"',
"block_exploits": '"$BLOCK_EXPLOITS"',
"advanced_config": "'"$ADVANCED_CONFIG"'",
"meta": {
"letsencrypt_agree": '"$LETS_ENCRYPT_AGREE"',
"dns_challenge": '"$DNS_CHALLENGE"',
"letsencrypt_email": "'"$LETS_ENCRYPT_EMAIL"'"
},
"allow_websocket_upgrade": '"$ALLOW_WEBSOCKET_UPGRADE"',
"http2_support": '"$HTTP2_SUPPORT"',
"forward_scheme": "'"$FORWARD_SCHEME"'",
"enabled": true,
"locations": []
}'
echo "Data sent: $DATA"
RESPONSE=$(curl -s -X POST "$BASE_URL/nginx/proxy-hosts" \ RESPONSE=$(curl -s -X POST "$BASE_URL/nginx/proxy-hosts" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)" \ -H "Authorization: Bearer $(cat $TOKEN_FILE)" \
-H "Content-Type: application/json; charset=UTF-8" \ -H "Content-Type: application/json; charset=UTF-8" \
--data-raw "$DATA") --data-raw "$DATA")
echo "Réponse de la création du proxy host: $RESPONSE" echo "Response from creating proxy host: $RESPONSE"
echo "$RESPONSE" | jq echo "$RESPONSE" | jq
} }
# Fonction pour supprimer un proxy host # Function to delete a proxy host
delete_proxy_host() { delete_proxy_host() {
if [ -z "$HOST_ID" ]; then if [ -z "$HOST_ID" ]; then
echo "L'option --delete-host nécessite un ID de host." echo "The --delete-host option requires a host ID."
usage usage
fi fi
echo "Suppression du proxy host ID: $HOST_ID..." echo "Deleting proxy host ID: $HOST_ID..."
RESPONSE=$(curl -s -X DELETE "$BASE_URL/nginx/proxy-hosts/$HOST_ID" \ RESPONSE=$(curl -s -X DELETE "$BASE_URL/nginx/proxy-hosts/$HOST_ID" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)") -H "Authorization: Bearer $(cat $TOKEN_FILE)")
echo "Réponse de la suppression du proxy host: $RESPONSE" echo "Response from deleting proxy host: $RESPONSE"
echo "$RESPONSE" | jq echo "$RESPONSE" | jq
} }
# Fonction pour lister tous les proxy hosts (simple) # Function to list all proxy hosts (simple)
list_proxy_hosts_old() {
echo " Liste des proxy hosts..."
RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/proxy-hosts" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)")
echo "$RESPONSE" | jq -c '.[] | {id, domain_names}'
}
list_proxy_hosts() { list_proxy_hosts() {
echo " Liste des 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 '{ printf " id: \033[33m%-4s\033[0m \033[32m%s\033[0m\n", $1, $2 }' echo "$RESPONSE" | jq -r '.[] | "\(.id) \(.domain_names[])"' | awk '{ printf " id: \033[33m%-4s\033[0m \033[32m%s\033[0m\n", $1, $2 }'
# echo "$RESPONSE" | jq -r '.[] | "\(.id) \(.domain_names[])"' | sort -n | awk '{ printf "id: \033[33m%-4s\033[0m \033[32m%s\033[0m\n", $1, $2 }'
} }
# Function to list all proxy hosts with full details
list_proxy_hosts_full() { list_proxy_hosts_full() {
echo "Liste des proxy hosts avec détails complets..." echo "List of proxy hosts with full details..."
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)")
@ -298,41 +372,25 @@ list_proxy_hosts_full() {
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')
# Colorisation des noms de domaines en vert # Colorize domain names in green
echo -e "\033[32m$domain_names\033[0m" echo -e "\033[32m$domain_names\033[0m"
# Colorisation des IPs en jaune sans chevauchement # Colorize IPs in yellow without overlap
echo -e "$advanced_config" | awk '{ echo -e "$advanced_config" | awk '{
gsub(/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/, "\033[33m&\033[0m"); gsub(/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/, "\033[33m&\033[0m");
print print
}' | sed 's/^/ /' # Indentation de chaque ligne }' | sed 's/^/ /' # Indentation of each line
echo echo
done done
} }
# Function to search for a proxy host and display details if found
# Fonction pour lister tous les proxy hosts avec détails formatés
list_proxy_hosts_full_old() {
echo " Liste des proxy hosts avec détails complets..."
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
domain_names=$(echo "$line" | jq -r '.domain_names[]')
advanced_config=$(echo "$line" | jq -r '.advanced_config')
echo "domain_names: $domain_names"
echo "$advanced_config" | sed 's/^/ /' # Indentation de chaque ligne
echo
done
}
# Fonction pour rechercher un proxy host et afficher les détails si trouvé
search_proxy_host() { search_proxy_host() {
if [ -z "$SEARCH_HOSTNAME" ]; then if [ -z "$SEARCH_HOSTNAME" ]; then
echo "L'option --search-host nécessite un nom de domaine." echo "The --search-host option requires a domain name."
usage usage
fi fi
echo "Recherche du proxy host pour $SEARCH_HOSTNAME..." echo "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)")
echo "$RESPONSE" | jq -c --arg search "$SEARCH_HOSTNAME" '.[] | select(.domain_names[] | contains($search))' | while IFS= read -r line; do echo "$RESPONSE" | jq -c --arg search "$SEARCH_HOSTNAME" '.[] | select(.domain_names[] | contains($search))' | while IFS= read -r line; do
@ -340,34 +398,34 @@ search_proxy_host() {
advanced_config=$(echo "$line" | jq -r '.advanced_config') advanced_config=$(echo "$line" | jq -r '.advanced_config')
echo "domain_names: $domain_names" echo "domain_names: $domain_names"
echo "$advanced_config" | sed 's/^/ /' # Indentation de chaque ligne echo "$advanced_config" | sed 's/^/ /' # Indentation of each line
echo echo
done done
} }
# Fonction pour lister tous les certificats SSL # Function to list all SSL certificates
list_ssl_certificates() { list_ssl_certificates() {
echo "Liste des certificats SSL..." echo "List of SSL certificates..."
RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/certificates" \ RESPONSE=$(curl -s -X GET "$BASE_URL/nginx/certificates" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)") -H "Authorization: Bearer $(cat $TOKEN_FILE)")
echo "$RESPONSE" | jq echo "$RESPONSE" | jq
} }
# Fonction pour lister tous les utilisateurs # Function to list all users
list_users() { list_users() {
echo "Liste des utilisateurs..." echo "List of users..."
RESPONSE=$(curl -s -X GET "$BASE_URL/users" \ RESPONSE=$(curl -s -X GET "$BASE_URL/users" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)") -H "Authorization: Bearer $(cat $TOKEN_FILE)")
echo "$RESPONSE" | jq echo "$RESPONSE" | jq
} }
# Fonction pour créer un utilisateur # Function to create a user
create_user() { create_user() {
if [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then if [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
echo "Les paramètres username et password sont obligatoires pour créer un utilisateur." echo "The username and password parameters are required to create a user."
usage usage
fi fi
echo "Création de l'utilisateur $USERNAME..." echo "Creating user $USERNAME..."
RESPONSE=$(curl -s -X POST "$BASE_URL/users" \ RESPONSE=$(curl -s -X POST "$BASE_URL/users" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)" \ -H "Authorization: Bearer $(cat $TOKEN_FILE)" \
-H "Content-Type: application/json; charset=UTF-8" \ -H "Content-Type: application/json; charset=UTF-8" \
@ -376,31 +434,31 @@ create_user() {
"password": "'"$PASSWORD"'", "password": "'"$PASSWORD"'",
"roles": ["user"] "roles": ["user"]
}') }')
echo "Réponse de la création de l'utilisateur: $RESPONSE" echo "Response from creating user: $RESPONSE"
echo "$RESPONSE" | jq echo "$RESPONSE" | jq
} }
# Fonction pour supprimer un utilisateur # Function to delete a user
delete_user() { delete_user() {
if [ -z "$USERNAME" ]; then if [ -z "$USERNAME" ]; then
echo "L'option --delete-user nécessite un nom d'utilisateur." echo "The --delete-user option requires a username."
usage usage
fi fi
echo "Suppression de l'utilisateur $USERNAME..." echo "Deleting user $USERNAME..."
USER_ID=$(curl -s -X GET "$BASE_URL/users" \ USER_ID=$(curl -s -X GET "$BASE_URL/users" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)" | jq -r '.[] | select(.username == "'"$USERNAME"'") | .id') -H "Authorization: Bearer $(cat $TOKEN_FILE)" | jq -r '.[] | select(.username == "'"$USERNAME"'") | .id')
if [ -n "$USER_ID" ]; then if [ -n "$USER_ID" ]; then
RESPONSE=$(curl -s -X DELETE "$BASE_URL/users/$USER_ID" \ RESPONSE=$(curl -s -X DELETE "$BASE_URL/users/$USER_ID" \
-H "Authorization: Bearer $(cat $TOKEN_FILE)") -H "Authorization: Bearer $(cat $TOKEN_FILE)")
echo "Réponse de la suppression de l'utilisateur: $RESPONSE" echo "Response from deleting user: $RESPONSE"
echo "$RESPONSE" | jq echo "$RESPONSE" | jq
else else
echo "Utilisateur non trouvé : $USERNAME" echo "User not found: $USERNAME"
fi fi
} }
# Appel des fonctions en fonction des options # Call functions based on options
if [ "$CREATE_USER" = true ]; then if [ "$CREATE_USER" = true ]; then
create_user create_user
elif [ "$DELETE_USER" = true ]; then elif [ "$DELETE_USER" = true ]; then