Let PlayerCredentials house the connecting remote address.

This commit is contained in:
atomicint
2015-03-31 19:45:20 -04:00
parent 218739a4ff
commit bdec06a4ca
2 changed files with 24 additions and 3 deletions
@@ -6,6 +6,7 @@ import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import java.math.BigInteger;
import java.net.InetSocketAddress;
import java.security.SecureRandom;
import java.util.List;
@@ -18,6 +19,8 @@ import org.apollo.security.PlayerCredentials;
import org.apollo.util.BufferUtil;
import org.apollo.util.StatefulFrameDecoder;
import com.google.common.net.InetAddresses;
/**
* A {@link StatefulFrameDecoder} which decodes the login request frames.
*
@@ -174,6 +177,8 @@ public final class LoginDecoder extends StatefulFrameDecoder<LoginDecoderState>
int uid = secure.readInt();
String username = BufferUtil.readString(secure);
String password = BufferUtil.readString(secure);
InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
String hostAddress = InetAddresses.toAddrString(socketAddress.getAddress());
if (password.length() < 6 || password.length() > 20 || username.isEmpty() || username.length() > 12) {
writeResponseCode(ctx, LoginConstants.STATUS_INVALID_CREDENTIALS);
@@ -193,7 +198,7 @@ public final class LoginDecoder extends StatefulFrameDecoder<LoginDecoderState>
IsaacRandom encodingRandom = new IsaacRandom(seed);
PlayerCredentials credentials = new PlayerCredentials(username, password, usernameHash, uid);
PlayerCredentials credentials = new PlayerCredentials(username, password, usernameHash, uid, hostAddress);
IsaacRandomPair randomPair = new IsaacRandomPair(encodingRandom, decodingRandom);
out.add(new LoginRequest(credentials, randomPair, reconnecting, lowMemory, release, crcs, version));
@@ -207,7 +212,7 @@ public final class LoginDecoder extends StatefulFrameDecoder<LoginDecoderState>
* @param response The response code to write.
*/
private void writeResponseCode(ChannelHandlerContext ctx, int response) {
ByteBuf buffer = ctx.alloc().buffer(1);
ByteBuf buffer = ctx.alloc().buffer(Byte.BYTES);
buffer.writeByte(response);
ctx.write(buffer).addListener(ChannelFutureListener.CLOSE);
+17 -1
View File
@@ -34,6 +34,11 @@ public final class PlayerCredentials {
*/
private final int usernameHash;
/**
* The Player's host address, represented as a String.
*/
private final String hostAddress;
/**
* Creates a new {@link PlayerCredentials} object with the specified name, password and uid.
*
@@ -41,13 +46,15 @@ public final class PlayerCredentials {
* @param password The player's password.
* @param usernameHash The hash of the player's username.
* @param uid The computer's uid.
* @param hostAddress The Player's connecting host address.
*/
public PlayerCredentials(String username, String password, int usernameHash, int uid) {
public PlayerCredentials(String username, String password, int usernameHash, int uid, String hostAddress) {
this.username = username;
encodedUsername = NameUtil.encodeBase37(username);
this.password = password;
this.usernameHash = usernameHash;
this.uid = uid;
this.hostAddress = hostAddress;
}
/**
@@ -104,6 +111,15 @@ public final class PlayerCredentials {
return usernameHash;
}
/**
* Gets the Player's connecting host address.
*
* @return The Player's host address, represented as a String.
*/
public String getHostAddress() {
return hostAddress;
}
@Override
public int hashCode() {
return Long.hashCode(encodedUsername);