Obtain the true IP and MAC address of Windows version client when using reverse proxy such as Nginx

Source: Internet
Author: User
Tags nginx reverse proxy

Note: There are two limitations to the code:

1. The client must be a Windows operating system

2. The program code deployed to Linux when the desired effect, access to the IP is 127.0.0.1 so IP access to the MAC address is also inaccurate, to be improved ....

3. Please the enthusiastic friend message comments, thanks for pointing the maze!

Import java.io.IOException;
Import Java.io.InputStreamReader;

Import Java.io.LineNumberReader;

Import Javax.servlet.http.HttpServletRequest;
	public class Getipmacbywindows {//Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
	/** * Use Nginx reverse proxy, get client real IP address. * @return Client IP address */public static string GetIP (HttpServletRequest request) {String IP = request.getheader ("X_forwa
	    Rded_for "); if (IP = null | | ip.length () = = 0 | |
	    "Unknown". Equalsignorecase (IP) {ip = request.getheader ("Proxy-client-ip"); } if (IP = null | | ip.length () = = 0 | |
	    "Unknown". Equalsignorecase (IP) {ip = request.getheader ("Wl-proxy-client-ip"); } if (IP = null | | ip.length () = = 0 | |
	    "Unknown". Equalsignorecase (IP) {ip = request.getremoteaddr ();  
            } if (Ip.length () >) {string[] ips = Ip.split (","); for (int index = 0; index < ips.length; index++) {String strIp = (String) ips[indEX]; if (! ()  
                    Unknown ". Equalsignorecase (Strip)) {IP = strip;  
                Break
	}} return IP;
	/** * Gets the MAC address of the client.  * @return MAC address/public static string Getmacbywindows (HttpServletRequest request) {//Get the true IP String for this client realip
    	= GetIP (request);
		Obtain client MAC address String mac_s = getmacaddress (realip) via IP;
   return mac_s;   
        //IP Get client MAC address public static string getmacaddress (string IP) {string macaddr = "";   
            try {Process p = runtime.getruntime (). EXEC ("nbtstat-a" + IP);   
            InputStreamReader ir = new InputStreamReader (P.getinputstream (), "GBK");   
            LineNumberReader input = new LineNumberReader (IR); 
                for (int i = 1; i < i++) {String str = input.readline ();//System.out.println (str); Gets the client MAC address of line 14th if (i==14){macaddr = str.split ("=") [1].trim ();   
        A catch (IOException e) {e.printstacktrace (System.out);   
    return macaddr; }  
 
}

3.nginx Configuration


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.