L2LoginServer.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. // Prepare Database
  95. try
  96. {
  97. L2DatabaseFactory.getInstance();
  98. }
  99. catch (SQLException e)
  100. {
  101. _log.log(Level.SEVERE, "FATAL: Failed initializing database. Reason: " + e.getMessage(), e);
  102. System.exit(1);
  103. }
  104. try
  105. {
  106. LoginController.load();
  107. }
  108. catch (GeneralSecurityException e)
  109. {
  110. _log.log(Level.SEVERE, "FATAL: Failed initializing LoginController. Reason: " + e.getMessage(), e);
  111. System.exit(1);
  112. }
  113. try
  114. {
  115. GameServerTable.load();
  116. }
  117. catch (GeneralSecurityException e)
  118. {
  119. _log.log(Level.SEVERE, "FATAL: Failed to load GameServerTable. Reason: " + e.getMessage(), e);
  120. System.exit(1);
  121. }
  122. catch (SQLException e)
  123. {
  124. _log.log(Level.SEVERE, "FATAL: Failed to load GameServerTable. Reason: " + e.getMessage(), e);
  125. System.exit(1);
  126. }
  127. loadBanFile();
  128. InetAddress bindAddress = null;
  129. if (!Config.LOGIN_BIND_ADDRESS.equals("*"))
  130. {
  131. try
  132. {
  133. bindAddress = InetAddress.getByName(Config.LOGIN_BIND_ADDRESS);
  134. }
  135. catch (UnknownHostException e)
  136. {
  137. _log.log(Level.WARNING, "WARNING: The LoginServer bind address is invalid, using all avaliable IPs. Reason: " + e.getMessage(), e);
  138. }
  139. }
  140. final SelectorConfig sc = new SelectorConfig();
  141. sc.MAX_READ_PER_PASS = Config.MMO_MAX_READ_PER_PASS;
  142. sc.MAX_SEND_PER_PASS = Config.MMO_MAX_SEND_PER_PASS;
  143. sc.SLEEP_TIME = Config.MMO_SELECTOR_SLEEP_TIME;
  144. sc.HELPER_BUFFER_COUNT = Config.MMO_HELPER_BUFFER_COUNT;
  145. final L2LoginPacketHandler lph = new L2LoginPacketHandler();
  146. final SelectorHelper sh = new SelectorHelper();
  147. try
  148. {
  149. _selectorThread = new SelectorThread<L2LoginClient>(sc, sh, lph, sh, sh);
  150. }
  151. catch (IOException e)
  152. {
  153. _log.log(Level.SEVERE, "FATAL: Failed to open Selector. Reason: " + e.getMessage(), e);
  154. System.exit(1);
  155. }
  156. try
  157. {
  158. _gameServerListener = new GameServerListener();
  159. _gameServerListener.start();
  160. _log.info("Listening for GameServers on " + Config.GAME_SERVER_LOGIN_HOST + ":" + Config.GAME_SERVER_LOGIN_PORT);
  161. }
  162. catch (IOException e)
  163. {
  164. _log.log(Level.SEVERE, "FATAL: Failed to start the Game Server Listener. Reason: " + e.getMessage(), e);
  165. System.exit(1);
  166. }
  167. if (Config.IS_TELNET_ENABLED)
  168. {
  169. try
  170. {
  171. _statusServer = new Status(Server.serverMode);
  172. _statusServer.start();
  173. }
  174. catch (IOException e)
  175. {
  176. _log.log(Level.WARNING, "Failed to start the Telnet Server. Reason: " + e.getMessage(), e);
  177. }
  178. }
  179. else
  180. {
  181. _log.info("Telnet server is currently disabled.");
  182. }
  183. try
  184. {
  185. _selectorThread.openServerSocket(bindAddress, Config.PORT_LOGIN);
  186. }
  187. catch (IOException e)
  188. {
  189. _log.log(Level.SEVERE, "FATAL: Failed to open server socket. Reason: " + e.getMessage(), e);
  190. System.exit(1);
  191. }
  192. _selectorThread.start();
  193. _log.info("Login Server ready on " + (bindAddress == null ? "*" : bindAddress.getHostAddress()) + ":" + Config.PORT_LOGIN);
  194. }
  195. public Status getStatusServer()
  196. {
  197. return _statusServer;
  198. }
  199. public GameServerListener getGameServerListener()
  200. {
  201. return _gameServerListener;
  202. }
  203. private void loadBanFile()
  204. {
  205. File bannedFile = new File("./banned_ip.cfg");
  206. if (bannedFile.exists() && bannedFile.isFile())
  207. {
  208. FileInputStream fis = null;
  209. try
  210. {
  211. fis = new FileInputStream(bannedFile);
  212. }
  213. catch (FileNotFoundException e)
  214. {
  215. _log.log(Level.WARNING, "Failed to load banned IPs file (" + bannedFile.getName() + ") for reading. Reason: " + e.getMessage(), e);
  216. return;
  217. }
  218. LineNumberReader reader = null;
  219. String line;
  220. String[] parts;
  221. try
  222. {
  223. reader = new LineNumberReader(new InputStreamReader(fis));
  224. while ((line = reader.readLine()) != null)
  225. {
  226. line = line.trim();
  227. // check if this line isnt a comment line
  228. if (line.length() > 0 && line.charAt(0) != '#')
  229. {
  230. // split comments if any
  231. parts = line.split("#", 2);
  232. // discard comments in the line, if any
  233. line = parts[0];
  234. parts = line.split(" ");
  235. String address = parts[0];
  236. long duration = 0;
  237. if (parts.length > 1)
  238. {
  239. try
  240. {
  241. duration = Long.parseLong(parts[1]);
  242. }
  243. catch (NumberFormatException e)
  244. {
  245. _log.warning("Skipped: Incorrect ban duration (" + parts[1] + ") on (" + bannedFile.getName() + "). Line: " + reader.getLineNumber());
  246. continue;
  247. }
  248. }
  249. try
  250. {
  251. LoginController.getInstance().addBanForAddress(address, duration);
  252. }
  253. catch (UnknownHostException e)
  254. {
  255. _log.warning("Skipped: Invalid address (" + parts[0] + ") on (" + bannedFile.getName() + "). Line: " + reader.getLineNumber());
  256. }
  257. }
  258. }
  259. }
  260. catch (IOException e)
  261. {
  262. _log.log(Level.WARNING, "Error while reading the bans file (" + bannedFile.getName() + "). Details: " + e.getMessage(), e);
  263. }
  264. finally
  265. {
  266. try
  267. {
  268. reader.close();
  269. }
  270. catch (Exception e)
  271. {
  272. }
  273. try
  274. {
  275. fis.close();
  276. }
  277. catch (Exception e)
  278. {
  279. }
  280. }
  281. _log.info("Loaded " + LoginController.getInstance().getBannedIps().size() + " IP Bans.");
  282. }
  283. else
  284. {
  285. _log.warning("IP Bans file (" + bannedFile.getName() + ") is missing or is a directory, skipped.");
  286. }
  287. }
  288. public void shutdown(boolean restart)
  289. {
  290. Runtime.getRuntime().exit(restart ? 2 : 0);
  291. }
  292. }