Skip to content

Commit 37f7be2

Browse files
authored
Update wg.sh
1 parent 936a03c commit 37f7be2

File tree

1 file changed

+65
-46
lines changed

1 file changed

+65
-46
lines changed

wireguard/wg.sh

Lines changed: 65 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,132 +2,151 @@
22
set -euo pipefail
33

44
# =========================================
5-
# SETUP WIREGUARD VPN - IMPROVED VERSION
5+
# WIREGUARD VPN SETUP SCRIPT
66
# =========================================
77

8-
# Configuration variables
8+
# === CONFIGURATION ===
99
readonly WG_PORT=8888
1010
readonly WG_NETWORK="10.88.88.1/22"
1111
readonly SCRIPTS_BASE_URL="https://raw.githubusercontent.com/givps/AutoScriptXray/master/wireguard"
1212

13-
# Colors for output
13+
# === COLORS ===
1414
readonly RED='\033[0;31m'
1515
readonly GREEN='\033[0;32m'
1616
readonly YELLOW='\033[1;33m'
1717
readonly NC='\033[0m'
1818

19-
# Logging functions
19+
# === LOGGING ===
2020
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
2121
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
2222
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
2323

24-
# Check if running as root
24+
# === ROOT CHECK ===
2525
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!"
2727
exit 1
2828
fi
2929

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
4034
fi
4135

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
4343
rm -rf /etc/wireguard
4444

45-
# Update and install dependencies
46-
log_info "Updating system and installing dependencies..."
45+
# === INSTALL DEPENDENCIES ===
46+
log_info "Updating packages and installing dependencies..."
4747
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
4949

50-
# Create configuration directory
50+
# === CREATE CONFIG DIRECTORY ===
5151
mkdir -p /etc/wireguard
52+
chmod 700 /etc/wireguard
5253

53-
# Generate keys with proper permissions
54+
# === GENERATE SERVER KEYS ===
5455
log_info "Generating WireGuard keys..."
5556
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
5762
privkey=$(wg genkey)
5863
pubkey=$(echo "$privkey" | wg pubkey)
5964
echo "$privkey" > /etc/wireguard/private.key
6065
echo "$pubkey" > /etc/wireguard/public.key
61-
else
62-
privkey=$(< /etc/wireguard/private.key)
63-
pubkey=$(< /etc/wireguard/public.key)
6466
fi
6567

66-
# Detect default interface
67-
log_info "Detecting network interface..."
68+
# === DETECT DEFAULT INTERFACE ===
69+
log_info "Detecting default network interface..."
6870
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)}
6972
if [ -z "$interface" ]; then
7073
log_error "Failed to detect default network interface!"
7174
exit 1
7275
fi
73-
log_info "Default interface detected: $interface"
76+
log_info "Default interface: $interface"
7477

75-
# Create WireGuard config
78+
# === CREATE WIREGUARD CONFIG ===
7679
log_info "Creating WireGuard configuration..."
7780
cat > /etc/wireguard/wg0.conf <<EOF
7881
[Interface]
7982
Address = $WG_NETWORK
8083
ListenPort = $WG_PORT
8184
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
8489
SaveConfig = true
8590
EOF
8691

8792
chmod 600 /etc/wireguard/wg0.conf
8893

89-
# Enable IP forwarding
94+
# === ENABLE IP FORWARDING ===
9095
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
92100
sysctl --system >/dev/null 2>&1
93101

94-
# Start WireGuard service
95-
log_info "Starting WireGuard service..."
102+
# === ENABLE SERVICE ===
103+
log_info "Enabling WireGuard service..."
104+
systemctl daemon-reload
96105
systemctl enable wg-quick@wg0.service >/dev/null 2>&1
97106

98107
if systemctl start wg-quick@wg0.service; then
99108
sleep 2
100109
if systemctl is-active --quiet wg-quick@wg0.service; then
101110
log_info "WireGuard service started successfully!"
102111
else
103-
log_error "WireGuard service failed to start"
112+
log_error "WireGuard service failed to start."
104113
exit 1
105114
fi
106115
else
107-
log_error "Failed to start WireGuard service"
116+
log_error "Unable to start WireGuard service."
108117
exit 1
109118
fi
110119

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
114125

126+
# === DOWNLOAD MANAGEMENT SCRIPTS ===
127+
log_info "Downloading WireGuard management tools..."
128+
cd /usr/bin || exit 1
115129
scripts=("m-wg" "wg-add" "wg-del" "wg-renew" "wg-show")
116130
for script in "${scripts[@]}"; do
117131
if wget -q -O "$script" "$SCRIPTS_BASE_URL/${script}.sh"; then
118132
chmod +x "$script"
119-
log_info "Downloaded $script"
133+
log_info "Installed: $script"
120134
else
121-
log_error "Failed to download $script"
135+
log_warn "Failed to download $script"
122136
fi
123137
done
124138

125-
# Display server information
139+
# === FINAL INFORMATION ===
126140
echo
127-
log_info "=== WireGuard Setup Complete ==="
141+
log_info "===================================="
142+
log_info " WireGuard Setup Completed"
143+
log_info "===================================="
128144
echo "Public Key : $pubkey"
129145
echo "Listen Port: $WG_PORT"
130146
echo "Interface : $interface"
131147
echo "Network : $WG_NETWORK"
132148
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

Comments
 (0)