IP地址白名单的检查设计

需要能够检查 128.23.51.58、128.23.*.*、128.23.[51-58].*128.23.[51,52,53].* 的支持。

import java.util.ArrayList;
import java.util.List;

public class IPWhitelist {
    private List<String> whitelist;

    public IPWhitelist() {
        whitelist = new ArrayList<>();
    }

    public void addToWhitelist(String ipAddress) {
        whitelist.add(ipAddress);
    }

    public boolean isIPAllowed(String ipAddress) {
        for (String whitelistEntry : whitelist) {
            if (isIpMatching(ipAddress, whitelistEntry)) {
                return true;
            }
        }
        return false;
    }

    private boolean isIpMatching(String ipAddress, String whitelistEntry) {
        String[] ipAddressParts = ipAddress.split("\\.");
        String[] whitelistEntryParts = whitelistEntry.split("\\.");

        if (ipAddressParts.length != whitelistEntryParts.length) {
            return false;
        }

        for (int i = 0; i < ipAddressParts.length; i++) {
            String ipAddressPart = ipAddressParts[i];
            String whitelistEntryPart = whitelistEntryParts[i];

            if (whitelistEntryPart.equals("*")) {
                continue;
            }

            if (whitelistEntryPart.startsWith("[") && whitelistEntryPart.endsWith("]")) {
                String whitelistContent = whitelistEntryPart.substring(1, whitelistEntryPart.length() - 1);
                String[] whitelistValues = whitelistContent.split(",");
                boolean matchFound = false;
                for (String value : whitelistValues) {
                    value = value.trim();
                    if (value.contains("-")) {
                        String[] rangeBounds = value.split("-");
                        if (rangeBounds.length != 2) {
                            continue;
                        }
                        int startRange = Integer.parseInt(rangeBounds[0].trim());
                        int endRange = Integer.parseInt(rangeBounds[1].trim());
                        int ipPart = Integer.parseInt(ipAddressPart.trim());
                        if (ipPart >= startRange && ipPart <= endRange) {
                            matchFound = true;
                            break;
                        }
                    } else {
                        int whitelistValue = Integer.parseInt(value.trim());
                        int ipPart = Integer.parseInt(ipAddressPart.trim());
                        if (ipPart == whitelistValue) {
                            matchFound = true;
                            break;
                        }
                    }
                }
                if (!matchFound) {
                    return false;
                }
            } else {
                int whitelistValue = Integer.parseInt(whitelistEntryPart.trim());
                int ipPart = Integer.parseInt(ipAddressPart.trim());
                if (ipPart != whitelistValue) {
                    return false;
                }
            }
        }

        return true;
    }

    public static void main(String[] args) {
        IPWhitelist whitelist = new IPWhitelist();
        whitelist.addToWhitelist("128.23.[51-58].*");
        whitelist.addToWhitelist("128.23.[51,52,53].*");

        String ipAddress1 = "192.168.0.42";
        String ipAddress2 = "128.23.55.100";
        String ipAddress3 = "128.23.50.52";

        System.out.println(whitelist.isIPAllowed(ipAddress1));  // false
        System.out.println(whitelist.isIPAllowed(ipAddress2));  // true
        System.out.println(whitelist.isIPAllowed(ipAddress3));  // true
    }
}