L2LoginClient.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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.loginserver.network;
  16. import java.io.IOException;
  17. import java.net.InetAddress;
  18. import java.nio.ByteBuffer;
  19. import java.security.interfaces.RSAPrivateKey;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.logging.Logger;
  23. import org.mmocore.network.MMOClient;
  24. import org.mmocore.network.MMOConnection;
  25. import org.mmocore.network.SendablePacket;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.loginserver.LoginController;
  28. import com.l2jserver.loginserver.SessionKey;
  29. import com.l2jserver.loginserver.network.serverpackets.L2LoginServerPacket;
  30. import com.l2jserver.loginserver.network.serverpackets.LoginFail;
  31. import com.l2jserver.loginserver.network.serverpackets.LoginFail.LoginFailReason;
  32. import com.l2jserver.loginserver.network.serverpackets.PlayFail;
  33. import com.l2jserver.loginserver.network.serverpackets.PlayFail.PlayFailReason;
  34. import com.l2jserver.util.Rnd;
  35. import com.l2jserver.util.crypt.LoginCrypt;
  36. import com.l2jserver.util.crypt.ScrambledKeyPair;
  37. /**
  38. * Represents a client connected into the LoginServer
  39. * @author KenM
  40. */
  41. public final class L2LoginClient extends MMOClient<MMOConnection<L2LoginClient>>
  42. {
  43. private static Logger _log = Logger.getLogger(L2LoginClient.class.getName());
  44. public static enum LoginClientState
  45. {
  46. CONNECTED,
  47. AUTHED_GG,
  48. AUTHED_LOGIN
  49. }
  50. private LoginClientState _state;
  51. // Crypt
  52. private final LoginCrypt _loginCrypt;
  53. private final ScrambledKeyPair _scrambledPair;
  54. private final byte[] _blowfishKey;
  55. private String _account;
  56. private int _accessLevel;
  57. private int _lastServer;
  58. private SessionKey _sessionKey;
  59. private final int _sessionId;
  60. private boolean _joinedGS;
  61. private Map<Integer, Integer> _charsOnServers;
  62. private Map<Integer, long[]> _charsToDelete;
  63. private final long _connectionStartTime;
  64. /**
  65. * @param con
  66. */
  67. public L2LoginClient(MMOConnection<L2LoginClient> con)
  68. {
  69. super(con);
  70. _state = LoginClientState.CONNECTED;
  71. _scrambledPair = LoginController.getInstance().getScrambledRSAKeyPair();
  72. _blowfishKey = LoginController.getInstance().getBlowfishKey();
  73. _sessionId = Rnd.nextInt();
  74. _connectionStartTime = System.currentTimeMillis();
  75. _loginCrypt = new LoginCrypt();
  76. _loginCrypt.setKey(_blowfishKey);
  77. }
  78. /**
  79. * @see org.mmocore.network.MMOClient#decrypt(java.nio.ByteBuffer, int)
  80. */
  81. @Override
  82. public boolean decrypt(ByteBuffer buf, int size)
  83. {
  84. boolean isChecksumValid = false;
  85. try
  86. {
  87. isChecksumValid = _loginCrypt.decrypt(buf.array(), buf.position(), size);
  88. if (!isChecksumValid)
  89. {
  90. _log.warning("Wrong checksum from client: " + toString());
  91. super.getConnection().close((SendablePacket<L2LoginClient>) null);
  92. return false;
  93. }
  94. return true;
  95. }
  96. catch (IOException e)
  97. {
  98. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  99. super.getConnection().close((SendablePacket<L2LoginClient>) null);
  100. return false;
  101. }
  102. }
  103. /**
  104. * @see org.mmocore.network.MMOClient#encrypt(java.nio.ByteBuffer, int)
  105. */
  106. @Override
  107. public boolean encrypt(ByteBuffer buf, int size)
  108. {
  109. final int offset = buf.position();
  110. try
  111. {
  112. size = _loginCrypt.encrypt(buf.array(), offset, size);
  113. }
  114. catch (IOException e)
  115. {
  116. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  117. return false;
  118. }
  119. buf.position(offset + size);
  120. return true;
  121. }
  122. public LoginClientState getState()
  123. {
  124. return _state;
  125. }
  126. public void setState(LoginClientState state)
  127. {
  128. _state = state;
  129. }
  130. public byte[] getBlowfishKey()
  131. {
  132. return _blowfishKey;
  133. }
  134. public byte[] getScrambledModulus()
  135. {
  136. return _scrambledPair._scrambledModulus;
  137. }
  138. public RSAPrivateKey getRSAPrivateKey()
  139. {
  140. return (RSAPrivateKey) _scrambledPair._pair.getPrivate();
  141. }
  142. public String getAccount()
  143. {
  144. return _account;
  145. }
  146. public void setAccount(String account)
  147. {
  148. _account = account;
  149. }
  150. public void setAccessLevel(int accessLevel)
  151. {
  152. _accessLevel = accessLevel;
  153. }
  154. public int getAccessLevel()
  155. {
  156. return _accessLevel;
  157. }
  158. public void setLastServer(int lastServer)
  159. {
  160. _lastServer = lastServer;
  161. }
  162. public int getLastServer()
  163. {
  164. return _lastServer;
  165. }
  166. public int getSessionId()
  167. {
  168. return _sessionId;
  169. }
  170. public boolean hasJoinedGS()
  171. {
  172. return _joinedGS;
  173. }
  174. public void setJoinedGS(boolean val)
  175. {
  176. _joinedGS = val;
  177. }
  178. public void setSessionKey(SessionKey sessionKey)
  179. {
  180. _sessionKey = sessionKey;
  181. }
  182. public SessionKey getSessionKey()
  183. {
  184. return _sessionKey;
  185. }
  186. public long getConnectionStartTime()
  187. {
  188. return _connectionStartTime;
  189. }
  190. public void sendPacket(L2LoginServerPacket lsp)
  191. {
  192. getConnection().sendPacket(lsp);
  193. }
  194. public void close(LoginFailReason reason)
  195. {
  196. getConnection().close(new LoginFail(reason));
  197. }
  198. public void close(PlayFailReason reason)
  199. {
  200. getConnection().close(new PlayFail(reason));
  201. }
  202. public void close(L2LoginServerPacket lsp)
  203. {
  204. getConnection().close(lsp);
  205. }
  206. public void setCharsOnServ(int servId, int chars)
  207. {
  208. if (_charsOnServers == null)
  209. {
  210. _charsOnServers = new HashMap<>();
  211. }
  212. _charsOnServers.put(servId, chars);
  213. }
  214. public Map<Integer, Integer> getCharsOnServ()
  215. {
  216. return _charsOnServers;
  217. }
  218. public void serCharsWaitingDelOnServ(int servId, long[] charsToDel)
  219. {
  220. if (_charsToDelete == null)
  221. {
  222. _charsToDelete = new HashMap<>();
  223. }
  224. _charsToDelete.put(servId, charsToDel);
  225. }
  226. public Map<Integer, long[]> getCharsWaitingDelOnServ()
  227. {
  228. return _charsToDelete;
  229. }
  230. @Override
  231. public void onDisconnection()
  232. {
  233. if (Config.DEBUG)
  234. {
  235. _log.info("DISCONNECTED: " + toString());
  236. }
  237. if (!hasJoinedGS() || ((getConnectionStartTime() + LoginController.LOGIN_TIMEOUT) < System.currentTimeMillis()))
  238. {
  239. LoginController.getInstance().removeAuthedLoginClient(getAccount());
  240. }
  241. }
  242. @Override
  243. public String toString()
  244. {
  245. InetAddress address = getConnection().getInetAddress();
  246. if (getState() == LoginClientState.AUTHED_LOGIN)
  247. {
  248. return "[" + getAccount() + " (" + (address == null ? "disconnected" : address.getHostAddress()) + ")]";
  249. }
  250. return "[" + (address == null ? "disconnected" : address.getHostAddress()) + "]";
  251. }
  252. @Override
  253. protected void onForcedDisconnection()
  254. {
  255. // Empty
  256. }
  257. }