GameServerThread.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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<String>();
  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. data = _blowfish.decrypt(data);
  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
  145. * <li>Updates the GameServerInfo values based on GameServerAuth packet</li>
  146. * <li><b>Sets the GameServerInfo as Authed</b></li>
  147. * @param gsi The GameServerInfo to be attached.
  148. * @param port
  149. * @param hosts
  150. * @param maxPlayers
  151. */
  152. public void attachGameServerInfo(GameServerInfo gsi, int port, String[] hosts, int maxPlayers)
  153. {
  154. setGameServerInfo(gsi);
  155. gsi.setGameServerThread(this);
  156. gsi.setPort(port);
  157. setGameHosts(hosts);
  158. gsi.setMaxPlayers(maxPlayers);
  159. gsi.setAuthed(true);
  160. }
  161. public void forceClose(int reason)
  162. {
  163. sendPacket(new LoginServerFail(reason));
  164. try
  165. {
  166. _connection.close();
  167. }
  168. catch (IOException e)
  169. {
  170. _log.finer("GameServerThread: Failed disconnecting banned server, server already disconnected.");
  171. }
  172. }
  173. /**
  174. * @param ipAddress
  175. * @return
  176. */
  177. public static boolean isBannedGameserverIP(String ipAddress)
  178. {
  179. return false;
  180. }
  181. public GameServerThread(Socket con)
  182. {
  183. _connection = con;
  184. _connectionIp = con.getInetAddress().getHostAddress();
  185. try
  186. {
  187. _in = _connection.getInputStream();
  188. _out = new BufferedOutputStream(_connection.getOutputStream());
  189. }
  190. catch (IOException e)
  191. {
  192. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  193. }
  194. KeyPair pair = GameServerTable.getInstance().getKeyPair();
  195. _privateKey = (RSAPrivateKey) pair.getPrivate();
  196. _publicKey = (RSAPublicKey) pair.getPublic();
  197. _blowfish = new NewCrypt("_;v.]05-31!|+-%xT!^[$\00");
  198. setName(getClass().getSimpleName()+"-"+getId()+"@"+_connectionIp);
  199. start();
  200. }
  201. /**
  202. * @param sl
  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 hosts 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. }