L2LoginServer.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileNotFoundException;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.io.LineNumberReader;
  23. import java.net.InetAddress;
  24. import java.net.UnknownHostException;
  25. import java.security.GeneralSecurityException;
  26. import java.sql.SQLException;
  27. import java.util.logging.Level;
  28. import java.util.logging.LogManager;
  29. import java.util.logging.Logger;
  30. import org.mmocore.network.SelectorConfig;
  31. import org.mmocore.network.SelectorThread;
  32. import com.l2jserver.Config;
  33. import com.l2jserver.L2DatabaseFactory;
  34. import com.l2jserver.Server;
  35. import com.l2jserver.status.Status;
  36. /**
  37. *
  38. * @author KenM
  39. */
  40. public class L2LoginServer
  41. {
  42. public static final int PROTOCOL_REV = 0x0104;
  43. private static L2LoginServer _instance;
  44. private Logger _log = Logger.getLogger(L2LoginServer.class.getName());
  45. private GameServerListener _gameServerListener;
  46. private SelectorThread<L2LoginClient> _selectorThread;
  47. private Status _statusServer;
  48. public static void main(String[] args)
  49. {
  50. _instance = new L2LoginServer();
  51. }
  52. public static L2LoginServer getInstance()
  53. {
  54. return _instance;
  55. }
  56. public L2LoginServer()
  57. {
  58. Server.serverMode = Server.MODE_LOGINSERVER;
  59. // Local Constants
  60. final String LOG_FOLDER = "log"; // Name of folder for log file
  61. final String LOG_NAME = "./log.cfg"; // Name of log file
  62. /*** Main ***/
  63. // Create log folder
  64. File logFolder = new File(Config.DATAPACK_ROOT, LOG_FOLDER);
  65. logFolder.mkdir();
  66. // Create input stream for log file -- or store file data into memory
  67. InputStream is = null;
  68. try
  69. {
  70. is = new FileInputStream(new File(LOG_NAME));
  71. LogManager.getLogManager().readConfiguration(is);
  72. is.close();
  73. }
  74. catch (IOException e)
  75. {
  76. e.printStackTrace();
  77. }
  78. finally
  79. {
  80. try
  81. {
  82. if (is != null)
  83. {
  84. is.close();
  85. }
  86. }
  87. catch (IOException e)
  88. {
  89. e.printStackTrace();
  90. }
  91. }
  92. // Load Config
  93. Config.load();
  94. L2DatabaseFactory.getInstance();
  95. try
  96. {
  97. LoginController.load();
  98. }
  99. catch (GeneralSecurityException e)
  100. {
  101. _log.log(Level.SEVERE, "FATAL: Failed initializing LoginController. Reason: " + e.getMessage(), e);
  102. System.exit(1);
  103. }
  104. try
  105. {
  106. GameServerTable.load();
  107. }
  108. catch (GeneralSecurityException e)
  109. {
  110. _log.log(Level.SEVERE, "FATAL: Failed to load GameServerTable. Reason: " + e.getMessage(), e);
  111. System.exit(1);
  112. }
  113. catch (SQLException e)
  114. {
  115. _log.log(Level.SEVERE, "FATAL: Failed to load GameServerTable. Reason: " + e.getMessage(), e);
  116. System.exit(1);
  117. }
  118. loadBanFile();
  119. InetAddress bindAddress = null;
  120. if (!Config.LOGIN_BIND_ADDRESS.equals("*"))
  121. {
  122. try
  123. {
  124. bindAddress = InetAddress.getByName(Config.LOGIN_BIND_ADDRESS);
  125. }
  126. catch (UnknownHostException e)
  127. {
  128. _log.log(Level.WARNING, "WARNING: The LoginServer bind address is invalid, using all avaliable IPs. Reason: " + e.getMessage(), e);
  129. }
  130. }
  131. final SelectorConfig sc = new SelectorConfig();
  132. sc.MAX_READ_PER_PASS = Config.MMO_MAX_READ_PER_PASS;
  133. sc.MAX_SEND_PER_PASS = Config.MMO_MAX_SEND_PER_PASS;
  134. sc.SLEEP_TIME = Config.MMO_SELECTOR_SLEEP_TIME;
  135. sc.HELPER_BUFFER_COUNT = Config.MMO_HELPER_BUFFER_COUNT;
  136. final L2LoginPacketHandler lph = new L2LoginPacketHandler();
  137. final SelectorHelper sh = new SelectorHelper();
  138. try
  139. {
  140. _selectorThread = new SelectorThread<L2LoginClient>(sc, sh, lph, sh, sh);
  141. }
  142. catch (IOException e)
  143. {
  144. _log.log(Level.SEVERE, "FATAL: Failed to open Selector. Reason: " + e.getMessage(), e);
  145. System.exit(1);
  146. }
  147. try
  148. {
  149. _gameServerListener = new GameServerListener();
  150. _gameServerListener.start();
  151. _log.info("Listening for GameServers on " + Config.GAME_SERVER_LOGIN_HOST + ":" + Config.GAME_SERVER_LOGIN_PORT);
  152. }
  153. catch (IOException e)
  154. {
  155. _log.log(Level.SEVERE, "FATAL: Failed to start the Game Server Listener. Reason: " + e.getMessage(), e);
  156. System.exit(1);
  157. }
  158. if (Config.IS_TELNET_ENABLED)
  159. {
  160. try
  161. {
  162. _statusServer = new Status(Server.serverMode);
  163. _statusServer.start();
  164. }
  165. catch (IOException e)
  166. {
  167. _log.log(Level.WARNING, "Failed to start the Telnet Server. Reason: " + e.getMessage(), e);
  168. }
  169. }
  170. else
  171. {
  172. _log.info("Telnet server is currently disabled.");
  173. }
  174. try
  175. {
  176. _selectorThread.openServerSocket(bindAddress, Config.PORT_LOGIN);
  177. }
  178. catch (IOException e)
  179. {
  180. _log.log(Level.SEVERE, "FATAL: Failed to open server socket. Reason: " + e.getMessage(), e);
  181. System.exit(1);
  182. }
  183. _selectorThread.start();
  184. _log.info("Login Server ready on " + (bindAddress == null ? "*" : bindAddress.getHostAddress()) + ":" + Config.PORT_LOGIN);
  185. }
  186. public Status getStatusServer()
  187. {
  188. return _statusServer;
  189. }
  190. public GameServerListener getGameServerListener()
  191. {
  192. return _gameServerListener;
  193. }
  194. private void loadBanFile()
  195. {
  196. File bannedFile = new File("./banned_ip.cfg");
  197. if (bannedFile.exists() && bannedFile.isFile())
  198. {
  199. FileInputStream fis = null;
  200. try
  201. {
  202. fis = new FileInputStream(bannedFile);
  203. }
  204. catch (FileNotFoundException e)
  205. {
  206. _log.log(Level.WARNING, "Failed to load banned IPs file (" + bannedFile.getName() + ") for reading. Reason: " + e.getMessage(), e);
  207. return;
  208. }
  209. LineNumberReader reader = null;
  210. String line;
  211. String[] parts;
  212. try
  213. {
  214. reader = new LineNumberReader(new InputStreamReader(fis));
  215. while ((line = reader.readLine()) != null)
  216. {
  217. line = line.trim();
  218. // check if this line isnt a comment line
  219. if (line.length() > 0 && line.charAt(0) != '#')
  220. {
  221. // split comments if any
  222. parts = line.split("#", 2);
  223. // discard comments in the line, if any
  224. line = parts[0];
  225. parts = line.split(" ");
  226. String address = parts[0];
  227. long duration = 0;
  228. if (parts.length > 1)
  229. {
  230. try
  231. {
  232. duration = Long.parseLong(parts[1]);
  233. }
  234. catch (NumberFormatException e)
  235. {
  236. _log.warning("Skipped: Incorrect ban duration (" + parts[1] + ") on (" + bannedFile.getName() + "). Line: " + reader.getLineNumber());
  237. continue;
  238. }
  239. }
  240. try
  241. {
  242. LoginController.getInstance().addBanForAddress(address, duration);
  243. }
  244. catch (UnknownHostException e)
  245. {
  246. _log.warning("Skipped: Invalid address (" + parts[0] + ") on (" + bannedFile.getName() + "). Line: " + reader.getLineNumber());
  247. }
  248. }
  249. }
  250. }
  251. catch (IOException e)
  252. {
  253. _log.log(Level.WARNING, "Error while reading the bans file (" + bannedFile.getName() + "). Details: " + e.getMessage(), e);
  254. }
  255. finally
  256. {
  257. try
  258. {
  259. reader.close();
  260. }
  261. catch (Exception e)
  262. {
  263. }
  264. try
  265. {
  266. fis.close();
  267. }
  268. catch (Exception e)
  269. {
  270. }
  271. }
  272. _log.info("Loaded " + LoginController.getInstance().getBannedIps().size() + " IP Bans.");
  273. }
  274. else
  275. {
  276. _log.warning("IP Bans file (" + bannedFile.getName() + ") is missing or is a directory, skipped.");
  277. }
  278. }
  279. public void shutdown(boolean restart)
  280. {
  281. Runtime.getRuntime().exit(restart ? 2 : 0);
  282. }
  283. }