public class IpUtils {
    /**
     * 日志信息
     */
    private static final Logger logger = LoggerFactory.getLogger(IpUtils.class);
    /**
     * ip的匹配规则
     */
    private static final String IP_REG = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";

    /**
     * 判断ip是否在局域网中
     * <p/>
     * 内网ip
     * A类 10.0.0.0 /8
     * B类 172.16.0.0--172.31.0.0   /16
     * C类 192.168.0.0--192.168.255.0 /24
     *
     * @param ip
     * @return
     */
    private IPBlock classA = new IPBlock("10.0.0.0", "255.0.0.0");

    private IPBlock classC = new IPBlock("192.168.0.0", "255.255.0.0");

    private boolean inLAN(String ip) {

        ip = ip.trim();

        int[] netIdSegment = divideToSegment(ip);
        if (netIdSegment == null) {
            logger.error("--errorIP--" + ip);
            return false;
        }

        if (classA.inMyBlock(netIdSegment)) {
            //a 类ip
            return true;
        }

        if (classC.inMyBlock(netIdSegment)) {
            //c类ip
            return true;
        }

        if (netIdSegment[0] == 172 && netIdSegment[1] >= 16 && netIdSegment[1] <= 31) {
            //b类ip
            return true;
        }

        if ("127.0.0.1".equals(ip)) {
            //本机地址
            return true;
        }

        return false;
    }


    private static String serverIP = null;

    public static String getServerIp() {

        if (serverIP == null) {
            try {
                Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
                InetAddress ip = null;
                while (allNetInterfaces.hasMoreElements()) {
                    NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                    if (netInterface.isLoopback()) {
                        continue;
                    }

                    Enumeration addresses = netInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        ip = (InetAddress) addresses.nextElement();
                        if (ip != null && ip instanceof Inet4Address) {
                            serverIP = ip.getHostAddress().toString();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return serverIP;
    }

    /**
     * ip 点分十进制
     *
     * @param ip
     * @return
     */
    public static int[] divideToSegment(String ip) {

        if (!ip.matches(IP_REG)) {
            return null;
        }

        String[] segments = ip.split("\\.");
        int int_segments[] = new int[segments.length];
        for (int i = 0; i < segments.length; i++) {
            int_segments[i] = Integer.parseInt(segments[i]);
        }

        return int_segments;
    }

    static class IPBlock {

        private String subnetMask;        //子网掩码
        private String netId;     //网络号

        private int[] subnetMaskSegment;
        private int[] netIdSegment;


        public IPBlock(String netId, String subnetMask) {

            this.netId = netId;
            this.subnetMask = subnetMask;

            initSubnetMaskSegment(subnetMask);
            intiNetIdSegment(netId);
        }


        private void initSubnetMaskSegment(String ip) {
            this.subnetMaskSegment = IpUtils.divideToSegment(ip);
        }

        private void intiNetIdSegment(String ip) {
            this.netIdSegment = IpUtils.divideToSegment(ip);
        }

        public String getSubnetMask() {
            return subnetMask;
        }

        public void setSubnetMask(String subnetMask) {
            this.subnetMask = subnetMask;
        }

        public String getNetId() {
            return netId;
        }


        public int[] getSubnetMaskSegment() {
            return subnetMaskSegment;
        }


        public int[] getNetIdSegment() {
            return netIdSegment;
        }

        public boolean inMyBlock(String ip) {

            int segments[] = IpUtils.divideToSegment(ip);
            if (segments == null || segments.length < 4) {
                return false;
            }

            for (int i = 0; i < subnetMaskSegment.length; i++) {
                if ((segments[i] & subnetMaskSegment[i]) != netIdSegment[i]) {
                    return false;
                }
            }

            return true;
        }

        public boolean inMyBlock(int[] segments) {

            if (segments == null || segments.length < 4) {
                return false;
            }

            for (int i = 0; i < subnetMaskSegment.length; i++) {
                if ((segments[i] & subnetMaskSegment[i]) != netIdSegment[i]) {
                    return false;
                }
            }

            return true;
        }

    }


}

 

posted on 2021-06-18 11:01  vow007  阅读(5)  评论(0编辑  收藏  举报  来源