All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wl4g.infra.common.net.InetUtils Maven / Gradle / Ivy

There is a newer version: 3.1.72
Show newest version
/*
 * Copyright 2017 ~ 2025 the original author or authors. James Wong 
 *
 * Licensed 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 com.wl4g.infra.common.net;

import static com.wl4g.infra.common.lang.Assert2.notNullOf;

import java.io.Closeable;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import lombok.CustomLog;

/**
 * {@link InetUtils}
 * 
 * @author James Wong <[email protected]>
 * @version 2022-07-12
 * @since {@link org.springframework.cloud.commons.util.InetUtils}
 */
@CustomLog
public class InetUtils implements Closeable {

    // TODO: maybe shutdown the thread pool if it isn't being used?
    private final ExecutorService executorService;
    private final InetUtilsProperties properties;

    public InetUtils(final InetUtilsProperties properties) {
        this.properties = notNullOf(properties, "properties");
        this.executorService = Executors.newSingleThreadExecutor(r -> {
            Thread t = new Thread(r);
            t.setName(InetUtils.class.getSimpleName());
            t.setDaemon(true);
            return t;
        });
    }

    @Override
    public void close() {
        this.executorService.shutdown();
    }

    public HostInfo findFirstNonLoopbackHostInfo() {
        InetAddress address = findFirstNonLoopbackAddress();
        if (address != null) {
            return convertAddress(address);
        }
        HostInfo hostInfo = new HostInfo();
        hostInfo.setHostname(this.properties.getDefaultHostname());
        hostInfo.setIpAddress(this.properties.getDefaultIpAddress());
        return hostInfo;
    }

    public InetAddress findFirstNonLoopbackAddress() {
        InetAddress result = null;
        try {
            int lowest = Integer.MAX_VALUE;
            for (Enumeration nics = NetworkInterface.getNetworkInterfaces(); nics.hasMoreElements();) {
                NetworkInterface ifc = nics.nextElement();
                if (ifc.isUp()) {
                    log.trace("Testing interface: " + ifc.getDisplayName());
                    if (ifc.getIndex() < lowest || result == null) {
                        lowest = ifc.getIndex();
                    } else if (result != null) {
                        continue;
                    }

                    // @formatter:off
					if (!ignoreInterface(ifc.getDisplayName())) {
						for (Enumeration addrs = ifc
								.getInetAddresses(); addrs.hasMoreElements();) {
							InetAddress address = addrs.nextElement();
							if (address instanceof Inet4Address
									&& !address.isLoopbackAddress()
									&& isPreferredAddress(address)) {
								log.trace("Found non-loopback interface: "
										+ ifc.getDisplayName());
								result = address;
							}
						}
					}
					// @formatter:on
                }
            }
        } catch (IOException ex) {
            log.error("Cannot get first non-loopback address", ex);
        }

        if (result != null) {
            return result;
        }

        try {
            return InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            log.warn("Unable to retrieve localhost");
        }

        return null;
    }

    // For testing.
    boolean isPreferredAddress(InetAddress address) {
        if (this.properties.isUseOnlySiteLocalInterfaces()) {
            final boolean siteLocalAddress = address.isSiteLocalAddress();
            if (!siteLocalAddress) {
                log.trace("Ignoring address: " + address.getHostAddress());
            }
            return siteLocalAddress;
        }
        final List preferredNetworks = this.properties.getPreferredNetworks();
        if (preferredNetworks.isEmpty()) {
            return true;
        }
        for (String regex : preferredNetworks) {
            final String hostAddress = address.getHostAddress();
            if (hostAddress.matches(regex) || hostAddress.startsWith(regex)) {
                return true;
            }
        }
        log.trace("Ignoring address: " + address.getHostAddress());
        return false;
    }

    // For testing
    boolean ignoreInterface(String interfaceName) {
        for (String regex : this.properties.getIgnoredInterfaces()) {
            if (interfaceName.matches(regex)) {
                log.trace("Ignoring interface: " + interfaceName);
                return true;
            }
        }
        return false;
    }

