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

View File

@ -264,25 +264,15 @@ public class TFM_SuperadminList
} }
else else
{ {
String[] user_octets = user_ip.split("\\.");
if (user_octets.length != 4)
{
return false;
}
String match_ip = null; String match_ip = null;
for (String test_ip : getSuperadminIPs()) for (String test_ip : getSuperadminIPs())
{ {
String[] test_octets = test_ip.split("\\."); if (TFM_Util.fuzzyIpMatch(user_ip, test_ip, 3))
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]))
{ {
match_ip = test_ip; match_ip = test_ip;
break; break;
} }
} }
}
if (match_ip != null) if (match_ip != null)
{ {
@ -326,6 +316,8 @@ public class TFM_SuperadminList
} }
public static void addSuperadmin(String admin_name, List<String> ips) public static void addSuperadmin(String admin_name, List<String> ips)
{
try
{ {
admin_name = admin_name.toLowerCase(); admin_name = admin_name.toLowerCase();
@ -349,6 +341,11 @@ public class TFM_SuperadminList
saveSuperadminList(); saveSuperadminList();
} }
catch (Exception ex)
{
TFM_Log.severe(ex);
}
}
public static void addSuperadmin(Player p) public static void addSuperadmin(Player p)
{ {
@ -364,6 +361,8 @@ public class TFM_SuperadminList
} }
public static void removeSuperadmin(String admin_name) public static void removeSuperadmin(String admin_name)
{
try
{ {
admin_name = admin_name.toLowerCase(); admin_name = admin_name.toLowerCase();
@ -374,6 +373,11 @@ public class TFM_SuperadminList
saveSuperadminList(); saveSuperadminList();
} }
} }
catch (Exception ex)
{
TFM_Log.severe(ex);
}
}
public static void removeSuperadmin(Player p) 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 DATE_STORAGE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z";
public static String dateToString(Date date) public static String dateToString(Date date)
@ -913,13 +912,55 @@ public class TFM_Util
return restricted_senders.contains(sender_name.toLowerCase()); 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>(); List<String> new_list = new ArrayList<String>();
hash.addAll(list); for (String entry : old_list)
list.clear(); {
list.addAll(hash); if (!new_list.contains(entry))
return list; {
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... // I wrote all this before i discovered getTargetBlock >.> - might come in handy some day...
// public static final double LOOKAT_VIEW_HEIGHT = 1.65; // public static final double LOOKAT_VIEW_HEIGHT = 1.65;