IP转换到int类型实现验证

将IP转换到int类型实现匹配

 /**
     * 解析IP验证规则到int类型范围
     *
     * @param ipV4Rule 规则示例: 192.168.0/128.0/255
     * @return
     */
    private static int[] parseIpV4Template(String ipV4Rule) {
        String array[] = ipV4Rule.split("\\.");
        byte[] b1 = new byte[4];
        byte[] b2 = new byte[4];//将IP段转换到二进制形式
        for (int i = 0; i < array.length; i++) {
            int index = array[i].indexOf("/");
            if (index > 0) {
                b1[i] = (byte) Integer.parseInt(array[i].substring(0, index));
                b2[i] = (byte) Integer.parseInt(array[i].substring(index + 1));
            } else {
                int num = Integer.parseInt(array[i]);
                b1[i] = (byte) num;
                b2[i] = (byte) num;
            }
        }
        int f1 = fromBytes(b1[0], b1[1], b1[2], b1[3]);
        int f2 = fromBytes(b2[0], b2[1], b2[2], b2[3]);
        return new int[]{f1, f2};
    }

    private static int convertInt(String ip) {
        String array[] = ip.split("\\.");
        byte[] bytes = new byte[4];
        for (int i = 0; i < array.length; i++) {
            int num = Integer.parseInt(array[i]);
            bytes[i] = (byte) num;
        }
        return fromBytes(bytes[0], bytes[1], bytes[2], bytes[3]);
    }

    /**
     * 将IP Int类型范围解码到真实的IP范围
     *
     * @param rule
     * @return
     */
    private static int[][] decodeIpRange(int[] rule) {
        return new int[][]{new int[]{getIpBitValue(rule[0], 0), getIpBitValue(rule[0], 1), getIpBitValue(rule[0], 2), getIpBitValue(rule[0], 3)}, new int[]{getIpBitValue(rule[1], 0), getIpBitValue(rule[1], 1), getIpBitValue(rule[1], 2), getIpBitValue(rule[1], 3)}};
    }

    private static int getIpBitValue(int num, int ipBit) {
        switch (ipBit) {
            case 0:
                return (num >> 24) & 0XFF;
            case 1:
                return (num >> 16) & 0XFF;
            case 2:
                return (num >> 8) & 0XFF;
            default:
                return num & 0XFF;
        }
    }

    /**
     * 验证IP是否匹配规则
     *
     * @param ip
     * @param ipRange 验证IP范围
     * @return
     */
    public static boolean checkIpRule(int ip, int[] ipRange) {
        int[][] rule = decodeIpRange(ipRange);
        for (int i = 0; i < 4; i++) {
            int ipBitValue = getIpBitValue(ip,i);
            if (!(ipBitValue >= rule[0][i] && rule[1][i] >= ipBitValue)) {
                return false;
            }
        }
        return true;
    }

    private static int fromBytes(byte b1, byte b2, byte b3, byte b4) {
        return b1 << 24 | (b2 & 0xFF) << 16 | (b3 & 0xFF) << 8 | (b4 & 0xFF);
    }


    public static void main(String[] args) {
        //根据IP规则计算出白名单开始到结束的范围
        int[] ipRange = parseIpV4Template("192.168.1.0/5");
        System.out.println(String.format("ip范围:192.168.1.0->int(%d),192.168.0.5->int(%d)", ipRange[0], ipRange[1]));
        for (int x = 0; x < 10; x++) {
            for (int y = 0; y < 10; y++) {
                String ip = "192.168." + x + "." + y;
                int ipInt= convertInt(ip);
                if (checkIpRule(ipInt, ipRange)) {
                    System.out.println("匹配IP:" + ip);
                }
            }
        }
    }

运行结果:

ip范围:192.168.1.0->int(-1062731520),192.168.0.5->int(-1062731515)
匹配IP:192.168.1.0
匹配IP:192.168.1.1
匹配IP:192.168.1.2
匹配IP:192.168.1.3
匹配IP:192.168.1.4
匹配IP:192.168.1.5
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值