L2LoginServer.java 9.2 KB

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