How can we help?
Установка:
git clone https://github.com/pprometey/wireguard_aws.git wireguard_aws cd wireguard_aws sudo ./install.sh # установит сервер sudo ./initial.sh # установит сервер и одного клиента sudo ./remove.sh # удалит сервер
Добавить клиента на сервер:
sudo ./add-client.sh #OR sudo ./add-client.sh $CLIENT_NAME # создаст клиента с заданным именем sudo ./reset.sh # удалит всех клиентов с сервера
После установки не забыть открыть порт:
sudo ufw allow 54321
Содержимое скриптов:
initial.sh:
#!/bin/bash echo "# Installing Wireguard" ./remove.sh ./install.sh ./add-client.sh echo "# Wireguard installed"
install.sh:
#!/bin/bash apt install software-properties-common -y add-apt-repository ppa:wireguard/wireguard -y apt update apt install wireguard-dkms wireguard-tools qrencode -y NET_FORWARD="net.ipv4.ip_forward=1" sysctl -w ${NET_FORWARD} sed -i "s:#${NET_FORWARD}:${NET_FORWARD}:" /etc/sysctl.conf cd /etc/wireguard umask 077 SERVER_PRIVKEY=$( wg genkey ) SERVER_PUBKEY=$( echo $SERVER_PRIVKEY | wg pubkey ) echo $SERVER_PUBKEY > ./server_public.key echo $SERVER_PRIVKEY > ./server_private.key read -p "Enter the endpoint (external ip and port) in format [ipv4:port] (e.g. 4.3.2.1:54321):" ENDPOINT if [ -z $ENDPOINT ] then echo "[#]Empty endpoint. Exit" exit 1; fi echo $ENDPOINT > ./endpoint.var if [ -z "$1" ] then read -p "Enter the server address in the VPN subnet (CIDR format), [ENTER] set to default: 10.50.0.1: " SERVER_IP if [ -z $SERVER_IP ] then SERVER_IP="10.50.0.1" fi else SERVER_IP=$1 fi echo $SERVER_IP | grep -o -E '([0-9]+\.){3}' > ./vpn_subnet.var read -p "Enter the ip address of the server DNS (CIDR format), [ENTER] set to default: 1.1.1.1): " DNS if [ -z $DNS ] then DNS="1.1.1.1" fi echo $DNS > ./dns.var echo 1 > ./last_used_ip.var read -p "Enter the name of the WAN network interface ([ENTER] set to default: eth0): " WAN_INTERFACE_NAME if [ -z $WAN_INTERFACE_NAME ] then WAN_INTERFACE_NAME="eth0" fi echo $WAN_INTERFACE_NAME > ./wan_interface_name.var cat ./endpoint.var | sed -e "s/:/ /" | while read SERVER_EXTERNAL_IP SERVER_EXTERNAL_PORT do cat > ./wg0.conf.def << EOF [Interface] Address = $SERVER_IP SaveConfig = false PrivateKey = $SERVER_PRIVKEY ListenPort = $SERVER_EXTERNAL_PORT PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o $WAN_INTERFACE_NAME -j MASQUERADE; PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o $WAN_INTERFACE_NAME -j MASQUERADE; EOF done cp -f ./wg0.conf.def ./wg0.conf systemctl enable wg-quick@wg0
add-client.sh:
#!/bin/bash # We read from the input parameter the name of the client if [ -z "$1" ] then read -p "Enter VPN user name: " USERNAME if [ -z $USERNAME ] then echo "[#]Empty VPN user name. Exit" exit 1; fi else USERNAME=$1 fi cd /etc/wireguard/ read DNS < ./dns.var read ENDPOINT < ./endpoint.var read VPN_SUBNET < ./vpn_subnet.var PRESHARED_KEY="_preshared.key" PRIV_KEY="_private.key" PUB_KEY="_public.key" ALLOWED_IP="0.0.0.0/0, ::/0" # Go to the wireguard directory and create a directory structure in which we will store client configuration files mkdir -p ./clients cd ./clients mkdir ./$USERNAME cd ./$USERNAME umask 077 CLIENT_PRESHARED_KEY=$( wg genpsk ) CLIENT_PRIVKEY=$( wg genkey ) CLIENT_PUBLIC_KEY=$( echo $CLIENT_PRIVKEY | wg pubkey ) #echo $CLIENT_PRESHARED_KEY > ./"$USERNAME$PRESHARED_KEY" #echo $CLIENT_PRIVKEY > ./"$USERNAME$PRIV_KEY" #echo $CLIENT_PUBLIC_KEY > ./"$USERNAME$PUB_KEY" read SERVER_PUBLIC_KEY < /etc/wireguard/server_public.key # We get the following client IP address read OCTET_IP < /etc/wireguard/last_used_ip.var OCTET_IP=$(($OCTET_IP+1)) echo $OCTET_IP > /etc/wireguard/last_used_ip.var CLIENT_IP="$VPN_SUBNET$OCTET_IP/32" # Create a blank configuration file client cat > /etc/wireguard/clients/$USERNAME/$USERNAME.conf << EOF [Interface] PrivateKey = $CLIENT_PRIVKEY Address = $CLIENT_IP DNS = $DNS [Peer] PublicKey = $SERVER_PUBLIC_KEY PresharedKey = $CLIENT_PRESHARED_KEY AllowedIPs = $ALLOWED_IP Endpoint = $ENDPOINT PersistentKeepalive=25 EOF # Add new client data to the Wireguard configuration file cat >> /etc/wireguard/wg0.conf << EOF [Peer] PublicKey = $CLIENT_PUBLIC_KEY PresharedKey = $CLIENT_PRESHARED_KEY AllowedIPs = $CLIENT_IP EOF # Restart Wireguard systemctl stop wg-quick@wg0 systemctl start wg-quick@wg0 # Show QR config to display qrencode -t ansiutf8 < ./$USERNAME.conf # Show config file echo "# Display $USERNAME.conf" cat ./$USERNAME.conf # Save QR config to png file #qrencode -t png -o ./$USERNAME.png < ./$USERNAME.conf
remove.sh:
echo "# Removing" wg-quick down wg0 systemctl stop wg-quick@wg0 systemctl disable wg-quick@wg0 yes | apt autoremove wireguard wireguard-dkms wireguard-tools #yes | apt autoremove software-properties-common yes | apt update rm -rf /etc/wireguard echo "# Removed"
reset.sh:
echo "# Reseting..." cd /etc/wireguard # Delete the folder with customer data rm -rf ./clients # Zero IP counter echo "1" > last_used_ip.var # Resetting the server configuration template to default settings cp -f wg0.conf.def wg0.conf systemctl stop wg-quick@wg0 wg-quick down wg0 echo "# Reseted"