Script: Your US proxy server in one minute using Amazon EC2

Contents

Many of the well known websites determine your location based on your IP address and restrict their content or functionalities based on the country you’re in. Some examples are Gmail (Germans get only @googlemail.com-addresses, legal reasons), YouTube (content is restricted by the GEMA), and Pandora (limited to US citizens) to name only a few. To circumvent these restrictions, being able to quickly get an IP address outside of your own country is most helpful.

To do exactly that I wrote a little script that will start your very own US proxy server in one minute using Amazon EC2. In combination with browser plug-ins such as FoxyProxy, the script enables you to route all your web traffic through a proxy on an Amazon-owned machine – with an IP address in the US, Ireland, Singapore, Tokyo or Sao Paulo (location of Amazon data centers).

Updates

June 2014: I realized the script wasn’t working anymore, so I updated the blog post and the script to work with the current EC2 API tools again. The old script is still available here.

1. Only once: Amazon AWS setup

A little one time effort is necessary. But it’ll take not more than 10 minutes.

  1. Set up an Amazon AWS account: Amazon EC2 is ridiculously cheap (≥ 2ct/hr), but it is not free. Go to the AWS website to sign up.
  2. Create a X.509 Certificate and a Key Pair: To access Amazon AWS, a certificate and a key pair is necessary. You can do that in the Security Credentials page and the AWS console (key pairs page). Save them somewhere, e.g. at ~/.amazon
  3. Install the Amazon API Tools: To start/stop virtual machines, we need the API tools. For Debian-based systems sudo apt-get install ec2-api-tools does the job.

You’re almost done. All you have to do now is make your X.509 certificate (cert-xxx.pem) and your private key (any name, suffix is .pem) known to the API tools. All you need to do is set the four environment variables below. I added this to my ~/.bashrc:

export AWS_ACCESS_KEY="AK..."
export AWS_SECRET_KEY="..."
export AWS_KEYPAIR_NAME="keypair1"
export AWS_KEYPAIR_FILE=~/.amazon/keypair1.pem

That’s it for the AWS setup. You can play around with it by trying the ec2-*-tools, e.g. ec2-describe-keypairs.

2. Only once: US proxy script installation

Now download and configure the proxy control script. If you haven’t already, install the PHP command line interpreter:

sudo apt-get install php5-cli
wget https://heckel.io/uploads/2012/06/ec2-proxy
chmod +x ec2-proxy
vi ec2-proxy

In the config-section, at least update the two variables $KEYPAIR and $PRIVATE_KEY. You can update the virtual machine image (AMI) as well (as long as it’s an Ubuntu), if you want.

Hopefully that should be it for the configuration.

3. Start and stop the US proxy

Once it’s set up, starting and stopping the proxy is really easy. It’s controlled like an init.d-script: ec2-proxy (start|stop|status).

Here’s an example with a ‘micro’ AMI image. With a ’tiny’ image, it’ll actually only take one minute:

ec2-proxy start
[01:12:58] Check for running instance of ami-06ad526f ... Not running.
[01:13:02] Launching instance of AMI ami-06ad526f ... Launched i-24006c5d
[01:13:07] Waiting until ready ..... Running at ec2-xxx.amazonaws.com
[01:13:38] Testing SSH ....... ok.
[01:13:56] Installing TinyProxy via SSH ... Now installed.
[01:14:10] Starting SSH Tunnel to Proxy at localhost:8888 ... started.
[01:14:10] Testing proxy localhost:8888 ...... works!

If you’ve set up your browser proxy to localhost at port 8888, you can now browse via the EC2 machine – with its IP address. To stop it again, do this:

ec2-proxy stop
[01:18:18] Check for running instance of ami-06ad526f ... i-24006c5d running at ec2-xxx.amazonaws.com
[01:18:22] Terminating instance i-24006c5d ... Shutting down.

4. Possible Issues

The script might not work for every machine: It is based on many command line tools and their output. Because the script parses the output, it might not work for other locales or tool versions. Please do report if you’re having trouble with the script.