Commit beafa94b authored by Matija Obreza's avatar Matija Obreza

Merge branch 'hazelcast-network-interface' into 'master'

Configure HZ with network interface name

See merge request genesys-pgr/genesys-server!352
parents 04adbdc7 b8128f27
......@@ -17,14 +17,16 @@
package org.genesys2.spring.config;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import net.javacrumbs.shedlock.provider.hazelcast.HazelcastLockProvider;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.security.lockout.AccountLockoutManager.AttemptStatistics;
import org.genesys2.spring.hazelcast.HazelcastCacheRegionFactoryWrapper;
......@@ -59,6 +61,9 @@ import com.hazelcast.spring.context.SpringManagedContext;
import com.hazelcast.web.WebFilter;
import com.hazelcast.web.spring.SpringAwareWebFilter;
import net.javacrumbs.shedlock.provider.hazelcast.HazelcastLockProvider;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
@Configuration
@EnableCaching
@EnableSchedulerLock(defaultLockAtMostFor = "PT15M")
......@@ -156,15 +161,21 @@ public abstract class HazelcastConfig {
if (StringUtils.isNotBlank(hazelInterfaces)) {
network.getInterfaces().setEnabled(true);
for (final String hazelInterface : hazelInterfaces.split(",")) {
final String trimmed = hazelInterface.trim();
if (StringUtils.isNotBlank(trimmed)) {
System.out.println("Hazelcast: Adding single interface " + trimmed);
try {
List<NetworkInterface> nets = Collections.list(NetworkInterface.getNetworkInterfaces());
final InetAddress ipAddress = InetAddress.getByName(trimmed);
LOG.warn("Interface ip={} ={}", ipAddress.getHostAddress(), ipAddress.isSiteLocalAddress());
network.getInterfaces().addInterface(ipAddress.getHostAddress());
} catch (final UnknownHostException e) {
LOG.warn("Requested interface address ip={} local={}", ipAddress.getHostAddress(), ipAddress.isSiteLocalAddress());
InterfaceAddress netIp = findInterfaceAddress(nets, ipAddress);
LOG.warn("Found interface {} for {}", netIp, ipAddress);
network.getInterfaces().addInterface(netIp.getAddress().getHostAddress());
} catch (final UnknownHostException | SocketException e) {
LOG.warn("Could not resolve {}, error {}", trimmed, e.getMessage());
network.getInterfaces().addInterface(trimmed);
}
......@@ -234,6 +245,47 @@ public abstract class HazelcastConfig {
return instance;
}
private InterfaceAddress findInterfaceAddress(List<NetworkInterface> nets, InetAddress ipAddress) {
for (NetworkInterface net : nets) {
for (InterfaceAddress netIp : net.getInterfaceAddresses()) {
if (interfaceMatchForIp(netIp, ipAddress)) {
LOG.warn("Got match for {} on interace {}", ipAddress, net.getName());
return netIp;
}
}
}
return null;
}
private boolean interfaceMatchForIp(InterfaceAddress netIp, InetAddress ipAddress) {
LOG.warn("Comparing {} to {}/{}", ipAddress.getHostAddress(), netIp.getAddress().getHostAddress(), netIp.getNetworkPrefixLength());
byte[] ipBytes = ipAddress.getAddress();
byte[] netBytes = netIp.getAddress().getAddress();
if (ipBytes.length == netBytes.length) {
byte[] maskBytes = new byte[netBytes.length];
int a = netIp.getNetworkPrefixLength() / 8; // index of last byte to update
int b = netIp.getNetworkPrefixLength() % 8; // number of 1s in the last byte
LOG.warn("Network mask {} -> a={} b={}", netIp.getNetworkPrefixLength(), a, b);
for (int i = 0; i<a; i++) {
maskBytes[i] = (byte) 0xFF; // all 1
}
for (int i = 0; i< b; i++) {
maskBytes[a] = (byte) (maskBytes[a] << 1 | 0x1);
}
LOG.warn("Network mask {}", Arrays.toString(maskBytes));
for (int i=0; i<maskBytes.length; i++) {
if ((ipBytes[i] & maskBytes[i]) != (netBytes[i] & maskBytes[i])) {
LOG.warn("Byte {} does not match {} != {}", i, (ipBytes[i] & maskBytes[i]), (netBytes[i] & maskBytes[i]));
return false;
}
}
return true;
}
return false;
}
/**
* Checks for configured members.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment