Przeglądaj źródła

Allowing ranged IP Bans

GodKratos 16 lat temu
rodzic
commit
1704f20e81

+ 5 - 0
L2_GameServer/java/banned_ip.cfg

@@ -1,2 +1,7 @@
+# Subnets can be defined like 123.45.0.0
+# Comments after # are ignored
+# Expiration timestamp can be added after IP for when to disable the ban
+# Example:
+# 192.168.0.0 1237618800000  # Bans all address starting with 192.168 and will expire on 21 Mar 2009 07:00:00 GMT
 10.255.255.255
 10.254.254.254

+ 1 - 1
L2_GameServer/java/net/sf/l2j/loginserver/L2LoginServer.java

@@ -288,7 +288,7 @@ public class L2LoginServer
 					if (line.length() > 0 && line.charAt(0) != '#')
 					{
 						// split comments if any
-						parts = line.split("#");
+						parts = line.split("#", 2);
 
 						// discard comments in the line, if any
 						line = parts[0];

+ 16 - 7
L2_GameServer/java/net/sf/l2j/loginserver/LoginController.java

@@ -62,7 +62,7 @@ public class LoginController
 	/** Authed Clients on LoginServer*/
 	protected FastMap<String, L2LoginClient> _loginServerClients = new FastMap<String, L2LoginClient>().setShared(true);
 
-	private Map<InetAddress, BanInfo> _bannedIps = new FastMap<InetAddress, BanInfo>().setShared(true);
+	private Map<String, BanInfo> _bannedIps = new FastMap<String, BanInfo>().setShared(true);
 
 	private Map<InetAddress, FailedLoginAttempt> _hackProtection;
 
@@ -239,7 +239,8 @@ public class LoginController
 	public void addBanForAddress(String address, long expiration) throws UnknownHostException
 	{
 		InetAddress netAddress = InetAddress.getByName(address);
-		_bannedIps.put(netAddress, new BanInfo(netAddress,  expiration));
+		if (!_bannedIps.containsKey(netAddress.getHostAddress()))
+			_bannedIps.put(netAddress.getHostAddress(), new BanInfo(netAddress,  expiration));
 	}
 
 	/**
@@ -250,17 +251,25 @@ public class LoginController
 	 */
 	public void addBanForAddress(InetAddress address, long duration)
 	{
-		_bannedIps.put(address, new BanInfo(address,  System.currentTimeMillis() + duration));
+		if (!_bannedIps.containsKey(address.getHostAddress()))
+			_bannedIps.put(address.getHostAddress(), new BanInfo(address,  System.currentTimeMillis() + duration));
 	}
 
 	public boolean isBannedAddress(InetAddress address)
 	{
-		BanInfo bi = _bannedIps.get(address);
+		String[] parts = address.getHostAddress().split("\\.");
+		BanInfo bi = _bannedIps.get(address.getHostAddress());
+		if (bi == null)
+			bi = _bannedIps.get(parts[0]+"."+parts[1]+"."+parts[2]+".0");
+		if (bi == null)
+			bi = _bannedIps.get(parts[0]+"."+parts[1]+".0.0");
+		if (bi == null)
+			bi = _bannedIps.get(parts[0]+".0.0.0");
 		if (bi != null)
 		{
 			if (bi.hasExpired())
 			{
-				_bannedIps.remove(address);
+				_bannedIps.remove(address.getHostAddress());
 				return false;
 			}
 			else
@@ -271,7 +280,7 @@ public class LoginController
 		return false;
 	}
 
-	public Map<InetAddress, BanInfo> getBannedIps()
+	public Map<String, BanInfo> getBannedIps()
 	{
 		return _bannedIps;
 	}
@@ -284,7 +293,7 @@ public class LoginController
 	 */
 	public boolean removeBanForAddress(InetAddress address)
 	{
-		return _bannedIps.remove(address) != null;
+		return _bannedIps.remove(address.getHostAddress()) != null;
 	}
 
 	/**