I have embedded the applet on my login page, to fetch real IP (not VPN one) as well as MAC Addrees of Client (over vpn) whosoever accesses the login page.
Some forums on web, I came across claim Java applets can access the original client IP address even when using proxy or vpn using:
java.net.InetAddress.getLocalHost().getHostAddress();
I tried above, but it didn't work for me. It's giving the Vpn IP Address, not the actual Client one. Hence, I thought of fetching client ip by running ipconfig/all through the java applet.
Here the problem is:
It lists so many IPv4 Addresses (Ethernet Adapters so many LAN's, Network Adapter's) other than vpn ones. So It's not easy to decide which Client IP among all these has been pushing VPN Connection i.e. I mean Which Client Ip among all these has initiated the VPN Connection.
Thus, after a bit of playing with DOS Network Commands, I found one:
route print
With this, I am able to successfully dig out the real client IP Address which has initiated the VPN Connection on Client's machine.
If you run route print command after connecting to VPN, the right first row of IPv4 Route Table metrics, i.e the real IP Address value under column named as Interface which initiated VPN .
Here goes my Java Applet Code which fetches IP Address as well as MAC Address of Client user behind VPN, And Also sends over both Addresses through javascript to your JSP (Applet Embedded) Page.
import java.awt.Font;
import java.io.*;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.*;
import netscape.javascript.JSException;
import netscape.javascript.JSObject;
public class localfile extends JApplet
{
public String[] array=new String[5];
public String[] arrayips=new String[500];
private KeyStore keystore;
private String certiname="";
private String sAlias="";
JSObject window=null;
public String myipaddress;
public String mymacaddress;
public String[] ipaddresses =new String[200];
int i=0;
public String[] macaddresses =new String[200];
int j=0;
public localfile()
{
}
public void init()
{
try {
p1=Runtime.getRuntime().exec("route print");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(p1.getInputStream()!=null)
{
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(
new InputStreamReader(p1.getInputStream(), "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
}
catch (Exception e) {
// TODO: handle exception
}
int index=writer.toString().indexOf("Metric");
String finals=writer.toString().substring(index);
arrayips=finals.split("\\s+");
myipaddress=arrayips[4];
System.out.println(myipaddress);
try {
NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getByName(myipaddress));
byte[] mac = network.getHardwareAddress();
StringBuilder macaddress = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
macaddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
mymacaddress=macaddress.toString();
System.out.println(mymacaddress);
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
try {
p1.getInputStream().close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.toString());
e.printStackTrace();
}
try
{
//calling javascript function setipmac defined on jsp page
// setipmac is javascript function, you need to define on your jsp page to
// receive both Addresses from this Applet
getAppletContext().showDocument (new URL("javascript:setipmac('" + myipaddress +"','"+mymacaddress+"')"));
}
catch (JSException jse)
{
jse.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void start()
{
try
{
window = JSObject.getWindow(this);
}
catch (JSException jse)
{
jse.printStackTrace();
}
}
Process p1;
}
=>That's all !! ;)