GameStatusThread.java 5.8 KB

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