Add support command line parameters

This commit is contained in:
Denis 2019-03-23 22:07:42 +05:00
parent 0ce2775a3a
commit 4ab14b48b1

View File

@ -4,6 +4,62 @@
#
# Copyright (c) 2013 Nyr. Released under the MIT License.
usage()
{
cat << EOF
usage: $0 options
This script will setup your own VPN server in no more than a minute.
OPTIONS:
-i VALUE IP address
-p VALUE Protocol
-P VALUE PORT
-d VALUE Type of DNS
-c VALUE Client name
-y Not asking press any key
-h Display this help
EOF
}
IP_ADDR=
PROTOCOL=
PORT=
DNS=
CLIENT=
NOT_ASK=
while getopts ":i:p:P:d:c:yh" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
i)
IP_ADDR=$OPTARG
;;
p)
PROTOCOL=$OPTARG
;;
P)
PORT=$OPTARG
;;
d)
DNS=$OPTARG
;;
c)
CLIENT=$OPTARG
;;
y)
NOT_ASK=1
;;
?)
usage
exit
;;
esac
done
# Detect Debian users running the script with "sh" instead of bash
if readlink /proc/$$/exe | grep -q "dash"; then
@ -171,11 +227,18 @@ else
echo "I need to ask you a few questions before starting the setup."
echo "You can leave the default options and just press enter if you are ok with them."
echo
if [ -z "$IP_ADDR" ]; then
echo "First, provide the IPv4 address of the network interface you want OpenVPN"
echo "listening to."
# Autodetect IP address and pre-fill for the user
IP=$(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 -p "IP address: " -e -i $IP IP
else
IP=$IP_ADDR
echo "IP address: $IP_ADDR"
fi
# If $IP is a private IP address, the server must be behind NAT
if echo "$IP" | grep -qE '^(10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.|192\.168)'; then
echo
@ -183,10 +246,15 @@ else
read -p "Public IP address / hostname: " -e PUBLICIP
fi
echo
if [ -z "$PROTOCOL" ]; then
echo "Which protocol do you want for OpenVPN connections?"
echo " 1) UDP (recommended)"
echo " 2) TCP"
read -p "Protocol [1-2]: " -e -i 1 PROTOCOL
else
echo "Connections protocol: $PROTOCOL"
fi
case $PROTOCOL in
1)
PROTOCOL=udp
@ -196,9 +264,15 @@ else
;;
esac
echo
if [ -z "$PORT" ]; then
echo "What port do you want OpenVPN listening to?"
read -p "Port: " -e -i 1194 PORT
else
echo "Listening port: $PORT"
fi
echo
if [ -z "$DNS" ]; then
echo "Which DNS do you want to use with the VPN?"
echo " 1) Current system resolvers"
echo " 2) 1.1.1.1"
@ -206,13 +280,26 @@ else
echo " 4) OpenDNS"
echo " 5) Verisign"
read -p "DNS [1-5]: " -e -i 1 DNS
else
echo "DNS type: $DNS"
fi
echo
if [ -z "$CLIENT" ]; then
echo "Finally, tell me your name for the client certificate."
echo "Please, use one word only, no special characters."
read -p "Client name: " -e -i client CLIENT
else
echo "Client name: $CLIENT"
fi
echo
echo "Okay, that was all I needed. We are ready to set up your OpenVPN server now."
if [ -z "$NOT_ASK" ]; then
read -n1 -r -p "Press any key to continue..."
fi
if [[ "$OS" = 'debian' ]]; then
apt-get update
apt-get install openvpn iptables openssl ca-certificates -y