diff --git a/BashScripts/install-openvpn-server.sh b/BashScripts/install-openvpn-server.sh index 0422c41..5c5a559 100644 --- a/BashScripts/install-openvpn-server.sh +++ b/BashScripts/install-openvpn-server.sh @@ -1,9 +1,29 @@ +#!/bin/bash # Install using: sudo su -c "bash <(wget -qO- /url/to/install-openvpn-server.sh)" # Make sure script is ran as root if [[ $EUID -ne 0 ]]; then exec sudo /bin/bash "$0" "$@" fi +# Helper functions +add_iptables_rule() { + local RULE="$1" + local TABLE="filter" # Default table is filter + if [[ "$RULE" =~ -t[[:space:]]+(nat|mangle|raw|filter) ]]; then + TABLE="${BASH_REMATCH[1]}" + RULE="${RULE/-t ${BASH_REMATCH[1]}/}" # Remove "-t " from RULE + fi + local RULE_ACTION=$(echo "$RULE" | awk '{print $1}') + local RULE_REST=$(echo "$RULE" | cut -d' ' -f2-) + if iptables-save -t "$TABLE" | grep -Fq -- "$RULE_REST"; then + echo "Rule already exists in table $TABLE, skipping: -t $TABLE $RULE" + else + echo "Adding iptables rule to table $TABLE: -t $TABLE $RULE" + iptables -t "$TABLE" $RULE + fi +} + +# Setup script read -e -p "Enter lan NIC: " -i $(ip route | grep default | sed -e 's/^.*dev.//' -e 's/.proto.*//') NIC_NAME read -e -p "Enter VPN subnet: " -i "172.19.100" VPN_SUBNET read -e -p "Enter LAN subnet: " -i "192.168.0" LAN_SUBNET @@ -44,6 +64,7 @@ fi if [ ! -f /etc/openvpn/myserver.conf ]; then tee /etc/openvpn/myserver.conf > /dev/null < /usr/local/bin/add-openvpn-client.sh +#!/bin/bash +# Make sure script is ran as root +if [[ $EUID -ne 0 ]]; then + exec sudo /bin/bash "$0" "$@" +fi + +DIR=$(pwd) +for i in {1..255}; do + CLIENT_NAME="client$i" + if [ ! -f "/etc/openvpn/easy-rsa/pki/issued/$CLIENT_NAME.crt" ]; then + break + fi +done +VPN_SUBNET=$(grep -E '^server ' "/etc/openvpn/myserver.conf" | awk '{print $2}') +VPN_PUBLIC_HOST=$(grep -E '^#public-host ' "/etc/openvpn/myserver.conf" | awk '{print $2}') +VPN_PUBLIC_PORT=$(grep -E '^port ' "/etc/openvpn/myserver.conf" | awk '{print $2}') +echo "Adding VPN client to $VPN_PUBLIC_HOST:$VPN_PUBLIC_PORT" +read -e -p "Enter client name: " -i "$CLIENT_NAME" CLIENT_NAME +if [ -f "/etc/openvpn/easy-rsa/pki/issued/$CLIENT_NAME.crt" ]; then + echo Client $CLIENT_NAME already exists... + exit 1 +fi +read -e -p "Use static IP for this client? VPN subnet is $VPN_SUBNET (Leave empty for dynamic): " -i "" CLIENT_IP +if [ ! -z "${CLIENT_IP}" ]; then + echo Setting IP... +cat > "/etc/openvpn/ccd/$CLIENT_NAME" < $CLIENT_NAME.conf < +$CA_CERT + + +$CLIENT_CERT + + +$CLIENT_KEY + +key-direction 1 + +$TA_KEY + +EOL +EOF +chmod +755 /usr/local/bin/add-openvpn-client.sh +cat << 'EOF' > /usr/local/bin/remove-iptable-dups.sh +#!/bin/bash +iptables-save | awk '!seen[$0]++ || /^(\*|COMMIT)/' | iptables-restore +EOF