判断域名是内网还是公网

一、流程

以下是判断一个域名是内网还是公网的流程:

步骤 描述
1 解析域名对应的IP地址
2 判断IP地址是否为内网IP
3 结果是内网IP的话,判断是否为保留IP
4 如果是保留IP,则为内网IP,否则为公网IP

二、步骤及代码实现

  1. 解析域名对应的IP地址

首先,通过域名解析获取对应的IP地址。我们可以使用InetAddress类来实现域名解析,代码如下:

import java.net.InetAddress;
import java.net.UnknownHostException;

public class DomainChecker {
    public static void main(String[] args) {
        try {
            String domain = "www.example.com"; // 替换成要判断的域名
            InetAddress inetAddress = InetAddress.getByName(domain);
            String ipAddress = inetAddress.getHostAddress();
            System.out.println("IP地址:" + ipAddress);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
  1. 判断IP地址是否为内网IP

接下来,我们需要判断获取到的IP地址是否为内网IP。内网IP的范围一般为:10.0.0.0~10.255.255.255、172.16.0.0~172.31.255.255、192.168.0.0~192.168.255.255。

我们可以编写一个方法来判断是否为内网IP,代码如下:

public static boolean isPrivateIP(String ipAddress) {
    String[] ipParts = ipAddress.split("\\.");
    int firstPart = Integer.parseInt(ipParts[0]);

    if (firstPart == 10) {
        return true;
    } else if (firstPart == 172) {
        int secondPart = Integer.parseInt(ipParts[1]);
        if (secondPart >= 16 && secondPart <= 31) {
            return true;
        }
    } else if (firstPart == 192) {
        int secondPart = Integer.parseInt(ipParts[1]);
        int thirdPart = Integer.parseInt(ipParts[2]);
        if (secondPart == 168 && thirdPart >= 0 && thirdPart <= 255) {
            return true;
        }
    }

    return false;
}
  1. 判断是否为保留IP

如果判断结果为内网IP,我们还需要进一步判断是否为保留IP。保留IP是指在网络中预留给特定用途或不分配给任何设备的IP地址。

我们可以编写一个方法来判断是否为保留IP,代码如下:

public static boolean isReservedIP(String ipAddress) {
    String[] ipParts = ipAddress.split("\\.");
    int firstPart = Integer.parseInt(ipParts[0]);
    int secondPart = Integer.parseInt(ipParts[1]);

    if (firstPart == 0 || firstPart == 127) {
        return true;
    } else if (firstPart == 169 && secondPart == 254) {
        return true;
    } else if (firstPart == 192 && secondPart == 0 && ipParts[2].equals("2")) {
        return true;
    }

    return false;
}
  1. 判断是内网还是公网IP

最后,我们可以根据上述方法的结果判断域名是内网还是公网IP。

完整的代码如下:

import java.net.InetAddress;
import java.net.UnknownHostException;

public class DomainChecker {
    public static void main(String[] args) {
        try {
            String domain = "www.example.com"; // 替换成要判断的域名
            InetAddress inetAddress = InetAddress.getByName(domain);
            String ipAddress = inetAddress.getHostAddress();
            System.out.println("IP地址:" + ipAddress);

            boolean isPrivateIP = isPrivateIP(ipAddress);
            if (isPrivateIP) {
                boolean isReservedIP = isReservedIP(ipAddress);
                if (isReservedIP) {
                    System.out.println("内网保留IP");
                } else {
                    System.out.println("内网非保留IP");
                }
            } else {
                System.out.println("公网IP");
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    public static boolean isPrivateIP(String ipAddress) {
        String[] ipParts = ipAddress.split("\\.");
        int firstPart = Integer.parseInt(ipParts[0]);

        if (firstPart == 10) {
            return true;
        } else if (firstPart == 172) {
            int secondPart = Integer.parseInt(ipParts[1]);
            if (