L2LoginServer.java 8.2 KB

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