How do I set a proxy after runtime - java

I am making a Minecraft mod, and I need to be able to make the JVM use a proxy while sending all the packets to the Minecraft servers and what-not. I basically need to be able to set the http.proxyHost and http.proxyPort arguments at any time that I need to.
I have not come here empty handed, and I have tried multiple things, one of these being just using System.setProperty like this:
System.setProperty("http.proxyHost", getProxyHost());
System.setProperty("http.proxyPort", getProxyPort());
But to no avail, my real IP was still being used to connect.
I also tried to set https proxy information just like the code shown above, but with https instead of http.
I know that the proxies work, and that this is possible, because I used this batch file to run Minecraft, and the proxies worked perfectly:
java -Dhttp.proxyHost=<host> -Dhttp.proxyPort=<port> -Dhttps.proxyHost=<host> -Dhttps.proxyPort=<port> -Xmx800m -jar Minecraft.exe
Whenever connecting to a server after running minecraft like shown above, the server console and just anything to do with my IP was now using the proxy, now before you go asking Well why don't you just use the batch file to run your Minecraft with a proxy?
Know that I want to be able to change this at any point, without having to turn off Minecraft, and then turn it back on, I want to be able to have a menu where I can just input the proxy host and proxy port and then be able to use that.
Thank you for any help that you may give me.

Related

Getting information from a cisco / juniper router with java

What method could I use to login to a Cisco or Juniper routers? I know I can use telnet to make a connection to the router itself but I am not aware of an API or anything that allows me to login non-interactively. So how could I do this?
Are there any libraries I can use to achieve this?
I have seen people use scripts that implement things like expect to know when to send the username and the password. But that is for shell scripting. How can I do this in java? I would prefer to keep strictly in java too.
The goal of this is allow a java program login into a router so I can query the router for interface states and execute commands on the router.
Any ideas?
You need to use some Java library for doing the same. http://www.pitman.co.za/projects/jssh/ is a good Java SSH client. http://commons.apache.org/proper/commons-net/ has a good telnet implementation
As far as extracting information, you should be able to do it by using SNMP to query the router. Pretty much everything is available over SNMP. Now, as far as command execution, there are SNMP set commands used to "write" information to a router, but you will need to look into that more carefully as I doubt the full command set can be replaced with SNMP set commands.
For Cisco, find out what the IOS image filename is for your device, then you can get the MIB file here: http://tools.cisco.com/Support/SNMP/do/MIBSupport.do?local=en&step=3. Review the MIB file and you will see what is available via SNMP for that device.
You should be able to do something similar for Juniper on their site.
Now, I don't know much about Java, but I do know that there are SNMP libraries available for it (like http://www.snmp4j.org/ ).
With that you will not need to worry about logging into the router, or about any interactive stuff.
Note that, in the case of Cisco, it's common to connect using a serial port on a PC directly into the service port on the router. This connection uses a programming protocol called IOS.
The easiest way to utilize this is with a programming language called expect. This name describes the language in that you expect to get a prompt from the port and then you respond. Sending that message (your response), you expect to get another response, to which you respond, ad completum - my term ;)
Note that this cable is proprietary to Cisco, I'm sure there are pin-outs available though.
Finally, it would be possible to emulate this program on java, via a serial port library.

Java 1.7.0u45 redirects connections to localhost

