L2LoginClient.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 final 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. @Override
  79. public boolean decrypt(ByteBuffer buf, int size)
  80. {
  81. boolean isChecksumValid = false;
  82. try
  83. {
  84. isChecksumValid = _loginCrypt.decrypt(buf.array(), buf.position(), size);
  85. if (!isChecksumValid)
  86. {
  87. _log.warning("Wrong checksum from client: " + toString());
  88. super.getConnection().close((SendablePacket<L2LoginClient>) null);
  89. return false;
  90. }
  91. return true;
  92. }
  93. catch (IOException e)
  94. {
  95. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  96. super.getConnection().close((SendablePacket<L2LoginClient>) null);
  97. return false;
  98. }
  99. }
  100. @Override
  101. public boolean encrypt(ByteBuffer buf, int size)
  102. {
  103. final int offset = buf.position();
  104. try
  105. {
  106. size = _loginCrypt.encrypt(buf.array(), offset, size);
  107. }
  108. catch (IOException e)
  109. {
  110. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  111. return false;
  112. }
  113. buf.position(offset + size);
  114. return true;
  115. }
  116. public LoginClientState getState()
  117. {
  118. return _state;
  119. }
  120. public void setState(LoginClientState state)
  121. {
  122. _state = state;
  123. }
  124. public byte[] getBlowfishKey()
  125. {
  126. return _blowfishKey;
  127. }
  128. public byte[] getScrambledModulus()
  129. {
  130. return _scrambledPair._scrambledModulus;
  131. }
  132. public RSAPrivateKey getRSAPrivateKey()
  133. {
  134. return (RSAPrivateKey) _scrambledPair._pair.getPrivate();
  135. }
  136. public String getAccount()
  137. {
  138. return _account;
  139. }
  140. public void setAccount(String account)
  141. {
  142. _account = account;
  143. }
  144. public void setAccessLevel(int accessLevel)
  145. {
  146. _accessLevel = accessLevel;
  147. }
  148. public int getAccessLevel()
  149. {
  150. return _accessLevel;
  151. }
  152. public void setLastServer(int lastServer)
  153. {
  154. _lastServer = lastServer;
  155. }
  156. public int getLastServer()
  157. {
  158. return _lastServer;
  159. }
  160. public int getSessionId()
  161. {
  162. return _sessionId;
  163. }
  164. public boolean hasJoinedGS()
  165. {
  166. return _joinedGS;
  167. }
  168. public void setJoinedGS(boolean val)
  169. {
  170. _joinedGS = val;
  171. }
  172. public void setSessionKey(SessionKey sessionKey)
  173. {
  174. _sessionKey = sessionKey;
  175. }
  176. public SessionKey getSessionKey()
  177. {
  178. return _sessionKey;
  179. }
  180. public long getConnectionStartTime()
  181. {
  182. return _connectionStartTime;
  183. }
  184. public void sendPacket(L2LoginServerPacket lsp)
  185. {
  186. getConnection().sendPacket(lsp);
  187. }
  188. public void close(LoginFailReason reason)
  189. {
  190. getConnection().close(new LoginFail(reason));
  191. }
  192. public void close(PlayFailReason reason)
  193. {
  194. getConnection().close(new PlayFail(reason));
  195. }
  196. public void close(L2LoginServerPacket lsp)
  197. {
  198. getConnection().close(lsp);
  199. }
  200. public void setCharsOnServ(int servId, int chars)
  201. {
  202. if (_charsOnServers == null)
  203. {
  204. _charsOnServers = new HashMap<>();
  205. }
  206. _charsOnServers.put(servId, chars);
  207. }
  208. public Map<Integer, Integer> getCharsOnServ()
  209. {
  210. return _charsOnServers;
  211. }
  212. public void serCharsWaitingDelOnServ(int servId, long[] charsToDel)
  213. {
  214. if (_charsToDelete == null)
  215. {
  216. _charsToDelete = new HashMap<>();
  217. }
  218. _charsToDelete.put(servId, charsToDel);
  219. }
  220. public Map<Integer, long[]> getCharsWaitingDelOnServ()
  221. {
  222. return _charsToDelete;
  223. }
  224. @Override
  225. public void onDisconnection()
  226. {
  227. if (Config.DEBUG)
  228. {
  229. _log.info("DISCONNECTED: " + toString());
  230. }
  231. if (!hasJoinedGS() || ((getConnectionStartTime() + LoginController.LOGIN_TIMEOUT) < System.currentTimeMillis()))
  232. {
  233. LoginController.getInstance().removeAuthedLoginClient(getAccount());
  234. }
  235. }
  236. @Override
  237. public String toString()
  238. {
  239. InetAddress address = getConnection().getInetAddress();
  240. if (getState() == LoginClientState.AUTHED_LOGIN)
  241. {
  242. return "[" + getAccount() + " (" + (address == null ? "disconnected" : address.getHostAddress()) + ")]";
  243. }
  244. return "[" + (address == null ? "disconnected" : address.getHostAddress()) + "]";
  245. }
  246. @Override
  247. protected void onForcedDisconnection()
  248. {
  249. // Empty
  250. }
  251. }