Skip to content

Instantly share code, notes, and snippets.

@muellermartin
Last active August 29, 2015 14:25
Show Gist options
  • Save muellermartin/dea5648fa9eaef04abf8 to your computer and use it in GitHub Desktop.
Save muellermartin/dea5648fa9eaef04abf8 to your computer and use it in GitHub Desktop.
Anonymously open a redirecting URL to mitigate tracking.
#!/bin/sh
# Anonymously open a redirecting URL to mitigate tracking.
# Uses http://labs.wis.nu/ua/ to retrieve a random user agent string.
# Requires a local tor SOCKS5 proxy and Python 3.
anonredirect()
{
origURL=$1
proxyPort=9050
randomUA=$(python3 -c "import urllib.request;import json;print(json.loads(urllib.request.urlopen('http://labs.wis.nu/ua/').read().decode('UTF-8'))['ua'])")
proxyUp=$(netstat -an | grep $proxyPort)
if [ "$proxyUp" = '' ]; then
echo "Error: Proxy is not running on port $proxyPort." >&2
exit 1
fi
url=$(curl --socks5-hostname 127.0.0.1:9050 -A "$randomUA" -s -I "$origURL" | sed -n -e 's/Location:[[:space:]]\(.*\)$/\1/p')
# Assume URL to start with http, so "open" passes it to a web browser
if [ ! "${url%${url#http}}" = 'http' ]; then
origURL=http://$url
fi
echo $url
}
openurl()
{
uname=$(uname)
if [ "$uname" = "Linux" ]; then
xdg-open "$1"
elif [ "$uname" = "Darwin" ]; then
open "$1"
fi
}
openurl $(anonredirect $1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment