Removal of IPv4Filter class.

This commit is contained in:
MobiusDevelopment 2021-02-23 05:04:57 +00:00
parent 82c816ccbf
commit 18216eecf6
29 changed files with 58 additions and 3187 deletions

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -46,7 +46,6 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
private final IPacketHandler<T> _packetHandler;
private final IMMOExecutor<T> _executor;
private final IClientFactory<T> _clientFactory;
private final IAcceptFilter _acceptFilter;
// Configurations
private final int HELPER_BUFFER_SIZE;
private final int HELPER_BUFFER_COUNT;
@ -66,7 +65,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
private boolean _shutdown;
public SelectorThread(SelectorConfig sc, IMMOExecutor<T> executor, IPacketHandler<T> packetHandler, IClientFactory<T> clientFactory, IAcceptFilter acceptFilter) throws IOException
public SelectorThread(SelectorConfig sc, IMMOExecutor<T> executor, IPacketHandler<T> packetHandler, IClientFactory<T> clientFactory) throws IOException
{
super.setName("SelectorThread-" + super.getId());
@ -91,7 +90,6 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
_bufferPool.addLast(ByteBuffer.wrap(new byte[HELPER_BUFFER_SIZE]).order(BYTE_ORDER));
}
_acceptFilter = acceptFilter;
_packetHandler = packetHandler;
_clientFactory = clientFactory;
_executor = executor;
@ -116,7 +114,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
selectable.register(_selector, SelectionKey.OP_ACCEPT);
}
final ByteBuffer getPooledBuffer()
protected ByteBuffer getPooledBuffer()
{
if (_bufferPool.isEmpty())
{
@ -126,7 +124,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
return _bufferPool.removeFirst();
}
final void recycleBuffer(ByteBuffer buf)
protected void recycleBuffer(ByteBuffer buf)
{
if (_bufferPool.size() < HELPER_BUFFER_COUNT)
{
@ -244,36 +242,29 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
}
@SuppressWarnings("all")
private final void acceptConnection(SelectionKey key, MMOConnection<T> con)
private void acceptConnection(SelectionKey key, MMOConnection<T> con)
{
final ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
SocketChannel sc;
SocketChannel sc = null;
try
{
while ((sc = ssc.accept()) != null)
{
if ((_acceptFilter == null) || _acceptFilter.accept(sc))
{
sc.configureBlocking(false);
final SelectionKey clientKey = sc.register(_selector, SelectionKey.OP_READ);
con = new MMOConnection<>(this, sc.socket(), clientKey);
con.setClient(_clientFactory.create(con));
clientKey.attach(con);
}
else
{
sc.socket().close();
}
sc.configureBlocking(false);
final SelectionKey clientKey = sc.register(_selector, SelectionKey.OP_READ);
con = new MMOConnection<>(this, sc.socket(), clientKey);
con.setClient(_clientFactory.create(con));
clientKey.attach(con);
}
}
catch (IOException e)
catch (Exception e)
{
e.printStackTrace();
}
}
private final void readPacket(SelectionKey key, MMOConnection<T> con)
private void readPacket(SelectionKey key, MMOConnection<T> con)
{
if (con.isClosed())
{
@ -354,7 +345,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
}
}
private final boolean tryReadPacket(SelectionKey key, T client, ByteBuffer buf, MMOConnection<T> con)
private boolean tryReadPacket(SelectionKey key, T client, ByteBuffer buf, MMOConnection<T> con)
{
switch (buf.remaining())
{
@ -434,7 +425,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
}
}
private final void allocateReadBuffer(MMOConnection<T> con)
private void allocateReadBuffer(MMOConnection<T> con)
{
con.setReadBuffer(getPooledBuffer().put(READ_BUFFER));
READ_BUFFER.clear();
@ -467,7 +458,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
}
}
private final void writeClosePacket(MMOConnection<T> con)
private void writeClosePacket(MMOConnection<T> con)
{
SendablePacket<T> sp;
synchronized (con.getSendQueue())
@ -497,7 +488,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
}
}
protected final void writePacket(SelectionKey key, MMOConnection<T> con)
protected void writePacket(SelectionKey key, MMOConnection<T> con)
{
if (!prepareWriteBuffer(con))
{
@ -548,7 +539,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
}
}
private final boolean prepareWriteBuffer(MMOConnection<T> con)
private boolean prepareWriteBuffer(MMOConnection<T> con)
{
boolean hasPending = false;
DIRECT_WRITE_BUFFER.clear();
@ -603,7 +594,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
return hasPending;
}
private final void putPacketIntoWriteBuffer(T client, SendablePacket<T> sp)
private void putPacketIntoWriteBuffer(T client, SendablePacket<T> sp)
{
WRITE_BUFFER.clear();
@ -633,7 +624,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
WRITE_BUFFER.position(dataPos + dataSize);
}
final void closeConnection(MMOConnection<T> con)
protected void closeConnection(MMOConnection<T> con)
{
synchronized (_pendingClose)
{
@ -641,7 +632,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
}
}
private final void closeConnectionImpl(SelectionKey key, MMOConnection<T> con)
private void closeConnectionImpl(SelectionKey key, MMOConnection<T> con)
{
try
{

View File

@ -1,141 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.commons.util;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import org.l2jmobius.commons.mmocore.IAcceptFilter;
/**
* Formatted Forsaiken's IPv4 filter [DrHouse]
* @author Forsaiken
*/
public class IPv4Filter implements IAcceptFilter, Runnable
{
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this);
t.setName(getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static final int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
@Override
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -35,7 +35,6 @@ import org.l2jmobius.commons.mmocore.SelectorConfig;
import org.l2jmobius.commons.mmocore.SelectorThread;
import org.l2jmobius.commons.util.Chronos;
import org.l2jmobius.commons.util.DeadlockDetector;
import org.l2jmobius.commons.util.IPv4Filter;
import org.l2jmobius.commons.util.Util;
import org.l2jmobius.gameserver.cache.CrestCache;
import org.l2jmobius.gameserver.cache.HtmCache;
@ -512,7 +511,7 @@ public class GameServer
_gamePacketHandler = new GamePacketHandler();
_selectorThread = new SelectorThread<>(sc, _gamePacketHandler, _gamePacketHandler, _gamePacketHandler, new IPv4Filter());
_selectorThread = new SelectorThread<>(sc, _gamePacketHandler, _gamePacketHandler, _gamePacketHandler);
InetAddress bindAddress = null;
if (!Config.GAMESERVER_HOSTNAME.equals("*"))

View File

@ -76,7 +76,7 @@ public class RequestAuthLogin extends ClientBasePacket
final LoginController login = LoginController.getInstance();
// ip BANNED due to entering wrong password many times
// IP banned due to entering wrong password many times.
if (login.isBannedAddress(getClient().getSocket().getInetAddress().getHostAddress()))
{
getClient().sendPacket(new AccountKicked(AccountKicked.REASON_ILLEGAL_USE));
@ -90,7 +90,6 @@ public class RequestAuthLogin extends ClientBasePacket
}
// Account BANNED (must always be checked after isLoginValid)
if (getClient().getAccessLevel() < 0)
{
getClient().sendPacket(new AccountKicked(AccountKicked.REASON_ILLEGAL_USE));

View File

@ -46,7 +46,6 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
private final IPacketHandler<T> _packetHandler;
private final IMMOExecutor<T> _executor;
private final IClientFactory<T> _clientFactory;
private final IAcceptFilter _acceptFilter;
// Configurations
private final int HELPER_BUFFER_SIZE;
private final int HELPER_BUFFER_COUNT;
@ -66,7 +65,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
private boolean _shutdown;
public SelectorThread(SelectorConfig sc, IMMOExecutor<T> executor, IPacketHandler<T> packetHandler, IClientFactory<T> clientFactory, IAcceptFilter acceptFilter) throws IOException
public SelectorThread(SelectorConfig sc, IMMOExecutor<T> executor, IPacketHandler<T> packetHandler, IClientFactory<T> clientFactory) throws IOException
{
super.setName("SelectorThread-" + super.getId());
@ -91,7 +90,6 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
_bufferPool.addLast(ByteBuffer.wrap(new byte[HELPER_BUFFER_SIZE]).order(BYTE_ORDER));
}
_acceptFilter = acceptFilter;
_packetHandler = packetHandler;
_clientFactory = clientFactory;
_executor = executor;
@ -116,7 +114,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
selectable.register(_selector, SelectionKey.OP_ACCEPT);
}
final ByteBuffer getPooledBuffer()
protected ByteBuffer getPooledBuffer()
{
if (_bufferPool.isEmpty())
{
@ -126,7 +124,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
return _bufferPool.removeFirst();
}
final void recycleBuffer(ByteBuffer buf)
protected void recycleBuffer(ByteBuffer buf)
{
if (_bufferPool.size() < HELPER_BUFFER_COUNT)
{
@ -244,36 +242,29 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
}
@SuppressWarnings("all")
private final void acceptConnection(SelectionKey key, MMOConnection<T> con)
private void acceptConnection(SelectionKey key, MMOConnection<T> con)
{
final ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
SocketChannel sc;
SocketChannel sc = null;
try
{
while ((sc = ssc.accept()) != null)
{
if ((_acceptFilter == null) || _acceptFilter.accept(sc))
{
sc.configureBlocking(false);
final SelectionKey clientKey = sc.register(_selector, SelectionKey.OP_READ);
con = new MMOConnection<>(this, sc.socket(), clientKey);
con.setClient(_clientFactory.create(con));
clientKey.attach(con);
}
else
{
sc.socket().close();
}
sc.configureBlocking(false);
final SelectionKey clientKey = sc.register(_selector, SelectionKey.OP_READ);
con = new MMOConnection<>(this, sc.socket(), clientKey);
con.setClient(_clientFactory.create(con));
clientKey.attach(con);
}
}
catch (IOException e)
catch (Exception e)
{
e.printStackTrace();
}
}
private final void readPacket(SelectionKey key, MMOConnection<T> con)
private void readPacket(SelectionKey key, MMOConnection<T> con)
{
if (con.isClosed())
{
@ -354,7 +345,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
}
}
private final boolean tryReadPacket(SelectionKey key, T client, ByteBuffer buf, MMOConnection<T> con)
private boolean tryReadPacket(SelectionKey key, T client, ByteBuffer buf, MMOConnection<T> con)
{
switch (buf.remaining())
{
@ -434,7 +425,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
}
}
private final void allocateReadBuffer(MMOConnection<T> con)
private void allocateReadBuffer(MMOConnection<T> con)
{
con.setReadBuffer(getPooledBuffer().put(READ_BUFFER));
READ_BUFFER.clear();
@ -467,7 +458,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
}
}
private final void writeClosePacket(MMOConnection<T> con)
private void writeClosePacket(MMOConnection<T> con)
{
SendablePacket<T> sp;
synchronized (con.getSendQueue())
@ -497,7 +488,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
}
}
protected final void writePacket(SelectionKey key, MMOConnection<T> con)
protected void writePacket(SelectionKey key, MMOConnection<T> con)
{
if (!prepareWriteBuffer(con))
{
@ -548,7 +539,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
}
}
private final boolean prepareWriteBuffer(MMOConnection<T> con)
private boolean prepareWriteBuffer(MMOConnection<T> con)
{
boolean hasPending = false;
DIRECT_WRITE_BUFFER.clear();
@ -603,7 +594,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
return hasPending;
}
private final void putPacketIntoWriteBuffer(T client, SendablePacket<T> sp)
private void putPacketIntoWriteBuffer(T client, SendablePacket<T> sp)
{
WRITE_BUFFER.clear();
@ -633,7 +624,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
WRITE_BUFFER.position(dataPos + dataSize);
}
final void closeConnection(MMOConnection<T> con)
protected void closeConnection(MMOConnection<T> con)
{
synchronized (_pendingClose)
{
@ -641,7 +632,7 @@ public class SelectorThread<T extends MMOClient<?>>extends Thread
}
}
private final void closeConnectionImpl(SelectionKey key, MMOConnection<T> con)
private void closeConnectionImpl(SelectionKey key, MMOConnection<T> con)
{
try
{

View File

@ -1,141 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.commons.util;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import org.l2jmobius.commons.mmocore.IAcceptFilter;
/**
* Formatted Forsaiken's IPv4 filter [DrHouse]
* @author Forsaiken
*/
public class IPv4Filter implements IAcceptFilter, Runnable
{
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this);
t.setName(getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static final int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
@Override
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -35,7 +35,6 @@ import org.l2jmobius.commons.mmocore.SelectorConfig;
import org.l2jmobius.commons.mmocore.SelectorThread;
import org.l2jmobius.commons.util.Chronos;
import org.l2jmobius.commons.util.DeadlockDetector;
import org.l2jmobius.commons.util.IPv4Filter;
import org.l2jmobius.commons.util.Util;
import org.l2jmobius.gameserver.cache.CrestCache;
import org.l2jmobius.gameserver.cache.HtmCache;
@ -524,7 +523,7 @@ public class GameServer
_gamePacketHandler = new GamePacketHandler();
_selectorThread = new SelectorThread<>(sc, _gamePacketHandler, _gamePacketHandler, _gamePacketHandler, new IPv4Filter());
_selectorThread = new SelectorThread<>(sc, _gamePacketHandler, _gamePacketHandler, _gamePacketHandler);
InetAddress bindAddress = null;
if (!Config.GAMESERVER_HOSTNAME.equals("*"))

View File

@ -139,7 +139,7 @@ public class LoginServer
final SelectorHelper sh = new SelectorHelper();
try
{
_selectorThread = new SelectorThread<>(sc, sh, lph, sh, sh);
_selectorThread = new SelectorThread<>(sc, sh, lph, sh);
}
catch (IOException e)
{

View File

@ -26,7 +26,6 @@ import org.l2jmobius.commons.mmocore.IClientFactory;
import org.l2jmobius.commons.mmocore.IMMOExecutor;
import org.l2jmobius.commons.mmocore.MMOConnection;
import org.l2jmobius.commons.mmocore.ReceivablePacket;
import org.l2jmobius.commons.util.IPv4Filter;
import org.l2jmobius.loginserver.network.serverpackets.Init;
/**
@ -35,12 +34,10 @@ import org.l2jmobius.loginserver.network.serverpackets.Init;
public class SelectorHelper implements IMMOExecutor<LoginClient>, IClientFactory<LoginClient>, IAcceptFilter
{
private final ThreadPoolExecutor _generalPacketsThreadPool;
private final IPv4Filter _ipv4filter;
public SelectorHelper()
{
_generalPacketsThreadPool = new ThreadPoolExecutor(4, 6, 15L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
_ipv4filter = new IPv4Filter();
}
@Override
@ -60,7 +57,6 @@ public class SelectorHelper implements IMMOExecutor<LoginClient>, IClientFactory
@Override
public boolean accept(SocketChannel sc)
{
// return !LoginController.getInstance().isBannedAddress(sc.socket().getInetAddress());
return _ipv4filter.accept(sc) && !LoginController.getInstance().isBannedAddress(sc.socket().getInetAddress());
return !LoginController.getInstance().isBannedAddress(sc.socket().getInetAddress());
}
}

View File

@ -105,14 +105,23 @@ public class RequestAuthLogin extends LoginClientPacket
final LoginController lc = LoginController.getInstance();
final LoginClient client = getClient();
final InetAddress address = getClient().getConnection().getInetAddress();
final InetAddress address = client.getConnection().getInetAddress();
if (address == null)
{
LOGGER.warning("Socket is not connected: " + client.getAccount());
client.close(LoginFailReason.REASON_SYSTEM_ERROR);
return;
}
final AuthLoginResult result = lc.tryAuthLogin(_user, _password, getClient());
// IP banned or entering wrong password many times.
if (lc.isBannedAddress(address))
{
client.close(new AccountKicked(AccountKickedReason.REASON_PERMANENTLY_BANNED));
return;
}
final AuthLoginResult result = lc.tryAuthLogin(_user, _password, client);
switch (result)
{
@ -123,11 +132,11 @@ public class RequestAuthLogin extends LoginClientPacket
client.setSessionKey(lc.assignSessionKeyToClient(_user, client));
if (Config.SHOW_LICENCE)
{
client.sendPacket(new LoginOk(getClient().getSessionKey()));
client.sendPacket(new LoginOk(client.getSessionKey()));
}
else
{
getClient().sendPacket(new ServerList(getClient()));
client.sendPacket(new ServerList(client));
}
break;
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}

View File

@ -1,149 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.l2jmobius.loginserver.network.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.l2jmobius.commons.util.Chronos;
/**
* IPv4 filter.
* @author Forsaiken
*/
public class IPv4Filter implements Runnable
{
protected final Logger LOGGER = Logger.getLogger(getClass().getName());
private final HashMap<Integer, Flood> _ipFloodMap;
private static final long SLEEP_TIME = 5000;
public IPv4Filter()
{
_ipFloodMap = new HashMap<>();
final Thread t = new Thread(this, getClass().getSimpleName());
t.setDaemon(true);
t.start();
}
/**
* @param ip
* @return
*/
private static int hash(byte[] ip)
{
return (ip[0] & 0xFF) | ((ip[1] << 8) & 0xFF00) | ((ip[2] << 16) & 0xFF0000) | ((ip[3] << 24) & 0xFF000000);
}
protected static final class Flood
{
long lastAccess;
int trys;
Flood()
{
lastAccess = Chronos.currentTimeMillis();
trys = 0;
}
}
public boolean accept(SocketChannel sc)
{
final InetAddress addr = sc.socket().getInetAddress();
if (!(addr instanceof Inet4Address))
{
LOGGER.info("Someone tried to connect from something other than IPv4: " + addr.getHostAddress());
return false;
}
final int h = hash(addr.getAddress());
final long current = Chronos.currentTimeMillis();
Flood f;
synchronized (_ipFloodMap)
{
f = _ipFloodMap.get(h);
}
if (f != null)
{
if (f.trys == -1)
{
f.lastAccess = current;
return false;
}
if ((f.lastAccess + 1000) > current)
{
f.lastAccess = current;
if (f.trys >= 3)
{
f.trys = -1;
return false;
}
f.trys++;
}
else
{
f.lastAccess = current;
}
}
else
{
synchronized (_ipFloodMap)
{
_ipFloodMap.put(h, new Flood());
}
}
return true;
}
@Override
public void run()
{
while (true)
{
final long reference = Chronos.currentTimeMillis() - (1000 * 300);
synchronized (_ipFloodMap)
{
final Iterator<Entry<Integer, Flood>> it = _ipFloodMap.entrySet().iterator();
while (it.hasNext())
{
final Flood f = it.next().getValue();
if (f.lastAccess < reference)
{
it.remove();
}
}
}
try
{
Thread.sleep(SLEEP_TIME);
}
catch (InterruptedException e)
{
return;
}
}
}
}