Status.java 4.4 KB

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