configure.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env bash
  2. set -ex
  3. EASY_RSA_LOC="/etc/openvpn/easyrsa"
  4. SERVER_CERT="${EASY_RSA_LOC}/pki/issued/server.crt"
  5. cd $EASY_RSA_LOC
  6. if [ -e "$SERVER_CERT" ]; then
  7. echo "Found existing certs - reusing"
  8. else
  9. if [ ${OVPN_ROLE:-"master"} = "slave" ]; then
  10. echo "Waiting for initial sync data from master"
  11. while [ $(wget -q localhost/api/sync/last/try -O - | wc -m) -lt 1 ]
  12. do
  13. sleep 5
  14. done
  15. else
  16. echo "Generating new certs"
  17. easyrsa init-pki
  18. cp -R /usr/share/easy-rsa/* $EASY_RSA_LOC/pki
  19. echo "ca" | easyrsa build-ca nopass
  20. easyrsa build-server-full server nopass
  21. easyrsa gen-dh
  22. openvpn --genkey --secret ./pki/ta.key
  23. fi
  24. fi
  25. easyrsa gen-crl
  26. iptables -t nat -A POSTROUTING -s 172.16.100.0/255.255.255.0 ! -d 172.16.100.0/255.255.255.0 -j MASQUERADE
  27. mkdir -p /dev/net
  28. if [ ! -c /dev/net/tun ]; then
  29. mknod /dev/net/tun c 10 200
  30. fi
  31. cp -f /etc/openvpn/setup/openvpn.conf /etc/openvpn/openvpn.conf
  32. if [ ${OVPN_PASSWD_AUTH} = "true" ]; then
  33. mkdir -p /etc/openvpn/scripts/
  34. cp -f /etc/openvpn/setup/auth.sh /etc/openvpn/scripts/auth.sh
  35. chmod +x /etc/openvpn/scripts/auth.sh
  36. echo "auth-user-pass-verify /etc/openvpn/scripts/auth.sh via-file" | tee -a /etc/openvpn/openvpn.conf
  37. echo "script-security 2" | tee -a /etc/openvpn/openvpn.conf
  38. echo "verify-client-cert require" | tee -a /etc/openvpn/openvpn.conf
  39. openvpn-user db-init --db.path=$EASY_RSA_LOC/pki/users.db
  40. fi
  41. [ -d $EASY_RSA_LOC/pki ] && chmod 755 $EASY_RSA_LOC/pki
  42. [ -f $EASY_RSA_LOC/pki/crl.pem ] && chmod 644 $EASY_RSA_LOC/pki/crl.pem
  43. mkdir -p /etc/openvpn/ccd
  44. openvpn --config /etc/openvpn/openvpn.conf --client-config-dir /etc/openvpn/ccd --port 1194 --proto tcp --management 127.0.0.1 8989 --dev tun0