    public HostInfo convertAddress(final InetAddress address) {
        HostInfo hostInfo = new HostInfo();
        Future result = this.executorService.submit(address::getHostName);

        String hostname;
        try {
            hostname = result.get(this.properties.getTimeoutSeconds(), TimeUnit.SECONDS);
        } catch (Exception e) {
            log.info("Cannot determine local hostname");
            hostname = "localhost";
        }
        hostInfo.setHostname(hostname);
        hostInfo.setIpAddress(address.getHostAddress());
        return hostInfo;
    }

    /**
     * Host information pojo.
     */
    public static class HostInfo {

        /**
         * Should override the host info.
         */
        public boolean override;

        private String ipAddress;

        private String hostname;

        public HostInfo(String hostname) {
            this.hostname = hostname;
        }

        public HostInfo() {
        }

        public int getIpAddressAsInt() {
            InetAddress inetAddress = null;
            String host = this.ipAddress;
            if (host == null) {
                host = this.hostname;
            }
            try {
                inetAddress = InetAddress.getByName(host);
            } catch (final UnknownHostException e) {
                throw new IllegalArgumentException(e);
            }
            return ByteBuffer.wrap(inetAddress.getAddress()).getInt();
        }

        public boolean isOverride() {
            return this.override;
        }

        public void setOverride(boolean override) {
            this.override = override;
        }

        public String getIpAddress() {
            return this.ipAddress;
        }

        public void setIpAddress(String ipAddress) {
            this.ipAddress = ipAddress;
        }

        public String getHostname() {
            return this.hostname;
        }

        public void setHostname(String hostname) {
            this.hostname = hostname;
        }

    }

    public static class InetUtilsProperties {

        /**
         * The default hostname. Used in case of errors.
         */
        private String defaultHostname = "localhost";

        /**
         * The default IP address. Used in case of errors.
         */
        private String defaultIpAddress = "127.0.0.1";

        /**
         * Timeout, in seconds, for calculating hostname.
         */
        private int timeoutSeconds = 1;

        /**
         * List of Java regular expressions for network interfaces that will be
         * ignored.
         */
        private List ignoredInterfaces = new ArrayList<>();

        /**
         * Whether to use only interfaces with site local addresses. See
         * {@link InetAddress#isSiteLocalAddress()} for more details.
         */
        private boolean useOnlySiteLocalInterfaces = false;

        /**
         * List of Java regular expressions for network addresses that will be
         * preferred.
         */
        private List preferredNetworks = new ArrayList<>();

        public String getDefaultHostname() {
            return this.defaultHostname;
        }

        public void setDefaultHostname(String defaultHostname) {
            this.defaultHostname = defaultHostname;
        }

        public String getDefaultIpAddress() {
            return this.defaultIpAddress;
        }

        public void setDefaultIpAddress(String defaultIpAddress) {
            this.defaultIpAddress = defaultIpAddress;
        }

        public int getTimeoutSeconds() {
            return this.timeoutSeconds;
        }

        public void setTimeoutSeconds(int timeoutSeconds) {
            this.timeoutSeconds = timeoutSeconds;
        }

        public List getIgnoredInterfaces() {
            return this.ignoredInterfaces;
        }

        public void setIgnoredInterfaces(List ignoredInterfaces) {
            this.ignoredInterfaces = ignoredInterfaces;
        }

        public boolean isUseOnlySiteLocalInterfaces() {
            return this.useOnlySiteLocalInterfaces;
        }

        public void setUseOnlySiteLocalInterfaces(boolean useOnlySiteLocalInterfaces) {
            this.useOnlySiteLocalInterfaces = useOnlySiteLocalInterfaces;
        }

        public List getPreferredNetworks() {
            return this.preferredNetworks;
        }

        public void setPreferredNetworks(List preferredNetworks) {
            this.preferredNetworks = preferredNetworks;
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy