LoginStatusThread.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.loginserver.GameServerTable;
  29. import com.l2jserver.loginserver.L2LoginServer;
  30. import com.l2jserver.loginserver.LoginController;
  31. public class LoginStatusThread extends Thread
  32. {
  33. private static final Logger _log = Logger.getLogger(LoginStatusThread.class.getName());
  34. private Socket _cSocket;
  35. private PrintWriter _print;
  36. private BufferedReader _read;
  37. private boolean _redirectLogger;
  38. private void telnetOutput(int type, String text)
  39. {
  40. if (type == 1)
  41. System.out.println("TELNET | " + text);
  42. else if (type == 2)
  43. System.out.print("TELNET | " + text);
  44. else if (type == 3)
  45. System.out.print(text);
  46. else if (type == 4)
  47. System.out.println(text);
  48. else
  49. System.out.println("TELNET | " + text);
  50. }
  51. private boolean isValidIP(Socket client)
  52. {
  53. boolean result = false;
  54. InetAddress ClientIP = client.getInetAddress();
  55. // convert IP to String, and compare with list
  56. String clientStringIP = ClientIP.getHostAddress();
  57. telnetOutput(1, "Connection from: " + clientStringIP);
  58. // read and loop thru list of IPs, compare with newIP
  59. if (Config.DEVELOPER)
  60. telnetOutput(2, "");
  61. final File file = new File(Config.TELNET_FILE);
  62. try (InputStream telnetIS = new FileInputStream(file))
  63. {
  64. Properties telnetSettings = new Properties();
  65. telnetSettings.load(telnetIS);
  66. String HostList = telnetSettings.getProperty("ListOfHosts", "127.0.0.1,localhost,::1");
  67. if (Config.DEVELOPER)
  68. telnetOutput(3, "Comparing ip to list...");
  69. // compare
  70. String ipToCompare = null;
  71. for (String ip : HostList.split(","))
  72. {
  73. if (!result)
  74. {
  75. ipToCompare = InetAddress.getByName(ip).getHostAddress();
  76. if (clientStringIP.equals(ipToCompare))
  77. result = true;
  78. if (Config.DEVELOPER)
  79. telnetOutput(3, clientStringIP + " = " + ipToCompare + "(" + ip + ") = " + result);
  80. }
  81. }
  82. }
  83. catch (IOException e)
  84. {
  85. if (Config.DEVELOPER)
  86. telnetOutput(4, "");
  87. telnetOutput(1, "Error: " + e);
  88. }
  89. if (Config.DEVELOPER)
  90. telnetOutput(4, "Allow IP: " + result);
  91. return result;
  92. }
  93. public LoginStatusThread(Socket client, int uptime, String StatusPW) throws IOException
  94. {
  95. _cSocket = client;
  96. _print = new PrintWriter(_cSocket.getOutputStream());
  97. _read = new BufferedReader(new InputStreamReader(_cSocket.getInputStream()));
  98. if (isValidIP(client))
  99. {
  100. telnetOutput(1, client.getInetAddress().getHostAddress() + " accepted.");
  101. _print.println("Welcome To The L2J Telnet Session.");
  102. _print.println("Please Insert Your Password!");
  103. _print.print("Password: ");
  104. _print.flush();
  105. String tmpLine = _read.readLine();
  106. if (tmpLine == null)
  107. {
  108. _print.println("Error.");
  109. _print.println("Disconnected...");
  110. _print.flush();
  111. _cSocket.close();
  112. }
  113. else
  114. {
  115. if (tmpLine.compareTo(StatusPW) != 0)
  116. {
  117. _print.println("Incorrect Password!");
  118. _print.println("Disconnected...");
  119. _print.flush();
  120. _cSocket.close();
  121. }
  122. else
  123. {
  124. _print.println("Password Correct!");
  125. _print.println("[L2J Login Server]");
  126. _print.print("");
  127. _print.flush();
  128. start();
  129. }
  130. }
  131. }
  132. else
  133. {
  134. telnetOutput(5, "Connection attempt from " + client.getInetAddress().getHostAddress() + " rejected.");
  135. _cSocket.close();
  136. }
  137. }
  138. @Override
  139. public void run()
  140. {
  141. String _usrCommand = "";
  142. try
  143. {
  144. while (_usrCommand.compareTo("quit") != 0 && _usrCommand.compareTo("exit") != 0)
  145. {
  146. _usrCommand = _read.readLine();
  147. if (_usrCommand == null)
  148. {
  149. _cSocket.close();
  150. break;
  151. }
  152. if (_usrCommand.equals("help"))
  153. {
  154. _print.println("The following is a list of all available commands: ");
  155. _print.println("help - shows this help.");
  156. _print.println("status - displays basic server statistics.");
  157. _print.println("unblock <ip> - removes <ip> from banlist.");
  158. _print.println("shutdown - shuts down server.");
  159. _print.println("restart - restarts the server.");
  160. _print.println("RedirectLogger - Telnet will give you some info about server in real time.");
  161. _print.println("quit - closes telnet session.");
  162. _print.println("");
  163. }
  164. else if (_usrCommand.equals("status"))
  165. {
  166. //TODO enhance the output
  167. _print.println("Registered Server Count: " + GameServerTable.getInstance().getRegisteredGameServers().size());
  168. }
  169. else if (_usrCommand.startsWith("unblock"))
  170. {
  171. try
  172. {
  173. _usrCommand = _usrCommand.substring(8);
  174. if (LoginController.getInstance().removeBanForAddress(_usrCommand))
  175. {
  176. _log.warning("IP removed via TELNET by host: " + _cSocket.getInetAddress().getHostAddress());
  177. _print.println("The IP " + _usrCommand + " has been removed from the hack protection list!");
  178. }
  179. else
  180. {
  181. _print.println("IP not found in hack protection list...");
  182. }
  183. }
  184. catch (StringIndexOutOfBoundsException e)
  185. {
  186. _print.println("Please Enter the IP to Unblock!");
  187. }
  188. }
  189. else if (_usrCommand.startsWith("shutdown"))
  190. {
  191. L2LoginServer.getInstance().shutdown(false);
  192. _print.println("Bye Bye!");
  193. _print.flush();
  194. _cSocket.close();
  195. }
  196. else if (_usrCommand.startsWith("restart"))
  197. {
  198. L2LoginServer.getInstance().shutdown(true);
  199. _print.println("Bye Bye!");
  200. _print.flush();
  201. _cSocket.close();
  202. }
  203. else if (_usrCommand.equals("RedirectLogger"))
  204. {
  205. _redirectLogger = true;
  206. }
  207. else if (_usrCommand.equals("quit"))
  208. { /* Do Nothing :p - Just here to save us from the "Command Not Understood" Text */
  209. }
  210. else if (_usrCommand.length() == 0)
  211. { /* Do Nothing Again - Same reason as the quit part */
  212. }
  213. else
  214. {
  215. _print.println("Invalid Command");
  216. }
  217. _print.print("");
  218. _print.flush();
  219. }
  220. if (!_cSocket.isClosed())
  221. {
  222. _print.println("Bye Bye!");
  223. _print.flush();
  224. _cSocket.close();
  225. }
  226. telnetOutput(1, "Connection from " + _cSocket.getInetAddress().getHostAddress() + " was closed by client.");
  227. }
  228. catch (IOException e)
  229. {
  230. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  231. }
  232. }
  233. public void printToTelnet(String msg)
  234. {
  235. synchronized (_print)
  236. {
  237. _print.println(msg);
  238. _print.flush();
  239. }
  240. }
  241. /**
  242. * @return Returns the redirectLogger.
  243. */
  244. public boolean isRedirectLogger()
  245. {
  246. return _redirectLogger;
  247. }
  248. }