Skip to content

Commit

Permalink
feat: ipv6 support (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
hancin authored Jun 12, 2024
1 parent 1d21086 commit 3b14889
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 26 deletions.
97 changes: 88 additions & 9 deletions src/main/java/com/aws/greengrass/detector/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,38 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

@SuppressWarnings("PMD.DataClass")
public class Config {
private final Logger logger = LogManager.getLogger(Config.class);

static final String INCLUDE_IPV4_LOOPBACK_ADDRESSES_CONFIG_KEY = "includeIPv4LoopbackAddrs";
static final String INCLUDE_IPV6_LOOPBACK_ADDRESSES_CONFIG_KEY = "includeIPv6LoopbackAddrs";
static final String INCLUDE_IPV4_LINK_LOCAL_ADDRESSES_CONFIG_KEY = "includeIPv4LinkLocalAddrs";
static final String INCLUDE_IPV6_LINK_LOCAL_ADDRESSES_CONFIG_KEY = "includeIPv6LinkLocalAddrs";
static final String INCLUDE_IPV4_ADDRESSES_CONFIG_KEY = "includeIPv4Addrs";
static final String INCLUDE_IPV6_ADDRESSES_CONFIG_KEY = "includeIPv6Addrs";
static final String DEFAULT_PORT_CONFIG_KEY = "defaultPort";
static final boolean DEFAULT_INCLUDE_IPV4_LOOPBACK_ADDRESSES = false;
static final boolean DEFAULT_INCLUDE_IPV6_LOOPBACK_ADDRESSES = false;
static final boolean DEFAULT_INCLUDE_IPV4_LINK_LOCAL_ADDRESSES = false;
static final boolean DEFAULT_INCLUDE_IPV6_LINK_LOCAL_ADDRESSES = false;
static final boolean DEFAULT_INCLUDE_IPV4_ADDRESSES = true;
static final boolean DEFAULT_INCLUDE_IPV6_ADDRESSES = false;
static final int DEFAULT_PORT = 8883;

private AtomicInteger defaultPort = new AtomicInteger(DEFAULT_PORT);
private AtomicBoolean includeIPv4LoopbackAddrs = new AtomicBoolean(DEFAULT_INCLUDE_IPV4_LOOPBACK_ADDRESSES);
private AtomicBoolean includeIPv4LinkLocalAddrs = new AtomicBoolean(DEFAULT_INCLUDE_IPV4_LINK_LOCAL_ADDRESSES);
private final AtomicInteger defaultPort = new AtomicInteger(DEFAULT_PORT);
private final AtomicBoolean includeIPv4LoopbackAddrs
= new AtomicBoolean(DEFAULT_INCLUDE_IPV4_LOOPBACK_ADDRESSES);
private final AtomicBoolean includeIPv6LoopbackAddrs
= new AtomicBoolean(DEFAULT_INCLUDE_IPV6_LOOPBACK_ADDRESSES);
private final AtomicBoolean includeIPv4LinkLocalAddrs
= new AtomicBoolean(DEFAULT_INCLUDE_IPV4_LINK_LOCAL_ADDRESSES);
private final AtomicBoolean includeIPv6LinkLocalAddrs
= new AtomicBoolean(DEFAULT_INCLUDE_IPV6_LINK_LOCAL_ADDRESSES);
private final AtomicBoolean includeIPv4Addrs
= new AtomicBoolean(DEFAULT_INCLUDE_IPV4_ADDRESSES);
private final AtomicBoolean includeIPv6Addrs
= new AtomicBoolean(DEFAULT_INCLUDE_IPV6_ADDRESSES);

/**
* Config constructor.
Expand All @@ -37,29 +56,57 @@ public Config(Topics topics) {
Topics configurationTopics = topics.lookupTopics(KernelConfigResolver.CONFIGURATION_CONFIG_KEY);
configurationTopics.subscribe((whatHappened, node) -> {
if (configurationTopics.isEmpty()) {
this.includeIPv4LoopbackAddrs = new AtomicBoolean(DEFAULT_INCLUDE_IPV4_LOOPBACK_ADDRESSES);
this.includeIPv4LinkLocalAddrs = new AtomicBoolean(DEFAULT_INCLUDE_IPV4_LINK_LOCAL_ADDRESSES);
this.defaultPort = new AtomicInteger(DEFAULT_PORT);
this.includeIPv4LoopbackAddrs.set(DEFAULT_INCLUDE_IPV4_LOOPBACK_ADDRESSES);
this.includeIPv6LoopbackAddrs.set(DEFAULT_INCLUDE_IPV6_LOOPBACK_ADDRESSES);
this.includeIPv4LinkLocalAddrs.set(DEFAULT_INCLUDE_IPV4_LINK_LOCAL_ADDRESSES);
this.includeIPv6LinkLocalAddrs.set(DEFAULT_INCLUDE_IPV6_LINK_LOCAL_ADDRESSES);
this.includeIPv4Addrs.set(DEFAULT_INCLUDE_IPV4_ADDRESSES);
this.includeIPv6Addrs.set(DEFAULT_INCLUDE_IPV6_ADDRESSES);
this.defaultPort.set(DEFAULT_PORT);
return;
}

this.includeIPv4LoopbackAddrs = new AtomicBoolean(
this.includeIPv4LoopbackAddrs.set(
Coerce.toBoolean(
configurationTopics.findOrDefault(
DEFAULT_INCLUDE_IPV4_LOOPBACK_ADDRESSES,
INCLUDE_IPV4_LOOPBACK_ADDRESSES_CONFIG_KEY)));
this.includeIPv4LinkLocalAddrs = new AtomicBoolean(
this.includeIPv6LoopbackAddrs.set(
Coerce.toBoolean(
configurationTopics.findOrDefault(
DEFAULT_INCLUDE_IPV6_LOOPBACK_ADDRESSES,
INCLUDE_IPV6_LOOPBACK_ADDRESSES_CONFIG_KEY)));
this.includeIPv4LinkLocalAddrs.set(
Coerce.toBoolean(
configurationTopics.findOrDefault(
DEFAULT_INCLUDE_IPV4_LINK_LOCAL_ADDRESSES,
INCLUDE_IPV4_LINK_LOCAL_ADDRESSES_CONFIG_KEY)));
this.defaultPort = new AtomicInteger(
this.includeIPv6LinkLocalAddrs.set(
Coerce.toBoolean(
configurationTopics.findOrDefault(
DEFAULT_INCLUDE_IPV6_LINK_LOCAL_ADDRESSES,
INCLUDE_IPV6_LINK_LOCAL_ADDRESSES_CONFIG_KEY)));
this.includeIPv4Addrs.set(
Coerce.toBoolean(
configurationTopics.findOrDefault(
DEFAULT_INCLUDE_IPV4_ADDRESSES,
INCLUDE_IPV4_ADDRESSES_CONFIG_KEY)));
this.includeIPv6Addrs.set(
Coerce.toBoolean(
configurationTopics.findOrDefault(
DEFAULT_INCLUDE_IPV6_ADDRESSES,
INCLUDE_IPV6_ADDRESSES_CONFIG_KEY)));
this.defaultPort.set(
Coerce.toInt(
configurationTopics.findOrDefault(DEFAULT_PORT,
DEFAULT_PORT_CONFIG_KEY)));

logger.atInfo().kv("includeIPv4LoopbackAddrs", includeIPv4LoopbackAddrs.get())
.kv("includeIPv4LinkLocalAddrs", includeIPv4LinkLocalAddrs.get())
.kv("includeIPv6LoopbackAddrs", includeIPv6LoopbackAddrs.get())
.kv("includeIPv6LinkLocalAddrs", includeIPv6LinkLocalAddrs.get())
.kv("includeIPv4Addrs", includeIPv4Addrs.get())
.kv("includeIPv6Addrs", includeIPv6Addrs.get())
.kv("defaultPort", defaultPort.get())
.log("Configuration updated");
});
Expand All @@ -81,6 +128,38 @@ public boolean isIncludeIPv4LinkLocalAddrs() {
return this.includeIPv4LinkLocalAddrs.get();
}

/**
* includeIPv6LoopbackAddrs getter.
* @return boolean includeIPv6LoopbackAddrs
*/
public boolean isIncludeIPv6LoopbackAddrs() {
return this.includeIPv6LoopbackAddrs.get();
}

/**
* includeIPv6LinkLocalAddrs getter.
* @return boolean includeIPv6LinkLocalAddrs
*/
public boolean isIncludeIPv6LinkLocalAddrs() {
return this.includeIPv6LinkLocalAddrs.get();
}

/**
* includeIPv4Addrs getter.
* @return boolean includeIPv4Addrs
*/
public boolean isIncludeIPv4Addrs() {
return this.includeIPv4Addrs.get();
}

/**
* includeIPv6Addrs getter.
* @return boolean includeIPv6Addrs
*/
public boolean isIncludeIPv6Addrs() {
return this.includeIPv6Addrs.get();
}

/**
* defaultPort getter.
* @return integer defaultPort
Expand Down
26 changes: 19 additions & 7 deletions src/main/java/com/aws/greengrass/detector/detector/IpDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,25 @@ List<InetAddress> getIpAddresses(Enumeration<NetworkInterface> interfaces, Confi
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
InetAddress address = interfaceAddress.getAddress();
if (address instanceof Inet6Address) {
continue;
}
if (address.isLoopbackAddress() && !config.isIncludeIPv4LoopbackAddrs()) {
continue;
}
if (address.isLinkLocalAddress() && !config.isIncludeIPv4LinkLocalAddrs()) {
continue;
if (!config.isIncludeIPv6Addrs()) {
continue;
}
if (address.isLinkLocalAddress() && !config.isIncludeIPv6LinkLocalAddrs()) {
continue;
}
if (address.isLoopbackAddress() && !config.isIncludeIPv6LoopbackAddrs()) {
continue;
}
} else {
if (!config.isIncludeIPv4Addrs()) {
continue;
}
if (address.isLoopbackAddress() && !config.isIncludeIPv4LoopbackAddrs()) {
continue;
}
if (address.isLinkLocalAddress() && !config.isIncludeIPv4LinkLocalAddrs()) {
continue;
}
}
ipAddresses.add(address);
}
Expand Down
Loading

0 comments on commit 3b14889

Please sign in to comment.