Added telnet.filter <full | chat | nonchat>

This commit is contained in:
Steven Lawson 2012-09-29 17:19:45 -04:00
parent e34693f2f7
commit 7a5128fc4b

View file

@ -17,224 +17,251 @@ import org.bukkit.plugin.Plugin;
public class BT_TelnetListener extends Handler implements CommandSender
{
private static final Logger log = Logger.getLogger("Minecraft");
private boolean is_running = false;
private boolean is_authenticated = false;
private boolean already_stopped = false;
private String telnet_username = null;
private Thread listenThread;
private Socket clientSocket;
private BufferedReader instream;
private BufferedWriter outstream;
private BukkitTelnet plugin;
private String client_ip;
private boolean show_full_log = true;
private static final String COMMAND_REGEX = "[^\\x20-\\x7E]";
private static final String LOGIN_REGEX = "[^a-zA-Z0-9\\-\\.\\_]";
private enum FilterMode
{
FULL, NONCHAT_ONLY, CHAT_ONLY
}
private static final Logger log = Logger.getLogger("Minecraft");
private boolean is_running = false;
private boolean is_authenticated = false;
private boolean already_stopped = false;
private String telnet_username = null;
private Thread listenThread;
private Socket clientSocket;
private BufferedReader instream;
private BufferedWriter outstream;
private BukkitTelnet plugin;
private String client_ip;
private FilterMode filter_mode = FilterMode.FULL;
private static final String COMMAND_REGEX = "[^\\x20-\\x7E]";
private static final String LOGIN_REGEX = "[^a-zA-Z0-9\\-\\.\\_]";
public BT_TelnetListener(Socket socket, BukkitTelnet plugin)
{
this.is_running = true;
this.clientSocket = socket;
this.plugin = plugin;
if (clientSocket.getInetAddress() != null)
{
this.client_ip = clientSocket.getInetAddress().getHostAddress();
}
public BT_TelnetListener(Socket socket, BukkitTelnet plugin)
{
this.is_running = true;
this.clientSocket = socket;
this.plugin = plugin;
if (clientSocket.getInetAddress() != null)
{
this.client_ip = clientSocket.getInetAddress().getHostAddress();
}
startListener();
}
startListener();
}
private void startListener()
{
listenThread = new Thread(new Runnable()
{
public void run()
{
init();
}
});
listenThread.start();
}
private void startListener()
{
listenThread = new Thread(new Runnable()
{
public void run()
{
init();
}
});
listenThread.start();
}
private void init()
{
try
{
instream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outstream = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
}
catch (Throwable ex)
{
is_running = false;
return;
}
private void init()
{
try
{
instream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outstream = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
}
catch (Throwable ex)
{
is_running = false;
return;
}
//sendTelnetCommand(WILL, LINEMODE);
//sendTelnetCommand(DO, LINEMODE);
//sendTelnetCommand(WONT, ECHO);
//sendTelnetCommand(DO, ECHO);
//sendTelnetCommand(WILL, LINEMODE);
//sendTelnetCommand(DO, LINEMODE);
//sendTelnetCommand(WONT, ECHO);
//sendTelnetCommand(DO, ECHO);
writeOut("[BukkitTelnet] - Session Started!\r\n");
writeOut("[BukkitTelnet] - Session Started!\r\n");
authenticateLoop();
commandLoop();
shutdown();
}
authenticateLoop();
commandLoop();
shutdown();
}
private void authenticateLoop()
{
int tries = 0;
private void authenticateLoop()
{
int tries = 0;
while (is_running && clientSocket.isConnected() && !is_authenticated)
{
try
{
//Get Username:
writeOut("Username: ");
String username = instream.readLine().replaceAll(LOGIN_REGEX, "").trim();
while (is_running && clientSocket.isConnected() && !is_authenticated)
{
try
{
//Get Username:
writeOut("Username: ");
String username = instream.readLine().replaceAll(LOGIN_REGEX, "").trim();
if (BT_Util.canBypassPassword(client_ip, plugin))
{
writeOut("Skipping password, you are on an authorized IP address.\r\n");
is_authenticated = true;
}
else
{
//sendTelnetCommand(WILL, ECHO);
//sendTelnetCommand(DONT, ECHO);
if (BT_Util.canBypassPassword(client_ip, plugin))
{
writeOut("Skipping password, you are on an authorized IP address.\r\n");
is_authenticated = true;
}
else
{
//sendTelnetCommand(WILL, ECHO);
//sendTelnetCommand(DONT, ECHO);
//Get Password:
writeOut("Password: ");
String password = instream.readLine().replaceAll(LOGIN_REGEX, "").trim();
writeOut("\r\n");
//Get Password:
writeOut("Password: ");
String password = instream.readLine().replaceAll(LOGIN_REGEX, "").trim();
writeOut("\r\n");
//sendTelnetCommand(WONT, ECHO);
//sendTelnetCommand(DO, ECHO);
//sendTelnetCommand(WONT, ECHO);
//sendTelnetCommand(DO, ECHO);
if (password.equals(plugin.password))
{
is_authenticated = true;
}
}
if (password.equals(plugin.password))
{
is_authenticated = true;
}
}
if (is_authenticated)
{
telnet_username = username;
writeOut("Logged In as " + getName() + ".\r\n:");
log.log(Level.INFO, "[" + plugin.getDescription().getName() + "]: " + client_ip + " logged in as \"" + getName() + "\".");
return;
}
else
{
try
{
Thread.sleep(2000);
}
catch (InterruptedException ex)
{
}
writeOut("Invalid Username or Password.\r\n\r\n");
}
if (is_authenticated)
{
telnet_username = username;
writeOut("Logged In as " + getName() + ".\r\n:");
log.log(Level.INFO, "[" + plugin.getDescription().getName() + "]: " + client_ip + " logged in as \"" + getName() + "\".");
return;
}
else
{
try
{
Thread.sleep(2000);
}
catch (InterruptedException ex)
{
}
writeOut("Invalid Username or Password.\r\n\r\n");
}
if (++tries >= 3)
{
writeOut("Too many failed login attempts.\r\n");
return;
}
}
catch (Throwable ex)
{
is_running = false;
telnet_username = null;
is_authenticated = false;
}
}
}
if (++tries >= 3)
{
writeOut("Too many failed login attempts.\r\n");
return;
}
}
catch (Throwable ex)
{
is_running = false;
telnet_username = null;
is_authenticated = false;
}
}
}
private void commandLoop()
{
if (!is_running || !is_authenticated)
{
return;
}
private void commandLoop()
{
if (!is_running || !is_authenticated)
{
return;
}
Logger.getLogger("Minecraft").addHandler(this);
Logger.getLogger("Minecraft").addHandler(this);
while (is_running && clientSocket.isConnected() && is_authenticated)
{
String command = null;
try
{
command = instream.readLine();
}
catch (IOException ex)
{
}
while (is_running && clientSocket.isConnected() && is_authenticated)
{
String command = null;
try
{
command = instream.readLine();
}
catch (IOException ex)
{
}
if (command != null)
{
command = command.replaceAll(COMMAND_REGEX, "").trim();
if (command != null)
{
command = command.replaceAll(COMMAND_REGEX, "").trim();
if (!command.isEmpty())
{
if (command.toLowerCase().startsWith("telnet"))
{
if (command.equalsIgnoreCase("telnet.log"))
{
show_full_log = !show_full_log;
if (show_full_log)
{
writeOut("Showing full console log.\r\n:");
}
else
{
writeOut("Showing chat log only.\r\n:");
}
}
if (command.equalsIgnoreCase("telnet.exit"))
{
shutdown();
}
}
else
{
plugin.getServer().dispatchCommand(this, command);
}
}
else
{
writeOut(":");
}
}
}
}
if (!command.isEmpty())
{
if (command.toLowerCase().startsWith("telnet"))
{
if (command.equalsIgnoreCase("telnet.log")) // for legacy use
{
if (filter_mode == FilterMode.CHAT_ONLY)
{
filter_mode = FilterMode.FULL;
writeOut("Showing full console log.\r\n:");
}
else
{
filter_mode = FilterMode.CHAT_ONLY;
writeOut("Showing chat log only.\r\n:");
}
}
else if (command.toLowerCase().startsWith("telnet.filter"))
{
if (command.equalsIgnoreCase("telnet.filter full"))
{
filter_mode = FilterMode.FULL;
writeOut("Showing full console log.\r\n:");
}
else if (command.equalsIgnoreCase("telnet.filter chat"))
{
filter_mode = FilterMode.CHAT_ONLY;
writeOut("Showing chat log only.\r\n:");
}
else if (command.equalsIgnoreCase("telnet.filter nonchat"))
{
filter_mode = FilterMode.NONCHAT_ONLY;
writeOut("Showing everything but chat.\r\n:");
}
else
{
writeOut("Usage: telnet.filter <full | chat | nonchat>.\r\n:");
}
}
else if (command.equalsIgnoreCase("telnet.exit"))
{
shutdown();
}
}
else
{
plugin.getServer().dispatchCommand(this, command);
}
}
else
{
writeOut(":");
}
}
}
}
private void shutdown()
{
if (already_stopped)
{
return;
}
already_stopped = true;
private void shutdown()
{
if (already_stopped)
{
return;
}
already_stopped = true;
is_running = false;
is_running = false;
log.log(Level.INFO, "[" + plugin.getDescription().getName() + "]: Closing connection: " + client_ip);
Logger.getLogger("Minecraft").removeHandler(this);
log.log(Level.INFO, "[" + plugin.getDescription().getName() + "]: Closing connection: " + client_ip);
Logger.getLogger("Minecraft").removeHandler(this);
if (!clientSocket.isClosed())
{
writeOut("[" + plugin.getDescription().getName() + "]: Closing connection.");
try
{
clientSocket.close();
}
catch (IOException ex)
{
}
}
}
if (!clientSocket.isClosed())
{
writeOut("[" + plugin.getDescription().getName() + "]: Closing connection.");
try
{
clientSocket.close();
}
catch (IOException ex)
{
}
}
}
// public static final int WILL = 251; //Sender wants to do something.
// public static final int WONT = 252; //Sender doesn't want to do something.
@ -248,157 +275,175 @@ public class BT_TelnetListener extends Handler implements CommandSender
// {
// writeOut(("" + ((char) 255) + ((char) command) + ((char) option)));
// }
private void writeOut(String message)
{
if (outstream != null)
{
if (clientSocket.isConnected())
{
try
{
outstream.write(message);
outstream.flush();
}
catch (IOException ex)
{
is_running = false;
}
}
}
}
private void writeOut(String message)
{
if (outstream != null)
{
if (clientSocket.isConnected())
{
try
{
outstream.write(message);
outstream.flush();
}
catch (IOException ex)
{
is_running = false;
}
}
}
}
public boolean isAlive()
{
return is_running;
}
public boolean isAlive()
{
return is_running;
}
public void killClient()
{
shutdown();
}
public void killClient()
{
shutdown();
}
@Override
public void publish(LogRecord record)
{
String message = ChatColor.stripColor(record.getMessage());
if (show_full_log || message.startsWith("<") || message.startsWith("[Server:") || message.startsWith("[CONSOLE]<"))
{
writeOut(message + "\r\n:");
}
}
@Override
public void publish(LogRecord record)
{
String message = ChatColor.stripColor(record.getMessage());
@Override
public void flush()
{
if (clientSocket.isConnected())
{
try
{
outstream.flush();
}
catch (IOException ex)
{
}
}
}
boolean is_chat = message.startsWith("<") || message.startsWith("[Server:") || message.startsWith("[CONSOLE]<");
@Override
public void close() throws SecurityException
{
shutdown();
}
switch (filter_mode)
{
case CHAT_ONLY:
{
if (!is_chat)
{
break;
}
}
case NONCHAT_ONLY:
{
if (is_chat)
{
break;
}
}
default:
{
writeOut(message + "\r\n:");
}
}
}
public void sendMessage(String string)
{
writeOut(ChatColor.stripColor(string) + "\r\n:");
}
@Override
public void flush()
{
if (clientSocket.isConnected())
{
try
{
outstream.flush();
}
catch (IOException ex)
{
}
}
}
public void sendMessage(String[] strings)
{
for (String string : strings)
{
sendMessage(string);
}
}
@Override
public void close() throws SecurityException
{
shutdown();
}
public Server getServer()
{
return plugin.getServer();
}
public void sendMessage(String string)
{
writeOut(ChatColor.stripColor(string) + "\r\n:");
}
public String getName()
{
if (telnet_username != null)
{
return telnet_username;
}
else
{
return plugin.getDescription().getName();
}
}
public void sendMessage(String[] strings)
{
for (String string : strings)
{
sendMessage(string);
}
}
public boolean isPermissionSet(String string)
{
return true;
}
public Server getServer()
{
return plugin.getServer();
}
public boolean isPermissionSet(Permission prmsn)
{
return true;
}
public String getName()
{
if (telnet_username != null)
{
return telnet_username;
}
else
{
return plugin.getDescription().getName();
}
}
public boolean hasPermission(String string)
{
return true;
}
public boolean isPermissionSet(String string)
{
return true;
}
public boolean hasPermission(Permission prmsn)
{
return true;
}
public boolean isPermissionSet(Permission prmsn)
{
return true;
}
public PermissionAttachment addAttachment(Plugin plugin, String string, boolean bln)
{
return null;
}
public boolean hasPermission(String string)
{
return true;
}
public PermissionAttachment addAttachment(Plugin plugin)
{
return null;
}
public boolean hasPermission(Permission prmsn)
{
return true;
}
public PermissionAttachment addAttachment(Plugin plugin, String string, boolean bln, int i)
{
return null;
}
public PermissionAttachment addAttachment(Plugin plugin, String string, boolean bln)
{
return null;
}
public PermissionAttachment addAttachment(Plugin plugin, int i)
{
return null;
}
public PermissionAttachment addAttachment(Plugin plugin)
{
return null;
}
public void removeAttachment(PermissionAttachment pa)
{
}
public PermissionAttachment addAttachment(Plugin plugin, String string, boolean bln, int i)
{
return null;
}
public void recalculatePermissions()
{
}
public PermissionAttachment addAttachment(Plugin plugin, int i)
{
return null;
}
public Set<PermissionAttachmentInfo> getEffectivePermissions()
{
return null;
}
public void removeAttachment(PermissionAttachment pa)
{
}
public boolean isOp()
{
return true;
}
public void recalculatePermissions()
{
}
public void setOp(boolean bln)
{
}
public Set<PermissionAttachmentInfo> getEffectivePermissions()
{
return null;
}
public boolean isOp()
{
return true;
}
public void setOp(boolean bln)
{
}
}