|
@@ -27,23 +27,21 @@ import javolution.util.FastMap;
|
|
import com.l2jserver.Config;
|
|
import com.l2jserver.Config;
|
|
|
|
|
|
/**
|
|
/**
|
|
- *
|
|
|
|
* @author -Wooden-
|
|
* @author -Wooden-
|
|
- *
|
|
|
|
*/
|
|
*/
|
|
public abstract class FloodProtectedListener extends Thread
|
|
public abstract class FloodProtectedListener extends Thread
|
|
{
|
|
{
|
|
- private Logger _log = Logger.getLogger(FloodProtectedListener.class.getName());
|
|
|
|
- private Map<String, ForeignConnection> _floodProtection = new FastMap<String, ForeignConnection>();
|
|
|
|
- private String _listenIp;
|
|
|
|
- private int _port;
|
|
|
|
|
|
+ private final Logger _log = Logger.getLogger(FloodProtectedListener.class.getName());
|
|
|
|
+ private final Map<String, ForeignConnection> _floodProtection = new FastMap<String, ForeignConnection>();
|
|
|
|
+ private final String _listenIp;
|
|
|
|
+ private final int _port;
|
|
private ServerSocket _serverSocket;
|
|
private ServerSocket _serverSocket;
|
|
|
|
|
|
public FloodProtectedListener(String listenIp, int port) throws IOException
|
|
public FloodProtectedListener(String listenIp, int port) throws IOException
|
|
{
|
|
{
|
|
_port = port;
|
|
_port = port;
|
|
_listenIp = listenIp;
|
|
_listenIp = listenIp;
|
|
- if(_listenIp.equals("*"))
|
|
|
|
|
|
+ if (_listenIp.equals("*"))
|
|
{
|
|
{
|
|
_serverSocket = new ServerSocket(_port);
|
|
_serverSocket = new ServerSocket(_port);
|
|
}
|
|
}
|
|
@@ -63,46 +61,58 @@ public abstract class FloodProtectedListener extends Thread
|
|
try
|
|
try
|
|
{
|
|
{
|
|
connection = _serverSocket.accept();
|
|
connection = _serverSocket.accept();
|
|
- if(Config.FLOOD_PROTECTION)
|
|
|
|
|
|
+ if (Config.FLOOD_PROTECTION)
|
|
{
|
|
{
|
|
ForeignConnection fConnection = _floodProtection.get(connection.getInetAddress().getHostAddress());
|
|
ForeignConnection fConnection = _floodProtection.get(connection.getInetAddress().getHostAddress());
|
|
- if(fConnection != null)
|
|
|
|
|
|
+ if (fConnection != null)
|
|
{
|
|
{
|
|
fConnection.connectionNumber += 1;
|
|
fConnection.connectionNumber += 1;
|
|
- if( (fConnection.connectionNumber > Config.FAST_CONNECTION_LIMIT
|
|
|
|
- && (System.currentTimeMillis() - fConnection.lastConnection) < Config.NORMAL_CONNECTION_TIME)
|
|
|
|
- || (System.currentTimeMillis() - fConnection.lastConnection) < Config.FAST_CONNECTION_TIME
|
|
|
|
- || fConnection.connectionNumber > Config.MAX_CONNECTION_PER_IP)
|
|
|
|
|
|
+ if (((fConnection.connectionNumber > Config.FAST_CONNECTION_LIMIT) && ((System.currentTimeMillis() - fConnection.lastConnection) < Config.NORMAL_CONNECTION_TIME)) || ((System.currentTimeMillis() - fConnection.lastConnection) < Config.FAST_CONNECTION_TIME) || (fConnection.connectionNumber > Config.MAX_CONNECTION_PER_IP))
|
|
{
|
|
{
|
|
fConnection.lastConnection = System.currentTimeMillis();
|
|
fConnection.lastConnection = System.currentTimeMillis();
|
|
connection.close();
|
|
connection.close();
|
|
fConnection.connectionNumber -= 1;
|
|
fConnection.connectionNumber -= 1;
|
|
- if(!fConnection.isFlooding)_log.warning("Potential Flood from "+connection.getInetAddress().getHostAddress());
|
|
|
|
|
|
+ if (!fConnection.isFlooding)
|
|
|
|
+ {
|
|
|
|
+ _log.warning("Potential Flood from " + connection.getInetAddress().getHostAddress());
|
|
|
|
+ }
|
|
fConnection.isFlooding = true;
|
|
fConnection.isFlooding = true;
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
- if(fConnection.isFlooding) //if connection was flooding server but now passed the check
|
|
|
|
|
|
+ if (fConnection.isFlooding) // if connection was flooding server but now passed the check
|
|
{
|
|
{
|
|
fConnection.isFlooding = false;
|
|
fConnection.isFlooding = false;
|
|
- _log.info(connection.getInetAddress().getHostAddress()+" is not considered as flooding anymore.");
|
|
|
|
|
|
+ _log.info(connection.getInetAddress().getHostAddress() + " is not considered as flooding anymore.");
|
|
}
|
|
}
|
|
fConnection.lastConnection = System.currentTimeMillis();
|
|
fConnection.lastConnection = System.currentTimeMillis();
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
fConnection = new ForeignConnection(System.currentTimeMillis());
|
|
fConnection = new ForeignConnection(System.currentTimeMillis());
|
|
- _floodProtection.put(connection.getInetAddress().getHostAddress(),fConnection);
|
|
|
|
|
|
+ _floodProtection.put(connection.getInetAddress().getHostAddress(), fConnection);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
addClient(connection);
|
|
addClient(connection);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
catch (Exception e)
|
|
{
|
|
{
|
|
- try { connection.close(); } catch (Exception e2) {}
|
|
|
|
- if (this.isInterrupted())
|
|
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ if (connection != null)
|
|
|
|
+ {
|
|
|
|
+ connection.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ catch (Exception e2)
|
|
|
|
+ {
|
|
|
|
+ }
|
|
|
|
+ if (isInterrupted())
|
|
{
|
|
{
|
|
// shutdown?
|
|
// shutdown?
|
|
- try { _serverSocket.close();}
|
|
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ _serverSocket.close();
|
|
|
|
+ }
|
|
catch (IOException io)
|
|
catch (IOException io)
|
|
{
|
|
{
|
|
_log.log(Level.INFO, "", io);
|
|
_log.log(Level.INFO, "", io);
|
|
@@ -133,10 +143,12 @@ public abstract class FloodProtectedListener extends Thread
|
|
|
|
|
|
public void removeFloodProtection(String ip)
|
|
public void removeFloodProtection(String ip)
|
|
{
|
|
{
|
|
- if(!Config.FLOOD_PROTECTION)
|
|
|
|
|
|
+ if (!Config.FLOOD_PROTECTION)
|
|
|
|
+ {
|
|
return;
|
|
return;
|
|
|
|
+ }
|
|
ForeignConnection fConnection = _floodProtection.get(ip);
|
|
ForeignConnection fConnection = _floodProtection.get(ip);
|
|
- if(fConnection != null)
|
|
|
|
|
|
+ if (fConnection != null)
|
|
{
|
|
{
|
|
fConnection.connectionNumber -= 1;
|
|
fConnection.connectionNumber -= 1;
|
|
if (fConnection.connectionNumber == 0)
|
|
if (fConnection.connectionNumber == 0)
|
|
@@ -146,7 +158,7 @@ public abstract class FloodProtectedListener extends Thread
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- _log.warning("Removing a flood protection for a GameServer that was not in the connection map??? :"+ip);
|
|
|
|
|
|
+ _log.warning("Removing a flood protection for a GameServer that was not in the connection map??? :" + ip);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|