Main

Use any free PHP hosting as an ultra-fast HTTP proxy

20 July 2008


If you're not a US resident, you know how annoying the Internet can be sometimes. All those "US only" sites really manage to get under my skin. Especially when it's the really good ones that block access, like Pandora or Hulu, for example.

The obvious answer to the problem is a proxy. Yet, free proxies tend to be really slow. They certainly do not fit a purpose such as streaming audio or video. There are paid solutions, but if paying for a proxy/tunnel/VPN is out of the question for you, today is your lucky day.

All you need is a free php hosting provider and 2 scripts. One script resides on your free hosting, and one on your local system. The only catch is that the host mustn't block functions like fsockopen(), otherwise proxying would be impossible. Luckily, from what I've seen, most hosts don't block it.

The script on the host is a regular php file. It forwards HTTP requests via a socket to the destination host, and outputs the response.

The client script is written in perl. It acts as an HTTP server on a local port of your machine (don't forget to firewall it). Then, it forwards the incoming requests from your browser to the php script, and does a similar thing for the response. The upstream is encapsulated in multipart/form-data POST requests, allowing binary data to be transfered without encoding it.

Please note, however, that this sort of usage will most likely violate your host's TOS. It's only a matter of time until they ban you. Then again, they are probably abused by worse things all the time.

Your bandwidth is obviously capped by what the free hosting package lets you have, but you could register many accounts with multiple hosts.

On the upside, the speed this proxy gives you is incredible. A quick speed test has shown me that I was downloading as fast as my connection would allow. Some downloads can actually be FASTER with the proxy, based on the geographical location of the host.

Ping times are a bit high though, in the 500ms range (the host is half-way around the world for me), but this is barely noticeable.

Most importantly, is that if your free host is based in the US, you'll have full access to "US only" content. I'm using Pandora with this, and it works great. Hulu doesn't work for some reason, but it's a temporary bug that I'm hoping to fix.

File uploads also don't seem to work, but I'll try to fix this as well.

Finally, here are the scripts:
proxy.php - This goes to the free hosting account
proxy.pl - This runs on your machine

Usage:
1) Change $authToken in both scripts to the same secret value. This will prevent others from using your proxy.

2) Alter the $proxyServ and $proxyPath values in proxy.pl so they link to the proxy.php file correctly. $localPort can be arbitrary and defaults to 13268 (make sure it's not taken, though).

3) Run the proxy.pl script in the background when you want to use your proxy.

4) Set up your browser's HTTP proxy to 127.0.0.1:13268. This is the default address the script will listen on.

Enjoy!

P.S.
Don't forget that the admins of the host you're proxying through can be bad people too :)

Posted by: kGen | In category: Free code | Comments (2)