L2LoginServer.java 8.3 KB

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