LoginServerThread.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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 com.l2jserver.gameserver;
  16. import java.io.BufferedOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.math.BigInteger;
  21. import java.net.Socket;
  22. import java.net.SocketException;
  23. import java.net.UnknownHostException;
  24. import java.security.GeneralSecurityException;
  25. import java.security.KeyFactory;
  26. import java.security.interfaces.RSAPublicKey;
  27. import java.security.spec.RSAKeyGenParameterSpec;
  28. import java.security.spec.RSAPublicKeySpec;
  29. import java.sql.Connection;
  30. import java.sql.PreparedStatement;
  31. import java.sql.ResultSet;
  32. import java.sql.SQLException;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. import java.util.logging.Level;
  36. import java.util.logging.LogRecord;
  37. import java.util.logging.Logger;
  38. import javolution.util.FastList;
  39. import javolution.util.FastMap;
  40. import com.l2jserver.Config;
  41. import com.l2jserver.L2DatabaseFactory;
  42. import com.l2jserver.gameserver.model.L2World;
  43. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  44. import com.l2jserver.gameserver.network.L2GameClient;
  45. import com.l2jserver.gameserver.network.L2GameClient.GameClientState;
  46. import com.l2jserver.gameserver.network.SystemMessageId;
  47. import com.l2jserver.gameserver.network.gameserverpackets.AuthRequest;
  48. import com.l2jserver.gameserver.network.gameserverpackets.BlowFishKey;
  49. import com.l2jserver.gameserver.network.gameserverpackets.ChangeAccessLevel;
  50. import com.l2jserver.gameserver.network.gameserverpackets.ChangePassword;
  51. import com.l2jserver.gameserver.network.gameserverpackets.PlayerAuthRequest;
  52. import com.l2jserver.gameserver.network.gameserverpackets.PlayerInGame;
  53. import com.l2jserver.gameserver.network.gameserverpackets.PlayerLogout;
  54. import com.l2jserver.gameserver.network.gameserverpackets.PlayerTracert;
  55. import com.l2jserver.gameserver.network.gameserverpackets.ReplyCharacters;
  56. import com.l2jserver.gameserver.network.gameserverpackets.SendMail;
  57. import com.l2jserver.gameserver.network.gameserverpackets.ServerStatus;
  58. import com.l2jserver.gameserver.network.gameserverpackets.TempBan;
  59. import com.l2jserver.gameserver.network.loginserverpackets.AuthResponse;
  60. import com.l2jserver.gameserver.network.loginserverpackets.ChangePasswordResponse;
  61. import com.l2jserver.gameserver.network.loginserverpackets.InitLS;
  62. import com.l2jserver.gameserver.network.loginserverpackets.KickPlayer;
  63. import com.l2jserver.gameserver.network.loginserverpackets.LoginServerFail;
  64. import com.l2jserver.gameserver.network.loginserverpackets.PlayerAuthResponse;
  65. import com.l2jserver.gameserver.network.loginserverpackets.RequestCharacters;
  66. import com.l2jserver.gameserver.network.serverpackets.CharSelectionInfo;
  67. import com.l2jserver.gameserver.network.serverpackets.LoginFail;
  68. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  69. import com.l2jserver.util.Util;
  70. import com.l2jserver.util.crypt.NewCrypt;
  71. import com.l2jserver.util.network.BaseSendablePacket;
  72. public class LoginServerThread extends Thread
  73. {
  74. protected static final Logger _log = Logger.getLogger(LoginServerThread.class.getName());
  75. protected static final Logger _logAccounting = Logger.getLogger("accounting");
  76. /**
  77. * @see com.l2jserver.loginserver.L2LoginServer#PROTOCOL_REV
  78. */
  79. private static final int REVISION = 0x0106;
  80. private RSAPublicKey _publicKey;
  81. private final String _hostname;
  82. private final int _port;
  83. private final int _gamePort;
  84. private Socket _loginSocket;
  85. private InputStream _in;
  86. private OutputStream _out;
  87. /**
  88. * The BlowFish engine used to encrypt packets<br>
  89. * It is first initialized with a unified key:<br>
  90. * "_;v.]05-31!|+-%xT!^[$\00"<br>
  91. * <br>
  92. * and then after handshake, with a new key sent by<br>
  93. * login server during the handshake. This new key is stored<br>
  94. * in {@link #_blowfishKey}
  95. */
  96. private NewCrypt _blowfish;
  97. private byte[] _blowfishKey;
  98. private byte[] _hexID;
  99. private final boolean _acceptAlternate;
  100. private int _requestID;
  101. private int _serverID;
  102. private final boolean _reserveHost;
  103. private int _maxPlayer;
  104. private final List<WaitingClient> _waitingClients;
  105. private final FastMap<String, L2GameClient> _accountsInGameServer = new FastMap<>();
  106. private int _status;
  107. private String _serverName;
  108. private final String[] _subnets;
  109. private final String[] _hosts;
  110. /**
  111. * Instantiates a new login server thread.
  112. */
  113. protected LoginServerThread()
  114. {
  115. super("LoginServerThread");
  116. _port = Config.GAME_SERVER_LOGIN_PORT;
  117. _gamePort = Config.PORT_GAME;
  118. _hostname = Config.GAME_SERVER_LOGIN_HOST;
  119. _hexID = Config.HEX_ID;
  120. if (_hexID == null)
  121. {
  122. _requestID = Config.REQUEST_ID;
  123. _hexID = Util.generateHex(16);
  124. }
  125. else
  126. {
  127. _requestID = Config.SERVER_ID;
  128. }
  129. _acceptAlternate = Config.ACCEPT_ALTERNATE_ID;
  130. _reserveHost = Config.RESERVE_HOST_ON_LOGIN;
  131. _subnets = Config.GAME_SERVER_SUBNETS;
  132. _hosts = Config.GAME_SERVER_HOSTS;
  133. _waitingClients = new FastList<>();
  134. _accountsInGameServer.shared();
  135. _maxPlayer = Config.MAXIMUM_ONLINE_USERS;
  136. }
  137. /**
  138. * Gets the single instance of LoginServerThread.
  139. * @return single instance of LoginServerThread
  140. */
  141. public static LoginServerThread getInstance()
  142. {
  143. return SingletonHolder._instance;
  144. }
  145. @Override
  146. public void run()
  147. {
  148. while (!isInterrupted())
  149. {
  150. int lengthHi = 0;
  151. int lengthLo = 0;
  152. int length = 0;
  153. boolean checksumOk = false;
  154. try
  155. {
  156. // Connection
  157. _log.info("Connecting to login on " + _hostname + ":" + _port);
  158. _loginSocket = new Socket(_hostname, _port);
  159. _in = _loginSocket.getInputStream();
  160. _out = new BufferedOutputStream(_loginSocket.getOutputStream());
  161. // init Blowfish
  162. _blowfishKey = Util.generateHex(40);
  163. _blowfish = new NewCrypt("_;v.]05-31!|+-%xT!^[$\00");
  164. while (!isInterrupted())
  165. {
  166. lengthLo = _in.read();
  167. lengthHi = _in.read();
  168. length = (lengthHi * 256) + lengthLo;
  169. if (lengthHi < 0)
  170. {
  171. _log.finer("LoginServerThread: Login terminated the connection.");
  172. break;
  173. }
  174. byte[] incoming = new byte[length - 2];
  175. int receivedBytes = 0;
  176. int newBytes = 0;
  177. int left = length - 2;
  178. while ((newBytes != -1) && (receivedBytes < (length - 2)))
  179. {
  180. newBytes = _in.read(incoming, receivedBytes, left);
  181. receivedBytes = receivedBytes + newBytes;
  182. left -= newBytes;
  183. }
  184. if (receivedBytes != (length - 2))
  185. {
  186. _log.warning("Incomplete Packet is sent to the server, closing connection.(LS)");
  187. break;
  188. }
  189. // decrypt if we have a key
  190. _blowfish.decrypt(incoming, 0, incoming.length);
  191. checksumOk = NewCrypt.verifyChecksum(incoming);
  192. if (!checksumOk)
  193. {
  194. _log.warning("Incorrect packet checksum, ignoring packet (LS)");
  195. break;
  196. }
  197. if (Config.DEBUG)
  198. {
  199. _log.warning("[C]\n" + Util.printData(incoming));
  200. }
  201. int packetType = incoming[0] & 0xff;
  202. switch (packetType)
  203. {
  204. case 0x00:
  205. InitLS init = new InitLS(incoming);
  206. if (Config.DEBUG)
  207. {
  208. _log.info("Init received");
  209. }
  210. if (init.getRevision() != REVISION)
  211. {
  212. // TODO: revision mismatch
  213. _log.warning("/!\\ Revision mismatch between LS and GS /!\\");
  214. break;
  215. }
  216. try
  217. {
  218. KeyFactory kfac = KeyFactory.getInstance("RSA");
  219. BigInteger modulus = new BigInteger(init.getRSAKey());
  220. RSAPublicKeySpec kspec1 = new RSAPublicKeySpec(modulus, RSAKeyGenParameterSpec.F4);
  221. _publicKey = (RSAPublicKey) kfac.generatePublic(kspec1);
  222. if (Config.DEBUG)
  223. {
  224. _log.info("RSA key set up");
  225. }
  226. }
  227. catch (GeneralSecurityException e)
  228. {
  229. _log.warning("Troubles while init the public key send by login");
  230. break;
  231. }
  232. // send the blowfish key through the rsa encryption
  233. BlowFishKey bfk = new BlowFishKey(_blowfishKey, _publicKey);
  234. sendPacket(bfk);
  235. if (Config.DEBUG)
  236. {
  237. _log.info("Sent new blowfish key");
  238. }
  239. // now, only accept packet with the new encryption
  240. _blowfish = new NewCrypt(_blowfishKey);
  241. if (Config.DEBUG)
  242. {
  243. _log.info("Changed blowfish key");
  244. }
  245. AuthRequest ar = new AuthRequest(_requestID, _acceptAlternate, _hexID, _gamePort, _reserveHost, _maxPlayer, _subnets, _hosts);
  246. sendPacket(ar);
  247. if (Config.DEBUG)
  248. {
  249. _log.info("Sent AuthRequest to login");
  250. }
  251. break;
  252. case 0x01:
  253. LoginServerFail lsf = new LoginServerFail(incoming);
  254. _log.info("Damn! Registeration Failed: " + lsf.getReasonString());
  255. // login will close the connection here
  256. break;
  257. case 0x02:
  258. AuthResponse aresp = new AuthResponse(incoming);
  259. _serverID = aresp.getServerId();
  260. _serverName = aresp.getServerName();
  261. Config.saveHexid(_serverID, hexToString(_hexID));
  262. _log.info("Registered on login as Server " + _serverID + " : " + _serverName);
  263. ServerStatus st = new ServerStatus();
  264. if (Config.SERVER_LIST_BRACKET)
  265. {
  266. st.addAttribute(ServerStatus.SERVER_LIST_SQUARE_BRACKET, ServerStatus.ON);
  267. }
  268. else
  269. {
  270. st.addAttribute(ServerStatus.SERVER_LIST_SQUARE_BRACKET, ServerStatus.OFF);
  271. }
  272. st.addAttribute(ServerStatus.SERVER_TYPE, Config.SERVER_LIST_TYPE);
  273. if (Config.SERVER_GMONLY)
  274. {
  275. st.addAttribute(ServerStatus.SERVER_LIST_STATUS, ServerStatus.STATUS_GM_ONLY);
  276. }
  277. else
  278. {
  279. st.addAttribute(ServerStatus.SERVER_LIST_STATUS, ServerStatus.STATUS_AUTO);
  280. }
  281. if (Config.SERVER_LIST_AGE == 15)
  282. {
  283. st.addAttribute(ServerStatus.SERVER_AGE, ServerStatus.SERVER_AGE_15);
  284. }
  285. else if (Config.SERVER_LIST_AGE == 18)
  286. {
  287. st.addAttribute(ServerStatus.SERVER_AGE, ServerStatus.SERVER_AGE_18);
  288. }
  289. else
  290. {
  291. st.addAttribute(ServerStatus.SERVER_AGE, ServerStatus.SERVER_AGE_ALL);
  292. }
  293. sendPacket(st);
  294. if (L2World.getInstance().getAllPlayersCount() > 0)
  295. {
  296. FastList<String> playerList = new FastList<>();
  297. for (L2PcInstance player : L2World.getInstance().getAllPlayersArray())
  298. {
  299. playerList.add(player.getAccountName());
  300. }
  301. PlayerInGame pig = new PlayerInGame(playerList);
  302. sendPacket(pig);
  303. }
  304. break;
  305. case 0x03:
  306. PlayerAuthResponse par = new PlayerAuthResponse(incoming);
  307. String account = par.getAccount();
  308. WaitingClient wcToRemove = null;
  309. synchronized (_waitingClients)
  310. {
  311. for (WaitingClient wc : _waitingClients)
  312. {
  313. if (wc.account.equals(account))
  314. {
  315. wcToRemove = wc;
  316. }
  317. }
  318. }
  319. if (wcToRemove != null)
  320. {
  321. if (par.isAuthed())
  322. {
  323. if (Config.DEBUG)
  324. {
  325. _log.info("Login accepted player " + wcToRemove.account + " waited(" + (GameTimeController.getGameTicks() - wcToRemove.timestamp) + "ms)");
  326. }
  327. PlayerInGame pig = new PlayerInGame(par.getAccount());
  328. sendPacket(pig);
  329. wcToRemove.gameClient.setState(GameClientState.AUTHED);
  330. wcToRemove.gameClient.setSessionId(wcToRemove.session);
  331. CharSelectionInfo cl = new CharSelectionInfo(wcToRemove.account, wcToRemove.gameClient.getSessionId().playOkID1);
  332. wcToRemove.gameClient.getConnection().sendPacket(cl);
  333. wcToRemove.gameClient.setCharSelection(cl.getCharInfo());
  334. }
  335. else
  336. {
  337. _log.warning("Session key is not correct. Closing connection for account " + wcToRemove.account + ".");
  338. // wcToRemove.gameClient.getConnection().sendPacket(new LoginFail(LoginFail.SYSTEM_ERROR_LOGIN_LATER));
  339. wcToRemove.gameClient.close(new LoginFail(LoginFail.SYSTEM_ERROR_LOGIN_LATER));
  340. _accountsInGameServer.remove(wcToRemove.account);
  341. }
  342. _waitingClients.remove(wcToRemove);
  343. }
  344. break;
  345. case 0x04:
  346. KickPlayer kp = new KickPlayer(incoming);
  347. doKickPlayer(kp.getAccount());
  348. break;
  349. case 0x05:
  350. RequestCharacters rc = new RequestCharacters(incoming);
  351. getCharsOnServer(rc.getAccount());
  352. break;
  353. case 0x06:
  354. new ChangePasswordResponse(incoming);
  355. break;
  356. }
  357. }
  358. }
  359. catch (UnknownHostException e)
  360. {
  361. if (Config.DEBUG)
  362. {
  363. _log.log(Level.WARNING, "", e);
  364. }
  365. }
  366. catch (SocketException e)
  367. {
  368. _log.warning("LoginServer not avaible, trying to reconnect...");
  369. }
  370. catch (IOException e)
  371. {
  372. _log.log(Level.WARNING, "Disconnected from Login, Trying to reconnect: " + e.getMessage(), e);
  373. }
  374. finally
  375. {
  376. try
  377. {
  378. _loginSocket.close();
  379. if (isInterrupted())
  380. {
  381. return;
  382. }
  383. }
  384. catch (Exception e)
  385. {
  386. }
  387. }
  388. try
  389. {
  390. Thread.sleep(5000); // 5 seconds tempo.
  391. }
  392. catch (InterruptedException e)
  393. {
  394. return; // never swallow an interrupt!
  395. }
  396. }
  397. }
  398. /**
  399. * Adds the waiting client and send request.
  400. * @param acc the account
  401. * @param client the game client
  402. * @param key the session key
  403. */
  404. public void addWaitingClientAndSendRequest(String acc, L2GameClient client, SessionKey key)
  405. {
  406. if (Config.DEBUG)
  407. {
  408. _log.info(String.valueOf(key));
  409. }
  410. WaitingClient wc = new WaitingClient(acc, client, key);
  411. synchronized (_waitingClients)
  412. {
  413. _waitingClients.add(wc);
  414. }
  415. PlayerAuthRequest par = new PlayerAuthRequest(acc, key);
  416. try
  417. {
  418. sendPacket(par);
  419. }
  420. catch (IOException e)
  421. {
  422. _log.warning("Error while sending player auth request");
  423. if (Config.DEBUG)
  424. {
  425. _log.log(Level.WARNING, "", e);
  426. }
  427. }
  428. }
  429. /**
  430. * Removes the waiting client.
  431. * @param client the client
  432. */
  433. public void removeWaitingClient(L2GameClient client)
  434. {
  435. WaitingClient toRemove = null;
  436. synchronized (_waitingClients)
  437. {
  438. for (WaitingClient c : _waitingClients)
  439. {
  440. if (c.gameClient == client)
  441. {
  442. toRemove = c;
  443. }
  444. }
  445. if (toRemove != null)
  446. {
  447. _waitingClients.remove(toRemove);
  448. }
  449. }
  450. }
  451. /**
  452. * Send logout for the given account.
  453. * @param account the account
  454. */
  455. public void sendLogout(String account)
  456. {
  457. if (account == null)
  458. {
  459. return;
  460. }
  461. PlayerLogout pl = new PlayerLogout(account);
  462. try
  463. {
  464. sendPacket(pl);
  465. }
  466. catch (IOException e)
  467. {
  468. _log.warning("Error while sending logout packet to login");
  469. if (Config.DEBUG)
  470. {
  471. _log.log(Level.WARNING, "", e);
  472. }
  473. }
  474. finally
  475. {
  476. _accountsInGameServer.remove(account);
  477. }
  478. }
  479. /**
  480. * Adds the game server login.
  481. * @param account the account
  482. * @param client the client
  483. */
  484. public void addGameServerLogin(String account, L2GameClient client)
  485. {
  486. _accountsInGameServer.put(account, client);
  487. }
  488. /**
  489. * Send access level.
  490. * @param account the account
  491. * @param level the access level
  492. */
  493. public void sendAccessLevel(String account, int level)
  494. {
  495. ChangeAccessLevel cal = new ChangeAccessLevel(account, level);
  496. try
  497. {
  498. sendPacket(cal);
  499. }
  500. catch (IOException e)
  501. {
  502. if (Config.DEBUG)
  503. {
  504. _log.log(Level.WARNING, "", e);
  505. }
  506. }
  507. }
  508. /**
  509. * Send client tracert.
  510. * @param account the account
  511. * @param address the address
  512. */
  513. public void sendClientTracert(String account, String[] address)
  514. {
  515. PlayerTracert ptc = new PlayerTracert(account, address[0], address[1], address[2], address[3], address[4]);
  516. try
  517. {
  518. sendPacket(ptc);
  519. }
  520. catch (IOException e)
  521. {
  522. if (Config.DEBUG)
  523. {
  524. _log.log(Level.WARNING, "", e);
  525. }
  526. }
  527. }
  528. /**
  529. * Send mail.
  530. * @param account the account
  531. * @param mailId the mail id
  532. * @param args the args
  533. */
  534. public void sendMail(String account, String mailId, String... args)
  535. {
  536. SendMail sem = new SendMail(account, mailId, args);
  537. try
  538. {
  539. sendPacket(sem);
  540. }
  541. catch (IOException e)
  542. {
  543. if (Config.DEBUG)
  544. {
  545. _log.log(Level.WARNING, "", e);
  546. }
  547. }
  548. }
  549. /**
  550. * Send temp ban.
  551. * @param account the account
  552. * @param ip the ip
  553. * @param time the time
  554. */
  555. public void sendTempBan(String account, String ip, long time)
  556. {
  557. TempBan tbn = new TempBan(account, ip, time);
  558. try
  559. {
  560. sendPacket(tbn);
  561. }
  562. catch (IOException e)
  563. {
  564. if (Config.DEBUG)
  565. {
  566. _log.log(Level.WARNING, "", e);
  567. }
  568. }
  569. }
  570. /**
  571. * Hex to string.
  572. * @param hex the hex value
  573. * @return the hex value as string
  574. */
  575. private String hexToString(byte[] hex)
  576. {
  577. return new BigInteger(hex).toString(16);
  578. }
  579. /**
  580. * Kick player for the given account.
  581. * @param account the account
  582. */
  583. public void doKickPlayer(String account)
  584. {
  585. L2GameClient client = _accountsInGameServer.get(account);
  586. if (client != null)
  587. {
  588. LogRecord record = new LogRecord(Level.WARNING, "Kicked by login");
  589. record.setParameters(new Object[]{client});
  590. _logAccounting.log(record);
  591. client.setAditionalClosePacket(SystemMessage.getSystemMessage(SystemMessageId.ANOTHER_LOGIN_WITH_ACCOUNT));
  592. client.closeNow();
  593. }
  594. }
  595. /**
  596. * Gets the chars on server.
  597. * @param account the account
  598. */
  599. private void getCharsOnServer(String account)
  600. {
  601. Connection con = null;
  602. int chars = 0;
  603. List<Long> charToDel = new ArrayList<>();
  604. try
  605. {
  606. con = L2DatabaseFactory.getInstance().getConnection();
  607. try (PreparedStatement ps = con.prepareStatement("SELECT deletetime FROM characters WHERE account_name=?"))
  608. {
  609. ps.setString(1, account);
  610. try (ResultSet rs = ps.executeQuery())
  611. {
  612. while (rs.next())
  613. {
  614. chars++;
  615. long delTime = rs.getLong("deletetime");
  616. if (delTime != 0)
  617. charToDel.add(delTime);
  618. }
  619. }
  620. }
  621. }
  622. catch (SQLException e)
  623. {
  624. _log.log(Level.WARNING, "Exception: getCharsOnServer: " + e.getMessage(), e);
  625. }
  626. finally
  627. {
  628. L2DatabaseFactory.close(con);
  629. }
  630. ReplyCharacters rec = new ReplyCharacters(account, chars, charToDel);
  631. try
  632. {
  633. sendPacket(rec);
  634. }
  635. catch (IOException e)
  636. {
  637. if (Config.DEBUG)
  638. {
  639. _log.log(Level.WARNING, "", e);
  640. }
  641. }
  642. }
  643. /**
  644. * Send packet.
  645. * @param sl the sendable packet
  646. * @throws IOException Signals that an I/O exception has occurred.
  647. */
  648. private void sendPacket(BaseSendablePacket sl) throws IOException
  649. {
  650. byte[] data = sl.getContent();
  651. NewCrypt.appendChecksum(data);
  652. if (Config.DEBUG)
  653. {
  654. _log.finest("[S]\n" + Util.printData(data));
  655. }
  656. _blowfish.crypt(data, 0, data.length);
  657. int len = data.length + 2;
  658. synchronized (_out) // avoids tow threads writing in the mean time
  659. {
  660. _out.write(len & 0xff);
  661. _out.write((len >> 8) & 0xff);
  662. _out.write(data);
  663. _out.flush();
  664. }
  665. }
  666. /**
  667. * Sets the max player.
  668. * @param maxPlayer The maxPlayer to set.
  669. */
  670. public void setMaxPlayer(int maxPlayer)
  671. {
  672. sendServerStatus(ServerStatus.MAX_PLAYERS, maxPlayer);
  673. _maxPlayer = maxPlayer;
  674. }
  675. /**
  676. * Gets the max player.
  677. * @return Returns the maxPlayer.
  678. */
  679. public int getMaxPlayer()
  680. {
  681. return _maxPlayer;
  682. }
  683. /**
  684. * Send server status.
  685. * @param id the id
  686. * @param value the value
  687. */
  688. public void sendServerStatus(int id, int value)
  689. {
  690. ServerStatus ss = new ServerStatus();
  691. ss.addAttribute(id, value);
  692. try
  693. {
  694. sendPacket(ss);
  695. }
  696. catch (IOException e)
  697. {
  698. if (Config.DEBUG)
  699. {
  700. _log.log(Level.WARNING, "", e);
  701. }
  702. }
  703. }
  704. /**
  705. * Send Server Type Config to LS.
  706. */
  707. public void sendServerType()
  708. {
  709. ServerStatus ss = new ServerStatus();
  710. ss.addAttribute(ServerStatus.SERVER_TYPE, Config.SERVER_LIST_TYPE);
  711. try
  712. {
  713. sendPacket(ss);
  714. }
  715. catch (IOException e)
  716. {
  717. if (Config.DEBUG)
  718. {
  719. _log.log(Level.WARNING, "", e);
  720. }
  721. }
  722. }
  723. /**
  724. * Send change password.
  725. * @param accountName the account name
  726. * @param charName the char name
  727. * @param oldpass the old pass
  728. * @param newpass the new pass
  729. */
  730. public void sendChangePassword(String accountName, String charName, String oldpass, String newpass)
  731. {
  732. ChangePassword cp = new ChangePassword(accountName, charName, oldpass, newpass);
  733. try
  734. {
  735. sendPacket(cp);
  736. }
  737. catch (IOException e)
  738. {
  739. if (Config.DEBUG)
  740. {
  741. _log.log(Level.WARNING, "", e);
  742. }
  743. }
  744. }
  745. /**
  746. * Gets the status string.
  747. * @return the status string
  748. */
  749. public String getStatusString()
  750. {
  751. return ServerStatus.STATUS_STRING[_status];
  752. }
  753. /**
  754. * Gets the server name.
  755. * @return the server name.
  756. */
  757. public String getServerName()
  758. {
  759. return _serverName;
  760. }
  761. /**
  762. * Sets the server status.
  763. * @param status the new server status
  764. */
  765. public void setServerStatus(int status)
  766. {
  767. switch (status)
  768. {
  769. case ServerStatus.STATUS_AUTO:
  770. sendServerStatus(ServerStatus.SERVER_LIST_STATUS, ServerStatus.STATUS_AUTO);
  771. _status = status;
  772. break;
  773. case ServerStatus.STATUS_DOWN:
  774. sendServerStatus(ServerStatus.SERVER_LIST_STATUS, ServerStatus.STATUS_DOWN);
  775. _status = status;
  776. break;
  777. case ServerStatus.STATUS_FULL:
  778. sendServerStatus(ServerStatus.SERVER_LIST_STATUS, ServerStatus.STATUS_FULL);
  779. _status = status;
  780. break;
  781. case ServerStatus.STATUS_GM_ONLY:
  782. sendServerStatus(ServerStatus.SERVER_LIST_STATUS, ServerStatus.STATUS_GM_ONLY);
  783. _status = status;
  784. break;
  785. case ServerStatus.STATUS_GOOD:
  786. sendServerStatus(ServerStatus.SERVER_LIST_STATUS, ServerStatus.STATUS_GOOD);
  787. _status = status;
  788. break;
  789. case ServerStatus.STATUS_NORMAL:
  790. sendServerStatus(ServerStatus.SERVER_LIST_STATUS, ServerStatus.STATUS_NORMAL);
  791. _status = status;
  792. break;
  793. default:
  794. throw new IllegalArgumentException("Status does not exists:" + status);
  795. }
  796. }
  797. public static class SessionKey
  798. {
  799. public int playOkID1;
  800. public int playOkID2;
  801. public int loginOkID1;
  802. public int loginOkID2;
  803. /**
  804. * Instantiates a new session key.
  805. * @param loginOK1 the login o k1
  806. * @param loginOK2 the login o k2
  807. * @param playOK1 the play o k1
  808. * @param playOK2 the play o k2
  809. */
  810. public SessionKey(int loginOK1, int loginOK2, int playOK1, int playOK2)
  811. {
  812. playOkID1 = playOK1;
  813. playOkID2 = playOK2;
  814. loginOkID1 = loginOK1;
  815. loginOkID2 = loginOK2;
  816. }
  817. @Override
  818. public String toString()
  819. {
  820. return "PlayOk: " + playOkID1 + " " + playOkID2 + " LoginOk:" + loginOkID1 + " " + loginOkID2;
  821. }
  822. }
  823. private static class WaitingClient
  824. {
  825. public int timestamp;
  826. public String account;
  827. public L2GameClient gameClient;
  828. public SessionKey session;
  829. /**
  830. * Instantiates a new waiting client.
  831. * @param acc the acc
  832. * @param client the client
  833. * @param key the key
  834. */
  835. public WaitingClient(String acc, L2GameClient client, SessionKey key)
  836. {
  837. account = acc;
  838. timestamp = GameTimeController.getGameTicks();
  839. gameClient = client;
  840. session = key;
  841. }
  842. }
  843. private static class SingletonHolder
  844. {
  845. protected static final LoginServerThread _instance = new LoginServerThread();
  846. }
  847. }