Status.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.status;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.net.ServerSocket;
  21. import java.net.Socket;
  22. import java.util.List;
  23. import java.util.Properties;
  24. import java.util.logging.Logger;
  25. import javolution.util.FastList;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.Server;
  28. import com.l2jserver.util.Rnd;
  29. public class Status extends Thread
  30. {
  31. protected static final Logger _log = Logger.getLogger(Status.class.getName());
  32. private final ServerSocket statusServerSocket;
  33. private final int _uptime;
  34. private final int _statusPort;
  35. private String _statusPw;
  36. private final int _mode;
  37. private final List<LoginStatusThread> _loginStatus;
  38. @Override
  39. public void run()
  40. {
  41. setPriority(Thread.MAX_PRIORITY);
  42. while (true)
  43. {
  44. try
  45. {
  46. Socket connection = statusServerSocket.accept();
  47. if (_mode == Server.MODE_GAMESERVER)
  48. {
  49. new GameStatusThread(connection, _uptime, _statusPw);
  50. }
  51. else if (_mode == Server.MODE_LOGINSERVER)
  52. {
  53. LoginStatusThread lst = new LoginStatusThread(connection, _uptime, _statusPw);
  54. if (lst.isAlive())
  55. {
  56. _loginStatus.add(lst);
  57. }
  58. }
  59. if (isInterrupted())
  60. {
  61. try
  62. {
  63. statusServerSocket.close();
  64. }
  65. catch (IOException io)
  66. {
  67. _log.warning(getClass().getSimpleName() + ": " + io.getMessage());
  68. }
  69. break;
  70. }
  71. }
  72. catch (IOException e)
  73. {
  74. if (isInterrupted())
  75. {
  76. try
  77. {
  78. statusServerSocket.close();
  79. }
  80. catch (IOException io)
  81. {
  82. _log.warning(getClass().getSimpleName() + ": " + io.getMessage());
  83. }
  84. break;
  85. }
  86. }
  87. }
  88. }
  89. public Status(int mode) throws IOException
  90. {
  91. super("Status");
  92. _mode = mode;
  93. Properties telnetSettings = new Properties();
  94. try (InputStream is = new FileInputStream(new File(Config.TELNET_FILE)))
  95. {
  96. telnetSettings.load(is);
  97. }
  98. _statusPort = Integer.parseInt(telnetSettings.getProperty("StatusPort", "12345"));
  99. _statusPw = telnetSettings.getProperty("StatusPW");
  100. if ((_mode == Server.MODE_GAMESERVER) || (_mode == Server.MODE_LOGINSERVER))
  101. {
  102. if (_statusPw == null)
  103. {
  104. _log.info("Server's Telnet Function Has No Password Defined!");
  105. _log.info("A Password Has Been Automaticly Created!");
  106. _statusPw = rndPW(10);
  107. _log.info("Password Has Been Set To: " + _statusPw);
  108. }
  109. _log.info("Telnet StatusServer started successfully, listening on Port: " + _statusPort);
  110. }
  111. statusServerSocket = new ServerSocket(_statusPort);
  112. _uptime = (int) System.currentTimeMillis();
  113. _loginStatus = new FastList<>();
  114. }
  115. private String rndPW(int length)
  116. {
  117. final String lowerChar = "qwertyuiopasdfghjklzxcvbnm";
  118. final String upperChar = "QWERTYUIOPASDFGHJKLZXCVBNM";
  119. final String digits = "1234567890";
  120. final StringBuilder password = new StringBuilder(length);
  121. for (int i = 0; i < length; i++)
  122. {
  123. int charSet = Rnd.nextInt(3);
  124. switch (charSet)
  125. {
  126. case 0:
  127. password.append(lowerChar.charAt(Rnd.nextInt(lowerChar.length() - 1)));
  128. break;
  129. case 1:
  130. password.append(upperChar.charAt(Rnd.nextInt(upperChar.length() - 1)));
  131. break;
  132. case 2:
  133. password.append(digits.charAt(Rnd.nextInt(digits.length() - 1)));
  134. break;
  135. }
  136. }
  137. return password.toString();
  138. }
  139. public void sendMessageToTelnets(String msg)
  140. {
  141. List<LoginStatusThread> lsToRemove = new FastList<>();
  142. for (LoginStatusThread ls : _loginStatus)
  143. {
  144. if (ls.isInterrupted())
  145. {
  146. lsToRemove.add(ls);
  147. }
  148. else
  149. {
  150. ls.printToTelnet(msg);
  151. }
  152. }
  153. }
  154. }