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).
Contents
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.
- 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.
- 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
- 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:
1 2 3 4 |
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:
1 2 3 4 |
sudo apt-get install php5-cli wget http://blog.philippheckel.com/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:
1 2 3 4 5 6 7 8 |
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:
1 2 3 |
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.
Doesn’t work for me. Script returns
“Launching instance of AMI ami-ac9943c5 … Client.InvalidKeyPair.NotFound: The key pair ‘mileskey’ does not exist”
if i use the default AMI or
“[20:27:40] Launching instance of AMI ami-4038b470 … Client.InvalidAMIID.NotFound: The AMI ID ‘ami-4038b470’ does not exist”
If i use the AMI i created and which has mileskey as its public key
Have you tried running the ec2-run-instances command on the same way the script calls it? Like this: ec2-run-instances ami-ac9943c5 -k mileskey –instance-type t1.micro
Thanks for replying Philipp :)
That also threw mileskey problems (also –instance-type t1.micro was discarded). However i have managed to ssh into my instance by giving it an elastic ip: ssh -i mileskey.pem ubuntu@. I’ve installed tinyproxy on my amazon instance (I have the power of cloud computing!) but am unsure how to use it(I am too noobish to use my power). Is there a way to modify your script to use IPs rather then AMI numbers to login?
that should be ssh -i mileskey.pem ubuntu@myelasticIP
I am sure it can be modified; however, I have never used elastic IPs so I cannot help you with that… Please post it here if you find a solution.
Great explanation! Thanks! Since some weeks to now I was thinking something like that, using clouds services to make my own US proxy server instead of using United States proxy lists like that.
Hi Phillip, thanks for this! I have followed your instructions on my Ubuntu 10.04 machine, got it to work and to launch an instance. Amazon’s EC-management console page displays that the instance has been launched and is in healthy state. However, the script does not get beyond the “Testing SSH ….” I have already waited 15 minutes but nothing happened.
Do you have any idea what might be going on here ?
===========
[19:04:39] Launching instance of AMI ami-ac9943c5 … Launched instance i-c25822bc
[19:04:46] Waiting until ready ….. Running at ec2-50-16-75-149.compute-1.amazonaws.com
[19:05:08] Testing SSH …………………….
Additional informtion: I have by now tried to connect manually to the instance and I have received a time out.
Ok, got it. it is an issue with the ssh settings in Ubuntu, please feel free to remove my previous comments.