_out = new BufferedOutputStream(_loginSocket.getOutputStream());
//init Blowfish
_blowfishKey = generateHex(40);
_blowfish = new NewCrypt("_;v.]05-31!|+-%xT!^[$\00");
while(!_interrupted)
{
lengthLo = _in.read();
lengthHi = _in.read();
length = lengthHi * 256 + lengthLo;
if(lengthHi < 0)
{
_log.finer("LoginServerThread: Login terminated the connection.");
break;
}
byte[] incoming = new byte[length];
incoming[0] = (byte) lengthLo;
incoming[1] = (byte) lengthHi;
int receivedBytes = 0;
int newBytes = 0;
while(newBytes != -1 && receivedBytes < length - 2)
{
newBytes = _in.read(incoming, 2, length - 2);
receivedBytes = receivedBytes + newBytes;
}
if(receivedBytes != length - 2)
{
_log.warning("Incomplete Packet is sent to the server, closing connection.(LS)");
break;
}
byte[] decrypt = new byte[length - 2];
System.arraycopy(incoming, 2, decrypt, 0, decrypt.length);
// decrypt if we have a key
decrypt = _blowfish.decrypt(decrypt);
checksumOk = NewCrypt.verifyChecksum(decrypt);
if(!checksumOk)
{
_log.warning("Incorrect packet checksum, ignoring packet (LS)");
break;
}
if(Config.DEBUG)
{
_log.warning("[C]\n" + Util.printData(decrypt));
}
int packetType = decrypt[0] & 0xff;
switch(packetType)
{
case 00:
InitLS init = new InitLS(decrypt);
if(Config.DEBUG)
{
_log.info("Init received");
}
if(init.getRevision() != REVISION)
{
//TODO: revision mismatch
_log.warning("/!\\ Revision mismatch between LS and GS /!\\");
break;
}
try
{
KeyFactory kfac = KeyFactory.getInstance("RSA");
BigInteger modulus = new BigInteger(init.getRSAKey());
RSAPublicKeySpec kspec1 = new RSAPublicKeySpec(modulus, RSAKeyGenParameterSpec.F4);
_publicKey = (RSAPublicKey) kfac.generatePublic(kspec1);
if(Config.DEBUG)
{
_log.info("RSA key set up");
}
}
catch(GeneralSecurityException e)
{
if(Config.ENABLE_ALL_EXCEPTIONS)
e.printStackTrace();
_log.warning("Troubles while init the public key send by login");
break;
}
//send the blowfish key through the rsa encryption
BlowFishKey bfk = new BlowFishKey(_blowfishKey, _publicKey);
sendPacket(bfk);
if(Config.DEBUG)
{
_log.info("Sent new blowfish key");
}
//now, only accept paket with the new encryption
_blowfish = new NewCrypt(_blowfishKey);
if(Config.DEBUG)
{
_log.info("Changed blowfish key");
}
AuthRequest ar = new AuthRequest(_requestID, _acceptAlternate, _hexID, _gameExternalHost, _gameInternalHost, _gamePort, _reserveHost, _maxPlayer);