views:

313

answers:

3

Hello, I'm trying to find a way to force any connection attempts a Jar makes to an external IP through my proxy server, which is running on localhost(Also a Java application). Once the proxy server receives the connection it will open a connection with the external IP and begin routing the IO to and from the client/server.

I've been Googling this for 2 days, and I haven't had any luck, I believe I'm using the wrong terms in my search attempts.

If you've got any ideas, please let me know, I'll try anything.

Thanks in advance. - Sean.

A: 

If it's HTTP traffic or FTP traffic, you could try the following system properties:

http.proxyHost (default: ) http.proxyPort (default: 80 if http.proxyHost specified) http.nonProxyHosts (default:

See this link for details:

http://java.sun.com/docs/books/tutorial/networking/urls/%5FsetProxy.html

khill
+1  A: 

If is that a "real" Proxy the you could specify the proxy to use using java system properties.

You have two alternatives:

  1. Specify the proxy in the command line
  2. Hardcode it into your app

Well you actually have three

  1. Specify a .properties file, and read from there, and set it as System property ( which is pretty much option 2 but more dynamic )

From command line you'll use:

 java -Dhttp.proxyHost=localhost -Dhttp.proxyPort=8080 -jar YourJar.jar

With that all the http connections you perform will go through localhost at port 8080

The second is add this at the main method of your program:

public static void main( String [] args ) { 
    System.setProperty("http.proxyHost", "localhost");
    System.setPropery("http.proxyPort", "8080");
    ....
}

Which does the same.

Finally loading from myapp.properties

public static void main( String [] args ) { 
    try { // there are cleaner ways of course 
        ResorceBundle bundle = ResourceBundle.getBundle("myapp");
        System.setProperty("http.proxyHost", bundle.getString("proxy.server"));
        System.setPropery("http.proxyPort", bundle.getString("proxy.portr"));
    } catch( MissingResourceException mre ){}
    ....
}

You just have to make sure myapp.properties is available from the classpath

More information about this functionality here

OscarRyz
+1  A: 

If you are asking about general (NOT HTTP / FTP specific!) proxying of Socket connections, then the simple answer is that it is not supported by Java.

When you configure a proxy for HTTP and FTP traffic, the proxying happens at the application protocol level. The Java-side proxy properties tell the URLConnection layer to connect to your designated proxy rather than the IP address from the URL your application is trying to connect to. The Java Socket level is unaware that this is happening. It just sees a requests to connect to the proxy.

This work because the HTTP and FTP protocols specifically support proxying. For instance, the first 'line' of an HTTP GET request message gives the full URL of the page that the client is requesting. If the GET request goes to a proxy, the proxy can figure out where is has to send it.

Looking at the problem of proxying at the Socket level, the first observation is that the standard Java class libraries don't support this. The second observation is that it is actually unimplementable ... unless you implement this as an alternative transport layer. The reason is that IP and TCP/IP simply do not support the notion of explicitly proxying or relaying messages / streams. And even if you did implement such a transport, it doesn't fit into the standard Socket model.

So, if you are really asking about proxying all of the network traffic for a Java application, this can only be implemented outside of the JVM; i.e. at the network transport level of the JVM's (physical or virtual) host operating system.

Stephen C
I do not completely agree with this. Since Java 1.5, there is build-in support for SOCKS proxies (see `java.net.Socket(Proxy proxy)` constructor). But, yes, for HTTP protocol one need some additional layer that will handle it. Apache `commons-httpclient` developers promise to add this support in 4.x version of the library (see http://wiki.apache.org/HttpComponents/FrequentlyAskedApplicationDesignQuestions#Proxy_Configuration).
dma_k
And... yes... there is full proxy support (at least in Java 6 and for HTTP and FTP). See `sun.net.www.protocol.ftp.FtpURLConnection#connect()` and `sun.net.www.protocol.http.HttpURLConnection#plainConnect()`. Plus support for authentication.
dma_k