External IP address updates now require 10 consecutive readings.

This commit is contained in:
CalDescent 2022-01-12 19:19:56 +00:00
parent 4e1b0a25bb
commit 90836afd91

View File

@ -1113,6 +1113,7 @@ public class Network {
return; return;
} }
// Validate IP address
String[] parts = peerAddress.split(":"); String[] parts = peerAddress.split(":");
if (parts.length != 2) { if (parts.length != 2) {
return; return;
@ -1128,32 +1129,47 @@ public class Network {
return; return;
} }
// Add to the list
this.ourExternalIpAddressHistory.add(host); this.ourExternalIpAddressHistory.add(host);
// Limit to 10 entries // Limit to 25 entries
while (this.ourExternalIpAddressHistory.size() > 10) { while (this.ourExternalIpAddressHistory.size() > 25) {
this.ourExternalIpAddressHistory.remove(0); this.ourExternalIpAddressHistory.remove(0);
} }
// If we've had 3 consecutive matching addresses, and they're different from // If we've had 10 consecutive matching addresses, and they're different from
// our stored IP address value, treat it as updated. // our stored IP address value, treat it as updated.
int consecutiveReadingsRequired = 10;
int size = this.ourExternalIpAddressHistory.size(); int size = this.ourExternalIpAddressHistory.size();
if (size < 3) { if (size < consecutiveReadingsRequired) {
// Need at least 3 readings // Need at least 10 readings
return; return;
} }
String ip1 = this.ourExternalIpAddressHistory.get(size - 1); // Count the number of consecutive IP address readings
String ip2 = this.ourExternalIpAddressHistory.get(size - 2); String lastReading = null;
String ip3 = this.ourExternalIpAddressHistory.get(size - 3); int consecutiveReadings = 0;
for (int i = size-1; i >= 0; i--) {
String reading = this.ourExternalIpAddressHistory.get(i);
if (lastReading != null) {
if (Objects.equals(reading, lastReading)) {
consecutiveReadings++;
}
else {
consecutiveReadings = 0;
}
}
lastReading = reading;
}
if (!Objects.equals(ip1, this.ourExternalIpAddress)) { if (consecutiveReadings >= consecutiveReadingsRequired) {
// Latest reading doesn't match our known value // Last 10 readings were the same - i.e. more than one peer agreed on the new IP address...
if (Objects.equals(ip1, ip2) && Objects.equals(ip1, ip3)) { String ip = this.ourExternalIpAddressHistory.get(size - 1);
// Last 3 readings were the same - i.e. more than one peer agreed on the new IP address if (!Objects.equals(ip, this.ourExternalIpAddress)) {
this.ourExternalIpAddress = ip1; // ... and the readings were different to our current recorded value, so
this.onExternalIpUpdate(ip1); // update our external IP address value
this.ourExternalIpAddress = ip;
this.onExternalIpUpdate(ip);
} }
} }
} }