package com.weiqi.mis;


import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.*;

import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.apache.commons.lang3.StringUtils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Pattern;

public class IPUtil {


    private final static String IP_SEPARATOR = ",";

    /**
     * return 机器名
     */
    public static String getHostName() {
        try {
            InetAddress ia = InetAddress.getByName("127.0.0.1");
            return ia.getHostName();
        } catch (UnknownHostException e) {
            return null;
        }
    }

    public static String[] getIps() {

        List<String> ips = new ArrayList<String>();
        try {
            Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
            for (NetworkInterface netint : Collections.list(nets)) {
                if (netint.getHardwareAddress() != null) {
                    List<InterfaceAddress> list = netint.getInterfaceAddresses();
                    for (InterfaceAddress interfaceAddress : list) {
                        String localIp = interfaceAddress.getAddress().getHostAddress();
                        if (StringUtils.isNoneBlank(localIp)) {
                            ips.add(localIp);
                        }
                    }
                }
            }
        } catch (SocketException e1) {
            return null;
        }
        return ips.toArray(new String[ips.size()]);
    }

//
//    public static String getIP(HttpServletRequest request) {
//
//        String ip = request.getHeader("X-Real-IP");
//
//        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("x-forwarded-for");
//        } else {
//            return ip;
//        }
//
//        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("Proxy-Client-IP");
//        } else {
//            //当有多级反向代理时，x-forwarded-for值为多个时取第一个ip地址
//            if (ip.indexOf(IP_SEPARATOR) != -1) {
//                ip = ip.substring(0, ip.indexOf(IP_SEPARATOR));
//            }
//            return ip;
//        }
//
//        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("WL-Proxy-Client-IP");
//        } else {
//            return ip;
//        }
//
//        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getRemoteAddr();
//        } else {
//            return ip;
//        }
//
//        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
//            ip = "";
//        }
//        return ip;
//    }

    /**
     * @Author : Jiaobailong
     * @Summary : getIpAddress
     * @Date : 2016/12/15 15:52
     */
    public static String getIpAddress(HttpServletRequest request) {

        String ip = null;

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CIP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");

            if (!Strings.isNullOrEmpty(ip)) {
                ip = Splitter.on(',').trimResults().omitEmptyStrings().splitToList(ip).get(0);
            }
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("REMOTE_ADDR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CDN_SRC_IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        if (ip != null && ip.startsWith("::")) {
            ip = "127.0.0.1";
        }

        return ip;
    }

    private static final Pattern lanIp = Pattern.compile(
            "(10|172|192)\\\\.([0-1][0-9]{0,2}|[2][0-5]{0,2}|[3-9][0-9]?)\\\\.([0-1][0-9]{0,2}|[2][0-5]{0,2}|[3-9][0-9]?)\\\\.([0-1][0-9]{0,2}|[2][0-5]{0,2}|[3-9][0-9]?)");


    /**
     * @Author : Jiaobailong
     * @Summary : isLanClient
     * @Date : 2016/12/15 16:07
     */
    public static boolean isLanClient(String ip) {

        /**
         * 华为云139开头
         */
        return !StringUtils.isEmpty(ip) && (ip.equals("localhost") || ip.equals("0:0:0:0:0:0:0:1")
                || ip.startsWith("127.") || ip.startsWith("10.") || ip.startsWith("192.168.") || lanIp.matcher(ip).find());

    }
}
