LoginController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.loginserver;
  20. import java.net.InetAddress;
  21. import java.net.UnknownHostException;
  22. import java.nio.charset.StandardCharsets;
  23. import java.security.GeneralSecurityException;
  24. import java.security.KeyPairGenerator;
  25. import java.security.MessageDigest;
  26. import java.security.interfaces.RSAPrivateKey;
  27. import java.security.spec.RSAKeyGenParameterSpec;
  28. import java.sql.Connection;
  29. import java.sql.PreparedStatement;
  30. import java.sql.ResultSet;
  31. import java.util.ArrayList;
  32. import java.util.Base64;
  33. import java.util.Collection;
  34. import java.util.HashMap;
  35. import java.util.List;
  36. import java.util.Map;
  37. import java.util.logging.Level;
  38. import java.util.logging.Logger;
  39. import javax.crypto.Cipher;
  40. import javolution.util.FastMap;
  41. import com.l2jserver.Config;
  42. import com.l2jserver.L2DatabaseFactory;
  43. import com.l2jserver.loginserver.GameServerTable.GameServerInfo;
  44. import com.l2jserver.loginserver.model.data.AccountInfo;
  45. import com.l2jserver.loginserver.network.L2LoginClient;
  46. import com.l2jserver.loginserver.network.gameserverpackets.ServerStatus;
  47. import com.l2jserver.loginserver.network.serverpackets.LoginFail.LoginFailReason;
  48. import com.l2jserver.util.Rnd;
  49. import com.l2jserver.util.crypt.ScrambledKeyPair;
  50. public class LoginController
  51. {
  52. protected static final Logger _log = Logger.getLogger(LoginController.class.getName());
  53. private static LoginController _instance;
  54. /** Time before kicking the client if he didn't logged yet */
  55. public static final int LOGIN_TIMEOUT = 60 * 1000;
  56. /** Authed Clients on LoginServer */
  57. protected FastMap<String, L2LoginClient> _loginServerClients = new FastMap<String, L2LoginClient>().shared();
  58. private final Map<InetAddress, Integer> _failedLoginAttemps = new HashMap<>();
  59. private final Map<InetAddress, Long> _bannedIps = new FastMap<InetAddress, Long>().shared();
  60. protected ScrambledKeyPair[] _keyPairs;
  61. private final Thread _purge;
  62. protected byte[][] _blowfishKeys;
  63. private static final int BLOWFISH_KEYS = 20;
  64. // SQL Queries
  65. private static final String USER_INFO_SELECT = "SELECT login, password, IF(? > value OR value IS NULL, accessLevel, -1) AS accessLevel, lastServer FROM accounts LEFT JOIN (account_data) ON (account_data.account_name=accounts.login AND account_data.var=\"ban_temp\") WHERE login=?";
  66. private static final String AUTOCREATE_ACCOUNTS_INSERT = "INSERT INTO accounts (login, password, lastactive, accessLevel, lastIP) values (?, ?, ?, ?, ?)";
  67. private static final String ACCOUNT_INFO_UPDATE = "UPDATE accounts SET lastactive = ?, lastIP = ? WHERE login = ?";
  68. private static final String ACCOUNT_LAST_SERVER_UPDATE = "UPDATE accounts SET lastServer = ? WHERE login = ?";
  69. private static final String ACCOUNT_ACCESS_LEVEL_UPDATE = "UPDATE accounts SET accessLevel = ? WHERE login = ?";
  70. private static final String ACCOUNT_IPS_UPDATE = "UPDATE accounts SET pcIp = ?, hop1 = ?, hop2 = ?, hop3 = ?, hop4 = ? WHERE login = ?";
  71. private static final String ACCOUNT_IPAUTH_SELECT = "SELECT * FROM accounts_ipauth WHERE login = ?";
  72. private LoginController() throws GeneralSecurityException
  73. {
  74. _log.info("Loading LoginController...");
  75. _keyPairs = new ScrambledKeyPair[10];
  76. KeyPairGenerator keygen = null;
  77. keygen = KeyPairGenerator.getInstance("RSA");
  78. RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4);
  79. keygen.initialize(spec);
  80. // generate the initial set of keys
  81. for (int i = 0; i < 10; i++)
  82. {
  83. _keyPairs[i] = new ScrambledKeyPair(keygen.generateKeyPair());
  84. }
  85. _log.info("Cached 10 KeyPairs for RSA communication");
  86. testCipher((RSAPrivateKey) _keyPairs[0]._pair.getPrivate());
  87. // Store keys for blowfish communication
  88. generateBlowFishKeys();
  89. _purge = new PurgeThread();
  90. _purge.setDaemon(true);
  91. _purge.start();
  92. }
  93. /**
  94. * This is mostly to force the initialization of the Crypto Implementation, avoiding it being done on runtime when its first needed.<BR>
  95. * In short it avoids the worst-case execution time on runtime by doing it on loading.
  96. * @param key Any private RSA Key just for testing purposes.
  97. * @throws GeneralSecurityException if a underlying exception was thrown by the Cipher
  98. */
  99. private void testCipher(RSAPrivateKey key) throws GeneralSecurityException
  100. {
  101. // avoid worst-case execution, KenM
  102. Cipher rsaCipher = Cipher.getInstance("RSA/ECB/nopadding");
  103. rsaCipher.init(Cipher.DECRYPT_MODE, key);
  104. }
  105. private void generateBlowFishKeys()
  106. {
  107. _blowfishKeys = new byte[BLOWFISH_KEYS][16];
  108. for (int i = 0; i < BLOWFISH_KEYS; i++)
  109. {
  110. for (int j = 0; j < _blowfishKeys[i].length; j++)
  111. {
  112. _blowfishKeys[i][j] = (byte) (Rnd.nextInt(255) + 1);
  113. }
  114. }
  115. _log.info("Stored " + _blowfishKeys.length + " keys for Blowfish communication");
  116. }
  117. /**
  118. * @return Returns a random key
  119. */
  120. public byte[] getBlowfishKey()
  121. {
  122. return _blowfishKeys[(int) (Math.random() * BLOWFISH_KEYS)];
  123. }
  124. public SessionKey assignSessionKeyToClient(String account, L2LoginClient client)
  125. {
  126. SessionKey key;
  127. key = new SessionKey(Rnd.nextInt(), Rnd.nextInt(), Rnd.nextInt(), Rnd.nextInt());
  128. _loginServerClients.put(account, client);
  129. return key;
  130. }
  131. public void removeAuthedLoginClient(String account)
  132. {
  133. if (account == null)
  134. {
  135. return;
  136. }
  137. _loginServerClients.remove(account);
  138. }
  139. public L2LoginClient getAuthedClient(String account)
  140. {
  141. return _loginServerClients.get(account);
  142. }
  143. public AccountInfo retriveAccountInfo(InetAddress clientAddr, String login, String password)
  144. {
  145. return retriveAccountInfo(clientAddr, login, password, true);
  146. }
  147. private void recordFailedLoginAttemp(InetAddress addr)
  148. {
  149. // We need to synchronize this!
  150. // When multiple connections from the same address fail to login at the
  151. // same time, unexpected behavior can happen.
  152. Integer failedLoginAttemps;
  153. synchronized (_failedLoginAttemps)
  154. {
  155. failedLoginAttemps = _failedLoginAttemps.get(addr);
  156. if (failedLoginAttemps == null)
  157. {
  158. failedLoginAttemps = 1;
  159. }
  160. else
  161. {
  162. ++failedLoginAttemps;
  163. }
  164. _failedLoginAttemps.put(addr, failedLoginAttemps);
  165. }
  166. if (failedLoginAttemps >= Config.LOGIN_TRY_BEFORE_BAN)
  167. {
  168. addBanForAddress(addr, Config.LOGIN_BLOCK_AFTER_BAN * 1000);
  169. // we need to clear the failed login attempts here, so after the ip ban is over the client has another 5 attempts
  170. clearFailedLoginAttemps(addr);
  171. _log.warning("Added banned address " + addr.getHostAddress() + "! Too many login attemps.");
  172. }
  173. }
  174. private void clearFailedLoginAttemps(InetAddress addr)
  175. {
  176. synchronized (_failedLoginAttemps)
  177. {
  178. _failedLoginAttemps.remove(addr);
  179. }
  180. }
  181. private AccountInfo retriveAccountInfo(InetAddress addr, String login, String password, boolean autoCreateIfEnabled)
  182. {
  183. try
  184. {
  185. MessageDigest md = MessageDigest.getInstance("SHA");
  186. byte[] raw = password.getBytes(StandardCharsets.UTF_8);
  187. String hashBase64 = Base64.getEncoder().encodeToString(md.digest(raw));
  188. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  189. PreparedStatement ps = con.prepareStatement(USER_INFO_SELECT))
  190. {
  191. ps.setString(1, Long.toString(System.currentTimeMillis()));
  192. ps.setString(2, login);
  193. try (ResultSet rset = ps.executeQuery())
  194. {
  195. if (rset.next())
  196. {
  197. if (Config.DEBUG)
  198. {
  199. _log.fine("Account '" + login + "' exists.");
  200. }
  201. AccountInfo info = new AccountInfo(rset.getString("login"), rset.getString("password"), rset.getInt("accessLevel"), rset.getInt("lastServer"));
  202. if (!info.checkPassHash(hashBase64))
  203. {
  204. // wrong password
  205. recordFailedLoginAttemp(addr);
  206. return null;
  207. }
  208. clearFailedLoginAttemps(addr);
  209. return info;
  210. }
  211. }
  212. }
  213. if (!autoCreateIfEnabled || !Config.AUTO_CREATE_ACCOUNTS)
  214. {
  215. // account does not exist and auto create account is not desired
  216. recordFailedLoginAttemp(addr);
  217. return null;
  218. }
  219. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  220. PreparedStatement ps = con.prepareStatement(AUTOCREATE_ACCOUNTS_INSERT))
  221. {
  222. ps.setString(1, login);
  223. ps.setString(2, hashBase64);
  224. ps.setLong(3, System.currentTimeMillis());
  225. ps.setInt(4, 0);
  226. ps.setString(5, addr.getHostAddress());
  227. ps.execute();
  228. }
  229. catch (Exception e)
  230. {
  231. _log.log(Level.WARNING, "Exception while auto creating account for '" + login + "'!", e);
  232. return null;
  233. }
  234. _log.info("Auto created account '" + login + "'.");
  235. return retriveAccountInfo(addr, login, password, false);
  236. }
  237. catch (Exception e)
  238. {
  239. _log.log(Level.WARNING, "Exception while retriving account info for '" + login + "'!", e);
  240. return null;
  241. }
  242. }
  243. public AuthLoginResult tryCheckinAccount(L2LoginClient client, InetAddress address, AccountInfo info)
  244. {
  245. if (info.getAccessLevel() < 0)
  246. {
  247. return AuthLoginResult.ACCOUNT_BANNED;
  248. }
  249. AuthLoginResult ret = AuthLoginResult.INVALID_PASSWORD;
  250. // check auth
  251. if (canCheckin(client, address, info))
  252. {
  253. // login was successful, verify presence on Gameservers
  254. ret = AuthLoginResult.ALREADY_ON_GS;
  255. if (!isAccountInAnyGameServer(info.getLogin()))
  256. {
  257. // account isnt on any GS verify LS itself
  258. ret = AuthLoginResult.ALREADY_ON_LS;
  259. if (_loginServerClients.putIfAbsent(info.getLogin(), client) == null)
  260. {
  261. ret = AuthLoginResult.AUTH_SUCCESS;
  262. }
  263. }
  264. }
  265. return ret;
  266. }
  267. /**
  268. * Adds the address to the ban list of the login server, with the given end time in milliseconds.
  269. * @param address The Address to be banned.
  270. * @param expiration Timestamp in milliseconds when this ban expires
  271. * @throws UnknownHostException if the address is invalid.
  272. */
  273. public void addBanForAddress(String address, long expiration) throws UnknownHostException
  274. {
  275. _bannedIps.putIfAbsent(InetAddress.getByName(address), expiration);
  276. }
  277. /**
  278. * Adds the address to the ban list of the login server, with the given duration.
  279. * @param address The Address to be banned.
  280. * @param duration is milliseconds
  281. */
  282. public void addBanForAddress(InetAddress address, long duration)
  283. {
  284. _bannedIps.putIfAbsent(address, System.currentTimeMillis() + duration);
  285. }
  286. public boolean isBannedAddress(InetAddress address)
  287. {
  288. String[] parts = address.getHostAddress().split("\\.");
  289. Long bi = _bannedIps.get(address);
  290. if (bi == null)
  291. {
  292. bi = _bannedIps.get(parts[0] + "." + parts[1] + "." + parts[2] + ".0");
  293. }
  294. if (bi == null)
  295. {
  296. bi = _bannedIps.get(parts[0] + "." + parts[1] + ".0.0");
  297. }
  298. if (bi == null)
  299. {
  300. bi = _bannedIps.get(parts[0] + ".0.0.0");
  301. }
  302. if (bi != null)
  303. {
  304. if ((bi > 0) && (bi < System.currentTimeMillis()))
  305. {
  306. _bannedIps.remove(address);
  307. _log.info("Removed expired ip address ban " + address.getHostAddress() + ".");
  308. return false;
  309. }
  310. return true;
  311. }
  312. return false;
  313. }
  314. public Map<InetAddress, Long> getBannedIps()
  315. {
  316. return _bannedIps;
  317. }
  318. /**
  319. * Remove the specified address from the ban list
  320. * @param address The address to be removed from the ban list
  321. * @return true if the ban was removed, false if there was no ban for this ip
  322. */
  323. public boolean removeBanForAddress(InetAddress address)
  324. {
  325. return _bannedIps.remove(address.getHostAddress()) != null;
  326. }
  327. /**
  328. * Remove the specified address from the ban list
  329. * @param address The address to be removed from the ban list
  330. * @return true if the ban was removed, false if there was no ban for this ip or the address was invalid.
  331. */
  332. public boolean removeBanForAddress(String address)
  333. {
  334. try
  335. {
  336. return this.removeBanForAddress(InetAddress.getByName(address));
  337. }
  338. catch (UnknownHostException e)
  339. {
  340. return false;
  341. }
  342. }
  343. public SessionKey getKeyForAccount(String account)
  344. {
  345. L2LoginClient client = _loginServerClients.get(account);
  346. if (client != null)
  347. {
  348. return client.getSessionKey();
  349. }
  350. return null;
  351. }
  352. public boolean isAccountInAnyGameServer(String account)
  353. {
  354. Collection<GameServerInfo> serverList = GameServerTable.getInstance().getRegisteredGameServers().values();
  355. for (GameServerInfo gsi : serverList)
  356. {
  357. GameServerThread gst = gsi.getGameServerThread();
  358. if ((gst != null) && gst.hasAccountOnGameServer(account))
  359. {
  360. return true;
  361. }
  362. }
  363. return false;
  364. }
  365. public GameServerInfo getAccountOnGameServer(String account)
  366. {
  367. Collection<GameServerInfo> serverList = GameServerTable.getInstance().getRegisteredGameServers().values();
  368. for (GameServerInfo gsi : serverList)
  369. {
  370. GameServerThread gst = gsi.getGameServerThread();
  371. if ((gst != null) && gst.hasAccountOnGameServer(account))
  372. {
  373. return gsi;
  374. }
  375. }
  376. return null;
  377. }
  378. public void getCharactersOnAccount(String account)
  379. {
  380. Collection<GameServerInfo> serverList = GameServerTable.getInstance().getRegisteredGameServers().values();
  381. for (GameServerInfo gsi : serverList)
  382. {
  383. if (gsi.isAuthed())
  384. {
  385. gsi.getGameServerThread().requestCharacters(account);
  386. }
  387. }
  388. }
  389. /**
  390. * @param client
  391. * @param serverId
  392. * @return
  393. */
  394. public boolean isLoginPossible(L2LoginClient client, int serverId)
  395. {
  396. GameServerInfo gsi = GameServerTable.getInstance().getRegisteredGameServerById(serverId);
  397. int access = client.getAccessLevel();
  398. if ((gsi != null) && gsi.isAuthed())
  399. {
  400. boolean loginOk = ((gsi.getCurrentPlayerCount() < gsi.getMaxPlayers()) && (gsi.getStatus() != ServerStatus.STATUS_GM_ONLY)) || (access > 0);
  401. if (loginOk && (client.getLastServer() != serverId))
  402. {
  403. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  404. PreparedStatement ps = con.prepareStatement(ACCOUNT_LAST_SERVER_UPDATE))
  405. {
  406. ps.setInt(1, serverId);
  407. ps.setString(2, client.getAccount());
  408. ps.executeUpdate();
  409. }
  410. catch (Exception e)
  411. {
  412. _log.log(Level.WARNING, "Could not set lastServer: " + e.getMessage(), e);
  413. }
  414. }
  415. return loginOk;
  416. }
  417. return false;
  418. }
  419. public void setAccountAccessLevel(String account, int banLevel)
  420. {
  421. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  422. PreparedStatement ps = con.prepareStatement(ACCOUNT_ACCESS_LEVEL_UPDATE))
  423. {
  424. ps.setInt(1, banLevel);
  425. ps.setString(2, account);
  426. ps.executeUpdate();
  427. }
  428. catch (Exception e)
  429. {
  430. _log.log(Level.WARNING, "Could not set accessLevel: " + e.getMessage(), e);
  431. }
  432. }
  433. public void setAccountLastTracert(String account, String pcIp, String hop1, String hop2, String hop3, String hop4)
  434. {
  435. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  436. PreparedStatement ps = con.prepareStatement(ACCOUNT_IPS_UPDATE))
  437. {
  438. ps.setString(1, pcIp);
  439. ps.setString(2, hop1);
  440. ps.setString(3, hop2);
  441. ps.setString(4, hop3);
  442. ps.setString(5, hop4);
  443. ps.setString(6, account);
  444. ps.executeUpdate();
  445. }
  446. catch (Exception e)
  447. {
  448. _log.log(Level.WARNING, "Could not set last tracert: " + e.getMessage(), e);
  449. }
  450. }
  451. public void setCharactersOnServer(String account, int charsNum, long[] timeToDel, int serverId)
  452. {
  453. L2LoginClient client = _loginServerClients.get(account);
  454. if (client == null)
  455. {
  456. return;
  457. }
  458. if (charsNum > 0)
  459. {
  460. client.setCharsOnServ(serverId, charsNum);
  461. }
  462. if (timeToDel.length > 0)
  463. {
  464. client.serCharsWaitingDelOnServ(serverId, timeToDel);
  465. }
  466. }
  467. /**
  468. * <p>
  469. * This method returns one of the cached {@link ScrambledKeyPair ScrambledKeyPairs} for communication with Login Clients.
  470. * </p>
  471. * @return a scrambled keypair
  472. */
  473. public ScrambledKeyPair getScrambledRSAKeyPair()
  474. {
  475. return _keyPairs[Rnd.nextInt(10)];
  476. }
  477. /**
  478. * @param client the client
  479. * @param address client host address
  480. * @param info the account info to checkin
  481. * @return true when ok to checkin, false otherwise
  482. */
  483. public boolean canCheckin(L2LoginClient client, InetAddress address, AccountInfo info)
  484. {
  485. try
  486. {
  487. List<InetAddress> ipWhiteList = new ArrayList<>();
  488. List<InetAddress> ipBlackList = new ArrayList<>();
  489. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  490. PreparedStatement ps = con.prepareStatement(ACCOUNT_IPAUTH_SELECT))
  491. {
  492. ps.setString(1, info.getLogin());
  493. try (ResultSet rset = ps.executeQuery())
  494. {
  495. String ip, type;
  496. while (rset.next())
  497. {
  498. ip = rset.getString("ip");
  499. type = rset.getString("type");
  500. if (!isValidIPAddress(ip))
  501. {
  502. continue;
  503. }
  504. else if (type.equals("allow"))
  505. {
  506. ipWhiteList.add(InetAddress.getByName(ip));
  507. }
  508. else if (type.equals("deny"))
  509. {
  510. ipBlackList.add(InetAddress.getByName(ip));
  511. }
  512. }
  513. }
  514. }
  515. // Check IP
  516. if (!ipWhiteList.isEmpty() || !ipBlackList.isEmpty())
  517. {
  518. if (!ipWhiteList.isEmpty() && !ipWhiteList.contains(address))
  519. {
  520. _log.warning("Account checkin attemp from address(" + address.getHostAddress() + ") not present on whitelist for account '" + info.getLogin() + "'.");
  521. return false;
  522. }
  523. if (!ipBlackList.isEmpty() && ipBlackList.contains(address))
  524. {
  525. _log.warning("Account checkin attemp from address(" + address.getHostAddress() + ") on blacklist for account '" + info.getLogin() + "'.");
  526. return false;
  527. }
  528. }
  529. client.setAccessLevel(info.getAccessLevel());
  530. client.setLastServer(info.getLastServer());
  531. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  532. PreparedStatement ps = con.prepareStatement(ACCOUNT_INFO_UPDATE))
  533. {
  534. ps.setLong(1, System.currentTimeMillis());
  535. ps.setString(2, address.getHostAddress());
  536. ps.setString(3, info.getLogin());
  537. ps.execute();
  538. }
  539. return true;
  540. }
  541. catch (Exception e)
  542. {
  543. _log.log(Level.WARNING, "Could not finish login process!", e);
  544. return false;
  545. }
  546. }
  547. public boolean isValidIPAddress(String ipAddress)
  548. {
  549. String[] parts = ipAddress.split("\\.");
  550. if (parts.length != 4)
  551. {
  552. return false;
  553. }
  554. for (String s : parts)
  555. {
  556. int i = Integer.parseInt(s);
  557. if ((i < 0) || (i > 255))
  558. {
  559. return false;
  560. }
  561. }
  562. return true;
  563. }
  564. public static void load() throws GeneralSecurityException
  565. {
  566. synchronized (LoginController.class)
  567. {
  568. if (_instance == null)
  569. {
  570. _instance = new LoginController();
  571. }
  572. else
  573. {
  574. throw new IllegalStateException("LoginController can only be loaded a single time.");
  575. }
  576. }
  577. }
  578. public static LoginController getInstance()
  579. {
  580. return _instance;
  581. }
  582. class PurgeThread extends Thread
  583. {
  584. public PurgeThread()
  585. {
  586. setName("PurgeThread");
  587. }
  588. @Override
  589. public void run()
  590. {
  591. while (!isInterrupted())
  592. {
  593. for (L2LoginClient client : _loginServerClients.values())
  594. {
  595. if (client == null)
  596. {
  597. continue;
  598. }
  599. if ((client.getConnectionStartTime() + LOGIN_TIMEOUT) < System.currentTimeMillis())
  600. {
  601. client.close(LoginFailReason.REASON_ACCESS_FAILED);
  602. }
  603. }
  604. try
  605. {
  606. Thread.sleep(LOGIN_TIMEOUT / 2);
  607. }
  608. catch (InterruptedException e)
  609. {
  610. return;
  611. }
  612. }
  613. }
  614. }
  615. public static enum AuthLoginResult
  616. {
  617. INVALID_PASSWORD,
  618. ACCOUNT_BANNED,
  619. ALREADY_ON_LS,
  620. ALREADY_ON_GS,
  621. AUTH_SUCCESS
  622. }
  623. }