Skip to content

Instantly share code, notes, and snippets.

@johnramsden
Last active February 20, 2022 22:08
Show Gist options
  • Save johnramsden/349581cb60acf9a4c61e0d551c55ddbf to your computer and use it in GitHub Desktop.
Save johnramsden/349581cb60acf9a4c61e0d551c55ddbf to your computer and use it in GitHub Desktop.
Freebsd openvpn bash script. Used to automate setup of OpenVPN on Private Internet Access.
#!/usr/local/bin/bash
###############################
# Created by John Ramsden
# Github Ramsdenj
###############################
# Used script by jedediahfrey@github and post by Tango@FreeNAS forums as reference
# https://forums.freenas.org/index.php?threads/guide-setting-up-transmission-with-openvpn-and-pia.24566/
# Setup OpenVPN
# Requires curl to use
# Run this script as root or sudo
set -e # Exit if any command has a non-zero exit status.
set -o errexit # Exit if a command fails.
# set -o nounset # Exit when your script tries to use undeclared variables.
set -o pipefail # Prevents errors in a pipeline from being masked.
# ============================================= #
# Variables:
privateinternetaccess="https://www.privateinternetaccess.com"
# PIA paths:
controlpanel="pages/client-control-panel"
configfiles="openvpn/openvpn.zip"
openvpndir="/usr/ports/security/openvpn"
vpnconfig="/usr/local/etc/openvpn"
# Check initial IP
startip=$(curl http://wtfismyip.com/text)
location="US_West.ovpn"
clear
echo "####################################"
echo "######## Setting up OpenVPN ########"
echo "####################################"
echo
echo "Your starting ip is: ${startip}"
echo
echo "Set credentials generated in control panel at:"
echo "Control Panel: ${privateinternetaccess}/${controlpanel}"
echo
read -p "PIA username: " user
read -p "PIA password: " pass
echo
echo "Update & upgrade pkgs..."
pkg update -f && pkg upgrade -y
echo
echo "Updating ports tree..."
portsnap fetch extract
echo
clear
cd "${openvpndir}"
read -p "Setup compile options by hand? [y/n] " answer
echo
echo "Installing OpenVPN..."
if [ "${answer}" = 'y' ]; then
make config-recursive
else
mkdir -p /var/db/ports/security_openvpn/
echo "# This file is auto-generated by 'make config'." > /var/db/ports/security_openvpn/options
echo "# Options for openvpn-2.3.6_1" >> /var/db/ports/security_openvpn/options
echo "_OPTIONS_READ=openvpn-2.3.6_1" >> /var/db/ports/security_openvpn/options
echo "_FILE_COMPLETE_OPTIONS_LIST=DOCS EASYRSA EXAMPLES PKCS11 PW_SAVE OPENSSL POLARSSL" >> /var/db/ports/security_openvpn/options
echo "OPTIONS_FILE_UNSET+=DOCS" >> /var/db/ports/security_openvpn/options
echo "OPTIONS_FILE_SET+=EASYRSA" >> /var/db/ports/security_openvpn/options
echo "OPTIONS_FILE_UNSET+=EXAMPLES" >> /var/db/ports/security_openvpn/options
echo "OPTIONS_FILE_UNSET+=PKCS11" >> /var/db/ports/security_openvpn/options
echo "OPTIONS_FILE_SET+=PW_SAVE" >> /var/db/ports/security_openvpn/options
echo "OPTIONS_FILE_SET+=OPENSSL" >> /var/db/ports/security_openvpn/options
echo "OPTIONS_FILE_UNSET+=POLARSSL" >> /var/db/ports/security_openvpn/options
mkdir -p /var/db/ports/archivers_lzo2
echo "# This file is auto-generated by 'make config'" > /var/db/ports/archivers_lzo2/options
echo "# Options for lzo2-2.08_1" >> /var/db/ports/archivers_lzo2/options
echo "_OPTIONS_READ=lzo2-2.08_1" >> /var/db/ports/archivers_lzo2/options
echo "_FILE_COMPLETE_OPTIONS_LIST=DOCS EXAMPLES" >> /var/db/ports/archivers_lzo2/options
echo "OPTIONS_FILE_UNSET+=DOCS" >> /var/db/ports/archivers_lzo2/options
echo "OPTIONS_FILE_UNSET+=EXAMPLES" >> /var/db/ports/archivers_lzo2/options
fi
make install clean
echo "Getting PrivateInternetAccess config..."
mkdir -p "${vpnconfig}" && cd "${vpnconfig}"
curl -OLk ${privateinternetaccess}/${configfiles}
unzip -q /usr/local/etc/${configfiles}
rm -f /usr/local/etc/${configfiles}
# For each of the ovpn settings.
for ovpn in *.ovpn; do
echo "auth-user-pass ${openvpndir}/pass.txt" >> "${ovpn}"
# Add the full paths to avoid ambiguity.
sed -i "" "s/crl\.pem/\/usr\/local\/etc\/openvpn\/crl.pem/g" "${ovpn}"
sed -i "" "s/ca\.crt/\/usr\/local\/etc\/openvpn\/ca.crt/g" "${ovpn}"
# Finally replace the spaces in the filename with underscores.
ovpn2="${ovpn// /_}"
mv "${ovpn}" "${ovpn2}"
done
echo "${user}" > "${openvpndir}/pass.txt"
echo -n "${pass}" >> "${openvpndir}/pass.txt"
echo "Would you like to change the location location from ${location}?"
echo "Your options are:"
ls ${vpnconfig}/*.ovpn
read -p "Change location: [Y/N]" newlocation
echo
while [[ ${newlocation} =~ ^[Yy]$ ]]; do
echo "Type the full name of the file you wish to use. "
read -p "e.g. 'US_West.ovpn': " location
echo "Checking for a match for ${location}"
if [[ $(ls ${vpnconfig}/*.ovpn) =~ ${location} ]]; then
echo "Changing location to: ${location}"
else
read -p "${location} not found, try a new location: [Y/N]" newlocation
fi
done
echo "Enabling openvpn in rc.conf..."
echo 'openvpn_enable="YES"' >> /etc/rc.conf
# Needs multiple quotes
echo 'openvpn_configfile=''"'${vpnconfig}/${location}'"' >> /etc/rc.conf
echo "Starting OpenVPN..."
service openvpn start
sleep 10
IP1=$(curl http://wtfismyip.com/text)
# Compare and Contrast.
echo "If these are different, OpenVPN is working"
echo "Old IP: ${IP0}"
echo "New IP: ${IP1}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment