Code improvements.
This commit is contained in:
@@ -34,18 +34,11 @@ public abstract class FloodProtectedListener extends Thread
|
||||
{
|
||||
private final Logger _log = Logger.getLogger(FloodProtectedListener.class.getName());
|
||||
private final Map<String, ForeignConnection> _floodProtection = new ConcurrentHashMap<>();
|
||||
private ServerSocket _serverSocket;
|
||||
private final ServerSocket _serverSocket;
|
||||
|
||||
public FloodProtectedListener(String listenIp, int port) throws IOException
|
||||
{
|
||||
if (listenIp.equals("*"))
|
||||
{
|
||||
_serverSocket = new ServerSocket(port);
|
||||
}
|
||||
else
|
||||
{
|
||||
_serverSocket = new ServerSocket(port, 50, InetAddress.getByName(listenIp));
|
||||
}
|
||||
_serverSocket = listenIp.equals("*") ? new ServerSocket(port) : new ServerSocket(port, 50, InetAddress.getByName(listenIp));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -277,11 +277,7 @@ public final class GameServerTable implements IXmlReader
|
||||
*/
|
||||
private String hexToString(byte[] hex)
|
||||
{
|
||||
if (hex == null)
|
||||
{
|
||||
return "null";
|
||||
}
|
||||
return new BigInteger(hex).toString(16);
|
||||
return hex == null ? "null" : new BigInteger(hex).toString(16);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -458,11 +454,7 @@ public final class GameServerTable implements IXmlReader
|
||||
*/
|
||||
public int getCurrentPlayerCount()
|
||||
{
|
||||
if (_gst == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return _gst.getPlayerCount();
|
||||
return _gst == null ? 0 : _gst.getPlayerCount();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -311,11 +311,7 @@ public class GameServerThread extends Thread
|
||||
*/
|
||||
public boolean isAuthed()
|
||||
{
|
||||
if (getGameServerInfo() == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return getGameServerInfo().isAuthed();
|
||||
return (getGameServerInfo() != null) && getGameServerInfo().isAuthed();
|
||||
}
|
||||
|
||||
public void setGameServerInfo(GameServerInfo gsi)
|
||||
@@ -338,11 +334,7 @@ public class GameServerThread extends Thread
|
||||
|
||||
public int getServerId()
|
||||
{
|
||||
if (getGameServerInfo() != null)
|
||||
{
|
||||
return getGameServerInfo().getId();
|
||||
}
|
||||
return -1;
|
||||
return getGameServerInfo() != null ? getGameServerInfo().getId() : -1;
|
||||
}
|
||||
|
||||
public RSAPrivateKey getPrivateKey()
|
||||
|
@@ -44,5 +44,4 @@ public class HackingException extends Exception
|
||||
{
|
||||
return _connects;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -84,9 +84,8 @@ public class LoginController
|
||||
|
||||
_keyPairs = new ScrambledKeyPair[10];
|
||||
|
||||
KeyPairGenerator keygen = null;
|
||||
final KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
|
||||
|
||||
keygen = KeyPairGenerator.getInstance("RSA");
|
||||
final RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4);
|
||||
keygen.initialize(spec);
|
||||
|
||||
@@ -115,9 +114,7 @@ public class LoginController
|
||||
*/
|
||||
private void testCipher(RSAPrivateKey key) throws GeneralSecurityException
|
||||
{
|
||||
// avoid worst-case execution, KenM
|
||||
final Cipher rsaCipher = Cipher.getInstance("RSA/ECB/nopadding");
|
||||
rsaCipher.init(Cipher.DECRYPT_MODE, key);
|
||||
Cipher.getInstance("RSA/ECB/nopadding").init(Cipher.DECRYPT_MODE, key);
|
||||
}
|
||||
|
||||
private void generateBlowFishKeys()
|
||||
@@ -144,9 +141,8 @@ public class LoginController
|
||||
|
||||
public SessionKey assignSessionKeyToClient(String account, L2LoginClient client)
|
||||
{
|
||||
SessionKey key;
|
||||
final SessionKey key = new SessionKey(Rnd.nextInt(), Rnd.nextInt(), Rnd.nextInt(), Rnd.nextInt());
|
||||
|
||||
key = new SessionKey(Rnd.nextInt(), Rnd.nextInt(), Rnd.nextInt(), Rnd.nextInt());
|
||||
_loginServerClients.put(account, client);
|
||||
return key;
|
||||
}
|
||||
@@ -328,11 +324,7 @@ public class LoginController
|
||||
public boolean isBannedAddress(InetAddress address) throws UnknownHostException
|
||||
{
|
||||
final String[] parts = address.getHostAddress().split("\\.");
|
||||
Long bi = _bannedIps.get(address);
|
||||
if (bi == null)
|
||||
{
|
||||
bi = _bannedIps.get(InetAddress.getByName(parts[0] + "." + parts[1] + "." + parts[2] + ".0"));
|
||||
}
|
||||
Long bi = _bannedIps.get(address) == null ? _bannedIps.get(InetAddress.getByName(parts[0] + "." + parts[1] + "." + parts[2] + ".0")) : _bannedIps.get(address);
|
||||
if (bi == null)
|
||||
{
|
||||
bi = _bannedIps.get(InetAddress.getByName(parts[0] + "." + parts[1] + ".0.0"));
|
||||
@@ -341,16 +333,16 @@ public class LoginController
|
||||
{
|
||||
bi = _bannedIps.get(InetAddress.getByName(parts[0] + ".0.0.0"));
|
||||
}
|
||||
if (bi != null)
|
||||
if (bi == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if ((bi <= 0) || (bi >= System.currentTimeMillis()))
|
||||
{
|
||||
if ((bi > 0) && (bi < System.currentTimeMillis()))
|
||||
{
|
||||
_bannedIps.remove(address);
|
||||
_log.info("Removed expired ip address ban " + address.getHostAddress() + ".");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
_bannedIps.remove(address);
|
||||
_log.info("Removed expired ip address ban " + address.getHostAddress() + ".");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -389,11 +381,7 @@ public class LoginController
|
||||
public SessionKey getKeyForAccount(String account)
|
||||
{
|
||||
final L2LoginClient client = _loginServerClients.get(account);
|
||||
if (client != null)
|
||||
{
|
||||
return client.getSessionKey();
|
||||
}
|
||||
return null;
|
||||
return client != null ? client.getSessionKey() : null;
|
||||
}
|
||||
|
||||
public boolean isAccountInAnyGameServer(String account)
|
||||
@@ -426,8 +414,7 @@ public class LoginController
|
||||
|
||||
public void getCharactersOnAccount(String account)
|
||||
{
|
||||
final Collection<GameServerInfo> serverList = GameServerTable.getInstance().getRegisteredGameServers().values();
|
||||
for (GameServerInfo gsi : serverList)
|
||||
for (GameServerInfo gsi : GameServerTable.getInstance().getRegisteredGameServers().values())
|
||||
{
|
||||
if (gsi.isAuthed())
|
||||
{
|
||||
@@ -445,27 +432,28 @@ public class LoginController
|
||||
{
|
||||
final GameServerInfo gsi = GameServerTable.getInstance().getRegisteredGameServerById(serverId);
|
||||
final int access = client.getAccessLevel();
|
||||
if ((gsi != null) && gsi.isAuthed())
|
||||
if ((gsi == null) || !gsi.isAuthed())
|
||||
{
|
||||
final boolean loginOk = ((gsi.getCurrentPlayerCount() < gsi.getMaxPlayers()) && (gsi.getStatus() != ServerStatus.STATUS_GM_ONLY)) || (access > 0);
|
||||
|
||||
if (loginOk && (client.getLastServer() != serverId))
|
||||
{
|
||||
try (Connection con = DatabaseFactory.getInstance().getConnection();
|
||||
PreparedStatement ps = con.prepareStatement(ACCOUNT_LAST_SERVER_UPDATE))
|
||||
{
|
||||
ps.setInt(1, serverId);
|
||||
ps.setString(2, client.getAccount());
|
||||
ps.executeUpdate();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, "Could not set lastServer: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
return loginOk;
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
final boolean loginOk = ((gsi.getCurrentPlayerCount() < gsi.getMaxPlayers()) && (gsi.getStatus() != ServerStatus.STATUS_GM_ONLY)) || (access > 0);
|
||||
|
||||
if (loginOk && (client.getLastServer() != serverId))
|
||||
{
|
||||
try (Connection con = DatabaseFactory.getInstance().getConnection();
|
||||
PreparedStatement ps = con.prepareStatement(ACCOUNT_LAST_SERVER_UPDATE))
|
||||
{
|
||||
ps.setInt(1, serverId);
|
||||
ps.setString(2, client.getAccount());
|
||||
ps.executeUpdate();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, "Could not set lastServer: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
return loginOk;
|
||||
}
|
||||
|
||||
public void setAccountAccessLevel(String account, int banLevel)
|
||||
@@ -561,7 +549,7 @@ public class LoginController
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else if (type.equals("allow"))
|
||||
if (type.equals("allow"))
|
||||
{
|
||||
ipWhiteList.add(InetAddress.getByName(ip));
|
||||
}
|
||||
|
@@ -60,16 +60,16 @@ public class AuthGameGuard extends L2LoginClientPacket
|
||||
@Override
|
||||
protected boolean readImpl()
|
||||
{
|
||||
if (super._buf.remaining() >= 20)
|
||||
if (super._buf.remaining() < 20)
|
||||
{
|
||||
_sessionId = readD();
|
||||
_data1 = readD();
|
||||
_data2 = readD();
|
||||
_data3 = readD();
|
||||
_data4 = readD();
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
_sessionId = readD();
|
||||
_data1 = readD();
|
||||
_data2 = readD();
|
||||
_data3 = readD();
|
||||
_data4 = readD();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -26,7 +26,6 @@ import javax.crypto.Cipher;
|
||||
import com.l2jmobius.Config;
|
||||
import com.l2jmobius.loginserver.GameServerTable.GameServerInfo;
|
||||
import com.l2jmobius.loginserver.LoginController;
|
||||
import com.l2jmobius.loginserver.LoginController.AuthLoginResult;
|
||||
import com.l2jmobius.loginserver.model.data.AccountInfo;
|
||||
import com.l2jmobius.loginserver.network.L2LoginClient;
|
||||
import com.l2jmobius.loginserver.network.L2LoginClient.LoginClientState;
|
||||
@@ -149,8 +148,7 @@ public class RequestAuthLogin extends L2LoginClientPacket
|
||||
return;
|
||||
}
|
||||
|
||||
final AuthLoginResult result = lc.tryCheckinAccount(client, clientAddr, info);
|
||||
switch (result)
|
||||
switch (lc.tryCheckinAccount(client, clientAddr, info))
|
||||
{
|
||||
case AUTH_SUCCESS:
|
||||
{
|
||||
|
@@ -64,14 +64,14 @@ public class RequestServerLogin extends L2LoginClientPacket
|
||||
@Override
|
||||
public boolean readImpl()
|
||||
{
|
||||
if (super._buf.remaining() >= 9)
|
||||
if (super._buf.remaining() < 9)
|
||||
{
|
||||
_skey1 = readD();
|
||||
_skey2 = readD();
|
||||
_serverId = readC();
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
_skey1 = readD();
|
||||
_skey2 = readD();
|
||||
_serverId = readC();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -73,8 +73,7 @@ public class ChangePassword extends BaseRecievePacket
|
||||
{
|
||||
final MessageDigest md = MessageDigest.getInstance("SHA");
|
||||
|
||||
byte[] raw = curpass.getBytes("UTF-8");
|
||||
raw = md.digest(raw);
|
||||
final byte[] raw = md.digest(curpass.getBytes("UTF-8"));
|
||||
final String curpassEnc = Base64.getEncoder().encodeToString(raw);
|
||||
String pass = null;
|
||||
int passUpdated = 0;
|
||||
@@ -95,9 +94,7 @@ public class ChangePassword extends BaseRecievePacket
|
||||
|
||||
if (curpassEnc.equals(pass))
|
||||
{
|
||||
byte[] password = newpass.getBytes("UTF-8");
|
||||
password = md.digest(password);
|
||||
|
||||
final byte[] password = md.digest(newpass.getBytes("UTF-8"));
|
||||
// SQL connection
|
||||
try (Connection con = DatabaseFactory.getInstance().getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("UPDATE accounts SET password=? WHERE login=?"))
|
||||
|
@@ -65,8 +65,8 @@ public class GameServerAuth extends BaseRecievePacket
|
||||
super(decrypt);
|
||||
_server = server;
|
||||
_desiredId = readC();
|
||||
_acceptAlternativeId = (readC() == 0 ? false : true);
|
||||
_hostReserved = (readC() == 0 ? false : true);
|
||||
_acceptAlternativeId = readC() != 0;
|
||||
_hostReserved = readC() != 0;
|
||||
_port = readH();
|
||||
_maxPlayers = readD();
|
||||
int size = readD();
|
||||
@@ -123,53 +123,36 @@ public class GameServerAuth extends BaseRecievePacket
|
||||
}
|
||||
else
|
||||
{
|
||||
// there is already a server registered with the desired id and different hex id
|
||||
// try to register this one with an alternative id
|
||||
if (Config.ACCEPT_NEW_GAMESERVER && _acceptAlternativeId)
|
||||
if (!Config.ACCEPT_NEW_GAMESERVER || !_acceptAlternativeId)
|
||||
{
|
||||
gsi = new GameServerInfo(id, hexId, _server);
|
||||
if (gameServerTable.registerWithFirstAvailableId(gsi))
|
||||
{
|
||||
_server.attachGameServerInfo(gsi, _port, _hosts, _maxPlayers);
|
||||
gameServerTable.registerServerOnDB(gsi);
|
||||
}
|
||||
else
|
||||
{
|
||||
_server.forceClose(LoginServerFail.REASON_NO_FREE_ID);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// server id is already taken, and we cant get a new one for you
|
||||
_server.forceClose(LoginServerFail.REASON_WRONG_HEXID);
|
||||
return false;
|
||||
}
|
||||
gsi = new GameServerInfo(id, hexId, _server);
|
||||
if (!gameServerTable.registerWithFirstAvailableId(gsi))
|
||||
{
|
||||
_server.forceClose(LoginServerFail.REASON_NO_FREE_ID);
|
||||
return false;
|
||||
}
|
||||
_server.attachGameServerInfo(gsi, _port, _hosts, _maxPlayers);
|
||||
gameServerTable.registerServerOnDB(gsi);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// can we register on this id?
|
||||
if (Config.ACCEPT_NEW_GAMESERVER)
|
||||
{
|
||||
gsi = new GameServerInfo(id, hexId, _server);
|
||||
if (gameServerTable.register(id, gsi))
|
||||
{
|
||||
_server.attachGameServerInfo(gsi, _port, _hosts, _maxPlayers);
|
||||
gameServerTable.registerServerOnDB(gsi);
|
||||
}
|
||||
else
|
||||
{
|
||||
// some one took this ID meanwhile
|
||||
_server.forceClose(LoginServerFail.REASON_ID_RESERVED);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
if (!Config.ACCEPT_NEW_GAMESERVER)
|
||||
{
|
||||
_server.forceClose(LoginServerFail.REASON_WRONG_HEXID);
|
||||
return false;
|
||||
}
|
||||
gsi = new GameServerInfo(id, hexId, _server);
|
||||
if (!gameServerTable.register(id, gsi))
|
||||
{
|
||||
_server.forceClose(LoginServerFail.REASON_ID_RESERVED);
|
||||
return false;
|
||||
}
|
||||
_server.attachGameServerInfo(gsi, _port, _hosts, _maxPlayers);
|
||||
gameServerTable.registerServerOnDB(gsi);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@@ -48,7 +48,7 @@ public class RequestTempBan extends BaseRecievePacket
|
||||
_accountName = readS();
|
||||
_ip = readS();
|
||||
_banTime = readQ();
|
||||
final boolean haveReason = readC() == 0 ? false : true;
|
||||
final boolean haveReason = readC() != 0;
|
||||
if (haveReason)
|
||||
{
|
||||
_banReason = readS();
|
||||
|
@@ -71,40 +71,42 @@ public class ServerStatus extends BaseRecievePacket
|
||||
super(decrypt);
|
||||
|
||||
final GameServerInfo gsi = GameServerTable.getInstance().getRegisteredGameServerById(server.getServerId());
|
||||
if (gsi != null)
|
||||
if (gsi == null)
|
||||
{
|
||||
final int size = readD();
|
||||
for (int i = 0; i < size; i++)
|
||||
return;
|
||||
}
|
||||
|
||||
final int size = readD();
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
final int type = readD();
|
||||
final int value = readD();
|
||||
switch (type)
|
||||
{
|
||||
final int type = readD();
|
||||
final int value = readD();
|
||||
switch (type)
|
||||
case SERVER_LIST_STATUS:
|
||||
{
|
||||
case SERVER_LIST_STATUS:
|
||||
{
|
||||
gsi.setStatus(value);
|
||||
break;
|
||||
}
|
||||
case SERVER_LIST_SQUARE_BRACKET:
|
||||
{
|
||||
gsi.setShowingBrackets(value == ON);
|
||||
break;
|
||||
}
|
||||
case MAX_PLAYERS:
|
||||
{
|
||||
gsi.setMaxPlayers(value);
|
||||
break;
|
||||
}
|
||||
case SERVER_TYPE:
|
||||
{
|
||||
gsi.setServerType(value);
|
||||
break;
|
||||
}
|
||||
case SERVER_AGE:
|
||||
{
|
||||
gsi.setAgeLimit(value);
|
||||
break;
|
||||
}
|
||||
gsi.setStatus(value);
|
||||
break;
|
||||
}
|
||||
case SERVER_LIST_SQUARE_BRACKET:
|
||||
{
|
||||
gsi.setShowingBrackets(value == ON);
|
||||
break;
|
||||
}
|
||||
case MAX_PLAYERS:
|
||||
{
|
||||
gsi.setMaxPlayers(value);
|
||||
break;
|
||||
}
|
||||
case SERVER_TYPE:
|
||||
{
|
||||
gsi.setServerType(value);
|
||||
break;
|
||||
}
|
||||
case SERVER_AGE:
|
||||
{
|
||||
gsi.setAgeLimit(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -103,7 +103,7 @@ public final class ServerList extends L2LoginServerPacket
|
||||
_ageLimit = 0;
|
||||
_brackets = gsi.isShowingBrackets();
|
||||
// If server GM-only - show status only to GMs
|
||||
_status = gsi.getStatus() != ServerStatus.STATUS_GM_ONLY ? gsi.getStatus() : client.getAccessLevel() > 0 ? gsi.getStatus() : ServerStatus.STATUS_DOWN;
|
||||
_status = (gsi.getStatus() == ServerStatus.STATUS_GM_ONLY) && (client.getAccessLevel() <= 0) ? ServerStatus.STATUS_DOWN : gsi.getStatus();
|
||||
_serverId = gsi.getId();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user