2014-06-04 03:54:47 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# OpenVPN + Docker Wrapper Script
|
|
|
|
#
|
|
|
|
|
2014-06-04 04:55:15 +00:00
|
|
|
OPENVPN="/etc/openvpn"
|
|
|
|
|
|
|
|
# Needed by easyrsa itself
|
|
|
|
export EASYRSA="/usr/local/share/easy-rsa/easyrsa3"
|
|
|
|
export EASYRSA_PKI="$OPENVPN/pki"
|
|
|
|
export EASYRSA_VARS_FILE="$OPENVPN/vars"
|
|
|
|
|
2014-06-04 03:54:47 +00:00
|
|
|
set -ex
|
|
|
|
|
|
|
|
abort() {
|
|
|
|
echo "Error: $@"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ $# -lt 1 ]; then
|
|
|
|
abort "No command specified"
|
|
|
|
fi
|
|
|
|
|
2014-06-04 04:55:15 +00:00
|
|
|
do_openvpn() {
|
2014-06-04 08:39:38 +00:00
|
|
|
mkdir -p /dev/net
|
2014-06-04 04:55:15 +00:00
|
|
|
if [ ! -c /dev/net/tun ]; then
|
|
|
|
mknod /dev/net/tun c 10 200
|
|
|
|
fi
|
|
|
|
|
2014-06-04 08:39:38 +00:00
|
|
|
iptables -t nat -A POSTROUTING -s 192.168.255.0/24 -o eth0 -j MASQUERADE
|
|
|
|
|
|
|
|
openvpn --config "$OPENVPN/udp1194.conf"
|
|
|
|
}
|
|
|
|
|
|
|
|
do_init() {
|
|
|
|
cn=$1
|
|
|
|
|
|
|
|
# Provides a sufficient warning before erasing pre-existing files
|
|
|
|
easyrsa init-pki
|
|
|
|
|
|
|
|
# For a CA key with a password, manually init; this is autopilot
|
|
|
|
easyrsa build-ca nopass
|
|
|
|
|
|
|
|
easyrsa gen-dh
|
|
|
|
openvpn --genkey --secret $OPENVPN/pki/ta.key
|
|
|
|
|
|
|
|
if [ -z "$cn"]; then
|
|
|
|
#TODO: Handle IPv6 (when I get a VPS with IPv6)...
|
|
|
|
ip4=$(dig +short myip.opendns.com @resolver1.opendns.com)
|
|
|
|
ptr=$(dig +short -x $ip4 | sed -e 's:\.$::')
|
|
|
|
|
|
|
|
[ -n "$ptr" ] && cn=$ptr || cn=$ip4
|
|
|
|
fi
|
|
|
|
|
2014-06-04 16:08:09 +00:00
|
|
|
echo "$cn" > $OPENVPN/servername
|
|
|
|
|
2014-06-04 08:39:38 +00:00
|
|
|
easyrsa build-server-full $cn nopass
|
|
|
|
|
|
|
|
[ -f "$OPENVPN/udp1194.conf" ] || cat > "$OPENVPN/udp1194.conf" <<EOF
|
|
|
|
server 192.168.255.128 255.255.255.128
|
|
|
|
verb 3
|
|
|
|
#duplicate-cn
|
|
|
|
key $EASYRSA_PKI/private/$cn.key
|
|
|
|
ca $EASYRSA_PKI/ca.crt
|
|
|
|
cert $EASYRSA_PKI/issued/$cn.crt
|
|
|
|
dh $EASYRSA_PKI/dh.pem
|
2014-06-04 16:15:59 +00:00
|
|
|
#tls-auth $EASYRSA_PKI/ta.key
|
|
|
|
#key-direction 0
|
2014-06-04 08:39:38 +00:00
|
|
|
keepalive 10 60
|
|
|
|
persist-key
|
|
|
|
persist-tun
|
|
|
|
push "dhcp-option DNS 8.8.4.4"
|
|
|
|
push "dhcp-option DNS 8.8.8.8"
|
|
|
|
|
|
|
|
proto udp
|
|
|
|
port 1194
|
|
|
|
dev tun1194
|
|
|
|
status /tmp/openvpn-status-1194.log
|
|
|
|
EOF
|
2014-06-04 04:55:15 +00:00
|
|
|
}
|
|
|
|
|
2014-06-04 16:15:59 +00:00
|
|
|
do_getclientconfig() {
|
|
|
|
cn=$1
|
|
|
|
|
|
|
|
[ -z "$cn" ] && abort "Common name not specified"
|
|
|
|
|
|
|
|
if [ ! -f "$EASYRSA_PKI/private/$cn.key" ]; then
|
|
|
|
easyrsa build-server-full $cn nopass
|
|
|
|
fi
|
|
|
|
|
|
|
|
servername=$(cat $OPENVPN/servername)
|
|
|
|
|
|
|
|
cat <<EOF
|
|
|
|
client
|
|
|
|
nobind
|
|
|
|
dev tun
|
|
|
|
redirect-gateway def1
|
|
|
|
|
|
|
|
<key>
|
|
|
|
$(cat $EASYRSA_PKI/private/$cn.key)
|
|
|
|
</key>
|
|
|
|
<cert>
|
|
|
|
$(cat $EASYRSA_PKI/issued/$cn.crt)
|
|
|
|
</cert>
|
|
|
|
<ca>
|
|
|
|
$(cat $EASYRSA_PKI/ca.crt)
|
|
|
|
</ca>
|
|
|
|
<dh>
|
|
|
|
$(cat $EASYRSA_PKI/dh.pem)
|
|
|
|
</dh>
|
|
|
|
#<tls-auth>
|
|
|
|
#$(echo cat $EASYRSA_PKI/ta.key)
|
|
|
|
#</tls-auth>
|
|
|
|
#key-direction 1
|
|
|
|
|
|
|
|
<connection>
|
|
|
|
remote $servername 1194 udp
|
|
|
|
</connection>
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2014-06-04 03:54:47 +00:00
|
|
|
# Read arguments from command line
|
|
|
|
cmd=$1
|
|
|
|
shift
|
|
|
|
|
|
|
|
case "$cmd" in
|
2014-06-04 04:55:15 +00:00
|
|
|
# nop for volume creation
|
|
|
|
init)
|
2014-06-04 08:39:38 +00:00
|
|
|
do_init "$@"
|
2014-06-04 04:55:15 +00:00
|
|
|
;;
|
|
|
|
easyrsa)
|
|
|
|
easyrsa "$@"
|
|
|
|
;;
|
|
|
|
easyrsa-export-vars)
|
|
|
|
if [ -f "$EASYRSA_VARS_FILE" ]; then
|
|
|
|
cat "$EASYRSA_VARS_FILE"
|
|
|
|
else
|
|
|
|
cat "$EASYRSA/vars.example"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
easyrsa-import-vars)
|
|
|
|
cat > "$EASYRSA_VARS_FILE"
|
|
|
|
;;
|
|
|
|
bash)
|
|
|
|
$cmd "$@"
|
|
|
|
;;
|
2014-06-04 16:15:59 +00:00
|
|
|
getclientconfig)
|
|
|
|
do_getclientconfig "$@"
|
|
|
|
;;
|
2014-06-04 04:55:15 +00:00
|
|
|
openvpn)
|
|
|
|
do_openvpn "$@"
|
|
|
|
;;
|
2014-06-04 16:29:17 +00:00
|
|
|
log)
|
|
|
|
tail -F /tmp/openvpn-status-1194.log
|
|
|
|
;;
|
2014-06-04 03:54:47 +00:00
|
|
|
*)
|
|
|
|
abort "Unknown cmd \"$cmd\""
|
|
|
|
;;
|
|
|
|
esac
|