Fixes IPv6 anonymisation and saving #355

This commit is contained in:
Rsl1122 2018-06-07 11:05:28 +03:00
parent 9237ae3ef0
commit a08d41f022
9 changed files with 67 additions and 28 deletions

View File

@ -9,6 +9,7 @@ import com.djrapitops.plan.utilities.SHA256Hash;
import com.google.common.base.Objects;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.security.NoSuchAlgorithmException;
/**
@ -23,9 +24,9 @@ public class GeoInfo {
private final String ipHash;
private final long lastUsed;
public GeoInfo(String ip, String geolocation, long lastUsed)
public GeoInfo(InetAddress address, String geolocation, long lastUsed)
throws UnsupportedEncodingException, NoSuchAlgorithmException {
this(FormatUtils.formatIP(ip), geolocation, lastUsed, new SHA256Hash(ip).create());
this(FormatUtils.formatIP(address), geolocation, lastUsed, new SHA256Hash(address.getHostAddress()).create());
}
public GeoInfo(String ip, String geolocation, long lastUsed, String ipHash) {

View File

@ -19,6 +19,8 @@ import com.djrapitops.plugin.task.RunnableFactory;
import com.djrapitops.plugin.utilities.Verify;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -99,7 +101,7 @@ public class GeoInfoTable extends UserIDTable {
continue;
}
GeoInfo updatedInfo = new GeoInfo(
geoInfo.getIp(),
InetAddress.getByName(geoInfo.getIp()),
geoInfo.getGeolocation(),
geoInfo.getLastUsed()
);
@ -107,7 +109,7 @@ public class GeoInfoTable extends UserIDTable {
statement.setString(2, updatedInfo.getIpHash());
statement.setString(3, geoInfo.getIp());
statement.addBatch();
} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
} catch (UnknownHostException | UnsupportedEncodingException | NoSuchAlgorithmException e) {
if (Settings.DEV_MODE.isTrue()) {
Log.toLog(this.getClass(), e);
}

View File

@ -18,6 +18,7 @@ import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.net.InetAddress;
import java.util.UUID;
/**
@ -89,7 +90,7 @@ public class PlayerOnlineListener implements Listener {
String world = player.getWorld().getName();
String gm = player.getGameMode().name();
String ip = player.getAddress().getAddress().getHostAddress();
InetAddress address = player.getAddress().getAddress();
String playerName = player.getName();
String displayName = player.getDisplayName();
@ -100,7 +101,7 @@ public class PlayerOnlineListener implements Listener {
Processing.submit(
new RegisterProcessor(uuid, player.getFirstPlayed(), time, playerName, playersOnline,
new IPUpdateProcessor(uuid, ip, time),
new IPUpdateProcessor(uuid, address, time),
new NameProcessor(uuid, playerName, displayName),
new PlayerPageUpdateProcessor(uuid)
)

View File

@ -13,6 +13,7 @@ import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import java.net.InetAddress;
import java.util.UUID;
/**
@ -28,11 +29,11 @@ public class PlayerOnlineListener implements Listener {
ProxiedPlayer player = event.getPlayer();
UUID uuid = player.getUniqueId();
String name = player.getName();
String ip = player.getAddress().getAddress().getHostAddress();
InetAddress address = player.getAddress().getAddress();
long now = System.currentTimeMillis();
Processing.submit(new BungeePlayerRegisterProcessor(uuid, name, now,
new IPUpdateProcessor(uuid, ip, now))
new IPUpdateProcessor(uuid, address, now))
);
} catch (Exception e) {
Log.toLog(this.getClass(), e);

View File

@ -21,6 +21,7 @@ import org.spongepowered.api.profile.GameProfile;
import org.spongepowered.api.service.ProviderRegistration;
import org.spongepowered.api.service.ban.BanService;
import java.net.InetAddress;
import java.util.Optional;
import java.util.UUID;
@ -92,7 +93,7 @@ public class SpongePlayerListener {
gm = gameMode.get().getName().toUpperCase();
}
String ip = player.getConnection().getAddress().getAddress().getHostAddress();
InetAddress address = player.getConnection().getAddress().getAddress();
String playerName = player.getName();
String displayName = player.getDisplayNameData().displayName().get().toPlain();
@ -103,7 +104,7 @@ public class SpongePlayerListener {
Processing.submit(
new RegisterProcessor(uuid, time, time, playerName, playersOnline,
new IPUpdateProcessor(uuid, ip, time),
new IPUpdateProcessor(uuid, address, time),
new NameProcessor(uuid, playerName, displayName),
new PlayerPageUpdateProcessor(uuid)
)

View File

@ -17,6 +17,7 @@ import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.processing.importing.ServerImportData;
import com.djrapitops.plan.system.processing.importing.UserImportData;
import com.djrapitops.plan.system.processing.importing.UserImportRefiner;
import com.djrapitops.plan.utilities.SHA256Hash;
import com.djrapitops.plugin.api.Benchmark;
import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.utilities.Verify;
@ -230,7 +231,7 @@ public abstract class Importer {
.map(ip -> {
String geoLoc = GeolocationCache.getCountry(ip);
try {
return new GeoInfo(ip, geoLoc, date);
return new GeoInfo(ip, geoLoc, date, new SHA256Hash(ip).create());
} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
throw new IllegalArgumentException(e);
}

View File

@ -13,6 +13,7 @@ import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plugin.api.utility.log.Log;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
@ -24,10 +25,10 @@ import java.util.UUID;
public class IPUpdateProcessor implements CriticalRunnable {
private final UUID uuid;
private final String ip;
private final InetAddress ip;
private final long time;
public IPUpdateProcessor(UUID uuid, String ip, long time) {
public IPUpdateProcessor(UUID uuid, InetAddress ip, long time) {
this.uuid = uuid;
this.ip = ip;
this.time = time;
@ -36,7 +37,7 @@ public class IPUpdateProcessor implements CriticalRunnable {
@Override
public void run() {
if (Settings.DATA_GEOLOCATIONS.isTrue()) {
String country = GeolocationCache.getCountry(ip);
String country = GeolocationCache.getCountry(ip.getHostAddress());
try {
GeoInfo geoInfo = new GeoInfo(ip, country, time);
Database.getActive().save().geoInfo(uuid, geoInfo);

View File

@ -4,6 +4,8 @@ import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plugin.api.TimeAmount;
import org.apache.commons.lang3.StringUtils;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -213,23 +215,40 @@ public class FormatUtils {
return df.format(d);
}
public static String formatIP(String ip) {
public static String formatIP(InetAddress address) {
String ip = address.getHostAddress();
if ("localhost".equals(ip)) {
return ip;
}
StringBuilder b = new StringBuilder();
int i = 0;
for (String part : ip.split("\\.")) {
if (i >= 2) {
break;
if (address instanceof Inet6Address) {
StringBuilder b = new StringBuilder();
int i = 0;
for (String part : ip.split(":")) {
if (i >= 3) {
break;
}
b.append(part).append(':');
i++;
}
b.append(part).append('.');
return b.append("xx..").toString();
} else {
StringBuilder b = new StringBuilder();
int i = 0;
for (String part : ip.split("\\.")) {
if (i >= 2) {
break;
}
i++;
b.append(part).append('.');
i++;
}
return b.append("xx.xx").toString();
}
return b.append("xx.xx").toString();
}
/**

View File

@ -11,6 +11,9 @@ import utilities.RandomData;
import utilities.Teardown;
import utilities.mocks.SystemMockUtil;
import java.net.InetAddress;
import java.net.UnknownHostException;
import static org.junit.Assert.*;
/**
@ -77,10 +80,10 @@ public class FormatUtilsTest {
}
@Test
public void testFormatIP() {
String ip = "1.2.3.4";
String ip2 = "1.2.3.26";
String ip3 = "1.2.3.235";
public void testFormatIP() throws UnknownHostException {
InetAddress ip = InetAddress.getByName("1.2.3.4");
InetAddress ip2 = InetAddress.getByName("1.2.3.26");
InetAddress ip3 = InetAddress.getByName("1.2.3.235");
String expected = "1.2.xx.xx";
assertEquals(expected, FormatUtils.formatIP(ip));
@ -88,4 +91,13 @@ public class FormatUtilsTest {
assertEquals(expected, FormatUtils.formatIP(ip3));
}
@Test
public void testFormatIPv6() throws UnknownHostException {
InetAddress ip = InetAddress.getByName("1234:1234:1234:1234:1234:1234:1234:1234%0");
String expected = "1234:1234:1234:xx..";
assertEquals(expected, FormatUtils.formatIP(ip));
}
}