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