2014-07-01 05:43:00 +00:00
|
|
|
#!/bin/bash
|
2014-06-04 23:49:13 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Generate OpenVPN configs
|
|
|
|
#
|
|
|
|
|
|
|
|
set -ex
|
|
|
|
|
2014-07-06 01:51:58 +00:00
|
|
|
OVPN_ENV=$OPENVPN/ovpn_env.sh
|
2014-06-04 23:49:13 +00:00
|
|
|
|
2014-07-06 01:51:58 +00:00
|
|
|
# Import defaults if present
|
|
|
|
[ -r "$OVPN_ENV" ] && source "$OVPN_ENV"
|
|
|
|
|
|
|
|
# Override config if set
|
|
|
|
[ -n "$1" ] && OVPN_SERVER_URL="$1"
|
|
|
|
|
|
|
|
# Server name is in the form "udp://vpn.example.com:1194"
|
|
|
|
if [[ "$OVPN_SERVER_URL" =~ ^((udp|tcp)://)?([0-9a-zA-Z\.]+)(:([0-9]+))?$ ]]; then
|
|
|
|
OVPN_PROTO=${BASH_REMATCH[2]};
|
|
|
|
OVPN_CN=${BASH_REMATCH[3]};
|
|
|
|
OVPN_PORT=${BASH_REMATCH[5]};
|
2014-07-01 05:43:00 +00:00
|
|
|
else
|
2014-06-04 23:49:13 +00:00
|
|
|
echo "Common name not specified"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2014-07-01 05:43:00 +00:00
|
|
|
# Apply defaults
|
2014-07-06 01:51:58 +00:00
|
|
|
[ -z "$OVPN_PROTO" ] && OVPN_PROTO=udp
|
|
|
|
[ -z "$OVPN_PORT" ] && OVPN_PORT=1194
|
|
|
|
|
|
|
|
|
|
|
|
# Preserve config
|
|
|
|
if [ -f "$OVPN_ENV" ]; then
|
|
|
|
bak_env=$OVPN_ENV.$(date +%s).bak
|
|
|
|
echo "Backing up $OVPN_ENV -> $bak_env"
|
|
|
|
mv "$OVPN_ENV" "$bak_env"
|
|
|
|
fi
|
|
|
|
export OVPN_SERVER_URL OVPN_ENV OVPN_PROTO OVPN_CN OVPN_PORT
|
|
|
|
env | grep ^OVPN_ > "$OVPN_ENV"
|
2014-07-01 05:43:00 +00:00
|
|
|
|
2014-07-01 05:56:26 +00:00
|
|
|
conf=$OPENVPN/openvpn.conf
|
2014-06-30 06:09:18 +00:00
|
|
|
if [ -f "$conf" ]; then
|
|
|
|
bak=$conf.$(date +%s).bak
|
|
|
|
echo "Backing up $conf -> $bak"
|
|
|
|
mv "$conf" "$bak"
|
|
|
|
fi
|
|
|
|
|
|
|
|
cat > "$conf" <<EOF
|
2014-06-30 05:45:30 +00:00
|
|
|
server 192.168.255.0 255.255.255.0
|
2014-06-04 23:49:13 +00:00
|
|
|
verb 3
|
|
|
|
#duplicate-cn
|
2014-07-06 01:51:58 +00:00
|
|
|
key $EASYRSA_PKI/private/$OVPN_CN.key
|
2014-06-04 23:49:13 +00:00
|
|
|
ca $EASYRSA_PKI/ca.crt
|
2014-07-06 01:51:58 +00:00
|
|
|
cert $EASYRSA_PKI/issued/$OVPN_CN.crt
|
2014-06-04 23:49:13 +00:00
|
|
|
dh $EASYRSA_PKI/dh.pem
|
|
|
|
tls-auth $EASYRSA_PKI/ta.key
|
|
|
|
key-direction 0
|
|
|
|
keepalive 10 60
|
|
|
|
persist-key
|
|
|
|
persist-tun
|
|
|
|
push "dhcp-option DNS 8.8.4.4"
|
|
|
|
push "dhcp-option DNS 8.8.8.8"
|
2014-06-30 07:10:52 +00:00
|
|
|
|
2014-06-30 05:44:05 +00:00
|
|
|
client-config-dir $OPENVPN/ccd
|
2014-06-30 07:10:52 +00:00
|
|
|
route 192.168.254.0 255.255.255.0
|
2014-06-04 23:49:13 +00:00
|
|
|
|
2014-07-06 01:51:58 +00:00
|
|
|
proto $OVPN_PROTO
|
|
|
|
port $OVPN_PORT
|
2014-07-01 05:56:26 +00:00
|
|
|
dev tun0
|
|
|
|
status /tmp/openvpn-status.log
|
2014-06-04 23:49:13 +00:00
|
|
|
EOF
|
2014-07-01 15:30:28 +00:00
|
|
|
|
2014-07-06 01:51:58 +00:00
|
|
|
# Clean-up duplicate configs (always return success)
|
|
|
|
diff -q "$bak_env" "$OVPN_ENV" 2> /dev/null && rm "$bak_env" || true
|
|
|
|
diff -q "$bak" "$conf" 2> /dev/null && rm "$bak" || true
|