It seems that with the latest Java update, 1.7.0u45, my JNLP application that is supposed to connect to a server is instead forced to try and connect to localhost for some reason that I cannot quite seem to figure out.
I know that some other changes have been made, such as requiring the Permissions attribute in the manifest and some such things, which I'm currently trying to figure out a reasonable way to comply to, but I cannot find anything that ought to cause this behavior.
My code just does an ordinary Socket.connect call to a specific server, but what happens is that the connection is instead made to localhost. This only happens when the code is running in the JNLP client; not when I run it "manually" by starting the VM on it.
Does anyone know why this is?
At last, I managed to figure out what was happening. It was not, in fact, that connections as such were being redirected, but rather that the JNLP client has started blocking JNLP files that are not signed from setting system properties with certain names. Particularly, only properties whose names begin with jnlp are, it seems, guaranteed to be configurable by unsigned JNLP files.
The problem, therefore, was that I specified what server to connect to via a system property whose name did not begin with jnlp, and it then being blocked (since my JNLP file isn't signed) led to my program using its default server specification instead, being localhost.

Java/Jmeter connect timeout but not on browser (Not a JMeter issue)

In windows 2003 using JDK 1.7_21
So I setup Jemeter on my desktop to do an HTTP request to a specific URL and it works fine. But when I try from another machine I get a connect timeout.
The URL works fine in the I.E browser and I can also ping the domain from the command line. But for wahat ever reason the JVM doesn't want to connect.
In fact this is to test a sample HTTPClient I wrote. it's not the code because the code works off the desktop but on the server environment the JVM wont connect, proof JMeter wont either. but the browser and pings works.
I suspect java is getting it's network settings from somewhere different??? Proxy???
Well.. is IE/Windows using a proxy? You can check by going to to Internet Options > Connections > LAN Settings
If it is, you can set some JVM properties so that your Java application uses a proxy, there a couple ways of doing that, one way is through the command line when you execute it, you can use something similar to:
java -Dhttp.proxyHost=your.proxy -Dhttp.proxyPort=your.proxy.port -jar your.jar
or
java -Dhttp.proxyHost=your.proxy -Dhttp.proxyPort=your.proxy.port YourMainClass

JSCH Java applet

I have embedded a JSCH SSH Java applet in a web page and need to know if it's possible to run a script (of any language like PHP) to automate logging in and running commands. I have heard of expect4j and java robot but cannot see any way to implement it. Keep in mind, I'm not great with Java so I don't know everything about it. Any help is appreciated.
JSch is an SSH client library, and by itself only allows programmatically steered connections to another server. The user interaction has to be build around it by users of the library.
The JCTerm applet provided on the website also contains a terminal emulator in form of a Java GUI. If you only want to automatically execute some command (and maybe show its output in the web page), you could do everything on the server side, and don't need the applet with its terminal emulator. (You would need either some PHP-Java bridge on the server side or some Java-enabled webserver with a Servlet or similar, though.)
(If the web server would be the same machine as the server you'll run the command on you wouldn't even need the SSH connection, but could execute the stuff directly.)
If the server can't do anything (i.e. a "static server"), an applet is the way to go, yes. You can either modify JCTerm or create a new applet from scratch (using JCTerm's connection code as an example on how to connect to to the server).
If you don't have to fear any malicious users in your LAN (i.e. between web server and user, the SSH server doesn't matter), you can embedd the password (or preferably a private key for public-key authentication) into the applet's jar file, and pass it to the library for connection. (You should also include the server's public key for easier checking.)
Provide the command(s) to a ChannelExec (instead of a ChannelShell), this makes it easier to provide input (if necessary) and capture the output. Pipe the output in a text area, or simply use a green/red label saying if the command was successfully executed.
(I might have a look at this in the next days and try to do it. No promise, though.)

enable jmxremote on windows

I'm trying to enable remote monitoring by using jmxremote. When i set the options in a '.bat' file, things are ok (i can connect to the process using JConsole), but fail to do so when i try to do that when the process is ran as a service, although i use the exact properties that i use when the process runs as a '.bat' file (e.g. -Dcom.sun.management.jmxremote etc.) can you please let me know what am i missing?
(Please note, the behavior is with and withouth password/access files, with 'authenticate' and 'password' set to true and false).
Thanks
Guy
If you specify only com.sun.management.jmxremote then the connection will only work as long as the process to be monitored and the monitoring tool run under the same user.
Since services usually run under a system account that's not the case.
You will need to specify a port to listen on to enable connections from other users: use com.sun.management.jmxremote. port for this.

Categories

Resources