From fce88a38c50ef0a1d41a5e8945df03b5fb212613 Mon Sep 17 00:00:00 2001 From: randomshell Date: Sat, 22 Aug 2020 19:56:40 +0000 Subject: [PATCH] Add headless support --- wireguard-install.sh | 49 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/wireguard-install.sh b/wireguard-install.sh index d450d99..5ccf81b 100644 --- a/wireguard-install.sh +++ b/wireguard-install.sh @@ -72,11 +72,16 @@ function installQuestions() { # Detect public IPv6 address SERVER_PUB_IP=$(ip -6 addr | sed -ne 's|^.* inet6 \([^/]*\)/.* scope global.*$|\1|p' | head -1) fi - read -rp "IPv4 or IPv6 public address: " -e -i "${SERVER_PUB_IP}" SERVER_PUB_IP + + APPROVE_IP=${APPROVE_IP:-n} + if [[ ${APPROVE_IP} =~ n ]]; then + read -rp "IPv4 or IPv6 public address: " -e -i "${SERVER_PUB_IP}" SERVER_PUB_IP + fi # Detect public interface and pre-fill for the user SERVER_NIC="$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1)" - until [[ ${SERVER_PUB_NIC} =~ ^[a-zA-Z0-9_]+$ ]]; do + APPROVE_NIC=${APPROVE_NIC:-n} + until [[ ${SERVER_PUB_NIC} =~ ^[a-zA-Z0-9_]+$ || ${APPROVE_NIC} =~ n ]]; do read -rp "Public interface: " -e -i "${SERVER_NIC}" SERVER_PUB_NIC done @@ -112,12 +117,34 @@ function installQuestions() { echo "" echo "Okay, that was all I needed. We are ready to setup your WireGuard server now." echo "You will be able to generate a client at the end of the installation." - read -n1 -r -p "Press any key to continue..." + APPROVE_INSTALL=${APPROVE_INSTALL:-n} + if [[ $APPROVE_INSTALL =~ n ]]; then + read -n1 -r -p "Press any key to continue..." + fi } function installWireGuard() { - # Run setup questions first - installQuestions + if [[ ${AUTO_INSTALL} == "y" ]]; then + # Set default choices so that no questions will be asked. + APPROVE_INSTALL=${APPROVE_INSTALL:-y} + APPROVE_IP=${APPROVE_IP:-y} + APPROVE_NIC=${APPROVE_NIC:-y} + SERVER_WG_NIC=${SERVER_WG_NIC:-wg0} + SERVER_WG_IPV4=${SERVER_WG_IPV4:-10.66.66.1} + SERVER_WG_IPV6=${SERVER_WG_IPV6:-fd42:42:42::1} + SERVER_PORT=${SERVER_PORT:-$(shuf -i49152-65535 -n1)} + CLIENT_DNS_1=${CLIENT_DNS_1:-176.103.130.130} + CLIENT_DNS_2=${CLIENT_DNS_2:-176.103.130.131} + CLIENT_NAME=${CLIENT_NAME:-client} + CLIENT_DOT_IPV4=${CLIENT_DOT_IPV4:-2} + CLIENT_DOT_IPV6=${CLIENT_DOT_IPV6:-2} + + # Behind NAT, we'll default to the publicly reachable IPv4. + SERVER_PUB_IP=${SERVER_PUB_IP:-$(curl https://ifconfig.co)} + else + # Run setup questions first + installQuestions + fi # Install WireGuard tools and module if [[ ${OS} == 'ubuntu' ]]; then @@ -239,8 +266,10 @@ function newClient() { fi until [[ ${IPV4_EXISTS} == '0' ]]; do - read -rp "Client's WireGuard IPv4: ${SERVER_WG_IPV4::-1}" -e -i "${DOT_IP}" DOT_IP - CLIENT_WG_IPV4="${SERVER_WG_IPV4::-1}${DOT_IP}" + until [[ ${CLIENT_DOT_IPV4} =~ ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ ]]; do + read -rp "Client's WireGuard IPv4: ${SERVER_WG_IPV4::-1}" -e -i "${CLIENT_DOT_IPV4}" CLIENT_DOT_IPV4 + done + CLIENT_WG_IPV4="${SERVER_WG_IPV4::-1}${CLIENT_DOT_IPV4}" IPV4_EXISTS=$(grep -c "$CLIENT_WG_IPV4" "/etc/wireguard/${SERVER_WG_NIC}.conf") if [[ ${IPV4_EXISTS} == '1' ]]; then @@ -251,8 +280,10 @@ function newClient() { done until [[ ${IPV6_EXISTS} == '0' ]]; do - read -rp "Client's WireGuard IPv6: ${SERVER_WG_IPV6::-1}" -e -i "${DOT_IP}" DOT_IP - CLIENT_WG_IPV6="${SERVER_WG_IPV6::-1}${DOT_IP}" + until [[ ${CLIENT_DOT_IPV6} =~ ^[a-f0-9]{1,4}$ ]]; do + read -rp "Client's WireGuard IPv6: ${SERVER_WG_IPV6::-1}" -e -i "${CLIENT_DOT_IPV6}" CLIENT_DOT_IPV6 + done + CLIENT_WG_IPV6="${SERVER_WG_IPV6::-1}${CLIENT_DOT_IPV6}" IPV6_EXISTS=$(grep -c "${CLIENT_WG_IPV6}" "/etc/wireguard/${SERVER_WG_NIC}.conf") if [[ ${IPV6_EXISTS} == '1' ]]; then