forked from boschkundendienst/guacamole-docker-compose
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip_manage.sh
More file actions
executable file
·151 lines (123 loc) · 4.16 KB
/
Copy pathip_manage.sh
File metadata and controls
executable file
·151 lines (123 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DOCKER_COMPOSE_DIR="$SCRIPT_DIR"
DOCKER_COMPOSE_FILE="$DOCKER_COMPOSE_DIR/docker-compose.yml"
DOCKER_COMPOSE_NGINX_SERVICE_NAME="nginx"
DATABASE="$SCRIPT_DIR/allowlist_database.txt"
ALLOWLIST_FILE="$SCRIPT_DIR/nginx/allowlist.conf"
# Ensure the database file exists
touch "$DATABASE"
# Function to display current allowlist
show_allowlist() {
echo -e "\nCurrent Allowed IPs:"
if [[ ! -s "$DATABASE" ]]; then
echo "[No IPs Whitelisted Yet]"
return
fi
local index=1
while IFS= read -r line; do
echo "$index) $line"
((index++))
done < "$DATABASE"
echo ""
}
# Function to add a new IP
add_ip() {
read -rp "Enter new IP or IP range: " new_ip
echo "$new_ip" >> "$DATABASE"
echo "Added: $new_ip"
}
# Function to edit an IP
edit_ip() {
# Validate the index is numeric and within the valid range
if [[ ! "$param" =~ ^[0-9]+$ ]]; then
echo "Invalid index: Please provide a valid number."
return
fi
old_ip=$(sed -n "${param}p" "$DATABASE")
# Validate if the index exists
if [[ -z "$old_ip" ]]; then
echo "Invalid index: No entry found at index #$param"
return
fi
echo "Current value: $old_ip"
read -rp "Enter new IP or IP range: " new_ip
sed -i "${param}s|.*|$new_ip|" "$DATABASE"
echo "Updated entry #$param to: $new_ip"
}
# Function to delete an IP
delete_ip() {
# Validate the index is numeric and within the valid range
if [[ ! "$param" =~ ^[0-9]+$ ]]; then
echo "Invalid index: Please provide a valid number."
return
fi
# Validate if the index exists
if ! sed -n "${param}p" "$DATABASE" >/dev/null; then
echo "Invalid index: No entry found at index #$param"
return
fi
if ! sed -i "${param}d" "$DATABASE"; then
echo "Invalid index."
return
fi
echo "Deleted entry #$param"
}
# Function to save changes and reload Nginx
save_and_reload() {
# Generate allowlist with the correct format
echo "# Auto-generated allowlist" > "$ALLOWLIST_FILE"
# Read the database file line by line
while IFS= read -r line; do
# Skip empty lines and comments
if [[ "$line" =~ ^\s*# ]] || [[ -z "$line" ]]; then
continue
fi
# Check if the line contains a '#' character
if [[ "$line" == *"#"* ]]; then
# If there's a '#' in the line, split the IP and comment
ip=$(echo "$line" | cut -d '#' -f 1 | xargs) # IP part (before #), remove any extra spaces
comment=$(echo "$line" | cut -d '#' -f 2-) # Comment part (after #), including the comment
# Add the IP with the comment
echo " $ip 1; #$comment" >> "$ALLOWLIST_FILE"
else
# If no '#' in the line, the whole line is the IP
ip=$(echo "$line" | xargs) # IP part (whole line), remove any extra spaces
# Add the IP with no comment
echo " $ip 1;" >> "$ALLOWLIST_FILE"
fi
done < "$DATABASE"
cat "$ALLOWLIST_FILE"
# Reload Nginx
docker compose -f "$DOCKER_COMPOSE_FILE" exec "$DOCKER_COMPOSE_NGINX_SERVICE_NAME" nginx -s reload || {
echo "Error: Could not reload Nginx. Is the container running?"
return
}
echo "Nginx allowlist updated and reloaded."
}
show_running_ngnix_config(){
docker compose -f "$DOCKER_COMPOSE_FILE" exec $DOCKER_COMPOSE_NGINX_SERVICE_NAME nginx -T
}
# Main menu loop
while true; do
show_allowlist # Always show the list before taking input
echo "Choose an action:"
echo "A - Add IP"
echo "E # - Edit IP (#)"
echo "D # - Delete IP (#)"
echo "S - Save & Apply changes"
echo "C - Show running ngnix config"
echo "Q - Quit"
read -rp "Enter command: " action param
case "$action" in
A|a) add_ip ;;
E|e) edit_ip "$param" ;;
D|d) delete_ip "$param" ;;
S|s) save_and_reload ;;
C|c) show_running_ngnix_config ;;
Q|q) echo "Exiting..."; exit 0 ;;
*) echo "Invalid input, try again." ;;
esac
# Clear the param after operation
param=""
done