2019-05-05 13:42:53 +03:00
|
|
|
#!/bin/bash
|
|
|
|
|
2019-05-05 14:05:47 +03:00
|
|
|
if [ "$EUID" -ne 0 ]; then
|
|
|
|
echo "You need to run this script as root"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-05-05 18:47:14 +03:00
|
|
|
if [ "$(systemd-detect-virt)" == "openvz" ]; then
|
|
|
|
echo "OpenVZ is not supported"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$(systemd-detect-virt)" == "lxc" ]; then
|
|
|
|
echo "LXC is not supported (yet)."
|
|
|
|
echo "WireGuard can techniaclly run in an LXC container,"
|
|
|
|
echo "but the kernel module has to be installed on the host,"
|
|
|
|
echo "the container has to be run with some specific parameters"
|
|
|
|
echo "and only the tools need to be installed in the container."
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2019-05-05 14:05:47 +03:00
|
|
|
# Detect public IPv4 address and pre-fill for the user
|
|
|
|
SERVER_PUB_IPV4=$(ip addr | grep 'inet' | grep -v inet6 | grep -vE '127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1)
|
|
|
|
read -rp "IPv4 or IPv6 public address: " -e -i "$SERVER_PUB_IPV4" SERVER_PUB_IP
|
|
|
|
|
|
|
|
# Detect public interface and pre-fill for the user
|
|
|
|
SERVER_PUB_NIC="$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1)"
|
|
|
|
read -rp "Public interface: " -e -i "$SERVER_PUB_NIC" SERVER_PUB_NIC
|
|
|
|
|
2019-05-05 13:42:53 +03:00
|
|
|
SERVER_WG_NIC="wg0"
|
2019-05-05 14:05:47 +03:00
|
|
|
read -rp "WireGuard interface name: " -e -i "$SERVER_WG_NIC" SERVER_WG_NIC
|
|
|
|
|
2019-05-05 13:42:53 +03:00
|
|
|
SERVER_WG_IPV4="10.66.66.1"
|
2019-05-05 14:05:47 +03:00
|
|
|
read -rp "Server's WireGuard IPv4 " -e -i "$SERVER_WG_IPV4" SERVER_WG_IPV4
|
|
|
|
|
2019-05-05 13:42:53 +03:00
|
|
|
SERVER_WG_IPV6="fd42:42:42::1"
|
2019-05-05 14:05:47 +03:00
|
|
|
read -rp "Server's WireGuard IPv6 " -e -i "$SERVER_WG_IPV6" SERVER_WG_IPV6
|
|
|
|
|
2019-05-05 13:42:53 +03:00
|
|
|
SERVER_PORT=1194
|
2019-05-05 14:05:47 +03:00
|
|
|
read -rp "Server's WireGuard port " -e -i "$SERVER_PORT" SERVER_PORT
|
|
|
|
|
|
|
|
CLIENT_WG_IPV4="10.66.66.2"
|
|
|
|
read -rp "Client's WireGuard IPv4 " -e -i "$CLIENT_WG_IPV4" CLIENT_WG_IPV4
|
|
|
|
|
|
|
|
CLIENT_WG_IPV6="fd42:42:42::2"
|
|
|
|
read -rp "Client's WireGuard IPv6 " -e -i "$CLIENT_WG_IPV6" CLIENT_WG_IPV6
|
|
|
|
|
|
|
|
# Adguard DNS by default
|
|
|
|
CLIENT_DNS_1="176.103.130.130"
|
|
|
|
read -rp "First DNS resolver to use for the client: " -e -i "$CLIENT_DNS_1" CLIENT_DNS_1
|
2019-05-05 13:42:53 +03:00
|
|
|
|
2019-05-05 14:05:47 +03:00
|
|
|
CLIENT_DNS_2="176.103.130.131"
|
|
|
|
read -rp "Second DNS resolver to use for the client: " -e -i "$CLIENT_DNS_2" CLIENT_DNS_2
|
2019-05-05 13:42:53 +03:00
|
|
|
|
2019-05-05 14:05:47 +03:00
|
|
|
# Install WireGuard on Ubuntu
|
2019-05-05 13:42:53 +03:00
|
|
|
add-apt-repository -y ppa:wireguard/wireguard
|
|
|
|
apt-get install -y wireguard
|
|
|
|
|
2019-05-05 14:05:47 +03:00
|
|
|
# Generate key pair for the server
|
2019-05-05 13:42:53 +03:00
|
|
|
SERVER_PRIV_KEY=$(wg genkey)
|
|
|
|
SERVER_PUB_KEY=$(echo "$SERVER_PRIV_KEY" | wg pubkey)
|
|
|
|
|
2019-05-05 14:05:47 +03:00
|
|
|
# Generate key pair for the server
|
2019-05-05 13:42:53 +03:00
|
|
|
CLIENT_PRIV_KEY=$(wg genkey)
|
|
|
|
CLIENT_PUB_KEY=$(echo "$CLIENT_PRIV_KEY" | wg pubkey)
|
|
|
|
|
|
|
|
# Add server interface
|
|
|
|
echo "[Interface]
|
|
|
|
Address = $SERVER_WG_IPV4/24,$SERVER_WG_IPV6/64
|
|
|
|
ListenPort = $SERVER_PORT
|
|
|
|
PrivateKey = $SERVER_PRIV_KEY
|
|
|
|
PostUp = iptables -t nat -A POSTROUTING -o $SERVER_PUB_NIC -j MASQUERADE; ip6tables -t nat -A POSTROUTING -o $SERVER_PUB_NIC -j MASQUERADE
|
2019-05-05 14:09:07 +03:00
|
|
|
PostDown = iptables -t nat -D POSTROUTING -o $SERVER_PUB_NIC -j MASQUERADE; ip6tables -t nat -D POSTROUTING -o $SERVER_PUB_NIC -j MASQUERADE" > "/etc/wireguard/$SERVER_WG_NIC.conf"
|
2019-05-05 13:42:53 +03:00
|
|
|
|
|
|
|
# Add the client as a peer to the server
|
|
|
|
echo "[Peer]
|
|
|
|
PublicKey = $CLIENT_PUB_KEY
|
2019-05-05 14:09:07 +03:00
|
|
|
AllowedIPs = $CLIENT_WG_IPV4/32,$CLIENT_WG_IPV6/128" >> "/etc/wireguard/$SERVER_WG_NIC.conf"
|
2019-05-05 13:42:53 +03:00
|
|
|
|
|
|
|
# Create client file with interface
|
|
|
|
echo "[Interface]
|
|
|
|
PrivateKey = $CLIENT_PRIV_KEY
|
2019-05-05 14:05:47 +03:00
|
|
|
Address = $CLIENT_IPV4/24,$CLIENT_IPV6/64
|
2019-05-05 14:09:07 +03:00
|
|
|
DNS = $CLIENT_DNS_1,$CLIENT_DNS_2" > "$HOME/$SERVER_WG_NIC-client.conf"
|
2019-05-05 13:42:53 +03:00
|
|
|
|
|
|
|
# Add the server as a peer to the client
|
|
|
|
echo "[Peer]
|
|
|
|
PublicKey = $SERVER_PUB_KEY
|
2019-05-05 14:09:07 +03:00
|
|
|
Endpoint = $SERVER_PUB_IP:1194
|
|
|
|
AllowedIPs = 0.0.0.0/0,::/0" >> "$HOME/$SERVER_WG_NIC-client.conf"
|
2019-05-05 13:42:53 +03:00
|
|
|
|
|
|
|
# Enable routing on the server
|
|
|
|
echo "net.ipv4.ip_forward = 1
|
|
|
|
net.ipv6.conf.all.forwarding = 1" > /etc/sysctl.d/wg.conf
|
|
|
|
|
|
|
|
sysctl --system
|
|
|
|
|
|
|
|
systemctl start "wg-quick@$SERVER_WG_NIC"
|
|
|
|
systemctl enable "wg-quick@$SERVER_WG_NIC"
|