-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse-proxy.sh
executable file
·395 lines (320 loc) · 10.4 KB
/
reverse-proxy.sh
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/bin/bash
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Updated paths for macOS Homebrew
NGINX_BASE="/opt/homebrew/etc/nginx"
NGINX_SITES_DIR="$NGINX_BASE/sites-enabled"
CERT_DIR="$HOME/.local/share/proxy-pal/certs"
LAST_DOMAIN_FILE="$HOME/.local/share/proxy-pal/.last_domain"
# Function to ensure directories exist with proper permissions
ensure_directories() {
local dirs=(
"$NGINX_BASE"
"$NGINX_SITES_DIR"
"/opt/homebrew/var/log/nginx"
"/opt/homebrew/var/run/nginx"
"$CERT_DIR"
)
for dir in "${dirs[@]}"; do
if [ ! -d "$dir" ]; then
echo "Creating directory: $dir"
sudo mkdir -p "$dir"
sudo chown $(whoami):admin "$dir"
sudo chmod 755 "$dir"
fi
done
}
# Function to create required directories
create_directories() {
echo -e "${YELLOW}Creating required directories...${NC}"
# Create Nginx directories
sudo mkdir -p "$NGINX_SITES_DIR"
sudo mkdir -p "/opt/homebrew/var/log/nginx"
sudo mkdir -p "/opt/homebrew/var/run/nginx"
# Create certificate directory
mkdir -p "$CERT_DIR"
# Set proper permissions
sudo chown -R $(whoami):admin "$NGINX_BASE"
sudo chown -R $(whoami):admin "/opt/homebrew/var/log/nginx"
sudo chown -R $(whoami):admin "/opt/homebrew/var/run/nginx"
}
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check if string exists in file
string_exists_in_file() {
local string=$1
local file=$2
grep -q "$string" "$file" 2>/dev/null
return $?
}
# Function to install required tools
install_requirements() {
if ! command_exists brew; then
echo -e "${RED}Homebrew is required. Please install it first.${NC}"
exit 1
fi
if ! command_exists nginx; then
echo -e "${YELLOW}Installing nginx...${NC}"
brew install nginx
fi
if ! command_exists mkcert; then
echo -e "${YELLOW}Installing mkcert...${NC}"
brew install mkcert
brew install nss # for Firefox support
fi
}
# Function to update Nginx main configuration
update_nginx_main_config() {
echo -e "${YELLOW}Updating Nginx main configuration...${NC}"
# Backup existing config if it exists
if [ -f "$NGINX_BASE/nginx.conf" ]; then
sudo cp "$NGINX_BASE/nginx.conf" "$NGINX_BASE/nginx.conf.backup"
fi
# Create new main configuration
sudo tee "$NGINX_BASE/nginx.conf" > /dev/null << 'EOF'
worker_processes auto;
pid /opt/homebrew/var/run/nginx/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /opt/homebrew/var/log/nginx/access.log;
error_log /opt/homebrew/var/log/nginx/error.log debug;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
# Include all site configurations
include /opt/homebrew/etc/nginx/sites-enabled/*.conf;
}
EOF
}
# Function to update /etc/hosts
update_hosts() {
local domain=$1
local base_domain=$(echo $domain | sed 's/^www\.//')
local hosts_entry="127.0.0.1 ${domain} ${base_domain}"
echo -e "${YELLOW}Updating /etc/hosts...${NC}"
if ! string_exists_in_file "${domain}" "/etc/hosts"; then
echo "Adding hosts entry..."
echo "$hosts_entry" | sudo tee -a /etc/hosts > /dev/null
echo -e "${GREEN}Hosts file updated successfully${NC}"
else
echo -e "${YELLOW}Domain already exists in /etc/hosts${NC}"
fi
# Save domain for later use
echo "$domain" > "$LAST_DOMAIN_FILE"
}
# Function to generate SSL certificates
generate_ssl() {
local domain=$1
local base_domain=$(echo $domain | sed 's/^www\.//')
echo -e "${YELLOW}Generating SSL certificates...${NC}"
# Create certificates directory if it doesn't exist
mkdir -p "$CERT_DIR"
# Initialize mkcert if not already done
if [ ! -f "$HOME/.local/share/mkcert/rootCA.pem" ]; then
echo "Initializing mkcert..."
mkcert -install
fi
# Generate certificate for both www and non-www versions
echo "Generating certificates for ${domain} and ${base_domain}..."
mkcert -key-file "$CERT_DIR/${base_domain}-key.pem" \
-cert-file "$CERT_DIR/${base_domain}-cert.pem" \
"${domain}" "${base_domain}" "localhost" "127.0.0.1" "::1"
echo -e "${GREEN}SSL certificates generated successfully${NC}"
}
# Function to create site configuration
create_nginx_config() {
local target_url=$1
local domain=$(cat "$LAST_DOMAIN_FILE")
local base_domain=$(echo $domain | sed 's/^www\.//')
echo -e "${YELLOW}Creating site configuration...${NC}"
# Ensure the sites-enabled directory exists
ensure_directories
# Create the configuration file
sudo tee "$NGINX_SITES_DIR/${base_domain}.conf" > /dev/null << EOF
# HTTPS Server
server {
listen 443 ssl;
http2;
listen [::]:443 ssl;
http2;
server_name ${domain} ${base_domain};
ssl_certificate $CERT_DIR/${base_domain}-cert.pem;
ssl_certificate_key $CERT_DIR/${base_domain}-key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
location / {
proxy_pass ${target_url};
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host \$host;
proxy_cache_bypass \$http_upgrade;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
# HTTP redirect
server {
listen 80;
listen [::]:80;
server_name ${domain} ${base_domain};
return 301 https://\$host\$request_uri;
}
EOF
# Verify the file was created
if [ ! -f "$NGINX_SITES_DIR/${base_domain}.conf" ]; then
echo -e "${RED}Failed to create Nginx configuration file${NC}"
exit 1
fi
}
# Function to restart Nginx
restart_nginx() {
echo -e "${YELLOW}Restarting Nginx...${NC}"
# Test configuration
if ! sudo nginx -t; then
echo -e "${RED}Nginx configuration test failed${NC}"
exit 1
fi
# Restart service
sudo brew services reload nginx
echo -e "${GREEN}Nginx restarted successfully${NC}"
}
# Setup command
setup() {
local domain=$1
# Validate domain format
if [[ ! $domain =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
echo -e "${RED}Invalid domain format. Please use a valid domain name.${NC}"
exit 1
fi
# Install required tools
install_requirements
# Ensure directories exist
ensure_directories
# Create required directories
create_directories
# Update main Nginx configuration
update_nginx_main_config
# Update hosts file
update_hosts "$domain"
# Generate SSL certificates
generate_ssl "$domain"
echo -e "\n${GREEN}Setup completed successfully!${NC}"
echo -e "${YELLOW}Use 'pp link <target-url>' to create the reverse proxy${NC}"
}
# Link command
link() {
local target_url=$1
# Check if setup was run first
if [ ! -f "$LAST_DOMAIN_FILE" ]; then
echo -e "${RED}Error: Please run 'pp setup <domain>' first${NC}"
exit 1
fi
# Validate target URL
if [[ ! $target_url =~ ^http[s]?:// ]]; then
echo -e "${RED}Invalid target URL. Please include http:// or https://${NC}"
exit 1
fi
# Create Nginx configuration
create_nginx_config "$target_url"
# Restart Nginx
restart_nginx
local domain=$(cat "$LAST_DOMAIN_FILE")
echo -e "\n${GREEN}Reverse proxy setup completed!${NC}"
echo -e "${YELLOW}Your site is now available at:${NC}"
echo -e " https://${domain}"
}
unlink() {
local domain=$1
if ! string_exists_in_file "$domain" "$LAST_DOMAIN_FILE"; then
echo -e "${RED}Error: Domain does not exist${NC}"
exit 1
fi
echo -e "${YELLOW}Unlinking ${domain}...${NC}"
local base_domain=$(echo $domain | sed 's/^www\.//')
# Remove Nginx configuration files
sudo rm -f "$NGINX_SITES_DIR/${base_domain}.conf"
# Restart Nginx
restart_nginx
echo -e "${GREEN}Unlink completed successfully${NC}"
}
# write me a cleanup function to remove the created files and directories
cleanup() {
echo -e "${YELLOW}Cleaning up...${NC}"
# Remove Nginx configuration files
local domain=$(cat "$LAST_DOMAIN_FILE")
local base_domain=$(echo $domain | sed 's/^www\.//')
sudo rm -f "$NGINX_SITES_DIR/${base_domain}.conf"
# Remove certificates
rm -rf "$CERT_DIR"
# Remove last domain file
rm -f "$LAST_DOMAIN_FILE"
echo -e "${GREEN}Cleanup completed successfully${NC}"
}
# Show help
show_help() {
echo "Usage: pp <command> <argument>"
echo ""
echo "Commands:"
echo " setup <domain> Setup local domain with SSL certificates"
echo " link <target-url> Create reverse proxy to target URL"
echo " unlink <domain> Remove reverse proxy for domain"
echo " cleanup Remove all created files and directories"
echo " help Show this help message"
echo ""
echo "Examples:"
echo " pp setup www.nixify.dev"
echo " pp link http://localhost:3000/"
echo ""
}
# Create necessary directories
mkdir -p "$HOME/.local/share/proxy-pal"
# Main script logic
case $1 in
setup)
if [ -z "$2" ]; then
echo -e "${RED}Error: Domain name is required${NC}"
show_help
exit 1
fi
setup "$2"
;;
link)
if [ -z "$2" ]; then
echo -e "${RED}Error: Target URL is required${NC}"
show_help
exit 1
fi
link "$2"
;;
unlink)
if [ -z "$2" ]; then
echo -e "${RED}Error: Domain name is required${NC}"
show_help
exit 1
fi
unlink "$2"
;;
help)
show_help
;;
*)
echo -e "${RED}Error: Unknown command '$1'${NC}"
show_help
exit 1
;;
esac