/* * This file is part of the L2J Mobius project. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package com.l2jmobius.status; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; import java.lang.management.ThreadMXBean; import java.net.InetAddress; import java.net.Socket; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Map; import java.util.Map.Entry; import java.util.NoSuchElementException; import java.util.Properties; import java.util.StringTokenizer; import com.l2jmobius.Config; import com.l2jmobius.L2DatabaseFactory; import com.l2jmobius.gameserver.Announcements; import com.l2jmobius.gameserver.GameTimeController; import com.l2jmobius.gameserver.LoginServerThread; import com.l2jmobius.gameserver.Shutdown; import com.l2jmobius.gameserver.ThreadPoolManager; import com.l2jmobius.gameserver.cache.HtmCache; import com.l2jmobius.gameserver.datatables.GmListTable; import com.l2jmobius.gameserver.datatables.ItemTable; import com.l2jmobius.gameserver.datatables.NpcTable; import com.l2jmobius.gameserver.datatables.SkillTable; import com.l2jmobius.gameserver.datatables.TeleportLocationTable; import com.l2jmobius.gameserver.instancemanager.Manager; import com.l2jmobius.gameserver.instancemanager.QuestManager; import com.l2jmobius.gameserver.model.GMAudit; import com.l2jmobius.gameserver.model.L2Character; import com.l2jmobius.gameserver.model.L2ItemInstance; import com.l2jmobius.gameserver.model.L2Multisell; import com.l2jmobius.gameserver.model.L2Object; import com.l2jmobius.gameserver.model.L2Summon; import com.l2jmobius.gameserver.model.L2World; import com.l2jmobius.gameserver.model.TradeList; import com.l2jmobius.gameserver.model.TradeList.TradeItem; import com.l2jmobius.gameserver.model.actor.instance.L2DoorInstance; import com.l2jmobius.gameserver.model.actor.instance.L2MonsterInstance; import com.l2jmobius.gameserver.model.actor.instance.L2NpcInstance; import com.l2jmobius.gameserver.model.actor.instance.L2PcInstance; import com.l2jmobius.gameserver.network.clientpackets.Say2; import com.l2jmobius.gameserver.network.serverpackets.CreatureSay; import com.l2jmobius.gameserver.network.serverpackets.InventoryUpdate; import com.l2jmobius.gameserver.network.serverpackets.SystemMessage; import com.l2jmobius.gameserver.taskmanager.DecayTaskManager; import com.l2jmobius.gameserver.util.DynamicExtension; import javolution.util.FastComparator; import javolution.util.FastTable; public class GameStatusThread extends Thread { // private static final Logger _log = Logger.getLogger(AdminTeleport.class.getName()); private final Socket _csocket; private final PrintWriter _print; private final BufferedReader _read; private final int _uptime; private void telnetOutput(int type, String text) { if (Config.DEVELOPER) { if (type == 1) { System.out.println("TELNET | " + text); } else if (type == 2) { System.out.print("TELNET | " + text); } else if (type == 3) { System.out.print(text); } else if (type == 4) { System.out.println(text); } else { System.out.println("TELNET | " + text); } } else { // only print output if the message is rejected if (type == 5) { System.out.println("TELNET | " + text); } } } private boolean isValidIP(Socket client) { boolean result = false; final InetAddress ClientIP = client.getInetAddress(); // convert IP to String, and compare with list final String clientStringIP = ClientIP.getHostAddress(); telnetOutput(1, "Connection from: " + clientStringIP); // read and loop thru list of IPs, compare with newIP if (Config.DEVELOPER) { telnetOutput(2, ""); } final Properties telnetSettings = new Properties(); try (InputStream telnetIS = new FileInputStream(new File(Config.TELNET_FILE))) { telnetSettings.load(telnetIS); final String HostList = telnetSettings.getProperty("ListOfHosts", "127.0.0.1,localhost,::1"); if (Config.DEVELOPER) { telnetOutput(3, "Comparing ip to list..."); } // compare String ipToCompare = null; for (final String ip : HostList.split(",")) { if (!result) { ipToCompare = InetAddress.getByName(ip).getHostAddress(); if (clientStringIP.equals(ipToCompare)) { result = true; } if (Config.DEVELOPER) { telnetOutput(3, clientStringIP + " = " + ipToCompare + "(" + ip + ") = " + result); } } } } catch (final IOException e) { if (Config.DEVELOPER) { telnetOutput(4, ""); } telnetOutput(1, "Error: " + e); } if (Config.DEVELOPER) { telnetOutput(4, "Allow IP: " + result); } return result; } public GameStatusThread(Socket client, int uptime, String StatusPW) throws IOException { setPriority(Thread.MAX_PRIORITY); _csocket = client; _uptime = uptime; _print = new PrintWriter(_csocket.getOutputStream()); _read = new BufferedReader(new InputStreamReader(_csocket.getInputStream())); if (isValidIP(client)) { telnetOutput(1, client.getInetAddress().getHostAddress() + " accepted."); _print.println("Welcome To The L2JMobius Telnet Session."); _print.println("Please Insert Your Password!"); _print.print("Password: "); _print.flush(); final String tmpLine = _read.readLine(); if (tmpLine == null) { _print.println("Error."); _print.println("Disconnected..."); _print.flush(); _csocket.close(); } else { if (!tmpLine.equals(StatusPW)) { _print.println("Incorrect Password!"); _print.println("Disconnected..."); _print.flush(); _csocket.close(); } else { _print.println("Password Correct!"); _print.println("[L2JMobius GameServer]"); _print.print(""); _print.flush(); start(); } } } else { telnetOutput(5, "Connection attempt from " + client.getInetAddress().getHostAddress() + " rejected."); _csocket.close(); } } @Override public void run() { String _usrCommand = ""; try { while ((_usrCommand.compareTo("quit") != 0) && (_usrCommand.compareTo("exit") != 0)) { _usrCommand = _read.readLine(); if (_usrCommand == null) { _csocket.close(); break; } if (_usrCommand.equals("help")) { _print.println("The following is a list of all available commands: "); _print.println("help - shows this help."); _print.println("status - displays basic server statistics."); _print.println("performance - shows server performance statistics."); _print.println("forcegc - forced garbage collection."); _print.println("purge - removes finished threads from thread pools."); _print.println("announce - announces in game."); _print.println("msg - Sends a whisper to char with ."); _print.println("gmchat - Sends a message to all GMs with ."); _print.println("gmlist - lists all gms online."); _print.println("kick - kick player from server."); _print.println("shutdown