package l2p.gameserver.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import l2p.commons.dbcp.DbUtils;
import l2p.commons.util.GArray;
import l2p.gameserver.database.DatabaseFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class IpManager
{
private static final Logger _log = LoggerFactory.getLogger(IpManager.class);
public static void BanIp(String ip, String admin, int time, String comments)
{
Connection con = null;
PreparedStatement statement = null;
try
{
long expiretime = 0L;
if (time != 0)
expiretime = System.currentTimeMillis() / 1000L + time;
con = DatabaseFactory.getInstanceLogin().getConnection();
statement = con.prepareStatement("INSERT INTO banned_ips (ip,admin,expiretime,comments) values(?,?,?,?)");
statement.setString(1, ip);
statement.setString(2, admin);
statement.setLong(3, expiretime);
statement.setString(4, comments);
statement.execute();
_log.info("Banning ip: " + ip + " for " + time + " seconds.");
}
catch (Exception e)
{
_log.error("error4 while writing banned_ips", e);
}
finally
{
DbUtils.closeQuietly(con, statement);
}
}
public static void UnbanIp(String ip)
{
Connection con = null;
PreparedStatement statement = null;
try
{
con = DatabaseFactory.getInstanceLogin().getConnection();
statement = con.prepareStatement("DELETE FROM banned_ips WHERE ip=?");
statement.setString(1, ip);
statement.execute();
_log.info("Removed ban for ip: " + ip);
}
catch (Exception e)
{
_log.error("error5 while deleting from banned_ips", e);
}
finally
{
DbUtils.closeQuietly(con, statement);
}
}
public static boolean CheckIp(String ip)
{
boolean result = false;
Connection con = null;
PreparedStatement statement = null;
ResultSet rset = null;
try
{
con = DatabaseFactory.getInstanceLogin().getConnection();
statement = con.prepareStatement("SELECT expiretime FROM banned_ips WHERE ip=?");
statement.setString(1, ip);
rset = statement.executeQuery();
if (rset.next())
{
long expiretime = rset.getLong("expiretime");
if ((expiretime != 0L) && (expiretime <= System.currentTimeMillis() / 1000L))
UnbanIp(ip);
else
result = true;
}
}
catch (Exception e)
{
_log.error("error6 while reading banned_ips", e);
}
finally
{
DbUtils.closeQuietly(con, statement, rset);
}
return result;
}
public static GArray<BannedIp> getBanList()
{
GArray result = new GArray();
Connection con = null;
PreparedStatement statement = null;
ResultSet rset = null;
try
{
con = DatabaseFactory.getInstanceLogin().getConnection();
statement = con.prepareStatement("SELECT ip,admin FROM banned_ips");
rset = statement.executeQuery();
while (rset.next())
{
BannedIp temp = new BannedIp();
temp.ip = rset.getString("ip");
temp.admin = rset.getString("admin");
result.add(temp);
}
}
catch (Exception e)
{
_log.error("error7 while reading banned_ips", e);
}
finally
{
DbUtils.closeQuietly(con, statement, rset);
}
return result;
}
}