Status.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.status;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.net.ServerSocket;
  25. import java.net.Socket;
  26. import java.util.List;
  27. import java.util.Properties;
  28. import java.util.logging.Logger;
  29. import javolution.util.FastList;
  30. import com.l2jserver.Config;
  31. import com.l2jserver.Server;
  32. import com.l2jserver.util.Rnd;
  33. public class Status extends Thread
  34. {
  35. protected static final Logger _log = Logger.getLogger(Status.class.getName());
  36. private final ServerSocket statusServerSocket;
  37. private final int _uptime;
  38. private final int _statusPort;
  39. private String _statusPw;
  40. private final int _mode;
  41. private final List<LoginStatusThread> _loginStatus;
  42. @Override
  43. public void run()
  44. {
  45. setPriority(Thread.MAX_PRIORITY);
  46. while (!isInterrupted())
  47. {
  48. try
  49. {
  50. Socket connection = statusServerSocket.accept();
  51. if (_mode == Server.MODE_GAMESERVER)
  52. {
  53. new GameStatusThread(connection, _uptime, _statusPw);
  54. }
  55. else if (_mode == Server.MODE_LOGINSERVER)
  56. {
  57. LoginStatusThread lst = new LoginStatusThread(connection, _uptime, _statusPw);
  58. if (lst.isAlive())
  59. {
  60. _loginStatus.add(lst);
  61. }
  62. }
  63. if (isInterrupted())
  64. {
  65. try
  66. {
  67. statusServerSocket.close();
  68. }
  69. catch (IOException io)
  70. {
  71. _log.warning(getClass().getSimpleName() + ": " + io.getMessage());
  72. }
  73. break;
  74. }
  75. }
  76. catch (IOException e)
  77. {
  78. if (isInterrupted())
  79. {
  80. try
  81. {
  82. statusServerSocket.close();
  83. }
  84. catch (IOException io)
  85. {
  86. _log.warning(getClass().getSimpleName() + ": " + io.getMessage());
  87. }
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. public Status(int mode) throws IOException
  94. {
  95. super("Status");
  96. _mode = mode;
  97. Properties telnetSettings = new Properties();
  98. try (InputStream is = new FileInputStream(new File(Config.TELNET_FILE)))
  99. {
  100. telnetSettings.load(is);
  101. }
  102. _statusPort = Integer.parseInt(telnetSettings.getProperty("StatusPort", "12345"));
  103. _statusPw = telnetSettings.getProperty("StatusPW");
  104. if ((_mode == Server.MODE_GAMESERVER) || (_mode == Server.MODE_LOGINSERVER))
  105. {
  106. if (_statusPw == null)
  107. {
  108. _log.info("Server's Telnet Function Has No Password Defined!");
  109. _log.info("A Password Has Been Automaticly Created!");
  110. _statusPw = rndPW(10);
  111. _log.info("Password Has Been Set To: " + _statusPw);
  112. }
  113. _log.info("Telnet StatusServer started successfully, listening on Port: " + _statusPort);
  114. }
  115. statusServerSocket = new ServerSocket(_statusPort);
  116. _uptime = (int) System.currentTimeMillis();
  117. _loginStatus = new FastList<>();
  118. }
  119. private String rndPW(int length)
  120. {
  121. final String lowerChar = "qwertyuiopasdfghjklzxcvbnm";
  122. final String upperChar = "QWERTYUIOPASDFGHJKLZXCVBNM";
  123. final String digits = "1234567890";
  124. final StringBuilder password = new StringBuilder(length);
  125. for (int i = 0; i < length; i++)
  126. {
  127. int charSet = Rnd.nextInt(3);
  128. switch (charSet)
  129. {
  130. case 0:
  131. password.append(lowerChar.charAt(Rnd.nextInt(lowerChar.length() - 1)));
  132. break;
  133. case 1:
  134. password.append(upperChar.charAt(Rnd.nextInt(upperChar.length() - 1)));
  135. break;
  136. case 2:
  137. password.append(digits.charAt(Rnd.nextInt(digits.length() - 1)));
  138. break;
  139. }
  140. }
  141. return password.toString();
  142. }
  143. public void sendMessageToTelnets(String msg)
  144. {
  145. List<LoginStatusThread> lsToRemove = new FastList<>();
  146. for (LoginStatusThread ls : _loginStatus)
  147. {
  148. if (ls.isInterrupted())
  149. {
  150. lsToRemove.add(ls);
  151. }
  152. else
  153. {
  154. ls.printToTelnet(msg);
  155. }
  156. }
  157. }
  158. }