Skip to content

Instantly share code, notes, and snippets.

@maxmarkus
Created January 29, 2018 15:21
Show Gist options
  • Save maxmarkus/95cf0d33ed14d6e96cc3ecc2a32f201b to your computer and use it in GitHub Desktop.
Save maxmarkus/95cf0d33ed14d6e96cc3ecc2a32f201b to your computer and use it in GitHub Desktop.
Tunnelblick route traffic based on urls and domains
#!/bin/sh
# Tunnelblick route traffic based on urls and domains
# Configuration at the end of this file:
# 1. Specify tunnel interface
# 2. Enter url/domain which is used to gather IP addresses
# Copy this file to (sudo):
# cd ~/Library/Application\ Support/Tunnelblick/Configurations/{NAME}/Contents/Resources
echo "network connected"
# Test an IP address for validity:
# Usage:
# valid_ip IP_ADDRESS
# if [[ $? -eq 0 ]]; then echo good; else echo bad; fi
# OR
# if valid_ip IP_ADDRESS; then echo good; else echo bad; fi
#
function valid_ip()
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
# VPN INTERFACE
VPN_INTERFACE=utun1
# TARGET URL
IPS=`dig +short somedomain.com`
for IP in $IPS
do
valid_ip $IP
if [[ $? -eq 0 ]]; then
echo "ADDING ROUTE: /sbin/route add -net $IP/32 -interface $VPN_INTERFACE"
/sbin/route add -net $IP/32 -interface $VPN_INTERFACE
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment