From 1e9cfc0fbbf2c64d1c867c78c0e6d0758cdb47aa Mon Sep 17 00:00:00 2001 From: brnl <3243133+brnl@users.noreply.github.com> Date: Fri, 28 Feb 2020 00:21:13 +0100 Subject: [PATCH] Allow split-tunnel via custom CIDR blocks This change allows the tunnel to be split-tunnel, routing only the selected subnets through the tunnel. The notation is in CIDR-format. Example usage: `sudo PUSH_CIDR_BLOCKS=(192.168.1.0/24 172.16.0.0/16) ./openvpn-install.sh` --- openvpn-install.sh | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/openvpn-install.sh b/openvpn-install.sh index 90ea433..2d86ff0 100755 --- a/openvpn-install.sh +++ b/openvpn-install.sh @@ -787,7 +787,26 @@ ifconfig-pool-persist ipp.txt" >> /etc/openvpn/server.conf fi ;; esac - echo 'push "redirect-gateway def1 bypass-dhcp"' >> /etc/openvpn/server.conf + + # Allow split-tunnel via custom CIDR blocks (ie. 192.168.0.0/24) + if [ ${#PUSH_CIDR_BLOCKS[@]} -gt 0 ]; then + for cidr in ${PUSH_CIDR_BLOCKS[@]}; do + echo "Adding $cidr to routed subnets..."; + ROUTE_IP=$(echo $cidr | cut -d"/" -f1) + ROUTE_BITS=$(echo $cidr | cut -d"/" -f2) + + case $ROUTE_BITS in + 8) ROUTE_MASK="255.0.0.0" ;; + 16) ROUTE_MASK="255.255.0.0" ;; + 24) ROUTE_MASK="255.255.255.0" ;; + 32) ROUTE_MASK="255.255.255.255" ;; + esac + + echo "push \"route ${ROUTE_IP} ${ROUTE_MASK}\"" >> /etc/openvpn/server.conf + done + else + echo 'push "redirect-gateway def1 bypass-dhcp"' >> /etc/openvpn/server.conf + fi # IPv6 network settings if needed if [[ "$IPV6_SUPPORT" = 'y' ]]; then