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 ServerSocket statusServerSocket;
  33. private int _uptime;
  34. private int _statusPort;
  35. private String _statusPw;
  36. private int _mode;
  37. private List<LoginStatusThread> _loginStatus;
  38. @Override
  39. public void run()
  40. {
  41. this.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 (this.isInterrupted())
  60. {
  61. try
  62. {
  63. statusServerSocket.close();
  64. }
  65. catch (IOException io)
  66. {
  67. io.printStackTrace();
  68. }
  69. break;
  70. }
  71. }
  72. catch (IOException e)
  73. {
  74. if (this.isInterrupted())
  75. {
  76. try
  77. {
  78. statusServerSocket.close();
  79. }
  80. catch (IOException io)
  81. {
  82. io.printStackTrace();
  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. InputStream is = new FileInputStream(new File(Config.TELNET_FILE));
  95. telnetSettings.load(is);
  96. is.close();
  97. _statusPort = Integer.parseInt(telnetSettings.getProperty("StatusPort", "12345"));
  98. _statusPw = telnetSettings.getProperty("StatusPW");
  99. if (_mode == Server.MODE_GAMESERVER || _mode == Server.MODE_LOGINSERVER)
  100. {
  101. if (_statusPw == null)
  102. {
  103. _log.info("Server's Telnet Function Has No Password Defined!");
  104. _log.info("A Password Has Been Automaticly Created!");
  105. _statusPw = rndPW(10);
  106. _log.info("Password Has Been Set To: " + _statusPw);
  107. }
  108. _log.info("Telnet StatusServer started successfully, listening on Port: " + _statusPort);
  109. }
  110. statusServerSocket = new ServerSocket(_statusPort);
  111. _uptime = (int) System.currentTimeMillis();
  112. _loginStatus = new FastList<LoginStatusThread>();
  113. }
  114. private String rndPW(int length)
  115. {
  116. final String lowerChar = "qwertyuiopasdfghjklzxcvbnm";
  117. final String upperChar = "QWERTYUIOPASDFGHJKLZXCVBNM";
  118. final String digits = "1234567890";
  119. final StringBuilder password = new StringBuilder(length);
  120. for (int i = 0; i < length; i++)
  121. {
  122. int charSet = Rnd.nextInt(3);
  123. switch (charSet)
  124. {
  125. case 0:
  126. password.append(lowerChar.charAt(Rnd.nextInt(lowerChar.length() - 1)));
  127. break;
  128. case 1:
  129. password.append(upperChar.charAt(Rnd.nextInt(upperChar.length() - 1)));
  130. break;
  131. case 2:
  132. password.append(digits.charAt(Rnd.nextInt(digits.length() - 1)));
  133. break;
  134. }
  135. }
  136. return password.toString();
  137. }
  138. public void sendMessageToTelnets(String msg)
  139. {
  140. List<LoginStatusThread> lsToRemove = new FastList<LoginStatusThread>();
  141. for (LoginStatusThread ls : _loginStatus)
  142. {
  143. if (ls.isInterrupted())
  144. {
  145. lsToRemove.add(ls);
  146. }
  147. else
  148. {
  149. ls.printToTelnet(msg);
  150. }
  151. }
  152. }
  153. }