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,25 +264,15 @@ 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 (user_octets[0].equals(test_octets[0]) && user_octets[1].equals(test_octets[1]) && user_octets[2].equals(test_octets[2]))
if (TFM_Util.fuzzyIpMatch(user_ip, test_ip, 3))
{
match_ip = test_ip;
break;
}
}
}
if (match_ip != null)
{
@ -326,6 +316,8 @@ public class TFM_SuperadminList
}
public static void addSuperadmin(String admin_name, List<String> ips)
{
try
{
admin_name = admin_name.toLowerCase();
@ -349,6 +341,11 @@ public class TFM_SuperadminList
saveSuperadminList();
}
catch (Exception ex)
{
TFM_Log.severe(ex);
}
}
public static void addSuperadmin(Player p)
{
@ -364,6 +361,8 @@ public class TFM_SuperadminList
}
public static void removeSuperadmin(String admin_name)
{
try
{
admin_name = admin_name.toLowerCase();
@ -374,6 +373,11 @@ public class TFM_SuperadminList
saveSuperadminList();
}
}
catch (Exception ex)
{
TFM_Log.severe(ex);
}
}
public static void removeSuperadmin(Player p)
{

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;