openvpn.bash 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/bin/bash
  2. # BonvScripts
  3. # https://github.com/Bonveio/BonvScripts
  4. PUBLIC_INTERFACE="$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1)"
  5. PUBLIC_IP_ADDRESS="$(curl -4sL http://ipinfo.io/ip || wget -4qO- http://ipinfo.io/ip)"
  6. if [[ ! -e /etc/openvpn/server.conf ]]; then
  7. echo -e "Missing OpenVPN Server config, exiting..."
  8. exit 1
  9. fi
  10. PRIVATE_IP="$(cat /etc/openvpn/server.conf | grep -i server | sed -e '/cert.*/d' -e '/key.*/d' | awk '{printf $2}')"
  11. CIDR_CLASS=""
  12. if grep -qs 255.255.0.0 /etc/openvpn/server.conf; then
  13. CIDR_CLASS="16"
  14. elif grep -qs 255.255.255.0 /etc/openvpn/server.conf; then
  15. CIDR_CLASS="24"
  16. fi
  17. if [[ "$(cat /proc/sys/net/ipv4/ip_forward)" != "1" ]]; then
  18. echo 1 > /proc/sys/net/ipv4/ip_forward
  19. fi
  20. iptables -I FORWARD -s $PRIVATE_IP/$CIDR_CLASS -j ACCEPT
  21. iptables -t nat -A POSTROUTING -o $PUBLIC_INTERFACE -j MASQUERADE
  22. iptables -t nat -A POSTROUTING -s $PRIVATE_IP/$CIDR_CLASS -o $PUBLIC_INTERFACE -j MASQUERADE
  23. #iptables -t nat -A POSTROUTING -s $PRIVATE_IP/$CIDR_CLASS -o $PUBLIC_INTERFACE -j SNAT --to-source $PUBLIC_IP_ADDRESS
  24. # After you run this script, Connect through your client config and test if its now working
  25. #