| /* |
| * 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.dubbo; |
| |
| import java.io.IOException; |
| import java.net.Inet6Address; |
| import java.net.InetAddress; |
| import java.net.NetworkInterface; |
| import java.net.UnknownHostException; |
| import java.util.Enumeration; |
| import java.util.Optional; |
| import java.util.regex.Pattern; |
| |
| public class DubboLocalIpDetector { |
| private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$"); |
| private static final String ANYHOST_VALUE = "0.0.0.0"; |
| private static final String LOCALHOST_VALUE = "127.0.0.1"; |
| |
| public static void main(String[] args) { |
| System.out.println(getAddress()); |
| } |
| |
| public static String getAddress() { |
| return getLocalAddress().getHostAddress(); |
| } |
| |
| private static InetAddress getLocalAddress() { |
| InetAddress localAddress = null; |
| try { |
| localAddress = InetAddress.getLocalHost(); |
| Optional<InetAddress> addressOp = toValidAddress(localAddress); |
| if (addressOp.isPresent()) { |
| return addressOp.get(); |
| } |
| } catch (Throwable e) { |
| e.printStackTrace(); |
| } |
| |
| try { |
| Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); |
| if (null == interfaces) { |
| return localAddress; |
| } |
| while (interfaces.hasMoreElements()) { |
| try { |
| NetworkInterface network = interfaces.nextElement(); |
| if (network.isLoopback() || network.isVirtual() || !network.isUp()) { |
| continue; |
| } |
| Enumeration<InetAddress> addresses = network.getInetAddresses(); |
| while (addresses.hasMoreElements()) { |
| try { |
| Optional<InetAddress> addressOp = toValidAddress(addresses.nextElement()); |
| if (addressOp.isPresent()) { |
| try { |
| if (addressOp.get().isReachable(100)) { |
| return addressOp.get(); |
| } |
| } catch (IOException e) { |
| // ignore |
| } |
| } |
| } catch (Throwable e) { |
| e.printStackTrace(); |
| } |
| } |
| } catch (Throwable e) { |
| e.printStackTrace(); |
| } |
| } |
| } catch (Throwable e) { |
| e.printStackTrace(); |
| } |
| return localAddress; |
| } |
| |
| private static Optional<InetAddress> toValidAddress(InetAddress address) { |
| if (address instanceof Inet6Address) { |
| Inet6Address v6Address = (Inet6Address) address; |
| if (isPreferIPV6Address()) { |
| return Optional.ofNullable(normalizeV6Address(v6Address)); |
| } |
| } |
| if (isValidV4Address(address)) { |
| return Optional.of(address); |
| } |
| return Optional.empty(); |
| } |
| |
| private static boolean isPreferIPV6Address() { |
| boolean preferIpv6 = Boolean.getBoolean("java.net.preferIPv6Addresses"); |
| if (!preferIpv6) { |
| return false; |
| } |
| return false; |
| } |
| |
| private static InetAddress normalizeV6Address(Inet6Address address) { |
| String addr = address.getHostAddress(); |
| int i = addr.lastIndexOf('%'); |
| if (i > 0) { |
| try { |
| return InetAddress.getByName(addr.substring(0, i) + '%' + address.getScopeId()); |
| } catch (UnknownHostException e) { |
| e.printStackTrace(); |
| } |
| } |
| return address; |
| } |
| |
| private static boolean isValidV4Address(InetAddress address) { |
| if (address == null || address.isLoopbackAddress()) { |
| return false; |
| } |
| String name = address.getHostAddress(); |
| boolean result = (name != null |
| && IP_PATTERN.matcher(name).matches() |
| && !ANYHOST_VALUE.equals(name) |
| && !LOCALHOST_VALUE.equals(name)); |
| return result; |
| } |
| } |