GameStatusThread.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.BufferedReader;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.io.PrintWriter;
  23. import java.net.InetAddress;
  24. import java.net.Socket;
  25. import java.util.Properties;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.gameserver.handler.ITelnetHandler;
  29. import com.l2jserver.gameserver.handler.TelnetHandler;
  30. public class GameStatusThread extends Thread
  31. {
  32. private static final Logger _log = Logger.getLogger(GameStatusThread.class.getName());
  33. private final Socket _cSocket;
  34. private final PrintWriter _print;
  35. private final BufferedReader _read;
  36. private final int _uptime;
  37. private void telnetOutput(int type, String text)
  38. {
  39. if (Config.DEVELOPER)
  40. {
  41. if (type == 1)
  42. System.out.println("TELNET | " + text);
  43. else if (type == 2)
  44. System.out.print("TELNET | " + text);
  45. else if (type == 3)
  46. System.out.print(text);
  47. else if (type == 4)
  48. System.out.println(text);
  49. else
  50. System.out.println("TELNET | " + text);
  51. }
  52. else
  53. {
  54. // only print output if the message is rejected
  55. if (type == 5)
  56. System.out.println("TELNET | " + text);
  57. }
  58. }
  59. private boolean isValidIP(Socket client)
  60. {
  61. boolean result = false;
  62. InetAddress ClientIP = client.getInetAddress();
  63. // convert IP to String, and compare with list
  64. String clientStringIP = ClientIP.getHostAddress();
  65. telnetOutput(1, "Connection from: " + clientStringIP);
  66. // read and loop thru list of IPs, compare with newIP
  67. if (Config.DEVELOPER)
  68. telnetOutput(2, "");
  69. final File file = new File(Config.TELNET_FILE);
  70. try (InputStream telnetIS = new FileInputStream(file);)
  71. {
  72. Properties telnetSettings = new Properties();
  73. telnetSettings.load(telnetIS);
  74. String HostList = telnetSettings.getProperty("ListOfHosts", "127.0.0.1,localhost,::1");
  75. if (Config.DEVELOPER)
  76. telnetOutput(3, "Comparing ip to list...");
  77. // compare
  78. String ipToCompare = null;
  79. for (String ip : HostList.split(","))
  80. {
  81. if (!result)
  82. {
  83. ipToCompare = InetAddress.getByName(ip).getHostAddress();
  84. if (clientStringIP.equals(ipToCompare))
  85. result = true;
  86. if (Config.DEVELOPER)
  87. telnetOutput(3, clientStringIP + " = " + ipToCompare + "(" + ip + ") = " + result);
  88. }
  89. }
  90. }
  91. catch (IOException e)
  92. {
  93. if (Config.DEVELOPER)
  94. telnetOutput(4, "");
  95. telnetOutput(1, "Error: " + e);
  96. }
  97. if (Config.DEVELOPER)
  98. telnetOutput(4, "Allow IP: " + result);
  99. return result;
  100. }
  101. public GameStatusThread(Socket client, int uptime, String StatusPW) throws IOException
  102. {
  103. setPriority(Thread.MAX_PRIORITY);
  104. _cSocket = client;
  105. _uptime = uptime;
  106. _print = new PrintWriter(_cSocket.getOutputStream());
  107. _read = new BufferedReader(new InputStreamReader(_cSocket.getInputStream()));
  108. if (isValidIP(client))
  109. {
  110. telnetOutput(1, client.getInetAddress().getHostAddress() + " accepted.");
  111. _print.println("Welcome To The L2J Telnet Session.");
  112. _print.println("Please Insert Your Password!");
  113. _print.print("Password: ");
  114. _print.flush();
  115. String tmpLine = _read.readLine();
  116. if (tmpLine == null)
  117. {
  118. _print.println("Error.");
  119. _print.println("Disconnected...");
  120. _print.flush();
  121. _cSocket.close();
  122. }
  123. else
  124. {
  125. if (tmpLine.compareTo(StatusPW) != 0)
  126. {
  127. _print.println("Incorrect Password!");
  128. _print.println("Disconnected...");
  129. _print.flush();
  130. _cSocket.close();
  131. }
  132. else
  133. {
  134. _print.println("Password Correct!");
  135. _print.println("[L2J Game Server]");
  136. _print.print("");
  137. _print.flush();
  138. start();
  139. }
  140. }
  141. }
  142. else
  143. {
  144. telnetOutput(5, "Connection attempt from " + client.getInetAddress().getHostAddress() + " rejected.");
  145. _cSocket.close();
  146. }
  147. }
  148. @Override
  149. public void run()
  150. {
  151. String _usrCommand = "";
  152. try
  153. {
  154. while (_usrCommand.compareTo("quit") != 0 && _usrCommand.compareTo("exit") != 0)
  155. {
  156. _usrCommand = _read.readLine();
  157. if (_usrCommand == null)
  158. {
  159. _cSocket.close();
  160. break;
  161. }
  162. final ITelnetHandler handler = TelnetHandler.getInstance().getHandler(_usrCommand);
  163. if (handler != null)
  164. handler.useCommand(_usrCommand, _print, _cSocket, _uptime);
  165. else if (_usrCommand.equalsIgnoreCase("quit") || _usrCommand.equalsIgnoreCase("exit") || _usrCommand.length() == 0)
  166. {
  167. /* Do Nothing :p - Just here to save us from the "Command Not Understood" Text */
  168. }
  169. else
  170. _print.print("Command: " + _usrCommand + " was not found!");
  171. _print.print("");
  172. _print.flush();
  173. }
  174. if (!_cSocket.isClosed())
  175. {
  176. _print.println("Bye Bye!");
  177. _print.flush();
  178. _cSocket.close();
  179. }
  180. telnetOutput(1, "Connection from " + _cSocket.getInetAddress().getHostAddress() + " was closed by client.");
  181. }
  182. catch (IOException e)
  183. {
  184. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  185. }
  186. }
  187. }