Skip to content

Instantly share code, notes, and snippets.

@alfetopito
Last active August 2, 2020 23:30
Show Gist options
  • Save alfetopito/e48c5ea6ab82763512d368bcf787bc8a to your computer and use it in GitHub Desktop.
Save alfetopito/e48c5ea6ab82763512d368bcf787bc8a to your computer and use it in GitHub Desktop.
Shell script for setting up PIA openvpn profiles on OpenWrt
#!/bin/sh
OVPN_DIR="/etc/openvpn"
install() {
BASE_PATH=https://www.privateinternetaccess.com/openvpn/
FILE_NAME=openvpn
if [ "$1" ]; then
FILE_NAME=$FILE_NAME-"$1"
fi
cd $OVPN_DIR
wget "$BASE_PATH$FILE_NAME.zip"
if [ -e "$FILE_NAME.zip" ]; then
echo "Clearing existing files"
rm *.ovpn
rm *.auth
clear
opkg install unzip
unzip "$FILE_NAME.zip"
opkg remove unzip
else
echo "Download failed"
exit 1
fi
cd -
exit 0
}
setup() {
OVPN_DEV="tun0"
OVPN_USER=$1
OVPN_PASSWD=$2
if [ ! "$1" ]; then
echo "vpn user:"
read OVPN_USER
fi
if [ ! "$2" ]; then
echo "vpn password:"
read OVPN_PASSWD
fi
cd $OVPN_DIR
for fname in *.ovpn; do
echo "Configuring ${fname}"
newfname=$(echo $fname|sed -r 's|\s+||g')
authfname=$(echo $newfname| sed 's|ovpn|auth|')
sed -i -e "
/^dev .*/s/^dev .*$/dev ${OVPN_DEV}/
/^auth-user-pass/s/^auth-user-pass.*$/auth-user-pass $authfname/
/^script-security/d
\$a script-security 2
/^up/d
\$a up /etc/openvpn/updown.sh
/^down/d
\$a down /etc/openvpn/updown.sh
" "${fname}"
echo $OVPN_USER > "$authfname"
echo $OVPN_PASSWD >> "$authfname"
mv "$fname" "$newfname"
done
cd -
exit 0
}
enable() {
configpath=$OVPN_DIR/"$1.ovpn"
if [ -e $configpath ]; then
echo "Disabling existing VPNs"
uci show openvpn | grep enabled | awk -F= '{print "uci delete "$1}' | sh
echo "Enabling $1"
uci set openvpn."$1"=openvpn
uci set openvpn."$1".config="$configpath"
uci set openvpn."$1".enabled=1
echo "Saving changes:"
uci changes
echo ""
uci commit
/etc/init.d/openvpn restart
exit 0
else
echo "Config file for $1 not found"
echo ""
echo "Options are:"
echo $(ls $OVPN_DIR/*.ovpn| sed -r 's|.*vpn/(.*)\..*|\1|')
echo ""
echo "Currently active:"
echo $(uci show openvpn | grep enabled | awk -F. '/\w+/{print $2}')
exit 1
fi
}
clear() {
echo "Clearing all existing profiles"
echo ""
echo '' > /etc/config/openvpn
/etc/init.d/openvpn restart
}
case $1 in
install)
shift
install $@
;;
setup)
shift
setup $@
;;
enable)
shift
enable $@
;;
clear)
clear
;;
*)
echo "Options are:"
echo ""
echo " install: downloads new profiles from vpn provider and unpacks them"
echo " setup: configures files from default provided by vpn provider"
echo " enable: enables given file"
echo " clear: removes all VPN entries"
;;
esac
#!/bin/sh
dns() {
DNS1="$1"
DNS2="$2"
uci -q delete dhcp.@dnsmasq[0].server
uci add_list dhcp.@dnsmasq[0].server="${DNS1}"
uci add_list dhcp.@dnsmasq[0].server="${DNS2}"
uci commit dhcp
/etc/init.d/dnsmasq restart
}
case ${script_type} in
(up) dns "209.222.18.222" "209.222.18.218" ;;
(down) dns "8.8.8.8" "4.4.4.4" ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment