Skip to content

Instantly share code, notes, and snippets.

@MartinMReed
Last active October 26, 2019 18:24
Show Gist options
  • Save MartinMReed/1cb51177ed3da8a6f17c47781120a9cf to your computer and use it in GitHub Desktop.
Save MartinMReed/1cb51177ed3da8a6f17c47781120a9cf to your computer and use it in GitHub Desktop.
VPN Routing, DD-WRT Startup Script
#!/bin/sh
#
# https://ipinfo.io/ASXXXXX
# https://ipinfo.io/countries/us
# curl https://api.hackertarget.com/aslookup/?q=example
set -e
default_iface=$(route | grep default | sed -n "s/^default .* \(.*\)$/\1/p")
vpn_iface=ppp0
asn_add() {
iface=$1
asn=$2
addresses=$(wget -qO- http://ipinfo.io/AS${asn} 2>/dev/null | grep -E "a href.*${asn}\/" | grep -v ":" | sed "s/^.*<a href=\"\/AS${asn}\///; s/\" >//")
for address in $addresses; do
ip route flush $address
ip route add $address dev $iface
done
return 0
}
asn_lookup() {
iface=$1
name=$2
results=$(wget -qO- http://api.hackertarget.com/aslookup/?q=$name | grep -E ', US"$' | cut -d, -f1 | sed -e 's/^"//' -e 's/"$//')
for asn in $results; do
asn_add $iface $asn
done
return 0
}
flash_red() {
io=0
seconds=0
while true; do
if [ "$(date +%s)" -gt "$((seconds+1))" ]; then
if [ "$io" -eq "0" ]; then
io=1 && (gpio disable 15 && gpio enable 16)
else
io=0 && (gpio enable 15 && gpio enable 16)
fi
seconds=$(date +%s)
fi
done
return 0
}
flash_white() {
io=0
seconds=0
while true; do
if [ "$(date +%s)" -gt "$((seconds+1))" ]; then
if [ "$io" -eq "0" ]; then
io=1 && (gpio enable 15 && gpio enable 16)
else
io=0 && (gpio disable 15 && gpio disable 16)
fi
seconds=$(date +%s)
fi
done
return 0
}
flash_red &
flash_red_pid=$!
# wait for $vpn_iface to connect
while [ -z "$(ifconfig $vpn_iface | grep "inet addr" | sed -n "s/^.* inet addr:\([^ ]*\) .*$/\1/p")" ]; do
sleep 10
done
sleep 10
kill -9 $flash_red_pid
flash_white &
flash_white_pid=$!
# using default=vpn will cause the default gateway to be $vpn_iface,
# and other asn listed will instead use $default_iface
options="default=off google amazon incapsula akamai netflix"
#options=$(wget -qO- http://s3.amazonaws.com/path/to/file)
asn_iface=$vpn_iface
if [ "$(echo $options | tr ' ' '\n' | grep 'default=vpn')" == "default=vpn" ]; then
route del default $default_iface
route add default dev $vpn_iface
asn_iface=$default_iface
fi
for option in $options; do
[ "$option" == "default=vpn" ] && continue
asn_lookup $asn_iface $option
done
sleep 10
kill -9 $flash_white_pid
gpio enable 15 && gpio enable 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment