Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch options to force Java socket connections to localhost?

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.

like image 522
Whatcannon Avatar asked Jun 27 '26 01:06

Whatcannon


1 Answers

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.setProperty("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.setProperty("http.proxyPort", bundle.getString("proxy.port"));
    } catch( MissingResourceException missingResourceException ){}
    ....
}

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

More information about this functionality here

like image 163
OscarRyz Avatar answered Jun 29 '26 13:06

OscarRyz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!