/**
 * Copyright (c) 2011-2014, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */

package com.wechat.sdk.kit;

import javax.servlet.http.HttpServletRequest;

import com.wechat.common.utils.StringsUtils;

public class IpKit {
	
	public static String getRealIp(HttpServletRequest request) {
//		String ip = request.getHeader("x-forwarded-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();
//		}
//		return ip;
		if (request == null) {
            return null;
        }
        String ipString = request.getHeader("x-forwarded-for");
        if (StringsUtils.isEmpty(ipString) || "unknown".equalsIgnoreCase(ipString)) {
            ipString = request.getHeader("Proxy-Client-IP");
        }
        if (StringsUtils.isEmpty(ipString) || "unknown".equalsIgnoreCase(ipString)) {
            ipString = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringsUtils.isEmpty(ipString) || "unknown".equalsIgnoreCase(ipString)) {
            ipString = request.getRemoteAddr();
        }

        // 多个路由时，取第一个非unknown的ip  
        final String[] arr = ipString.split(",");
        for (final String str : arr) {
            if (!"unknown".equalsIgnoreCase(str)) {
                ipString = str;
                break;
            }
        }
        return ipString;

	}
	
	public static String getRealIpV2(HttpServletRequest request) {
		String accessIP = request.getHeader("x-forwarded-for");
        if (null == accessIP)
            return request.getRemoteAddr();
        return accessIP;
	}
	
	public static String getIpAddr(HttpServletRequest request) {
		   String ip = request.getHeader("x-forwarded-for"); 
		   if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {  
		       // 多次反向代理后会有多个ip值，第一个ip才是真实ip
		       if( ip.indexOf(",")!=-1 ){
		           ip = ip.split(",")[0];
		       }
		   }  
		   if (StringsUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {  
		       ip = request.getHeader("Proxy-Client-IP");  
		   }  
		   if (StringsUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {  
		       ip = request.getHeader("WL-Proxy-Client-IP");  
		   }  
		   if (StringsUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {  
		       ip = request.getHeader("HTTP_CLIENT_IP");  
		   }  
		   if (StringsUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {  
		       ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
		   }  
		   if (StringsUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {  
		       ip = request.getHeader("X-Real-IP");  
		   }  
		   if (StringsUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {  
		       ip = request.getRemoteAddr();  
		   } 
		   return ip;  
		}
}
