GameStatusThread.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.status;
  20. import java.io.BufferedReader;
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.InputStreamReader;
  26. import java.io.PrintWriter;
  27. import java.net.InetAddress;
  28. import java.net.Socket;
  29. import java.util.Properties;
  30. import java.util.logging.Logger;
  31. import com.l2jserver.Config;
  32. import com.l2jserver.gameserver.handler.ITelnetHandler;
  33. import com.l2jserver.gameserver.handler.TelnetHandler;
  34. public final class GameStatusThread extends Thread
  35. {
  36. private static final Logger _log = Logger.getLogger(GameStatusThread.class.getName());
  37. private final Socket _cSocket;
  38. private final PrintWriter _print;
  39. private final BufferedReader _read;
  40. private final int _uptime;
  41. private void telnetOutput(int type, String text)
  42. {
  43. if (Config.DEVELOPER)
  44. {
  45. if (type == 1)
  46. {
  47. System.out.println("TELNET | " + text);
  48. }
  49. else if (type == 2)
  50. {
  51. System.out.print("TELNET | " + text);
  52. }
  53. else if (type == 3)
  54. {
  55. System.out.print(text);
  56. }
  57. else if (type == 4)
  58. {
  59. System.out.println(text);
  60. }
  61. else
  62. {
  63. System.out.println("TELNET | " + text);
  64. }
  65. }
  66. else
  67. {
  68. // only print output if the message is rejected
  69. if (type == 5)
  70. {
  71. System.out.println("TELNET | " + text);
  72. }
  73. }
  74. }
  75. private boolean isValidIP(Socket client)
  76. {
  77. boolean result = false;
  78. InetAddress ClientIP = client.getInetAddress();
  79. // convert IP to String, and compare with list
  80. String clientStringIP = ClientIP.getHostAddress();
  81. telnetOutput(1, "Connection from: " + clientStringIP);
  82. // read and loop thru list of IPs, compare with newIP
  83. if (Config.DEVELOPER)
  84. {
  85. telnetOutput(2, "");
  86. }
  87. final File file = new File(Config.TELNET_FILE);
  88. try (InputStream telnetIS = new FileInputStream(file))
  89. {
  90. Properties telnetSettings = new Properties();
  91. telnetSettings.load(telnetIS);
  92. String HostList = telnetSettings.getProperty("ListOfHosts", "127.0.0.1,localhost,::1");
  93. if (Config.DEVELOPER)
  94. {
  95. telnetOutput(3, "Comparing ip to list...");
  96. }
  97. // compare
  98. String ipToCompare = null;
  99. for (String ip : HostList.split(","))
  100. {
  101. if (!result)
  102. {
  103. ipToCompare = InetAddress.getByName(ip).getHostAddress();
  104. if (clientStringIP.equals(ipToCompare))
  105. {
  106. result = true;
  107. }
  108. if (Config.DEVELOPER)
  109. {
  110. telnetOutput(3, clientStringIP + " = " + ipToCompare + "(" + ip + ") = " + result);
  111. }
  112. }
  113. }
  114. }
  115. catch (IOException e)
  116. {
  117. if (Config.DEVELOPER)
  118. {
  119. telnetOutput(4, "");
  120. }
  121. telnetOutput(1, "Error: " + e);
  122. }
  123. if (Config.DEVELOPER)
  124. {
  125. telnetOutput(4, "Allow IP: " + result);
  126. }
  127. return result;
  128. }
  129. public GameStatusThread(Socket client, int uptime, String StatusPW) throws IOException
  130. {
  131. setPriority(Thread.MAX_PRIORITY);
  132. _cSocket = client;
  133. _uptime = uptime;
  134. _print = new PrintWriter(_cSocket.getOutputStream());
  135. _read = new BufferedReader(new InputStreamReader(_cSocket.getInputStream()));
  136. if (isValidIP(client))
  137. {
  138. telnetOutput(1, client.getInetAddress().getHostAddress() + " accepted.");
  139. _print.println("Welcome To The L2J Telnet Session.");
  140. _print.println("Please Insert Your Password!");
  141. _print.print("Password: ");
  142. _print.flush();
  143. String tmpLine = _read.readLine();
  144. if (tmpLine == null)
  145. {
  146. _print.println("Error.");
  147. _print.println("Disconnected...");
  148. _print.flush();
  149. _cSocket.close();
  150. }
  151. else
  152. {
  153. if (!tmpLine.equals(StatusPW))
  154. {
  155. _print.println("Incorrect Password!");
  156. _print.println("Disconnected...");
  157. _print.flush();
  158. _cSocket.close();
  159. }
  160. else
  161. {
  162. _print.println("Password Correct!");
  163. _print.println("[L2J Game Server]");
  164. _print.print("");
  165. _print.flush();
  166. start();
  167. }
  168. }
  169. }
  170. else
  171. {
  172. telnetOutput(5, "Connection attempt from " + client.getInetAddress().getHostAddress() + " rejected.");
  173. _cSocket.close();
  174. }
  175. }
  176. @Override
  177. public void run()
  178. {
  179. String _usrCommand = "";
  180. try
  181. {
  182. while ((_usrCommand.compareTo("quit") != 0) && (_usrCommand.compareTo("exit") != 0))
  183. {
  184. _usrCommand = _read.readLine();
  185. if (_usrCommand == null)
  186. {
  187. _cSocket.close();
  188. break;
  189. }
  190. final ITelnetHandler handler = TelnetHandler.getInstance().getHandler(_usrCommand);
  191. if (handler != null)
  192. {
  193. handler.useCommand(_usrCommand, _print, _cSocket, _uptime);
  194. }
  195. else if (_usrCommand.equalsIgnoreCase("quit") || _usrCommand.equalsIgnoreCase("exit") || _usrCommand.isEmpty())
  196. {
  197. /* Do Nothing :p - Just here to save us from the "Command Not Understood" Text */
  198. }
  199. else
  200. {
  201. _print.print("Command: " + _usrCommand + " was not found!");
  202. }
  203. _print.print("");
  204. _print.flush();
  205. }
  206. if (!_cSocket.isClosed())
  207. {
  208. _print.println("Bye Bye!");
  209. _print.flush();
  210. _cSocket.close();
  211. }
  212. telnetOutput(1, "Connection from " + _cSocket.getInetAddress().getHostAddress() + " was closed by client.");
  213. }
  214. catch (IOException e)
  215. {
  216. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  217. }
  218. }
  219. }