L2LoginServer.java 9.2 KB

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