L2LoginClient.java 6.6 KB

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