Home Made Proxy


JSP upload with progress bar.

Swing slow paint problem.

Applets and proxy detection.

If you have read the first part of this article you would have seen how the sandbox prevents applets from accessing remote websites. Though signing your code would allow your applets to connect to other websites, there will be situations where the user does not grant the required permissions hence the need for an alternative.

By default an applet can connect only to the same server on which it was hosted on. But that server can host a simple script which can retrieve the remote url and pass the contents back to the browser. This is very similar to the service performed by a proxy server such as squid. Our script is just more specialized, it's not suitable for use as a general purpose proxy since it cannot offer the same levels of scalability as squid of apache.

When connecting to a remote page, our applet would use a url similar to the following:


In this case example.com is the server from which the applet was delivered. And script.php is our proxy script. The remote web page that we are trying to retrieve is http://www.radinks.com/ but it appears as http%3A%2F%2Fwww.radinks.com%2F we have escaped all the special characters.

Java gurus will not disagree when I say the best server side programming language is PHP. It takes less than a minute to write the simple proxy script of the kind that we have been discussing.

	  $url = $_REQUEST['url'];
	  $fp = fopen($url,"rb");

Sponsered Content

Java online business card designer.

That's all there is to it! You still need to tighten up the code to ensure that it does not get abused. That needs to be done regardless of the language of choice. You should at least try to protect the script from being used as a open proxy. There are many ways of doing so, including checking the HTTP referer or exchanging keys between the applet and the script. That discussion would be a separate article on it's own right, so let's stop for now and look at how we should modify our applet from part I

 	 * in a production env, this variable could be loaded from a 
	 * property file.
	String proxyScript = "http://raditha/java/sandbox/test.php";	
	 * The actual URL would be what the user inputs.
	String target = URLEncoder.encode(

	URL u = new URL(proxyScript + "?url=" + target);

	img = getToolkit().createImage(u);

The above is an extract from the init method. The full code includes a try catch block as well. The two interesting points to note is that the applet is only connecting to it's originating server. Second the final destination url has to be escaped with the call to the static encode() method in the UrlEncoder class.

Part II of this article is short and sweet. Hope you found it usefull

Short cuts
  Part 1   Introduction, AccessController, keytool, jarsigner
  Part 2   The help of a proxy
Copyright © Raditha Dissanayake 2013