L2LoginClient.java 6.3 KB

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