Support for storing IP and IP prefix bans in database.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.gempukku.lotro.async;
|
||||
|
||||
import com.gempukku.lotro.async.handler.UriRequestHandler;
|
||||
import com.gempukku.lotro.db.IpBanDAO;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.jboss.netty.buffer.ChannelBuffers;
|
||||
@@ -34,10 +35,12 @@ public class LotroHttpRequestHandler extends SimpleChannelUpstreamHandler {
|
||||
|
||||
private Map<Type, Object> _objects;
|
||||
private UriRequestHandler _uriRequestHandler;
|
||||
private IpBanDAO _ipBanDAO;
|
||||
|
||||
public LotroHttpRequestHandler(Map<Type, Object> objects, UriRequestHandler uriRequestHandler) {
|
||||
_objects = objects;
|
||||
_uriRequestHandler = uriRequestHandler;
|
||||
_ipBanDAO = (IpBanDAO) _objects.get(IpBanDAO.class);
|
||||
}
|
||||
|
||||
private Collection<String> _bannedIps = Arrays.asList("108.251.13.101", "188.123.232.46", "193.235.73.197", "5.228.68.175", "89.88.173.207", "76.171.226.172", "97.73.50.119", "108.251.13.101");
|
||||
@@ -113,9 +116,9 @@ public class LotroHttpRequestHandler extends SimpleChannelUpstreamHandler {
|
||||
}
|
||||
|
||||
private boolean isBanned(String ipAddress) {
|
||||
if (_bannedIps.contains(ipAddress))
|
||||
if (_ipBanDAO.getIpBans().contains(ipAddress))
|
||||
return true;
|
||||
for (String bannedRange : _bannedRanges) {
|
||||
for (String bannedRange : _ipBanDAO.getIpPrefixBans()) {
|
||||
if (ipAddress.startsWith(bannedRange))
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -50,12 +50,17 @@ public class DaoBuilder {
|
||||
CachedTransferDAO transferDao = new CachedTransferDAO(dbTransferDao);
|
||||
objectMap.put(TransferDAO.class, transferDao);
|
||||
|
||||
DbIpBanDAO dbIpBanDao = new DbIpBanDAO(dbAccess);
|
||||
CachedIpBanDAO ipBanDao = new CachedIpBanDAO(dbIpBanDao);
|
||||
objectMap.put(DbIpBanDAO.class, ipBanDao);
|
||||
|
||||
CacheManager cacheManager = new CacheManager();
|
||||
cacheManager.addCache(merchantDao);
|
||||
cacheManager.addCache(deckDao);
|
||||
cacheManager.addCache(collectionDao);
|
||||
cacheManager.addCache(playerDao);
|
||||
cacheManager.addCache(transferDao);
|
||||
cacheManager.addCache(ipBanDao);
|
||||
objectMap.put(CacheManager.class, cacheManager);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.gempukku.lotro.db;
|
||||
|
||||
import com.gempukku.lotro.cache.Cached;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class CachedIpBanDAO implements IpBanDAO, Cached {
|
||||
private IpBanDAO _delegate;
|
||||
private Set<String> _bannedIps;
|
||||
private Set<String> _bannedIpPrefixes;
|
||||
|
||||
public CachedIpBanDAO(IpBanDAO delegate) {
|
||||
_delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearCache() {
|
||||
_bannedIps = null;
|
||||
_bannedIpPrefixes = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addIpBan(String ip) {
|
||||
_delegate.addIpBan(ip);
|
||||
_bannedIps = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addIpPrefixBan(String ipPrefix) {
|
||||
_delegate.addIpPrefixBan(ipPrefix);
|
||||
_bannedIpPrefixes = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getIpBans() {
|
||||
Set<String> result = _bannedIps;
|
||||
if (result != null)
|
||||
return result;
|
||||
|
||||
result = _delegate.getIpBans();
|
||||
_bannedIps = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getIpPrefixBans() {
|
||||
Set<String> result = _bannedIpPrefixes;
|
||||
if (result != null)
|
||||
return result;
|
||||
|
||||
result = _delegate.getIpPrefixBans();
|
||||
_bannedIpPrefixes = result;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package com.gempukku.lotro.db;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class DbIpBanDAO implements IpBanDAO {
|
||||
private DbAccess _dbAccess;
|
||||
|
||||
public DbIpBanDAO(DbAccess dbAccess) {
|
||||
_dbAccess = dbAccess;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addIpBan(String ip) {
|
||||
try {
|
||||
Connection connection = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("insert into ip_ban (ip, prefix) values (?, 0)");
|
||||
try {
|
||||
statement.setString(1, ip);
|
||||
|
||||
statement.execute();
|
||||
} finally {
|
||||
statement.close();
|
||||
}
|
||||
} finally {
|
||||
connection.close();
|
||||
}
|
||||
} catch (SQLException exp) {
|
||||
throw new RuntimeException("Unable to add an IP ban", exp);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addIpPrefixBan(String ipPrefix) {
|
||||
try {
|
||||
Connection connection = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("insert into ip_ban (ip, prefix) values (?, 1)");
|
||||
try {
|
||||
statement.setString(1, ipPrefix);
|
||||
|
||||
statement.execute();
|
||||
} finally {
|
||||
statement.close();
|
||||
}
|
||||
} finally {
|
||||
connection.close();
|
||||
}
|
||||
} catch (SQLException exp) {
|
||||
throw new RuntimeException("Unable to add an IP prefix ban", exp);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getIpBans() {
|
||||
try {
|
||||
Connection connection = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("select ip from ip_ban where prefix=0");
|
||||
try {
|
||||
ResultSet rs = statement.executeQuery();
|
||||
try {
|
||||
Set<String> result = new HashSet<String>();
|
||||
while (rs.next()) {
|
||||
String ip = rs.getString(1);
|
||||
|
||||
result.add(ip);
|
||||
}
|
||||
return result;
|
||||
} finally {
|
||||
rs.close();
|
||||
}
|
||||
} finally {
|
||||
statement.close();
|
||||
}
|
||||
} finally {
|
||||
connection.close();
|
||||
}
|
||||
} catch (SQLException exp) {
|
||||
throw new RuntimeException("Unable to get count of player games", exp);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getIpPrefixBans() {
|
||||
try {
|
||||
Connection connection = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("select ip from ip_ban where prefix=1");
|
||||
try {
|
||||
ResultSet rs = statement.executeQuery();
|
||||
try {
|
||||
Set<String> result = new HashSet<String>();
|
||||
while (rs.next()) {
|
||||
String ip = rs.getString(1);
|
||||
|
||||
result.add(ip);
|
||||
}
|
||||
return result;
|
||||
} finally {
|
||||
rs.close();
|
||||
}
|
||||
} finally {
|
||||
statement.close();
|
||||
}
|
||||
} finally {
|
||||
connection.close();
|
||||
}
|
||||
} catch (SQLException exp) {
|
||||
throw new RuntimeException("Unable to get count of player games", exp);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.gempukku.lotro.db;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface IpBanDAO {
|
||||
public Set<String> getIpBans();
|
||||
public Set<String> getIpPrefixBans();
|
||||
public void addIpBan(String ip);
|
||||
public void addIpPrefixBan(String ipPrefix);
|
||||
}
|
||||
Reference in New Issue
Block a user