L2LoginServer.java 8.4 KB

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