Status.java 5.1 KB

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