Status.java 5.1 KB

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