LoginController.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.loginserver;
  16. import java.net.InetAddress;
  17. import java.net.UnknownHostException;
  18. import java.security.GeneralSecurityException;
  19. import java.security.KeyPairGenerator;
  20. import java.security.MessageDigest;
  21. import java.security.interfaces.RSAPrivateKey;
  22. import java.security.spec.RSAKeyGenParameterSpec;
  23. import java.sql.PreparedStatement;
  24. import java.sql.ResultSet;
  25. import java.util.Collection;
  26. import java.util.Map;
  27. import java.util.logging.Logger;
  28. import javax.crypto.Cipher;
  29. import javolution.util.FastMap;
  30. import javolution.util.FastSet;
  31. import javolution.util.FastCollection.Record;
  32. import net.sf.l2j.Base64;
  33. import net.sf.l2j.Config;
  34. import net.sf.l2j.L2DatabaseFactory;
  35. import net.sf.l2j.gameserver.lib.Log;
  36. import net.sf.l2j.loginserver.GameServerTable.GameServerInfo;
  37. import net.sf.l2j.loginserver.crypt.ScrambledKeyPair;
  38. import net.sf.l2j.loginserver.gameserverpackets.ServerStatus;
  39. import net.sf.l2j.loginserver.serverpackets.LoginFail.LoginFailReason;
  40. import net.sf.l2j.util.Rnd;
  41. /**
  42. * This class ...
  43. *
  44. * @version $Revision: 1.7.4.3 $ $Date: 2005/03/27 15:30:09 $
  45. */
  46. public class LoginController
  47. {
  48. protected static final Logger _log = Logger.getLogger(LoginController.class.getName());
  49. private static LoginController _instance;
  50. /** Time before kicking the client if he didnt logged yet */
  51. private final static int LOGIN_TIMEOUT = 60*1000;
  52. /** Clients that are on the LS but arent assocated with a account yet*/
  53. protected FastSet<L2LoginClient> _clients = new FastSet<L2LoginClient>();
  54. /** Authed Clients on LoginServer*/
  55. protected FastMap<String, L2LoginClient> _loginServerClients = new FastMap<String, L2LoginClient>().setShared(true);
  56. private Map<InetAddress, BanInfo> _bannedIps = new FastMap<InetAddress, BanInfo>().setShared(true);
  57. private Map<InetAddress, FailedLoginAttempt> _hackProtection;
  58. protected ScrambledKeyPair[] _keyPairs;
  59. protected byte[][] _blowfishKeys;
  60. private static final int BLOWFISH_KEYS = 20;
  61. public static void load() throws GeneralSecurityException
  62. {
  63. if (_instance == null)
  64. {
  65. _instance = new LoginController();
  66. }
  67. else
  68. {
  69. throw new IllegalStateException("LoginController can only be loaded a single time.");
  70. }
  71. }
  72. public static LoginController getInstance()
  73. {
  74. return _instance;
  75. }
  76. private LoginController() throws GeneralSecurityException
  77. {
  78. _log.info("Loading LoginContoller...");
  79. _hackProtection = new FastMap<InetAddress, FailedLoginAttempt>();
  80. _keyPairs = new ScrambledKeyPair[10];
  81. KeyPairGenerator keygen = null;
  82. keygen = KeyPairGenerator.getInstance("RSA");
  83. RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4);
  84. keygen.initialize(spec);
  85. //generate the initial set of keys
  86. for (int i = 0; i < 10; i++)
  87. {
  88. _keyPairs[i] = new ScrambledKeyPair(keygen.generateKeyPair());
  89. }
  90. _log.info("Cached 10 KeyPairs for RSA communication");
  91. testCipher((RSAPrivateKey) _keyPairs[0]._pair.getPrivate());
  92. // Store keys for blowfish communication
  93. generateBlowFishKeys();
  94. }
  95. /**
  96. * This is mostly to force the initialization of the Crypto Implementation, avoiding it being done on runtime when its first needed.<BR>
  97. * In short it avoids the worst-case execution time on runtime by doing it on loading.
  98. * @param key Any private RSA Key just for testing purposes.
  99. * @throws GeneralSecurityException if a underlying exception was thrown by the Cipher
  100. */
  101. private void testCipher(RSAPrivateKey key) throws GeneralSecurityException
  102. {
  103. // avoid worst-case execution, KenM
  104. Cipher rsaCipher = Cipher.getInstance("RSA/ECB/nopadding");
  105. rsaCipher.init(Cipher.DECRYPT_MODE, key);
  106. }
  107. private void generateBlowFishKeys()
  108. {
  109. _blowfishKeys = new byte[BLOWFISH_KEYS][16];
  110. for (int i = 0; i < BLOWFISH_KEYS; i++)
  111. {
  112. for (int j = 0; j < _blowfishKeys[i].length; j++)
  113. {
  114. _blowfishKeys[i][j] = (byte) (Rnd.nextInt(255)+1);
  115. }
  116. }
  117. _log.info("Stored "+_blowfishKeys.length+" keys for Blowfish communication");
  118. }
  119. /**
  120. * @return Returns a random key
  121. */
  122. public byte[] getBlowfishKey()
  123. {
  124. return _blowfishKeys[(int) (Math.random()*BLOWFISH_KEYS)];
  125. }
  126. public void addLoginClient(L2LoginClient client)
  127. {
  128. synchronized (_clients)
  129. {
  130. _clients.add(client);
  131. }
  132. }
  133. public void removeLoginClient(L2LoginClient client)
  134. {
  135. synchronized (_clients)
  136. {
  137. _clients.remove(client);
  138. }
  139. }
  140. public SessionKey assignSessionKeyToClient(String account, L2LoginClient client)
  141. {
  142. SessionKey key;
  143. key = new SessionKey(Rnd.nextInt(), Rnd.nextInt(), Rnd.nextInt(), Rnd.nextInt());
  144. _loginServerClients.put(account, client);
  145. return key;
  146. }
  147. public void removeAuthedLoginClient(String account)
  148. {
  149. _loginServerClients.remove(account);
  150. }
  151. public boolean isAccountInLoginServer(String account)
  152. {
  153. return _loginServerClients.containsKey(account);
  154. }
  155. public L2LoginClient getAuthedClient(String account)
  156. {
  157. return _loginServerClients.get(account);
  158. }
  159. public static enum AuthLoginResult { INVALID_PASSWORD, ACCOUNT_BANNED, ALREADY_ON_LS, ALREADY_ON_GS, AUTH_SUCCESS };
  160. public AuthLoginResult tryAuthLogin(String account, String password, L2LoginClient client) throws HackingException
  161. {
  162. AuthLoginResult ret = AuthLoginResult.INVALID_PASSWORD;
  163. // check auth
  164. if (loginValid(account, password, client))
  165. {
  166. // login was successful, verify presence on Gameservers
  167. ret = AuthLoginResult.ALREADY_ON_GS;
  168. if (!isAccountInAnyGameServer(account))
  169. {
  170. // account isnt on any GS verify LS itself
  171. ret = AuthLoginResult.ALREADY_ON_LS;
  172. // dont allow 2 simultaneous login
  173. synchronized (_loginServerClients)
  174. {
  175. if (!_loginServerClients.containsKey(account))
  176. {
  177. _loginServerClients.put(account, client);
  178. ret = AuthLoginResult.AUTH_SUCCESS;
  179. // remove him from the non-authed list
  180. removeLoginClient(client);
  181. }
  182. }
  183. }
  184. }
  185. else
  186. {
  187. if (client.getAccessLevel() < 0)
  188. {
  189. ret = AuthLoginResult.ACCOUNT_BANNED;
  190. }
  191. }
  192. return ret;
  193. }
  194. /**
  195. * Adds the address to the ban list of the login server, with the given duration.
  196. *
  197. * @param address The Address to be banned.
  198. * @param expiration Timestamp in miliseconds when this ban expires
  199. * @throws UnknownHostException if the address is invalid.
  200. */
  201. public void addBanForAddress(String address, long expiration) throws UnknownHostException
  202. {
  203. InetAddress netAddress = InetAddress.getByName(address);
  204. _bannedIps.put(netAddress, new BanInfo(netAddress, expiration));
  205. }
  206. /**
  207. * Adds the address to the ban list of the login server, with the given duration.
  208. *
  209. * @param address The Address to be banned.
  210. * @param duration is miliseconds
  211. */
  212. public void addBanForAddress(InetAddress address, long duration)
  213. {
  214. _bannedIps.put(address, new BanInfo(address, System.currentTimeMillis() + duration));
  215. }
  216. public boolean isBannedAddress(InetAddress address)
  217. {
  218. BanInfo bi = _bannedIps.get(address);
  219. if (bi != null)
  220. {
  221. if (bi.hasExpired())
  222. {
  223. _bannedIps.remove(address);
  224. return false;
  225. }
  226. else
  227. {
  228. return true;
  229. }
  230. }
  231. return false;
  232. }
  233. public Map<InetAddress, BanInfo> getBannedIps()
  234. {
  235. return _bannedIps;
  236. }
  237. /**
  238. * Remove the specified address from the ban list
  239. * @param address The address to be removed from the ban list
  240. * @return true if the ban was removed, false if there was no ban for this ip
  241. */
  242. public boolean removeBanForAddress(InetAddress address)
  243. {
  244. return _bannedIps.remove(address) != null;
  245. }
  246. /**
  247. * Remove the specified address from the ban list
  248. * @param address The address to be removed from the ban list
  249. * @return true if the ban was removed, false if there was no ban for this ip or the address was invalid.
  250. */
  251. public boolean removeBanForAddress(String address)
  252. {
  253. try
  254. {
  255. return this.removeBanForAddress(InetAddress.getByName(address));
  256. }
  257. catch (UnknownHostException e)
  258. {
  259. return false;
  260. }
  261. }
  262. public SessionKey getKeyForAccount(String account)
  263. {
  264. L2LoginClient client = _loginServerClients.get(account);
  265. if (client != null)
  266. {
  267. return client.getSessionKey();
  268. }
  269. return null;
  270. }
  271. public int getOnlinePlayerCount(int serverId)
  272. {
  273. GameServerInfo gsi = GameServerTable.getInstance().getRegisteredGameServerById(serverId);
  274. if (gsi != null && gsi.isAuthed())
  275. {
  276. return gsi.getCurrentPlayerCount();
  277. }
  278. return 0;
  279. }
  280. public boolean isAccountInAnyGameServer(String account)
  281. {
  282. Collection<GameServerInfo> serverList = GameServerTable.getInstance().getRegisteredGameServers().values();
  283. for (GameServerInfo gsi : serverList)
  284. {
  285. GameServerThread gst = gsi.getGameServerThread();
  286. if (gst != null && gst.hasAccountOnGameServer(account))
  287. {
  288. return true;
  289. }
  290. }
  291. return false;
  292. }
  293. public GameServerInfo getAccountOnGameServer(String account)
  294. {
  295. Collection<GameServerInfo> serverList = GameServerTable.getInstance().getRegisteredGameServers().values();
  296. for (GameServerInfo gsi : serverList)
  297. {
  298. GameServerThread gst = gsi.getGameServerThread();
  299. if (gst != null && gst.hasAccountOnGameServer(account))
  300. {
  301. return gsi;
  302. }
  303. }
  304. return null;
  305. }
  306. public int getTotalOnlinePlayerCount()
  307. {
  308. int total = 0;
  309. Collection<GameServerInfo> serverList = GameServerTable.getInstance().getRegisteredGameServers().values();
  310. for (GameServerInfo gsi : serverList)
  311. {
  312. if (gsi.isAuthed())
  313. {
  314. total += gsi.getCurrentPlayerCount();
  315. }
  316. }
  317. return total;
  318. }
  319. public int getMaxAllowedOnlinePlayers(int id)
  320. {
  321. GameServerInfo gsi = GameServerTable.getInstance().getRegisteredGameServerById(id);
  322. if (gsi != null)
  323. {
  324. return gsi.getMaxPlayers();
  325. }
  326. return 0;
  327. }
  328. /**
  329. *
  330. * @return
  331. */
  332. public boolean isLoginPossible(L2LoginClient client, int serverId)
  333. {
  334. GameServerInfo gsi = GameServerTable.getInstance().getRegisteredGameServerById(serverId);
  335. int access = client.getAccessLevel();
  336. if (gsi != null && gsi.isAuthed())
  337. {
  338. boolean loginOk = (gsi.getCurrentPlayerCount() < gsi.getMaxPlayers() && gsi.getStatus() != ServerStatus.STATUS_GM_ONLY) || access >= Config.GM_MIN;
  339. if (loginOk && client.getLastServer() != serverId)
  340. {
  341. java.sql.Connection con = null;
  342. PreparedStatement statement = null;
  343. try
  344. {
  345. con = L2DatabaseFactory.getInstance().getConnection();
  346. String stmt = "UPDATE accounts SET lastServer = ? WHERE login = ?";
  347. statement = con.prepareStatement(stmt);
  348. statement.setInt(1, serverId);
  349. statement.setString(2, client.getAccount());
  350. statement.executeUpdate();
  351. statement.close();
  352. }
  353. catch (Exception e)
  354. {
  355. _log.warning("Could not set lastServer: "+e);
  356. }
  357. finally
  358. {
  359. try { con.close(); } catch (Exception e) { }
  360. try { statement.close(); }
  361. catch (Exception e) { }
  362. }
  363. }
  364. return loginOk;
  365. }
  366. return false;
  367. }
  368. public void setAccountAccessLevel(String account, int banLevel)
  369. {
  370. java.sql.Connection con = null;
  371. PreparedStatement statement = null;
  372. try
  373. {
  374. con = L2DatabaseFactory.getInstance().getConnection();
  375. String stmt = "UPDATE accounts SET accessLevel=? WHERE login=?";
  376. statement = con.prepareStatement(stmt);
  377. statement.setInt(1, banLevel);
  378. statement.setString(2, account);
  379. statement.executeUpdate();
  380. statement.close();
  381. }
  382. catch (Exception e)
  383. {
  384. _log.warning("Could not set accessLevel: "+e);
  385. }
  386. finally
  387. {
  388. try
  389. {
  390. statement.close();
  391. con.close();
  392. }
  393. catch (Exception e)
  394. {
  395. }
  396. }
  397. }
  398. public boolean isGM(String user)
  399. {
  400. boolean ok = false;
  401. java.sql.Connection con = null;
  402. PreparedStatement statement = null;
  403. try
  404. {
  405. con = L2DatabaseFactory.getInstance().getConnection();
  406. statement = con.prepareStatement("SELECT accessLevel FROM accounts WHERE login=?");
  407. statement.setString(1, user);
  408. ResultSet rset = statement.executeQuery();
  409. if (rset.next())
  410. {
  411. int accessLevel = rset.getInt(1);
  412. if (accessLevel >= Config.GM_MIN)
  413. {
  414. ok = true;
  415. }
  416. }
  417. rset.close();
  418. statement.close();
  419. }
  420. catch (Exception e)
  421. {
  422. _log.warning("could not check gm state:" + e);
  423. ok = false;
  424. }
  425. finally
  426. {
  427. try
  428. {
  429. con.close();
  430. }
  431. catch (Exception e)
  432. {
  433. }
  434. try
  435. {
  436. statement.close();
  437. }
  438. catch (Exception e)
  439. {
  440. }
  441. }
  442. return ok;
  443. }
  444. /**
  445. * <p>This method returns one of the cached {@link ScrambledKeyPair ScrambledKeyPairs} for communication with Login Clients.</p>
  446. * @return a scrambled keypair
  447. */
  448. public ScrambledKeyPair getScrambledRSAKeyPair()
  449. {
  450. return _keyPairs[Rnd.nextInt(10)];
  451. }
  452. /**
  453. * user name is not case sensitive any more
  454. * @param user
  455. * @param password
  456. * @param address
  457. * @return
  458. */
  459. public boolean loginValid(String user, String password, L2LoginClient client )// throws HackingException
  460. {
  461. boolean ok = false;
  462. InetAddress address = client.getConnection().getSocket().getInetAddress();
  463. // log it anyway
  464. Log.add("'" + (user == null ? "null" : user) + "' " + (address == null ? "null" : address.getHostAddress()), "logins_ip");
  465. // player disconnected meanwhile
  466. if (address == null)
  467. {
  468. return false;
  469. }
  470. java.sql.Connection con = null;
  471. try
  472. {
  473. MessageDigest md = MessageDigest.getInstance("SHA");
  474. byte[] raw = password.getBytes("UTF-8");
  475. byte[] hash = md.digest(raw);
  476. byte[] expected = null;
  477. int access = 0;
  478. int lastServer = 1;
  479. con = L2DatabaseFactory.getInstance().getConnection();
  480. PreparedStatement statement = con.prepareStatement("SELECT password, accessLevel, lastServer FROM accounts WHERE login=?");
  481. statement.setString(1, user);
  482. ResultSet rset = statement.executeQuery();
  483. if (rset.next())
  484. {
  485. expected = Base64.decode(rset.getString("password"));
  486. access = rset.getInt("accessLevel");
  487. lastServer = rset.getInt("lastServer");
  488. if (lastServer <= 0) lastServer = 1; // minServerId is 1 in Interlude
  489. if (Config.DEBUG) _log.fine("account exists");
  490. }
  491. rset.close();
  492. statement.close();
  493. // if account doesnt exists
  494. if (expected == null)
  495. {
  496. if (Config.AUTO_CREATE_ACCOUNTS)
  497. {
  498. if ((user.length() >= 2) && (user.length() <= 14))
  499. {
  500. statement = con.prepareStatement("INSERT INTO accounts (login,password,lastactive,accessLevel,lastIP) values(?,?,?,?,?)");
  501. statement.setString(1, user);
  502. statement.setString(2, Base64.encodeBytes(hash));
  503. statement.setLong(3, System.currentTimeMillis());
  504. statement.setInt(4, 0);
  505. statement.setString(5, address.getHostAddress());
  506. statement.execute();
  507. statement.close();
  508. _log.info("created new account for " + user);
  509. return true;
  510. }
  511. _log.warning("Invalid username creation/use attempt: " + user);
  512. return false;
  513. }
  514. _log.warning("account missing for user " + user);
  515. return false;
  516. }
  517. else
  518. {
  519. // is this account banned?
  520. if (access < 0)
  521. {
  522. client.setAccessLevel(access);
  523. return false;
  524. }
  525. // check password hash
  526. ok = true;
  527. for (int i = 0; i < expected.length; i++)
  528. {
  529. if (hash[i] != expected[i])
  530. {
  531. ok = false;
  532. break;
  533. }
  534. }
  535. }
  536. if (ok)
  537. {
  538. client.setAccessLevel(access);
  539. client.setLastServer(lastServer);
  540. statement = con.prepareStatement("UPDATE accounts SET lastactive=?, lastIP=? WHERE login=?");
  541. statement.setLong(1, System.currentTimeMillis());
  542. statement.setString(2, address.getHostAddress());
  543. statement.setString(3, user);
  544. statement.execute();
  545. statement.close();
  546. }
  547. }
  548. catch (Exception e)
  549. {
  550. _log.warning("Could not check password:" + e);
  551. ok = false;
  552. }
  553. finally
  554. {
  555. try
  556. {
  557. con.close();
  558. }
  559. catch (Exception e)
  560. {
  561. }
  562. }
  563. if (!ok)
  564. {
  565. Log.add("'" + user + "' " + address.getHostAddress(), "logins_ip_fails");
  566. FailedLoginAttempt failedAttempt = _hackProtection.get(address);
  567. int failedCount;
  568. if (failedAttempt == null)
  569. {
  570. _hackProtection.put(address, new FailedLoginAttempt(address, password));
  571. failedCount = 1;
  572. }
  573. else
  574. {
  575. failedAttempt.increaseCounter(password);
  576. failedCount = failedAttempt.getCount();
  577. }
  578. if (failedCount >= Config.LOGIN_TRY_BEFORE_BAN)
  579. {
  580. _log.info("Banning '"+address.getHostAddress()+"' for "+Config.LOGIN_BLOCK_AFTER_BAN+" seconds due to "+failedCount+" invalid user/pass attempts");
  581. this.addBanForAddress(address, Config.LOGIN_BLOCK_AFTER_BAN*1000);
  582. }
  583. }
  584. else
  585. {
  586. _hackProtection.remove(address);
  587. Log.add("'" + user + "' " + address.getHostAddress(), "logins_ip");
  588. }
  589. return ok;
  590. }
  591. public boolean loginBanned(String user)
  592. {
  593. boolean ok = false;
  594. java.sql.Connection con = null;
  595. try
  596. {
  597. con = L2DatabaseFactory.getInstance().getConnection();
  598. PreparedStatement statement = con.prepareStatement("SELECT accessLevel FROM accounts WHERE login=?");
  599. statement.setString(1, user);
  600. ResultSet rset = statement.executeQuery();
  601. if (rset.next())
  602. {
  603. int accessLevel = rset.getInt(1);
  604. if (accessLevel < 0) ok = true;
  605. }
  606. rset.close();
  607. statement.close();
  608. }
  609. catch (Exception e)
  610. {
  611. // digest algo not found ??
  612. // out of bounds should not be possible
  613. _log.warning("could not check ban state:" + e);
  614. ok = false;
  615. }
  616. finally
  617. {
  618. try
  619. {
  620. con.close();
  621. }
  622. catch (Exception e)
  623. {
  624. }
  625. }
  626. return ok;
  627. }
  628. class FailedLoginAttempt
  629. {
  630. //private InetAddress _ipAddress;
  631. private int _count;
  632. private long _lastAttempTime;
  633. private String _lastPassword;
  634. public FailedLoginAttempt(InetAddress address, String lastPassword)
  635. {
  636. //_ipAddress = address;
  637. _count = 1;
  638. _lastAttempTime = System.currentTimeMillis();
  639. _lastPassword = lastPassword;
  640. }
  641. public void increaseCounter(String password)
  642. {
  643. if (!_lastPassword.equals(password))
  644. {
  645. // check if theres a long time since last wrong try
  646. if (System.currentTimeMillis() - _lastAttempTime < 300*1000)
  647. {
  648. _count++;
  649. }
  650. else
  651. {
  652. // restart the status
  653. _count = 1;
  654. }
  655. _lastPassword = password;
  656. _lastAttempTime = System.currentTimeMillis();
  657. }
  658. else //trying the same password is not brute force
  659. {
  660. _lastAttempTime = System.currentTimeMillis();
  661. }
  662. }
  663. public int getCount()
  664. {
  665. return _count;
  666. }
  667. }
  668. class BanInfo
  669. {
  670. private InetAddress _ipAddress;
  671. // Expiration
  672. private long _expiration;
  673. public BanInfo(InetAddress ipAddress, long expiration)
  674. {
  675. _ipAddress = ipAddress;
  676. _expiration = expiration;
  677. }
  678. public InetAddress getAddress()
  679. {
  680. return _ipAddress;
  681. }
  682. public boolean hasExpired()
  683. {
  684. return System.currentTimeMillis() > _expiration && _expiration > 0;
  685. }
  686. }
  687. class PurgeThread extends Thread
  688. {
  689. @Override
  690. public void run()
  691. {
  692. for (;;)
  693. {
  694. synchronized (_clients)
  695. {
  696. for (Record e = _clients.head(), end = _clients.tail(); (e = e.getNext()) != end;)
  697. {
  698. L2LoginClient client = _clients.valueOf(e);
  699. if (client.getConnectionStartTime() + LOGIN_TIMEOUT >= System.currentTimeMillis())
  700. {
  701. client.close(LoginFailReason.REASON_ACCESS_FAILED);
  702. }
  703. }
  704. }
  705. synchronized (_loginServerClients)
  706. {
  707. for (FastMap.Entry<String, L2LoginClient> e = _loginServerClients.head(), end = _loginServerClients.tail(); (e = e.getNext()) != end;)
  708. {
  709. L2LoginClient client = e.getValue();
  710. if (client.getConnectionStartTime() + LOGIN_TIMEOUT >= System.currentTimeMillis())
  711. {
  712. client.close(LoginFailReason.REASON_ACCESS_FAILED);
  713. }
  714. }
  715. }
  716. try
  717. {
  718. Thread.sleep(2*LOGIN_TIMEOUT);
  719. }
  720. catch (InterruptedException e)
  721. {
  722. e.printStackTrace();
  723. }
  724. }
  725. }
  726. }
  727. }