|
2 | 2 | set -euo pipefail |
3 | 3 |
|
4 | 4 | # ========================================= |
5 | | -# SETUP WIREGUARD VPN - IMPROVED VERSION |
| 5 | +# WIREGUARD VPN SETUP SCRIPT |
6 | 6 | # ========================================= |
7 | 7 |
|
8 | | -# Configuration variables |
| 8 | +# === CONFIGURATION === |
9 | 9 | readonly WG_PORT=8888 |
10 | 10 | readonly WG_NETWORK="10.88.88.1/22" |
11 | 11 | readonly SCRIPTS_BASE_URL="https://raw.githubusercontent.com/givps/AutoScriptXray/master/wireguard" |
12 | 12 |
|
13 | | -# Colors for output |
| 13 | +# === COLORS === |
14 | 14 | readonly RED='\033[0;31m' |
15 | 15 | readonly GREEN='\033[0;32m' |
16 | 16 | readonly YELLOW='\033[1;33m' |
17 | 17 | readonly NC='\033[0m' |
18 | 18 |
|
19 | | -# Logging functions |
| 19 | +# === LOGGING === |
20 | 20 | log_info() { echo -e "${GREEN}[INFO]${NC} $1"; } |
21 | 21 | log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } |
22 | 22 | log_error() { echo -e "${RED}[ERROR]${NC} $1"; } |
23 | 23 |
|
24 | | -# Check if running as root |
| 24 | +# === ROOT CHECK === |
25 | 25 | if [[ $EUID -ne 0 ]]; then |
26 | | - log_error "This script must be run as root" |
| 26 | + log_error "This script must be run as root!" |
27 | 27 | exit 1 |
28 | 28 | fi |
29 | 29 |
|
30 | | -# Cleanup existing installation |
31 | | -log_info "Cleaning up existing WireGuard installation..." |
32 | | -rm -f /usr/bin/m-wg /usr/bin/wg-add /usr/bin/wg-del /usr/bin/wg-renew /usr/bin/wg-show |
33 | | - |
34 | | -if systemctl is-active --quiet wg-quick@wg0; then |
35 | | - systemctl stop wg-quick@wg0 |
36 | | -fi |
37 | | - |
38 | | -if systemctl is-enabled --quiet wg-quick@wg0; then |
39 | | - systemctl disable wg-quick@wg0 |
| 30 | +# === OS VALIDATION === |
| 31 | +if ! grep -qEi "debian|ubuntu" /etc/os-release; then |
| 32 | + log_error "Unsupported OS. Please use Debian or Ubuntu." |
| 33 | + exit 1 |
40 | 34 | fi |
41 | 35 |
|
42 | | -apt purge -y wireguard || true |
| 36 | +# === CLEANUP OLD INSTALLATION === |
| 37 | +log_info "Cleaning up any existing WireGuard installation..." |
| 38 | +systemctl stop wg-quick@wg0.service >/dev/null 2>&1 || true |
| 39 | +systemctl disable wg-quick@wg0.service >/dev/null 2>&1 || true |
| 40 | +systemctl reset-failed wg-quick@wg0.service >/dev/null 2>&1 || true |
| 41 | +rm -f /usr/bin/m-wg /usr/bin/wg-add /usr/bin/wg-del /usr/bin/wg-renew /usr/bin/wg-show |
| 42 | +apt purge -y wireguard >/dev/null 2>&1 || true |
43 | 43 | rm -rf /etc/wireguard |
44 | 44 |
|
45 | | -# Update and install dependencies |
46 | | -log_info "Updating system and installing dependencies..." |
| 45 | +# === INSTALL DEPENDENCIES === |
| 46 | +log_info "Updating packages and installing dependencies..." |
47 | 47 | apt update -qq |
48 | | -apt install -y wireguard qrencode resolvconf iproute2 iptables -qq |
| 48 | +apt install -y wget qrencode wireguard iproute2 iptables >/dev/null 2>&1 |
49 | 49 |
|
50 | | -# Create configuration directory |
| 50 | +# === CREATE CONFIG DIRECTORY === |
51 | 51 | mkdir -p /etc/wireguard |
| 52 | +chmod 700 /etc/wireguard |
52 | 53 |
|
53 | | -# Generate keys with proper permissions |
| 54 | +# === GENERATE SERVER KEYS === |
54 | 55 | log_info "Generating WireGuard keys..." |
55 | 56 | umask 077 |
56 | | -if [ ! -s /etc/wireguard/private.key ]; then |
| 57 | +if [ -s /etc/wireguard/private.key ]; then |
| 58 | + log_warn "Existing key found. Keeping old key." |
| 59 | + privkey=$(< /etc/wireguard/private.key) |
| 60 | + pubkey=$(< /etc/wireguard/public.key) |
| 61 | +else |
57 | 62 | privkey=$(wg genkey) |
58 | 63 | pubkey=$(echo "$privkey" | wg pubkey) |
59 | 64 | echo "$privkey" > /etc/wireguard/private.key |
60 | 65 | echo "$pubkey" > /etc/wireguard/public.key |
61 | | -else |
62 | | - privkey=$(< /etc/wireguard/private.key) |
63 | | - pubkey=$(< /etc/wireguard/public.key) |
64 | 66 | fi |
65 | 67 |
|
66 | | -# Detect default interface |
67 | | -log_info "Detecting network interface..." |
| 68 | +# === DETECT DEFAULT INTERFACE === |
| 69 | +log_info "Detecting default network interface..." |
68 | 70 | interface=$(ip route get 1 2>/dev/null | awk '{print $5; exit}') |
| 71 | +interface=${interface:-$(ip -o -4 route show to default | awk '{print $5}' | head -1)} |
69 | 72 | if [ -z "$interface" ]; then |
70 | 73 | log_error "Failed to detect default network interface!" |
71 | 74 | exit 1 |
72 | 75 | fi |
73 | | -log_info "Default interface detected: $interface" |
| 76 | +log_info "Default interface: $interface" |
74 | 77 |
|
75 | | -# Create WireGuard config |
| 78 | +# === CREATE WIREGUARD CONFIG === |
76 | 79 | log_info "Creating WireGuard configuration..." |
77 | 80 | cat > /etc/wireguard/wg0.conf <<EOF |
78 | 81 | [Interface] |
79 | 82 | Address = $WG_NETWORK |
80 | 83 | ListenPort = $WG_PORT |
81 | 84 | PrivateKey = $privkey |
82 | | -PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o $interface -j MASQUERADE |
83 | | -PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o $interface -j MASQUERADE |
| 85 | +PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; \ |
| 86 | + iptables -t nat -A POSTROUTING -o $interface -j MASQUERADE; iptables-save > /etc/iptables/rules.v4 |
| 87 | +PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; \ |
| 88 | + iptables -t nat -D POSTROUTING -o $interface -j MASQUERADE; iptables-save > /etc/iptables/rules.v4 |
84 | 89 | SaveConfig = true |
85 | 90 | EOF |
86 | 91 |
|
87 | 92 | chmod 600 /etc/wireguard/wg0.conf |
88 | 93 |
|
89 | | -# Enable IP forwarding |
| 94 | +# === ENABLE IP FORWARDING === |
90 | 95 | log_info "Configuring system networking..." |
91 | | -echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/30-wireguard.conf |
| 96 | +cat > /etc/sysctl.d/30-wireguard.conf <<EOF |
| 97 | +net.ipv4.ip_forward=1 |
| 98 | +net.ipv6.conf.all.forwarding=1 |
| 99 | +EOF |
92 | 100 | sysctl --system >/dev/null 2>&1 |
93 | 101 |
|
94 | | -# Start WireGuard service |
95 | | -log_info "Starting WireGuard service..." |
| 102 | +# === ENABLE SERVICE === |
| 103 | +log_info "Enabling WireGuard service..." |
| 104 | +systemctl daemon-reload |
96 | 105 | systemctl enable wg-quick@wg0.service >/dev/null 2>&1 |
97 | 106 |
|
98 | 107 | if systemctl start wg-quick@wg0.service; then |
99 | 108 | sleep 2 |
100 | 109 | if systemctl is-active --quiet wg-quick@wg0.service; then |
101 | 110 | log_info "WireGuard service started successfully!" |
102 | 111 | else |
103 | | - log_error "WireGuard service failed to start" |
| 112 | + log_error "WireGuard service failed to start." |
104 | 113 | exit 1 |
105 | 114 | fi |
106 | 115 | else |
107 | | - log_error "Failed to start WireGuard service" |
| 116 | + log_error "Unable to start WireGuard service." |
108 | 117 | exit 1 |
109 | 118 | fi |
110 | 119 |
|
111 | | -# Download management scripts |
112 | | -log_info "Downloading management scripts..." |
113 | | -cd /usr/bin || exit 1 |
| 120 | +# === PERSIST IPTABLES === |
| 121 | +if [ ! -d /etc/iptables ]; then |
| 122 | + mkdir -p /etc/iptables |
| 123 | +fi |
| 124 | +iptables-save > /etc/iptables/rules.v4 |
114 | 125 |
|
| 126 | +# === DOWNLOAD MANAGEMENT SCRIPTS === |
| 127 | +log_info "Downloading WireGuard management tools..." |
| 128 | +cd /usr/bin || exit 1 |
115 | 129 | scripts=("m-wg" "wg-add" "wg-del" "wg-renew" "wg-show") |
116 | 130 | for script in "${scripts[@]}"; do |
117 | 131 | if wget -q -O "$script" "$SCRIPTS_BASE_URL/${script}.sh"; then |
118 | 132 | chmod +x "$script" |
119 | | - log_info "Downloaded $script" |
| 133 | + log_info "Installed: $script" |
120 | 134 | else |
121 | | - log_error "Failed to download $script" |
| 135 | + log_warn "Failed to download $script" |
122 | 136 | fi |
123 | 137 | done |
124 | 138 |
|
125 | | -# Display server information |
| 139 | +# === FINAL INFORMATION === |
126 | 140 | echo |
127 | | -log_info "=== WireGuard Setup Complete ===" |
| 141 | +log_info "====================================" |
| 142 | +log_info " WireGuard Setup Completed" |
| 143 | +log_info "====================================" |
128 | 144 | echo "Public Key : $pubkey" |
129 | 145 | echo "Listen Port: $WG_PORT" |
130 | 146 | echo "Interface : $interface" |
131 | 147 | echo "Network : $WG_NETWORK" |
132 | 148 | echo |
133 | | -log_info "Use 'm-wg' to manage WireGuard clients" |
| 149 | +log_info "Use 'm-wg' command to manage WireGuard clients." |
| 150 | +echo |
| 151 | + |
| 152 | +# === END OF SCRIPT === |
0 commit comments