-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
73 lines (58 loc) · 2.01 KB
/
install.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
#!/bin/bash
# Exit on errors
set -e
# Variables
REPO_URL="https://github.com/associate2coder/vpn-api.git"
INSTALL_DIR="/opt/wireguard-api"
SERVICE_FILE="/etc/systemd/system/wireguard-api.service"
SERVER_IP=$(curl -s https://api64.ipify.org)
# Detect network interface dynamically (first non-loopback, non-virtual)
INTERFACE=$(ip -o -4 route show default | awk '{print $5}')
if [[ -z "$INTERFACE" ]]; then
echo "Error: No active network interface detected!" >&2
exit 1
fi
echo "Updating system..."
sudo apt update && sudo apt upgrade -y
echo "Installing required packages..."
sudo apt install -y wireguard qrencode nodejs npm git openresolv
echo "Cloning WireGuard API from GitHub..."
sudo rm -rf $INSTALL_DIR
sudo git clone $REPO_URL $INSTALL_DIR
cd $INSTALL_DIR
echo "Installing Node.js dependencies..."
sudo npm install
echo "Creating systemd service..."
sudo tee $SERVICE_FILE > /dev/null <<EOL
[Unit]
Description=WireGuard API Service
After=network.target
[Service]
ExecStart=/usr/bin/node $INSTALL_DIR/src/server.js
Restart=always
User=root
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
EOL
echo "Enabling and starting WireGuard API service..."
sudo systemctl daemon-reload
sudo systemctl enable wireguard-api
sudo systemctl restart wireguard-api # [MODIFIED] Restart instead of start (ensures reloading)
# Enable packet forwarding for VPN clients
echo "Configuring IP forwarding..."
sudo tee /etc/sysctl.d/99-wireguard.conf > /dev/null <<EOL
net.ipv4.ip_forward = 1
EOL
sudo sysctl --system
# Configure NAT for WireGuard
echo "Setting up NAT for WireGuard..."
sudo iptables -t nat -A POSTROUTING -o $INTERFACE -j MASQUERADE
sudo iptables -A FORWARD -i wg0 -o $INTERFACE -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i $INTERFACE -o wg0 -j ACCEPT
# [ADDED] Make iptables rules persistent
sudo apt install -y iptables-persistent
sudo netfilter-persistent save
sudo netfilter-persistent reload
echo "Installation complete!"
echo "Your WireGuard API is running on http://$SERVER_IP:3000"