Skip to content

Instantly share code, notes, and snippets.

@collinbarrett
Last active December 24, 2021 07:25
Show Gist options
  • Save collinbarrett/abeaf6edeb1cfb49d9beacd6d325d3c2 to your computer and use it in GitHub Desktop.
Save collinbarrett/abeaf6edeb1cfb49d9beacd6d325d3c2 to your computer and use it in GitHub Desktop.
DD-WRT script for selecting an optimal ProtonVPN server
#!/bin/sh
# https://collinmbarrett.com/protonvpn-dd-wrt-api-script/
# specify PATH to run from cron
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/jffs/sbin:/jffs/bin:/jffs/usr/sbin:/jffs/usr/bin:/mmc/sbin:/mmc/bin:/mmc/usr/sbin:/mmc/usr/bin:/opt/sbin:/opt/bin:/opt/usr/sbin:/opt/usr/bin
# kill openvpn
PID=$(pidof openvpn)
kill -s SIGTERM "$PID"
while $(kill -0 "$PID" 2>/dev/null); do
sleep 1
done
# fetch ProtonVPN server info
LOGICALS=$(curl -s -H "Cache-Control: no-cache" -H "Accept: application/json" https://api.protonmail.ch/vpn/logicals)
# query for optimal server
IPSTRING=$(echo "$LOGICALS" | jq '.LogicalServers | map(select(.Status == 1 and .Tier == 2 and (.Name | (startswith("US-TX#") or startswith("US-GA#")) and (endswith("TOR") | not)))) | [sort_by(.Score, .Load)[]][0] | .Servers[0].EntryIP')
# update openvpn config
if [ -n "$IPSTRING" ]
then
sed -i '/^remote/d' /tmp/openvpncl/openvpn.conf
IPSTRING="${IPSTRING%\"}"
IP="${IPSTRING#\"}"
REMOTE="remote ${IP} 443"
echo $REMOTE >> /tmp/openvpncl/openvpn.conf
fi
# restart openvpn
openvpn --config /tmp/openvpncl/openvpn.conf --route-up /tmp/openvpncl/route-up.sh --route-pre-down /tmp/openvpncl/route-down.sh --daemon
@collinbarrett
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment