GameServerThread.java 9.6 KB

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