mirror of
https://github.com/webbukkit/dynmap.git
synced 2025-01-24 08:31:31 +01:00
Added webchat-interval for anti-spam webmessaging.
This commit is contained in:
parent
6c6d29a402
commit
38e997cf37
@ -71,7 +71,8 @@ web:
|
||||
focuschatballoons: false
|
||||
|
||||
showchatwindow: true
|
||||
allowwebchat: true
|
||||
allowwebchat: true
|
||||
webchat-interval: 1000
|
||||
messagettl: 15000
|
||||
|
||||
showplayerfacesonmap: true
|
||||
|
@ -144,13 +144,16 @@ public class DynmapPlugin extends JavaPlugin {
|
||||
webServer.handlers.put("/up/configuration", new ClientConfigurationHandler((Map<?, ?>) configuration.getProperty("web")));
|
||||
|
||||
if (configuration.getNode("web").getBoolean("allowwebchat", false)) {
|
||||
SendMessageHandler messageHandler = new SendMessageHandler();
|
||||
messageHandler.onMessageReceived.addListener(new Listener<SendMessageHandler.Message>() {
|
||||
@Override
|
||||
public void triggered(Message t) {
|
||||
webChat(t.name, t.message);
|
||||
}
|
||||
});
|
||||
SendMessageHandler messageHandler = new SendMessageHandler() {{
|
||||
maximumMessageInterval = configuration.getNode("web").getInt("webchat-interval", 1000);
|
||||
onMessageReceived.addListener(new Listener<SendMessageHandler.Message>() {
|
||||
@Override
|
||||
public void triggered(Message t) {
|
||||
webChat(t.name, t.message);
|
||||
}
|
||||
});
|
||||
}};
|
||||
|
||||
webServer.handlers.put("/up/sendmessage", messageHandler);
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,9 @@
|
||||
package org.dynmap.web.handlers;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.dynmap.Event;
|
||||
@ -18,6 +21,12 @@ public class SendMessageHandler implements HttpHandler {
|
||||
|
||||
private static final JSONParser parser = new JSONParser();
|
||||
public Event<Message> onMessageReceived = new Event<SendMessageHandler.Message>();
|
||||
|
||||
public int maximumMessageInterval = 1000;
|
||||
private HashMap<String, WebUser> disallowedUsers = new HashMap<String, WebUser>();
|
||||
private LinkedList<WebUser> disallowedUserQueue = new LinkedList<WebUser>();
|
||||
private Object disallowedUsersLock = new Object();
|
||||
|
||||
@Override
|
||||
public void handle(String path, HttpRequest request, HttpResponse response) throws Exception {
|
||||
if (!request.method.equals(HttpMethod.Post)) {
|
||||
@ -29,9 +38,39 @@ public class SendMessageHandler implements HttpHandler {
|
||||
InputStreamReader reader = new InputStreamReader(request.body);
|
||||
|
||||
JSONObject o = (JSONObject)parser.parse(reader);
|
||||
Message message = new Message();
|
||||
final Message message = new Message();
|
||||
message.name = String.valueOf(o.get("name"));
|
||||
message.message = String.valueOf(o.get("message"));
|
||||
|
||||
final long now = System.currentTimeMillis();
|
||||
|
||||
synchronized(disallowedUsersLock) {
|
||||
// Allow users that user that are now allowed to send messages.
|
||||
while (!disallowedUserQueue.isEmpty()) {
|
||||
WebUser wu = disallowedUserQueue.getFirst();
|
||||
if (now >= wu.nextMessageTime) {
|
||||
disallowedUserQueue.remove();
|
||||
disallowedUsers.remove(wu.name);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
WebUser user = disallowedUsers.get(message.name);
|
||||
if (user == null) {
|
||||
user = new WebUser() {{
|
||||
name = message.name;
|
||||
nextMessageTime = now+maximumMessageInterval;
|
||||
}};
|
||||
disallowedUsers.put(user.name, user);
|
||||
disallowedUserQueue.add(user);
|
||||
} else {
|
||||
response.fields.put(HttpField.ContentLength, "0");
|
||||
response.status = HttpStatus.Forbidden;
|
||||
response.getBody();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
onMessageReceived.trigger(message);
|
||||
|
||||
@ -39,8 +78,13 @@ public class SendMessageHandler implements HttpHandler {
|
||||
response.status = HttpStatus.OK;
|
||||
response.getBody();
|
||||
}
|
||||
|
||||
public static class Message {
|
||||
public String name;
|
||||
public String message;
|
||||
}
|
||||
public static class WebUser {
|
||||
public long nextMessageTime;
|
||||
public String name;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user