2
0

L2LoginServer.java 8.8 KB

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