Implement some anti-spam measures. Resolves #12

This commit is contained in:
JeromSar 2016-06-18 17:38:33 +02:00
parent 8a6482c523
commit 3272f871e8
2 changed files with 30 additions and 21 deletions

View file

@ -24,37 +24,34 @@ public final class ClientSession extends Thread
{ {
public static final Pattern NONASCII_FILTER = Pattern.compile("[^\\x20-\\x7E]"); public static final Pattern NONASCII_FILTER = Pattern.compile("[^\\x20-\\x7E]");
public static final Pattern AUTH_INPUT_FILTER = Pattern.compile("[^a-zA-Z0-9]"); public static final Pattern AUTH_INPUT_FILTER = Pattern.compile("[^_a-zA-Z0-9]");
public static final Pattern COMMAND_INPUT_FILTER = Pattern.compile("^[^a-zA-Z0-9/\\?!\\.]+"); public static final Pattern COMMAND_INPUT_FILTER = Pattern.compile("^[^_a-zA-Z0-9/\\?!\\.]+");
// //
private final TelnetServer telnet; private final TelnetServer telnet;
private final Socket clientSocket; private final Socket clientSocket;
private final String clientAddress; private final String clientAddress;
private final SessionCommandSender commandSender; private final SessionCommandSender commandSender;
// //
private FilterMode filterMode; private FilterMode filterMode = FilterMode.NONE;
// //
private BufferedWriter writer; private BufferedWriter writer;
private BufferedReader reader; private BufferedReader reader;
private String username; private String username = "";
private boolean hasTerminated; private volatile boolean terminated = false;
private boolean enhancedMode = false; private volatile boolean authenticated = false;
private volatile boolean enhancedMode = false;
public ClientSession(TelnetServer telnet, Socket clientSocket) public ClientSession(TelnetServer telnet, Socket clientSocket)
{ {
this.telnet = telnet; this.telnet = telnet;
this.clientSocket = clientSocket; this.clientSocket = clientSocket;
this.clientAddress = clientSocket.getInetAddress().getHostAddress(); this.clientAddress = clientSocket.getInetAddress().getHostAddress();
this.username = "";
this.commandSender = new SessionCommandSender(this); this.commandSender = new SessionCommandSender(this);
this.filterMode = FilterMode.FULL;
this.hasTerminated = false;
} }
@Override @Override
public void run() public void run()
{ {
TelnetLogger.info("Client connected: " + clientAddress);
try try
{ {
synchronized (clientSocket) synchronized (clientSocket)
@ -89,6 +86,11 @@ public final class ClientSession extends Thread
syncTerminateSession(); syncTerminateSession();
} }
public boolean syncIsAuthenticated()
{
return authenticated;
}
public boolean syncIsConnected() public boolean syncIsConnected()
{ {
synchronized (clientSocket) synchronized (clientSocket)
@ -99,14 +101,19 @@ public final class ClientSession extends Thread
public synchronized void syncTerminateSession() public synchronized void syncTerminateSession()
{ {
if (hasTerminated) if (terminated)
{ {
return; return;
} }
hasTerminated = true; terminated = true;
TelnetLogger.info("Closing connection: " + clientAddress + (username.isEmpty() ? "" : " (" + username + ")")); if (authenticated)
{
TelnetLogger.info("Closing connection: " + clientAddress + (username.isEmpty() ? "" : " (" + username + ")"));
}
// Stop feeding the client with data
telnet.getPlugin().appender.removeSession(this); telnet.getPlugin().appender.removeSession(this);
synchronized (clientSocket) synchronized (clientSocket)
@ -124,7 +131,6 @@ public final class ClientSession extends Thread
catch (IOException ex) catch (IOException ex)
{ {
} }
} }
} }
@ -210,7 +216,7 @@ public final class ClientSession extends Thread
private boolean authenticate() private boolean authenticate()
{ {
if (hasTerminated) if (terminated)
{ {
return false; return false;
} }
@ -281,6 +287,7 @@ public final class ClientSession extends Thread
{ {
break; break;
} }
if (input.isEmpty()) if (input.isEmpty())
{ {
continue; continue;
@ -288,7 +295,7 @@ public final class ClientSession extends Thread
input = AUTH_INPUT_FILTER.matcher(input).replaceAll("").trim(); input = AUTH_INPUT_FILTER.matcher(input).replaceAll("").trim();
if (input.isEmpty()) if (input.isEmpty() || input.length() > 20)
{ {
writeLine("Invalid username."); writeLine("Invalid username.");
continue; continue;
@ -308,6 +315,7 @@ public final class ClientSession extends Thread
// don't ask for it. // don't ask for it.
if (passAuth) if (passAuth)
{ {
authenticated = true;
return true; return true;
} }
@ -325,7 +333,7 @@ public final class ClientSession extends Thread
} }
catch (IOException ex) catch (IOException ex)
{ {
break; return false;
} }
if (input == null) if (input == null)
@ -341,6 +349,7 @@ public final class ClientSession extends Thread
if (telnet.getConfig().getPassword().equals(input)) if (telnet.getConfig().getPassword().equals(input))
{ {
authenticated = true;
return true; return true;
} }
@ -359,7 +368,7 @@ public final class ClientSession extends Thread
private void mainLoop() private void mainLoop()
{ {
if (hasTerminated) if (terminated)
{ {
return; return;
} }
@ -429,7 +438,7 @@ public final class ClientSession extends Thread
{ {
switch (filterMode) switch (filterMode)
{ {
case FULL: case NONE:
{ {
filterMode = FilterMode.CHAT_ONLY; filterMode = FilterMode.CHAT_ONLY;
writeLine("Showing only chat logs."); writeLine("Showing only chat logs.");
@ -443,7 +452,7 @@ public final class ClientSession extends Thread
} }
case NONCHAT_ONLY: case NONCHAT_ONLY:
{ {
filterMode = FilterMode.FULL; filterMode = FilterMode.NONE;
writeLine("Showing all logs."); writeLine("Showing all logs.");
break; break;
} }

View file

@ -3,7 +3,7 @@ package me.totalfreedom.bukkittelnet.session;
public enum FilterMode public enum FilterMode
{ {
FULL, NONE,
NONCHAT_ONLY, NONCHAT_ONLY,
CHAT_ONLY; CHAT_ONLY;
} }