GameServerThread.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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.InitLS;
  32. import com.l2jserver.loginserver.network.loginserverpackets.KickPlayer;
  33. import com.l2jserver.loginserver.network.loginserverpackets.LoginServerFail;
  34. import com.l2jserver.loginserver.network.loginserverpackets.RequestCharacters;
  35. import com.l2jserver.util.Util;
  36. import com.l2jserver.util.crypt.NewCrypt;
  37. import com.l2jserver.util.network.BaseSendablePacket;
  38. /**
  39. * @author -Wooden-
  40. * @author KenM
  41. *
  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 gameServerAuth The server info.
  149. */
  150. public void attachGameServerInfo(GameServerInfo gsi, int port, String[] hosts, int maxPlayers)
  151. {
  152. setGameServerInfo(gsi);
  153. gsi.setGameServerThread(this);
  154. gsi.setPort(port);
  155. setGameHosts(hosts);
  156. gsi.setMaxPlayers(maxPlayers);
  157. gsi.setAuthed(true);
  158. }
  159. public void forceClose(int reason)
  160. {
  161. sendPacket(new LoginServerFail(reason));
  162. try
  163. {
  164. _connection.close();
  165. }
  166. catch (IOException e)
  167. {
  168. _log.finer("GameServerThread: Failed disconnecting banned server, server already disconnected.");
  169. }
  170. }
  171. /**
  172. * @param ipAddress
  173. * @return
  174. */
  175. public static boolean isBannedGameserverIP(String ipAddress)
  176. {
  177. return false;
  178. }
  179. public GameServerThread(Socket con)
  180. {
  181. _connection = con;
  182. _connectionIp = con.getInetAddress().getHostAddress();
  183. try
  184. {
  185. _in = _connection.getInputStream();
  186. _out = new BufferedOutputStream(_connection.getOutputStream());
  187. }
  188. catch (IOException e)
  189. {
  190. e.printStackTrace();
  191. }
  192. KeyPair pair = GameServerTable.getInstance().getKeyPair();
  193. _privateKey = (RSAPrivateKey) pair.getPrivate();
  194. _publicKey = (RSAPublicKey) pair.getPublic();
  195. _blowfish = new NewCrypt("_;v.]05-31!|+-%xT!^[$\00");
  196. setName(getClass().getSimpleName()+"-"+getId()+"@"+_connectionIp);
  197. start();
  198. }
  199. /**
  200. * @param sl
  201. * @throws IOException
  202. */
  203. public void sendPacket(BaseSendablePacket sl)
  204. {
  205. try
  206. {
  207. byte[] data = sl.getContent();
  208. NewCrypt.appendChecksum(data);
  209. if (Config.DEBUG)
  210. {
  211. _log.finest("[S] "+sl.getClass().getSimpleName()+":\n"+Util.printData(data));
  212. }
  213. data = _blowfish.crypt(data);
  214. int len = data.length+2;
  215. synchronized(_out)
  216. {
  217. _out.write(len & 0xff);
  218. _out.write(len >> 8 &0xff);
  219. _out.write(data);
  220. _out.flush();
  221. }
  222. }
  223. catch (IOException e)
  224. {
  225. _log.severe("IOException while sending packet "+sl.getClass().getSimpleName());
  226. }
  227. }
  228. public void broadcastToTelnet(String msg)
  229. {
  230. if (L2LoginServer.getInstance().getStatusServer() != null)
  231. {
  232. L2LoginServer.getInstance().getStatusServer().sendMessageToTelnets(msg);
  233. }
  234. }
  235. public void kickPlayer(String account)
  236. {
  237. sendPacket(new KickPlayer(account));
  238. }
  239. public void requestCharacters(String account)
  240. {
  241. sendPacket(new RequestCharacters(account));
  242. }
  243. /**
  244. * @param gameHost The gameHost to set.
  245. */
  246. public void setGameHosts(String[] hosts)
  247. {
  248. _log.info("Updated Gameserver ["+getServerId()+"] "+GameServerTable.getInstance().getServerNameById(getServerId())+" IP's:");
  249. _gsi.clearServerAddresses();
  250. for (int i = 0; i < hosts.length; i += 2)
  251. {
  252. try
  253. {
  254. _gsi.addServerAddress(hosts[i], hosts[i + 1]);
  255. }
  256. catch (Exception e)
  257. {
  258. _log.warning("Couldn't resolve hostname \""+e+"\"");
  259. }
  260. }
  261. for (String s : _gsi.getServerAddresses())
  262. _log.info(s);
  263. }
  264. /**
  265. * @return Returns the isAuthed.
  266. */
  267. public boolean isAuthed()
  268. {
  269. if (getGameServerInfo() == null)
  270. return false;
  271. return getGameServerInfo().isAuthed();
  272. }
  273. public void setGameServerInfo(GameServerInfo gsi)
  274. {
  275. _gsi = gsi;
  276. }
  277. public GameServerInfo getGameServerInfo()
  278. {
  279. return _gsi;
  280. }
  281. /**
  282. * @return Returns the connectionIpAddress.
  283. */
  284. public String getConnectionIpAddress()
  285. {
  286. return _connectionIPAddress;
  287. }
  288. public int getServerId()
  289. {
  290. if (getGameServerInfo() != null)
  291. {
  292. return getGameServerInfo().getId();
  293. }
  294. return -1;
  295. }
  296. public RSAPrivateKey getPrivateKey()
  297. {
  298. return _privateKey;
  299. }
  300. public void SetBlowFish(NewCrypt blowfish)
  301. {
  302. _blowfish = blowfish;
  303. }
  304. public void addAccountOnGameServer(String account)
  305. {
  306. _accountsOnGameServer.add(account);
  307. }
  308. public void removeAccountOnGameServer(String account)
  309. {
  310. _accountsOnGameServer.remove(account);
  311. }
  312. public GameServerState getLoginConnectionState()
  313. {
  314. return _loginConnectionState;
  315. }
  316. public void setLoginConnectionState(GameServerState state)
  317. {
  318. _loginConnectionState = state;
  319. }
  320. }