Invariant Properties

  • rss
  • Home

Getting through a firewall via proxy

Bear Giles | October 1, 2010

Corporate firewalls. sigh. But sometimes our applications have to get past a firewall. How do you do it?

The best solution is to let somebody else do it. If you’re using HttpComponents (HttpClient was renamed with version 4) it already has support for proxies in the HostConfiguration object. The library is designed to work it so you should just set up the configuration and be done with it.

Sometimes you can’t let somebody else do it. For instance you could be reading a naked stream. This isn’t HTTP at all but these services often sit on port 80 since that port is let through firewalls. A library like HttpComponents/HttpClient might look tempting but it’s not the answer.

So how do you get through the proxy firewall yourself?

The first step is to identify the type of proxy firewall you’re dealing with. All require that you connect to the proxy, most require you to also authenticate yourself.  But you might get lucky and only need to solve the first half of the problem.

Proxy connection

The easiest way to set up a proxy connection is to set two (or three) System properties:

  • http.proxyHost
  • http.proxyPort
  • http.nonProxyHosts (‘|’ separated list with ‘*’ wildcards)

You can set these properties programmatically or via the command line. You can only have one default proxy though – something that may not be acceptable to you.

  1. System.setProperty("http.proxyHost", proxyHost);
  2. System.setProperty("http.proxyPort", proxyPort);
  3. URL url = new URL(destination);
  4. URLConnection conn = url.openConnection();
  5. conn.connect();
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", proxyPort);
URL url = new URL(destination);
URLConnection conn = url.openConnection();
conn.connect();

A slightly more difficult approach is to create a Proxy object that is passed to the URL connection.

  1. Proxy proxy = new Proxy(Proxy.Type.HTTP,
  2.    new InetSocketAddress(proxyHost, proxyPort));
  3. URL url = new URL(destination);
  4. URLConnection conn = url.openConnection(proxy);
  5. conn.connect();
Proxy proxy = new Proxy(Proxy.Type.HTTP,
   new InetSocketAddress(proxyHost, proxyPort));
URL url = new URL(destination);
URLConnection conn = url.openConnection(proxy);
conn.connect();

This approach begs a question – where do you create the Proxy? There’s a solution to this since Java 5 – the ProxySelector class. It provides a list of Proxies for a specified URL. You need to create your own ProxySelector and register it as the default ProxyServer but that should be straightforward.

The application code must still know to retrieve a Proxy from the selector but there is no longer a need for that code to know the details of that Proxy.

  1. Proxy proxy = ProxySelector.getDefault().select(url.toURI());
  2. URLConnection conn = url.openConnection(proxy);
  3. conn.connect();
Proxy proxy = ProxySelector.getDefault().select(url.toURI());
URLConnection conn = url.openConnection(proxy);
conn.connect();

Authentication

The classic way to handle http proxy firewall authentication is to set a request header.

  1. Proxy proxy = ProxySelector.getDefault().select(url.toURI());
  2. HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
  3. conn.connect();
  4.  
  5. String encodedUserPwd = Base64.encodeBase64String("user:password".getBytes();
  6. conn.setRequestProxy("Proxy-Authorization", "Basic " +_ encodedUserPwd);
  7. InputStream is = conn.getInputStream();
Proxy proxy = ProxySelector.getDefault().select(url.toURI());
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
conn.connect();

String encodedUserPwd = Base64.encodeBase64String("user:password".getBytes();
conn.setRequestProxy("Proxy-Authorization", "Basic " +_ encodedUserPwd);
InputStream is = conn.getInputStream();

A second approach is to provide a default authenticator that will be used on all connections.

  1. Authenticator.setDefault(new Authenticator() {
  2.      protected PasswordAuthentication getPasswordAuthentication() {
  3.         return new PasswordAuthentication(username, password.toCharArray());
  4.     }
  5. });
Authenticator.setDefault(new Authenticator() {
     protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password.toCharArray());
    }
});

If we need to provide multiple authentications we must provide a local implementation of Authenticator that overrides the various static methods to

More info: http://www.rgagnon.com/javadetails/java-0085.html

Categories
java, security
Comments rss
Comments rss
Trackback
Trackback

« Using a SecurityManager: identifying requirements Solving eclipse's hang-on-startup problem »

Leave a Reply

Click here to cancel reply.

You must be logged in to post a comment.

Archives

  • May 2020 (1)
  • March 2019 (1)
  • August 2018 (1)
  • May 2018 (1)
  • February 2018 (1)
  • November 2017 (4)
  • January 2017 (3)
  • June 2016 (1)
  • May 2016 (1)
  • April 2016 (2)
  • March 2016 (1)
  • February 2016 (3)
  • January 2016 (6)
  • December 2015 (2)
  • November 2015 (3)
  • October 2015 (2)
  • August 2015 (4)
  • July 2015 (2)
  • June 2015 (2)
  • January 2015 (1)
  • December 2014 (6)
  • October 2014 (1)
  • September 2014 (2)
  • August 2014 (1)
  • July 2014 (1)
  • June 2014 (2)
  • May 2014 (2)
  • April 2014 (1)
  • March 2014 (1)
  • February 2014 (3)
  • January 2014 (6)
  • December 2013 (13)
  • November 2013 (6)
  • October 2013 (3)
  • September 2013 (2)
  • August 2013 (5)
  • June 2013 (1)
  • May 2013 (2)
  • March 2013 (1)
  • November 2012 (1)
  • October 2012 (3)
  • September 2012 (2)
  • May 2012 (6)
  • January 2012 (2)
  • December 2011 (12)
  • July 2011 (1)
  • June 2011 (2)
  • May 2011 (5)
  • April 2011 (6)
  • March 2011 (4)
  • February 2011 (3)
  • October 2010 (6)
  • September 2010 (8)

Recent Posts

  • 8-bit Breadboard Computer: Good Encapsulation!
  • Where are all the posts?
  • Better Ad Blocking Through Pi-Hole and Local Caching
  • The difference between APIs and SPIs
  • Hadoop: User Impersonation with Kerberos Authentication

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Pages

  • About Me
  • Notebook: Common XML Tasks
  • Notebook: Database/Webapp Security
  • Notebook: Development Tips

Syndication

Java Code Geeks

Know Your Rights

Support Bloggers' Rights
Demand Your dotRIGHTS

Security

  • Dark Reading
  • Krebs On Security Krebs On Security
  • Naked Security Naked Security
  • Schneier on Security Schneier on Security
  • TaoSecurity TaoSecurity

Politics

  • ACLU ACLU
  • EFF EFF

News

  • Ars technica Ars technica
  • Kevin Drum at Mother Jones Kevin Drum at Mother Jones
  • Raw Story Raw Story
  • Tech Dirt Tech Dirt
  • Vice Vice

Spam Blocked

53,793 spam blocked by Akismet
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox