GameStatusThread.java 5.9 KB

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