GameServerThread.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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.BufferedOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.net.Socket;
  21. import java.security.KeyPair;
  22. import java.security.interfaces.RSAPrivateKey;
  23. import java.security.interfaces.RSAPublicKey;
  24. import java.util.Set;
  25. import java.util.logging.Logger;
  26. import javolution.util.FastSet;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.loginserver.GameServerTable.GameServerInfo;
  29. import com.l2jserver.loginserver.network.L2JGameServerPacketHandler;
  30. import com.l2jserver.loginserver.network.L2JGameServerPacketHandler.GameServerState;
  31. import com.l2jserver.loginserver.network.loginserverpackets.ChangePasswordResponse;
  32. import com.l2jserver.loginserver.network.loginserverpackets.InitLS;
  33. import com.l2jserver.loginserver.network.loginserverpackets.KickPlayer;
  34. import com.l2jserver.loginserver.network.loginserverpackets.LoginServerFail;
  35. import com.l2jserver.loginserver.network.loginserverpackets.RequestCharacters;
  36. import com.l2jserver.util.Util;
  37. import com.l2jserver.util.crypt.NewCrypt;
  38. import com.l2jserver.util.network.BaseSendablePacket;
  39. /**
  40. * @author -Wooden-
  41. * @author KenM
  42. */
  43. public class GameServerThread extends Thread
  44. {
  45. protected static final Logger _log = Logger.getLogger(GameServerThread.class.getName());
  46. private final Socket _connection;
  47. private InputStream _in;
  48. private OutputStream _out;
  49. private final RSAPublicKey _publicKey;
  50. private final RSAPrivateKey _privateKey;
  51. private NewCrypt _blowfish;
  52. private GameServerState _loginConnectionState = GameServerState.CONNECTED;
  53. private final String _connectionIp;
  54. private GameServerInfo _gsi;
  55. /** Authed Clients on a GameServer */
  56. private final Set<String> _accountsOnGameServer = new FastSet<>();
  57. private String _connectionIPAddress;
  58. @Override
  59. public void run()
  60. {
  61. _connectionIPAddress = _connection.getInetAddress().getHostAddress();
  62. if (GameServerThread.isBannedGameserverIP(_connectionIPAddress))
  63. {
  64. _log.info("GameServerRegistration: IP Address " + _connectionIPAddress + " is on Banned IP list.");
  65. forceClose(LoginServerFail.REASON_IP_BANNED);
  66. // ensure no further processing for this connection
  67. return;
  68. }
  69. InitLS startPacket = new InitLS(_publicKey.getModulus().toByteArray());
  70. try
  71. {
  72. sendPacket(startPacket);
  73. int lengthHi = 0;
  74. int lengthLo = 0;
  75. int length = 0;
  76. boolean checksumOk = false;
  77. for (;;)
  78. {
  79. lengthLo = _in.read();
  80. lengthHi = _in.read();
  81. length = (lengthHi * 256) + lengthLo;
  82. if ((lengthHi < 0) || _connection.isClosed())
  83. {
  84. _log.finer("LoginServerThread: Login terminated the connection.");
  85. break;
  86. }
  87. byte[] data = new byte[length - 2];
  88. int receivedBytes = 0;
  89. int newBytes = 0;
  90. int left = length - 2;
  91. while ((newBytes != -1) && (receivedBytes < (length - 2)))
  92. {
  93. newBytes = _in.read(data, receivedBytes, left);
  94. receivedBytes = receivedBytes + newBytes;
  95. left -= newBytes;
  96. }
  97. if (receivedBytes != (length - 2))
  98. {
  99. _log.warning("Incomplete Packet is sent to the server, closing connection.(LS)");
  100. break;
  101. }
  102. // decrypt if we have a key
  103. _blowfish.decrypt(data, 0, data.length);
  104. checksumOk = NewCrypt.verifyChecksum(data);
  105. if (!checksumOk)
  106. {
  107. _log.warning("Incorrect packet checksum, closing connection (LS)");
  108. return;
  109. }
  110. if (Config.DEBUG)
  111. {
  112. _log.warning("[C]\n" + Util.printData(data));
  113. }
  114. L2JGameServerPacketHandler.handlePacket(data, this);
  115. }
  116. }
  117. catch (IOException e)
  118. {
  119. String serverName = (getServerId() != -1 ? "[" + getServerId() + "] " + GameServerTable.getInstance().getServerNameById(getServerId()) : "(" + _connectionIPAddress + ")");
  120. String msg = "GameServer " + serverName + ": Connection lost: " + e.getMessage();
  121. _log.info(msg);
  122. broadcastToTelnet(msg);
  123. }
  124. finally
  125. {
  126. if (isAuthed())
  127. {
  128. _gsi.setDown();
  129. _log.info("Server [" + getServerId() + "] " + GameServerTable.getInstance().getServerNameById(getServerId()) + " is now set as disconnected");
  130. }
  131. L2LoginServer.getInstance().getGameServerListener().removeGameServer(this);
  132. L2LoginServer.getInstance().getGameServerListener().removeFloodProtection(_connectionIp);
  133. }
  134. }
  135. public boolean hasAccountOnGameServer(String account)
  136. {
  137. return _accountsOnGameServer.contains(account);
  138. }
  139. public int getPlayerCount()
  140. {
  141. return _accountsOnGameServer.size();
  142. }
  143. /**
  144. * Attachs a GameServerInfo to this Thread<br>
  145. * <ul>
  146. * <li>Updates the GameServerInfo values based on GameServerAuth packet</li>
  147. * <li><b>Sets the GameServerInfo as Authed</b></li>
  148. * </ul>
  149. * @param gsi The GameServerInfo to be attached.
  150. * @param port
  151. * @param hosts
  152. * @param maxPlayers
  153. */
  154. public void attachGameServerInfo(GameServerInfo gsi, int port, String[] hosts, int maxPlayers)
  155. {
  156. setGameServerInfo(gsi);
  157. gsi.setGameServerThread(this);
  158. gsi.setPort(port);
  159. setGameHosts(hosts);
  160. gsi.setMaxPlayers(maxPlayers);
  161. gsi.setAuthed(true);
  162. }
  163. public void forceClose(int reason)
  164. {
  165. sendPacket(new LoginServerFail(reason));
  166. try
  167. {
  168. _connection.close();
  169. }
  170. catch (IOException e)
  171. {
  172. _log.finer("GameServerThread: Failed disconnecting banned server, server already disconnected.");
  173. }
  174. }
  175. /**
  176. * @param ipAddress
  177. * @return
  178. */
  179. public static boolean isBannedGameserverIP(String ipAddress)
  180. {
  181. return false;
  182. }
  183. public GameServerThread(Socket con)
  184. {
  185. _connection = con;
  186. _connectionIp = con.getInetAddress().getHostAddress();
  187. try
  188. {
  189. _in = _connection.getInputStream();
  190. _out = new BufferedOutputStream(_connection.getOutputStream());
  191. }
  192. catch (IOException e)
  193. {
  194. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  195. }
  196. KeyPair pair = GameServerTable.getInstance().getKeyPair();
  197. _privateKey = (RSAPrivateKey) pair.getPrivate();
  198. _publicKey = (RSAPublicKey) pair.getPublic();
  199. _blowfish = new NewCrypt("_;v.]05-31!|+-%xT!^[$\00");
  200. setName(getClass().getSimpleName() + "-" + getId() + "@" + _connectionIp);
  201. start();
  202. }
  203. /**
  204. * @param sl
  205. */
  206. public void sendPacket(BaseSendablePacket sl)
  207. {
  208. try
  209. {
  210. byte[] data = sl.getContent();
  211. NewCrypt.appendChecksum(data);
  212. if (Config.DEBUG)
  213. {
  214. _log.finest("[S] " + sl.getClass().getSimpleName() + ":\n" + Util.printData(data));
  215. }
  216. _blowfish.crypt(data, 0, data.length);
  217. int len = data.length + 2;
  218. synchronized (_out)
  219. {
  220. _out.write(len & 0xff);
  221. _out.write((len >> 8) & 0xff);
  222. _out.write(data);
  223. _out.flush();
  224. }
  225. }
  226. catch (IOException e)
  227. {
  228. _log.severe("IOException while sending packet " + sl.getClass().getSimpleName());
  229. }
  230. }
  231. public void broadcastToTelnet(String msg)
  232. {
  233. if (L2LoginServer.getInstance().getStatusServer() != null)
  234. {
  235. L2LoginServer.getInstance().getStatusServer().sendMessageToTelnets(msg);
  236. }
  237. }
  238. public void kickPlayer(String account)
  239. {
  240. sendPacket(new KickPlayer(account));
  241. }
  242. public void requestCharacters(String account)
  243. {
  244. sendPacket(new RequestCharacters(account));
  245. }
  246. public void ChangePasswordResponse(byte successful, String characterName, String msgToSend)
  247. {
  248. sendPacket(new ChangePasswordResponse(successful, characterName, msgToSend));
  249. }
  250. /**
  251. * @param hosts The gameHost to set.
  252. */
  253. public void setGameHosts(String[] hosts)
  254. {
  255. _log.info("Updated Gameserver [" + getServerId() + "] " + GameServerTable.getInstance().getServerNameById(getServerId()) + " IP's:");
  256. _gsi.clearServerAddresses();
  257. for (int i = 0; i < hosts.length; i += 2)
  258. {
  259. try
  260. {
  261. _gsi.addServerAddress(hosts[i], hosts[i + 1]);
  262. }
  263. catch (Exception e)
  264. {
  265. _log.warning("Couldn't resolve hostname \"" + e + "\"");
  266. }
  267. }
  268. for (String s : _gsi.getServerAddresses())
  269. {
  270. _log.info(s);
  271. }
  272. }
  273. /**
  274. * @return Returns the isAuthed.
  275. */
  276. public boolean isAuthed()
  277. {
  278. if (getGameServerInfo() == null)
  279. {
  280. return false;
  281. }
  282. return getGameServerInfo().isAuthed();
  283. }
  284. public void setGameServerInfo(GameServerInfo gsi)
  285. {
  286. _gsi = gsi;
  287. }
  288. public GameServerInfo getGameServerInfo()
  289. {
  290. return _gsi;
  291. }
  292. /**
  293. * @return Returns the connectionIpAddress.
  294. */
  295. public String getConnectionIpAddress()
  296. {
  297. return _connectionIPAddress;
  298. }
  299. public int getServerId()
  300. {
  301. if (getGameServerInfo() != null)
  302. {
  303. return getGameServerInfo().getId();
  304. }
  305. return -1;
  306. }
  307. public RSAPrivateKey getPrivateKey()
  308. {
  309. return _privateKey;
  310. }
  311. public void SetBlowFish(NewCrypt blowfish)
  312. {
  313. _blowfish = blowfish;
  314. }
  315. public void addAccountOnGameServer(String account)
  316. {
  317. _accountsOnGameServer.add(account);
  318. }
  319. public void removeAccountOnGameServer(String account)
  320. {
  321. _accountsOnGameServer.remove(account);
  322. }
  323. public GameServerState getLoginConnectionState()
  324. {
  325. return _loginConnectionState;
  326. }
  327. public void setLoginConnectionState(GameServerState state)
  328. {
  329. _loginConnectionState = state;
  330. }
  331. }