RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
ChannelFilter.java
1package com.osroyale.net;
2
3import com.google.common.collect.ConcurrentHashMultiset;
4import com.google.common.collect.Multiset;
5import com.osroyale.Config;
6import com.osroyale.game.world.entity.mob.player.IPBannedPlayers;
7import com.osroyale.net.codec.login.LoginResponse;
8import com.osroyale.net.codec.login.LoginResponsePacket;
9import io.netty.buffer.ByteBuf;
10import io.netty.channel.ChannelFutureListener;
11import io.netty.channel.ChannelHandler.Sharable;
12import io.netty.channel.ChannelHandlerContext;
13import io.netty.channel.ChannelInboundHandlerAdapter;
14
15import java.net.InetSocketAddress;
16import java.util.Objects;
17import java.util.concurrent.atomic.AtomicBoolean;
18
26@Sharable
63
64public class ChannelFilter extends ChannelInboundHandlerAdapter {
65
67 private final Multiset<Connection> connections = ConcurrentHashMultiset.create();
68
69 @Override
70 public void channelActive(ChannelHandlerContext ctx) throws Exception {
71 Connection connection = getConenction(ctx);
72 connection.setCanConnect(true);
73 }
74
75 @Override
76 public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
77 Connection connection = getConenction(ctx);
78
79 /* Make sure this channel is not going to get flooded. */
80 if (!connection.canConnect()) {
81 return;
82 }
83
84 /*
85 * If this local then, do nothing and proceed to next handler in the
86 * pipeline.
87 */
88 if (connection.getHost().equalsIgnoreCase("127.0.0.1")) {
89 return;
90 }
91
92 /* Add the host */
93 connection.addConnection();
94
95 /* Evaluate the host */
96 if (connection.getConnections() > Config.CONNECTION_LIMIT) {
97 disconnect(ctx, LoginResponse.LOGIN_LIMIT_EXCEEDED);
98 return;
99 }
100
101 if (IPBannedPlayers.ipBans.contains(connection.getHost())) {
102 disconnect(ctx, LoginResponse.ACCOUNT_DISABLED);
103 return;
104 }
105
106 connections.add(connection);
107 connection.setCanConnect(false);
108
109 /*
110 * Nothing went wrong, so register the channel and forward the randomevent to
111 * next handler in the pipeline.
112 */
113 ctx.fireChannelRegistered();
114 }
115
116 @Override
117 public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
118 Connection connection = getConenction(ctx);
119
120 /*
121 * If this is local, do nothing and proceed to next handler in the
122 * pipeline.
123 */
124 if (connection.getHost().equalsIgnoreCase("127.0.0.1")) {
125 return;
126 }
127
128 connection.removeConnection();
129
130 /* Remove the host from the connection list */
131 if (connection.getConnections() == 0) {
132 connections.remove(connection);
133 }
134
135 /*
136 * The connection is unregistered so forward the randomevent to the next
137 * handler in the pipeline.
138 */
139 ctx.fireChannelUnregistered();
140 }
141
150 private void disconnect(ChannelHandlerContext ctx, LoginResponse response) {
151 LoginResponsePacket message = new LoginResponsePacket(response);
152 ByteBuf initialMessage = ctx.alloc().buffer(8).writeLong(0);
153
154 ctx.write(initialMessage, ctx.voidPromise());
155 ctx.writeAndFlush(message).addListener(ChannelFutureListener.CLOSE);
156 }
157
166 private Connection getConenction(ChannelHandlerContext ctx) {
167 String host = ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostAddress();
168 return connections.stream().filter(conn -> conn.getHost().equalsIgnoreCase(host)).findFirst().orElse(new Connection(host));
169 }
170
176 private static class Connection {
177
179 private final AtomicBoolean canConnect = new AtomicBoolean(true);
180
182 private final String host;
183
185 private int connections;
186
193 public Connection(String host) {
194 this.host = host;
195 }
196
202 public String getHost() {
203 return host;
204 }
205
211 public int getConnections() {
212 return connections;
213 }
214
220 public boolean canConnect() {
221 return canConnect.get();
222 }
223
225 public void addConnection() {
226 connections++;
227 }
228
230 public void removeConnection() {
231 connections--;
232 }
233
234 public void setCanConnect(boolean val) {
235 canConnect.compareAndSet(!val, val);
236 }
237
238 @Override
239 public int hashCode() {
240 return Objects.hash(host);
241 }
242
243 @Override
244 public boolean equals(Object obj) {
245 if (obj instanceof Connection) {
246 Connection other = (Connection) obj;
247 return host.equalsIgnoreCase(other.getHost());
248 }
249 return obj == this;
250 }
251
252 @Override
253 public String toString() {
254 return String.format("Connection[host=%s, connections=%s, elapsed=%s]", host, connections, canConnect);
255 }
256
257 }
258
259}
static final int CONNECTION_LIMIT
Definition Config.java:114