| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package org.apache.shardingsphere.elasticjob.cloud.util.env; |
| |
| import lombok.AccessLevel; |
| import lombok.NoArgsConstructor; |
| |
| import java.net.InetAddress; |
| import java.net.NetworkInterface; |
| import java.net.SocketException; |
| import java.net.UnknownHostException; |
| import java.util.Enumeration; |
| |
| /** |
| * IP address utility. |
| */ |
| @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| public final class IpUtils { |
| |
| public static final String IP_REGEX = "((\\d|[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})"; |
| |
| private static volatile String cachedIpAddress; |
| |
| /** |
| * Get IP address for localhost. |
| * |
| * @return IP address for localhost |
| */ |
| public static String getIp() { |
| if (null != cachedIpAddress) { |
| return cachedIpAddress; |
| } |
| Enumeration<NetworkInterface> netInterfaces; |
| try { |
| netInterfaces = NetworkInterface.getNetworkInterfaces(); |
| } catch (final SocketException ex) { |
| throw new HostException(ex); |
| } |
| String localIpAddress = null; |
| while (netInterfaces.hasMoreElements()) { |
| NetworkInterface netInterface = netInterfaces.nextElement(); |
| Enumeration<InetAddress> ipAddresses = netInterface.getInetAddresses(); |
| while (ipAddresses.hasMoreElements()) { |
| InetAddress ipAddress = ipAddresses.nextElement(); |
| if (isPublicIpAddress(ipAddress)) { |
| String publicIpAddress = ipAddress.getHostAddress(); |
| cachedIpAddress = publicIpAddress; |
| return publicIpAddress; |
| } |
| if (isLocalIpAddress(ipAddress)) { |
| localIpAddress = ipAddress.getHostAddress(); |
| } |
| } |
| } |
| cachedIpAddress = localIpAddress; |
| return localIpAddress; |
| } |
| |
| private static boolean isPublicIpAddress(final InetAddress ipAddress) { |
| return !ipAddress.isSiteLocalAddress() && !ipAddress.isLoopbackAddress() && !isV6IpAddress(ipAddress); |
| } |
| |
| private static boolean isLocalIpAddress(final InetAddress ipAddress) { |
| return ipAddress.isSiteLocalAddress() && !ipAddress.isLoopbackAddress() && !isV6IpAddress(ipAddress); |
| } |
| |
| private static boolean isV6IpAddress(final InetAddress ipAddress) { |
| return ipAddress.getHostAddress().contains(":"); |
| } |
| |
| /** |
| * Get host name for localhost. |
| * |
| * @return host name for localhost |
| */ |
| public static String getHostName() { |
| try { |
| return InetAddress.getLocalHost().getHostName(); |
| } catch (final UnknownHostException ex) { |
| return "unknown"; |
| } |
| } |
| } |