yangys
2024-03-29 153cc3fd4ef015a8b1390b2eef3d102c5859a5e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.qianwen.core.launch.utils;
 
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.util.Enumeration;
import org.springframework.util.StringUtils;
 
public class INetUtil {
    public static final String LOCAL_HOST = "127.0.0.1";
 
    public static String getHostName() {
        String hostname;
        try {
            InetAddress address = InetAddress.getLocalHost();
            hostname = address.getHostName();
            if (StringUtils.isEmpty(hostname)) {
                hostname = address.toString();
            }
        } catch (UnknownHostException e) {
            hostname = LOCAL_HOST;
        }
        return hostname;
    }
 
    public static String getHostIp() {
        String hostAddress;
        try {
            InetAddress address = getLocalHostLANAddress();
            hostAddress = address.getHostAddress();
            if (StringUtils.isEmpty(hostAddress)) {
                hostAddress = address.toString();
            }
        } catch (UnknownHostException e) {
            hostAddress = LOCAL_HOST;
        }
        return hostAddress;
    }
 
    private static InetAddress getLocalHostLANAddress() throws UnknownHostException {
        try {
          InetAddress candidateAddress = null;
          for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) {
            NetworkInterface iface = ifaces.nextElement();
            for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) {
              InetAddress inetAddr = inetAddrs.nextElement();
              if (!inetAddr.isLoopbackAddress()) {
                if (inetAddr.isSiteLocalAddress())
                  return inetAddr; 
                if (candidateAddress == null)
                  candidateAddress = inetAddr; 
              } 
            } 
          } 
          if (candidateAddress != null)
            return candidateAddress; 
          InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
          if (jdkSuppliedAddress == null)
            throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null."); 
          return jdkSuppliedAddress;
        } catch (Exception e) {
          UnknownHostException unknownHostException = new UnknownHostException("Failed to determine LAN address: " + e);
          unknownHostException.initCause(e);
          throw unknownHostException;
        } 
    }
 
 
    public static boolean tryPort(int port) {
      try (ServerSocket ignore = new ServerSocket(port)) {
        return true;
      } catch (Exception e) {
        return false;
      } 
    }
 
    public static InetAddress getInetAddress(String ip) {
        try {
            return InetAddress.getByName(ip);
        } catch (UnknownHostException e) {
            return null;
        }
    }
 
    public static boolean isInternalIp(String ip) {
        return isInternalIp(getInetAddress(ip));
      }
      
      public static boolean isInternalIp(InetAddress address) {
        if (isLocalIp(address))
          return true; 
        return isInternalIp(address.getAddress());
      }
      
 
    public static boolean isLocalIp(InetAddress address) {
        return address.isAnyLocalAddress() || address.isLoopbackAddress() || address.isSiteLocalAddress();
    }
 
    public static boolean isInternalIp(byte[] addr) {
        byte b0 = addr[0];
        byte b1 = addr[1];
        byte section1 = 10;
        byte section2 = -84;
        byte section3 = 16;
        byte section4 = 31;
        byte section5 = -64;
        byte section6 = -88;
        switch (b0) {
          case 10:
            return true;
          case -84:
            if (b1 >= 16 && b1 <= 31)
              return true; 
          case -64:
            if (b1 == -88)
              return true; 
            break;
        } 
        return false;
    }
}