2020-11-17 12:48:26 -05:00
|
|
|
#!/usr/bin/env bash
|
2021-02-15 01:03:38 -05:00
|
|
|
set -ex
|
2020-11-17 12:48:26 -05:00
|
|
|
|
2020-10-15 12:12:31 -04:00
|
|
|
EASY_RSA_LOC="/etc/openvpn/easyrsa"
|
|
|
|
SERVER_CERT="${EASY_RSA_LOC}/pki/issued/server.crt"
|
2021-10-05 11:09:29 -04:00
|
|
|
|
|
|
|
OVPN_SRV_NET=${OVPN_SERVER_NET:-172.16.100.0}
|
|
|
|
OVPN_SRV_MASK=${OVPN_SERVER_MASK:-255.255.255.0}
|
|
|
|
|
|
|
|
|
2020-10-15 12:12:31 -04:00
|
|
|
cd $EASY_RSA_LOC
|
2021-10-05 11:09:29 -04:00
|
|
|
|
2020-10-15 12:12:31 -04:00
|
|
|
if [ -e "$SERVER_CERT" ]; then
|
2020-11-17 12:48:26 -05:00
|
|
|
echo "Found existing certs - reusing"
|
2020-10-15 12:12:31 -04:00
|
|
|
else
|
2021-03-17 06:44:12 -04:00
|
|
|
if [ ${OVPN_ROLE:-"master"} = "slave" ]; then
|
2020-11-19 12:08:55 -05:00
|
|
|
echo "Waiting for initial sync data from master"
|
|
|
|
while [ $(wget -q localhost/api/sync/last/try -O - | wc -m) -lt 1 ]
|
2020-11-17 12:48:26 -05:00
|
|
|
do
|
|
|
|
sleep 5
|
|
|
|
done
|
|
|
|
else
|
|
|
|
echo "Generating new certs"
|
|
|
|
easyrsa init-pki
|
|
|
|
cp -R /usr/share/easy-rsa/* $EASY_RSA_LOC/pki
|
|
|
|
echo "ca" | easyrsa build-ca nopass
|
|
|
|
easyrsa build-server-full server nopass
|
|
|
|
easyrsa gen-dh
|
|
|
|
openvpn --genkey --secret ./pki/ta.key
|
|
|
|
fi
|
2020-10-15 12:12:31 -04:00
|
|
|
fi
|
|
|
|
easyrsa gen-crl
|
|
|
|
|
2021-10-07 08:18:58 -04:00
|
|
|
iptables -t nat -D POSTROUTING -s ${OVPN_SRV_NET}/${OVPN_SRV_MASK} ! -d ${OVPN_SRV_NET}/${OVPN_SRV_MASK} -j MASQUERADE || true
|
2021-10-05 11:09:29 -04:00
|
|
|
iptables -t nat -A POSTROUTING -s ${OVPN_SRV_NET}/${OVPN_SRV_MASK} ! -d ${OVPN_SRV_NET}/${OVPN_SRV_MASK} -j MASQUERADE
|
2020-10-15 12:12:31 -04:00
|
|
|
|
|
|
|
mkdir -p /dev/net
|
|
|
|
if [ ! -c /dev/net/tun ]; then
|
|
|
|
mknod /dev/net/tun c 10 200
|
|
|
|
fi
|
|
|
|
|
|
|
|
cp -f /etc/openvpn/setup/openvpn.conf /etc/openvpn/openvpn.conf
|
|
|
|
|
2023-10-06 14:47:28 -04:00
|
|
|
if [ ${OVPN_AUTH} == "TOTP" ] || [ ${OVPN_AUTH} == "PASSWORD" ]; then
|
2021-02-15 01:03:38 -05:00
|
|
|
mkdir -p /etc/openvpn/scripts/
|
2023-10-10 11:20:40 -04:00
|
|
|
echo OVPN_LISTEN_BASE_URL=${OVPN_LISTEN_BASE_URL} > /etc/openvpn/scripts/.env
|
2021-02-15 01:03:38 -05:00
|
|
|
cp -f /etc/openvpn/setup/auth.sh /etc/openvpn/scripts/auth.sh
|
|
|
|
chmod +x /etc/openvpn/scripts/auth.sh
|
|
|
|
echo "auth-user-pass-verify /etc/openvpn/scripts/auth.sh via-file" | tee -a /etc/openvpn/openvpn.conf
|
|
|
|
echo "script-security 2" | tee -a /etc/openvpn/openvpn.conf
|
|
|
|
echo "verify-client-cert require" | tee -a /etc/openvpn/openvpn.conf
|
|
|
|
openvpn-user db-init --db.path=$EASY_RSA_LOC/pki/users.db
|
2022-12-30 09:25:21 -05:00
|
|
|
openvpn-user db-migrate --db.path=$EASY_RSA_LOC/pki/users.db
|
2021-02-15 01:03:38 -05:00
|
|
|
fi
|
|
|
|
|
2020-11-02 11:30:36 -05:00
|
|
|
[ -d $EASY_RSA_LOC/pki ] && chmod 755 $EASY_RSA_LOC/pki
|
|
|
|
[ -f $EASY_RSA_LOC/pki/crl.pem ] && chmod 644 $EASY_RSA_LOC/pki/crl.pem
|
2020-10-15 12:12:31 -04:00
|
|
|
|
2020-10-29 06:50:19 -04:00
|
|
|
mkdir -p /etc/openvpn/ccd
|
|
|
|
|
2021-10-05 11:09:29 -04:00
|
|
|
openvpn --config /etc/openvpn/openvpn.conf --client-config-dir /etc/openvpn/ccd --port 1194 --proto tcp --management 127.0.0.1 8989 --dev tun0 --server ${OVPN_SRV_NET} ${OVPN_SRV_MASK}
|