Skip to content

Commit

Permalink
Add option to exclude addresses from being blocked
Browse files Browse the repository at this point in the history
  • Loading branch information
Alemiz112 committed Mar 30, 2024
1 parent 2f77498 commit 94f729d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ public void onCloseTriggered(ChannelPromise promise) {
public boolean tryBlockAddress(InetAddress address, long time, TimeUnit unit) {
RakServerRateLimiter rateLimiter = this.pipeline().get(RakServerRateLimiter.class);
if (rateLimiter != null) {
rateLimiter.blockAddress(address, time, unit);
return true;
return rateLimiter.blockAddress(address, time, unit);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import org.cloudburstmc.netty.channel.raknet.RakServerChannel;

import java.net.InetAddress;
import java.util.Iterator;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -41,6 +40,8 @@ public class RakServerRateLimiter extends SimpleChannelInboundHandler<DatagramPa
private final ConcurrentHashMap<InetAddress, AtomicInteger> rateLimitMap = new ConcurrentHashMap<>();
private final Map<InetAddress, Long> blockedConnections = new ConcurrentHashMap<>();

private final Collection<InetAddress> exceptions = Collections.synchronizedCollection(new HashSet<>());

private final AtomicLong globalCounter = new AtomicLong(0);

private ScheduledFuture<?> tickFuture;
Expand Down Expand Up @@ -81,9 +82,14 @@ private void onBlockedTick() {
}
}

public void blockAddress(InetAddress address, long time, TimeUnit unit) {
public boolean blockAddress(InetAddress address, long time, TimeUnit unit) {
if (this.exceptions.contains(address)) {
return false;
}

long millis = unit.toMillis(time);
this.blockedConnections.put(address, System.currentTimeMillis() + millis);
return true;
}

public void unblockAddress(InetAddress address) {
Expand All @@ -96,6 +102,14 @@ public boolean isAddressBlocked(InetAddress address) {
return this.blockedConnections.containsKey(address);
}

public void addException(InetAddress address) {
this.exceptions.add(address);
}

public void removeException(InetAddress address) {
this.exceptions.remove(address);
}

@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket datagram) throws Exception {
if (this.globalCounter.incrementAndGet() > this.channel.config().getGlobalPacketLimit()) {
Expand All @@ -111,8 +125,8 @@ protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket datagram)
}

AtomicInteger counter = this.rateLimitMap.computeIfAbsent(address, a -> new AtomicInteger());
if (counter.incrementAndGet() > this.channel.config().getPacketLimit()) {
this.blockAddress(address, 10, TimeUnit.SECONDS);
if (counter.incrementAndGet() > this.channel.config().getPacketLimit() &&
this.blockAddress(address, 10, TimeUnit.SECONDS)) {
log.warn("[{}] Blocked because packet limit was reached");
} else {
ctx.fireChannelRead(datagram.retain());
Expand Down

0 comments on commit 94f729d

Please sign in to comment.