L2LoginClient.java 6.8 KB

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