Last active
December 24, 2021 07:25
-
-
Save collinbarrett/abeaf6edeb1cfb49d9beacd6d325d3c2 to your computer and use it in GitHub Desktop.
DD-WRT script for selecting an optimal ProtonVPN server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Blog post: https://collinmbarrett.com/protonvpn-dd-wrt-api-script/