GameStatusThread.java 5.7 KB

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