Added standard method for matching partial and *'d IPs (fuzzy ip matching).

This commit is contained in:
Steven Lawson 2012-11-22 21:32:55 -05:00
parent ed82272f5c
commit 502b055265
3 changed files with 90 additions and 73 deletions

View File

@ -680,33 +680,7 @@ public class TFM_PlayerListener implements Listener
break;
}
String[] test_ip_parts = test_ip.split("\\.");
String[] player_ip_parts = player_ip.split("\\.");
boolean is_match = false;
for (int i = 0; i < test_ip_parts.length && i < player_ip_parts.length; i++)
{
if (test_ip_parts[i].equals("*") && i >= 2)
{
is_match = true;
}
else if (test_ip_parts[i].equals(player_ip_parts[i]))
{
is_match = true;
}
else
{
is_match = false;
}
if (!is_match)
{
break;
}
}
if (is_match)
if (TFM_Util.fuzzyIpMatch(test_ip, player_ip, 4))
{
ban_entry = (BanEntry) banByIP.getEntries().get(test_ip);
is_ip_banned = true;
@ -741,9 +715,7 @@ public class TFM_PlayerListener implements Listener
for (String test_ip : TotalFreedomMod.permbanned_ips)
{
//TODO: Add support for wildcards in permbanned_ips list.
//TODO: Create generic wildcard IP matching method since we do this several times already in this project.
if (test_ip.equalsIgnoreCase(player_ip))
if (TFM_Util.fuzzyIpMatch(test_ip, player_ip, 4))
{
event.disallow(PlayerLoginEvent.Result.KICK_BANNED, ChatColor.RED + "Your IP address is permanently banned from this server.");
return;

View File

@ -264,23 +264,13 @@ public class TFM_SuperadminList
}
else
{
String[] user_octets = user_ip.split("\\.");
if (user_octets.length != 4)
{
return false;
}
String match_ip = null;
for (String test_ip : getSuperadminIPs())
{
String[] test_octets = test_ip.split("\\.");
if (test_octets.length == 4)
if (TFM_Util.fuzzyIpMatch(user_ip, test_ip, 3))
{
if (user_octets[0].equals(test_octets[0]) && user_octets[1].equals(test_octets[1]) && user_octets[2].equals(test_octets[2]))
{
match_ip = test_ip;
break;
}
match_ip = test_ip;
break;
}
}
@ -327,27 +317,34 @@ public class TFM_SuperadminList
public static void addSuperadmin(String admin_name, List<String> ips)
{
admin_name = admin_name.toLowerCase();
if (superadminList.containsKey(admin_name))
try
{
TFM_Superadmin superadmin = superadminList.get(admin_name);
superadmin.setActivated(true);
superadmin.getIps().addAll(ips);
superadmin.setLastLogin(new Date());
admin_name = admin_name.toLowerCase();
if (superadminList.containsKey(admin_name))
{
TFM_Superadmin superadmin = superadminList.get(admin_name);
superadmin.setActivated(true);
superadmin.getIps().addAll(ips);
superadmin.setLastLogin(new Date());
}
else
{
Date last_login = new Date();
String custom_login_message = "";
boolean is_senior_admin = false;
List<String> console_aliases = new ArrayList<String>();
TFM_Superadmin superadmin = new TFM_Superadmin(admin_name, ips, last_login, custom_login_message, is_senior_admin, console_aliases, true);
superadminList.put(admin_name.toLowerCase(), superadmin);
}
saveSuperadminList();
}
else
catch (Exception ex)
{
Date last_login = new Date();
String custom_login_message = "";
boolean is_senior_admin = false;
List<String> console_aliases = new ArrayList<String>();
TFM_Superadmin superadmin = new TFM_Superadmin(admin_name, ips, last_login, custom_login_message, is_senior_admin, console_aliases, true);
superadminList.put(admin_name.toLowerCase(), superadmin);
TFM_Log.severe(ex);
}
saveSuperadminList();
}
public static void addSuperadmin(Player p)
@ -365,13 +362,20 @@ public class TFM_SuperadminList
public static void removeSuperadmin(String admin_name)
{
admin_name = admin_name.toLowerCase();
if (superadminList.containsKey(admin_name))
try
{
TFM_Superadmin superadmin = superadminList.get(admin_name);
superadmin.setActivated(false);
saveSuperadminList();
admin_name = admin_name.toLowerCase();
if (superadminList.containsKey(admin_name))
{
TFM_Superadmin superadmin = superadminList.get(admin_name);
superadmin.setActivated(false);
saveSuperadminList();
}
}
catch (Exception ex)
{
TFM_Log.severe(ex);
}
}

View File

@ -888,7 +888,6 @@ public class TFM_Util
}
}
}
public static String DATE_STORAGE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z";
public static String dateToString(Date date)
@ -913,13 +912,55 @@ public class TFM_Util
return restricted_senders.contains(sender_name.toLowerCase());
}
public static List<String> removeDuplicates(List<String> list)
public static List<String> removeDuplicates(List<String> old_list)
{
HashSet<String> hash = new HashSet<String>();
hash.addAll(list);
list.clear();
list.addAll(hash);
return list;
List<String> new_list = new ArrayList<String>();
for (String entry : old_list)
{
if (!new_list.contains(entry))
{
new_list.add(entry);
}
}
return new_list;
}
public static boolean fuzzyIpMatch(String a, String b, int required_octets)
{
boolean is_match = true;
String[] a_parts = StringUtils.split(a, '.');
String[] b_parts = StringUtils.split(b, '.');
if (a_parts.length != 4 || b_parts.length != 4)
{
return false;
}
if (required_octets > 4)
{
required_octets = 4;
}
else if (required_octets < 1)
{
required_octets = 1;
}
for (int i = 0; i < required_octets && i < 4; i++)
{
if (a_parts[i].equals("*") || b_parts[i].equals("*"))
{
continue;
}
if (!a_parts[i].equals(b_parts[i]))
{
is_match = false;
break;
}
}
return is_match;
}
// I wrote all this before i discovered getTargetBlock >.> - might come in handy some day...
// public static final double LOOKAT_VIEW_HEIGHT = 1.65;