Block all Windows network connections using Java - java

I'd like to program a little application in Java which would block all network connections (for whole os, not just VM) on demand. The application is for the Windows platform only and I would like to use Windows specific APIs.
I looked at the Windows Firewall API, but didn't find anything useful. Also, I don't know how to call these APIs using JNA.
I'm asking you for a few pointers or examples for how I could accomplish these tasks:
Using Windows Firewall or other APIs in Java using JNA or some other library (JNI is not preferred)
Block/Unblock all connections

You can use the "netsh" command line, see http://support.microsoft.com/kb/875357
Invoke netsh command using the followinf syntax:
Runtime.getRuntime().exec(command)
But, I think, firewall is not what you need in this case. Here is the command line utility to enable/disable network interfaces for Windows: http://www.novell.com/communities/node/2338/network-configuration-command-line-control

There is no API support for this in Java. You cannot do it.

Related

Applet - Serialport communication

I'm trying to create a simple Java applet that reads data coming in through a serial port.
Is it possible?
By default, Java has no support for communicating with serial ports. There are libraries like RXTX that allow you to do that, but unfortunately RXTX requires a platform-specific native library in order to use the serial ports.
Further complication is that Java applets run in a very restricted sandbox by default, which means you need to a) sign the applet and b) manually install the necessary RXTX libraries on the host computer, which is not that user-friendly.
An alternative solution is discussed in this SO question (in short: use Java Web Start, not an applet, and everything gets a lot easier).
Yes, it is possible. Having said that, I think #andri pointed out the best path to a solution.
For that you can use jSSC lib (Java Simple Serial Connector). Page on google code: http://code.google.com/p/java-simple-serial-connector/
Also see this page: http://code.google.com/p/java-simple-serial-connector/wiki/jSSC_Terminal
This is jSSC based serial port terminal applet. Source code of jSSC-Terminal you can download on "Downloads" page.
Best regards, Sokolov Alexey.

Automated distribution for Java project

I have to make a tool for automated distribution of the Java code. Basically, I have a repository with compiled files, and about 50 locations to distribute the same code.
Does anyone know some opensource tool which can help me in this process?
If you are speaking about easy deployment of java applications, use JNLP. The only thing user has to do in this case is to surf to URL.
If you wish to do it without any user participation I believe the solution depends on target platform:
Use SSH for Unix platforms
WNI or telnet for windows platforms.
To make the solution more portable you can run
wget THE-JNLP-URL
on target machine using SSH for unix like platforms.
I do not know built-in command like wget for windows. But you can implement this in VBS or JS and then invoke the script using cscript over WMI or telnet.
Good luck.
Either you can distribute it out with rsync, or you can use Java WebStart to let the user JVM download and invoke the software as needed. For Windows based clients this is usually the easiest, especially when you want people to update to a newer version.

Best way to deal with COM ports for a cross-platform program?

I am writing an application which need to send data to a serial device attached to a COM port.
I am confused as to what is the best way to deal with such a device?
Shall I write the communication module in C++ or shall I write this in Java?
I want to run the application on Windows as well as Linux.
On Linux I would recommend libserial. You could also consider using Python. There is a multi platform pySerial module. If you decide for Java, rxtx is a multi platform library. On Windows you may use the Windows API for serial communications and combine it with libserial using #ifndef as #Dharma suggested.
I recommend you:
Boost ASIO
QextSerialPort (based on Qt)
You can write the module in c++ by specyfying the #ifndef "_WINDOWS_CODE" for windows code and else part for the linux code.
while compiling in Vc++ put the (_WINDOWS_CODE)macros in the projecty settings
and in gc++ remove the macro from project file
COM port is just a plain bit pipe. The APIs are expected to be fairly simple. In C++, the world has much more experience with writing to ports.
Other question would be - do you know what port number it is? There're several ways to find out which one is yours, like setup API on windows.
My suggestion, if you want to run you application in both linux and Windows, go for JAVA!. you can run your program without any need of compilation. otherwise you have to distribute seperate binary for both Windows and Linux.
Also programming model in both windows and linux for accessing SerialPort in C++, As i am preliminary a Windows C++ developer, you can use CreateFile WIn32 Api to access serial port.
shall i write the communication module
in C++ or shall i write this in JAVA?
Which are you more familiar with? Are you limited to these two languages? You could also use python with pyserial.
Are you doing this as a personal project? Is it for work? Is someone else working on this with you? What are they more familiar with?
For C++ you could use Boost.Asio.
For Java ... I don't know.

How do I log every time I start an application in Windows?

I'd like to log or record every time I start an application to gain insight into which applications I use most on my Windows system. I was thinking I could create an event in the event log and listen for it in a .Net program.
Questions:
Is this the best way to solve this problem?
If so, which .Net library should I use?
I am also open to using Java to solve this problem. Thanks!
In .NET, you could probably create a shell extension which you would register for EXE programs which would really just be a filter for the EXE extension. When your shell extension is called, you would execute the program (by invoking the old functionality) after you logged your information.
Note, however, that you can ONLY do this with .NET 4.0 or above, which is currently in beta. Because of the way that previous versions of the CLR worked, only one version was allowed to run in a process at a time (including explorer, the OS process).
.NET 4.0 introduces Side-by-Side (SxS) CLR instances within the same process, so it is safe to use it from .NET 4.0 on as a mechanism for shell extensions.
It will also require a good deal of COM interop, but it can be done.
In regards to LWoodyiii's comment asking if this can be done in older versions of .NET: Could it? Yes, it can be done, but the official decree from MS is that you shouldn't. The reason for this is because if someone else decides to run a shell extension, or interface with the OS in some way using .NET, and the version is different from the one that you are using, you run the risk of hosing the OS process.
maybe you can try to hook the CreateProcess API in system wide using unmanaged c++.
and in C# use .NET interop to handle events/notifies from you unmanaged hook module.
related links:
http://www.codeproject.com/KB/system/hooksys.aspx?msg=1322916
http://www.madshi.net/madCodeHookDescription.htm

Best way to run remote commands on a Windows server from Java?

Are there any existing solutions for remote execution of commands on a windows server from Java natively? psexec.exe is not an option since the java application has to be cross platform.
Even a preexisting solution using Java RM would be sufficient.
Currently, I'm using an SSH client library to ssh through java into a Windows server that's running the cygwin SSH daemon. Sadly the SSH daemon has some issues when it comes to quoting commands that go into a CMD (as opposed to bash) shell.
It'll require some work, but the remoting library in Hudson has very good support for running commands and doing file operations over the network on remote computers.
see https://jenkins.io/projects/remoting/ (you'll have to dive into the code)
Check out the Java RDP Client. Not really out of the box, but with little digging you should be able to trim it down to what you need.
Since it uses getopt, I would assume it's GPL'd.

Categories