LoginStatusThread.java 7.6 KB

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