-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·351 lines (306 loc) · 8.41 KB
/
setup.sh
File metadata and controls
executable file
·351 lines (306 loc) · 8.41 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
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
#!/bin/bash
#
# Pi Gateway - Setup Script
# Secure remote access gateway for Raspberry Pi
#
# Check Bash version (need 4.0+)
if [[ ${BASH_VERSION%%.*} -lt 4 ]]; then
echo "[ERROR] This script requires Bash 4.0 or later"
echo "Current version: $BASH_VERSION"
exit 1
fi
# Set strict error handling
set -eo pipefail
# Source common utilities
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/scripts/common.sh"
# Configuration
readonly CONFIG_FILE="$SCRIPT_DIR/config/setup.conf"
# Setup phases
readonly PHASES=(
"requirements"
"dependencies"
"hardening"
"ssh"
"vpn"
"firewall"
"remote-desktop"
"ddns"
)
declare -A PHASE_SCRIPTS=(
["requirements"]="scripts/check-requirements.sh"
["dependencies"]="scripts/install-dependencies.sh"
["hardening"]="scripts/system-hardening.sh"
["ssh"]="scripts/ssh-setup.sh"
["vpn"]="scripts/vpn-setup.sh"
["firewall"]="scripts/firewall-setup.sh"
["remote-desktop"]="scripts/remote-desktop.sh"
["ddns"]="scripts/ddns-setup.sh"
)
declare -A PHASE_NAMES=(
["requirements"]="System Requirements"
["dependencies"]="Dependencies"
["hardening"]="System Hardening"
["ssh"]="SSH Setup"
["vpn"]="VPN Setup"
["firewall"]="Firewall"
["remote-desktop"]="Remote Desktop"
["ddns"]="Dynamic DNS"
)
# Setup options
INTERACTIVE_MODE=true
DRY_RUN="${DRY_RUN:-false}"
SELECTED_PHASES=()
SETUP_ERROR_COUNT=0
# Show help
show_help() {
echo "Pi Gateway Setup - Secure Remote Access Gateway"
echo ""
echo "Usage: $(basename "$0") [OPTIONS]"
echo ""
echo "Options:"
echo " -h, --help Show this help message"
echo " -d, --dry-run Run without making system changes"
echo " -n, --non-interactive Run without prompts (full install)"
echo ""
echo "Examples:"
echo " $(basename "$0") # Interactive setup"
echo " $(basename "$0") --dry-run # Test without changes"
echo ""
}
# Parse arguments
parse_arguments() {
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-d|--dry-run)
DRY_RUN=true
shift
;;
-n|--non-interactive)
INTERACTIVE_MODE=false
shift
;;
*)
error "Unknown option: $1"
show_help
exit 1
;;
esac
done
}
# Interactive component selection
select_components() {
echo ""
echo "Pi Gateway Setup"
echo "================"
echo ""
echo "Select installation type:"
echo " 1. Full Installation (All components)"
echo " 2. Core Security (SSH + VPN + Firewall)"
echo " 3. Custom Selection"
echo ""
while true; do
read -r -p "Choice [1-3]: " choice
case $choice in
1)
SELECTED_PHASES=("${PHASES[@]}")
info "Selected: Full Installation"
break
;;
2)
SELECTED_PHASES=("requirements" "dependencies" "hardening" "ssh" "vpn" "firewall")
info "Selected: Core Security"
break
;;
3)
select_custom_components
break
;;
*)
warning "Invalid choice. Please enter 1, 2, or 3."
;;
esac
done
}
# Custom component selection
select_custom_components() {
echo ""
info "Core components (always included):"
echo " - System Requirements"
echo " - Dependencies"
echo " - System Hardening"
echo " - SSH Setup"
echo ""
SELECTED_PHASES=("requirements" "dependencies" "hardening" "ssh")
# VPN
read -r -p "Install VPN (WireGuard)? [Y/n]: " response
if [[ ! "$response" =~ ^[Nn]$ ]]; then
SELECTED_PHASES+=("vpn")
fi
# Firewall
read -r -p "Install Firewall? [Y/n]: " response
if [[ ! "$response" =~ ^[Nn]$ ]]; then
SELECTED_PHASES+=("firewall")
fi
# Remote Desktop
read -r -p "Install Remote Desktop (VNC)? [y/N]: " response
if [[ "$response" =~ ^[Yy]$ ]]; then
SELECTED_PHASES+=("remote-desktop")
fi
# DDNS
read -r -p "Install Dynamic DNS? [y/N]: " response
if [[ "$response" =~ ^[Yy]$ ]]; then
SELECTED_PHASES+=("ddns")
fi
}
# Execute a single phase
execute_phase() {
local phase="$1"
local script="${PHASE_SCRIPTS[$phase]}"
local name="${PHASE_NAMES[$phase]}"
info "Starting: $name"
if [[ ! -f "$script" ]]; then
error "Script not found: $script"
return 1
fi
export DRY_RUN
if "$script"; then
success "Completed: $name"
return 0
else
error "Failed: $name"
((SETUP_ERROR_COUNT++))
return 1
fi
}
# Run all selected phases
run_setup_phases() {
local total_phases=${#SELECTED_PHASES[@]}
local completed=0
local failed=0
info "Starting Pi Gateway setup ($total_phases phases)"
echo ""
for phase in "${SELECTED_PHASES[@]}"; do
((completed++))
echo "[$completed/$total_phases] ${PHASE_NAMES[$phase]}"
echo "----------------------------------------"
if execute_phase "$phase"; then
echo ""
else
((failed++))
if [[ "$INTERACTIVE_MODE" == "true" ]]; then
read -r -p "Continue with remaining phases? [y/N]: " response
if [[ ! "$response" =~ ^[Yy]$ ]]; then
error "Setup aborted by user"
return 1
fi
else
error "Aborting non-interactive setup after failure"
return 1
fi
fi
done
echo ""
if [[ $failed -eq 0 ]]; then
success "Setup completed successfully"
return 0
else
warning "Setup completed with $failed failures"
return 1
fi
}
# Show connection information
show_connection_info() {
echo ""
echo "Connection Information"
echo "======================"
echo ""
local local_ip
local_ip=$(hostname -I | awk '{print $1}' 2>/dev/null || echo "192.168.1.x")
echo "SSH Access:"
echo " ssh -p 2222 pi@$local_ip"
echo ""
if [[ " ${SELECTED_PHASES[*]} " =~ " vpn " ]]; then
echo "VPN Access:"
echo " Server: $local_ip:51820"
echo " Add clients: sudo ./scripts/vpn-client-manager.sh add <name>"
echo ""
fi
if [[ " ${SELECTED_PHASES[*]} " =~ " remote-desktop " ]]; then
echo "Remote Desktop:"
echo " VNC: $local_ip:5900"
echo ""
fi
}
# Show next steps
show_next_steps() {
echo "Next Steps"
echo "=========="
echo ""
echo "1. Configure router port forwarding:"
echo " - Port 2222 for SSH"
if [[ " ${SELECTED_PHASES[*]} " =~ " vpn " ]]; then
echo " - Port 51820 for VPN"
fi
echo ""
echo "2. Test connectivity from another device"
echo ""
echo "3. Check service status:"
echo " systemctl status ssh"
if [[ " ${SELECTED_PHASES[*]} " =~ " vpn " ]]; then
echo " systemctl status wg-quick@wg0"
fi
echo ""
}
# Main execution
main() {
# Parse arguments
parse_arguments "$@"
# Initialize
info "Pi Gateway v${PI_GATEWAY_VERSION}"
echo ""
if [[ "$DRY_RUN" == "true" ]]; then
warning "DRY RUN MODE - No system changes will be made"
echo ""
fi
# Component selection
if [[ "$INTERACTIVE_MODE" == "true" ]]; then
select_components
echo ""
read -r -p "Proceed with installation? [Y/n]: " response
if [[ "$response" =~ ^[Nn]$ ]]; then
info "Installation cancelled"
exit 0
fi
echo ""
else
SELECTED_PHASES=("${PHASES[@]}")
info "Running non-interactive setup (all components)"
echo ""
fi
# Run setup
local start_time
start_time=$(date +%s)
if run_setup_phases; then
local end_time
end_time=$(date +%s)
local duration=$((end_time - start_time))
echo ""
success "Pi Gateway setup completed in ${duration}s"
echo ""
show_connection_info
show_next_steps
return 0
else
error "Pi Gateway setup failed"
return 1
fi
}
# Run main function
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi