Super simple internet proxy script for Ubuntu users

Robert Taylor April 15, 2014

The following script can be handy when you want to securely proxy your web traffic through a VPS. There’s nothing complicated about it — just a simple chain of commands to create a SOCKS proxy through an SSH tunnel. As it is targeted specifically to my current operating system of choice, Ubuntu (14.04), it might not work on other Linux flavors.

Just create a script file called proxy.sh and copy the following code:

#!/usr/bin/env bash
#---------------------------------------------------------------#
# Create a SOCKS proxy on a remote server through an SSH tunnel
#---------------------------------------------------------------#
REMOTE='myusername@myremotehost'
HOST='localhost'
PORT=5222
set -e
function clear_proxy_settings()
{
echo 'Clearing system proxy settings'
gsettings set org.gnome.system.proxy mode 'none'
}
trap clear_proxy_settings EXIT # catch exit signals
echo 'Setting system proxy settings to '${HOST}':'${PORT}
gsettings set org.gnome.system.proxy.socks host $HOST
gsettings set org.gnome.system.proxy.socks port $PORT
gsettings set org.gnome.system.proxy mode 'manual'
echo 'Opening SSH tunnel'
ssh -N -D $PORT $REMOTE
trap - EXIT # reset the trap
clear_proxy_settings # call cleanup manually
exit 0

Make sure to edit the line REMOTE='myusername@myremotehost' to suit your specific case. And, of course, ensure that the script is executable: chmod +x proxy.sh. Then, to run:

./proxy.sh

In your browser, go to whatsmyip.org to check that your exposed IP is indeed that of your remote server. If all is working correctly, you should now be securely tunneling your traffic through your remote server!

The script will run indefinitely until you terminate it with CTRL-C.


GET IN TOUCH

Have a project in mind?

Reach out directly to hello@humaticlabs.com or use the contact form.

HUMATIC LABS LLC

All rights reserved