I haven't worked a lot with connections so thought it would be a great time to learn about it. I'm trying to increase my server security with Netty. I'm trying to make it so it will timeout or ignore multiple connections of the same mac address. How would I do that? Also just multiple connections of the same IP as well.
Code:
package sun.net;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.handler.timeout.ReadTimeoutException;
import sun.model.players.Client;
import sun.model.players.saving.PlayerSave;
public class ChannelHandler extends SimpleChannelHandler {
private Session session = null;
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
if (e.getCause() instanceof ReadTimeoutException) { if
(session.getClient() != null) { System.out.println("Player " +
session.getClient().playerName + " timed out!"); } } else
if(!(e.getCause() instanceof java.io.IOException)){
e.getCause().printStackTrace(); } ctx.getChannel().close();
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
if (e.getMessage() instanceof Client) {
session.setClient((Client) e.getMessage());
} else if (e.getMessage() instanceof Packet) {
if (session.getClient() != null) {
session.getClient().queueMessage((Packet) e.getMessage());
}
}
}
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
if (session == null)
session = new Session(ctx.getChannel());
}
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
if (session != null) {
Client client = session.getClient();
if (client != null) {
//ConnectionList.removeConnection(((InetSocketAddress)ctx.getChannel().getRemoteAddress()).getAddress().getHostAddress().toString());
client.disconnected = true;
PlayerSave.saveGame(client);
}
session = null;
}
}
}
Code:
package sun.world;
import java.util.HashMap;
public class ConnectionList {
static HashMap<String, Integer> connections = new HashMap<String, Integer>();
static final int MAX_CONNECTIONS = 1;
public static boolean allowedToConnect(String ip) {
if (!connections.containsKey(ip)) {
connections.put(ip, 2);
return true;
} else {
int connectionsAmt = connections.get(ip);
if (connectionsAmt < MAX_CONNECTIONS) {
connections.put(ip, connectionsAmt + 1);
return true;
} else
return false;
}
}
public static void removeConnection(String ip) {
int connectionsAmt = connections.get(ip);
if (connectionsAmt <= 1)
connections.remove(ip);
else
connections.put(ip, connectionsAmt - 1);
}
}
Code:
package sun.net;
import org.jboss.netty.channel.Channel;
import sun.model.players.Client;
public class Session {
private final Channel channel;
private Client client;
public Session(Channel channel) {
this.channel = channel;
}
public Channel getChannel() {
return channel;
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
}
Code:
case LOGGING_IN:
@SuppressWarnings("unused")
int loginType = -1,
loginPacketSize = -1,
loginEncryptPacketSize = -1;
if (2 <= buffer.capacity()) {
loginType = buffer.readByte() & 0xff; // should be 16 or 18
loginPacketSize = buffer.readByte() & 0xff;
loginEncryptPacketSize = loginPacketSize - (36 + 1 + 1 + 2);
if (loginPacketSize <= 0 || loginEncryptPacketSize <= 0) {
System.out.println("Zero or negative login size.");
channel.close();
return false;
}
}