mirror of
https://github.com/webbukkit/dynmap.git
synced 2025-01-25 00:51:31 +01:00
Added webchat-interval for anti-spam webmessaging.
This commit is contained in:
parent
6c6d29a402
commit
38e997cf37
@ -72,6 +72,7 @@ web:
|
|||||||
|
|
||||||
showchatwindow: true
|
showchatwindow: true
|
||||||
allowwebchat: true
|
allowwebchat: true
|
||||||
|
webchat-interval: 1000
|
||||||
messagettl: 15000
|
messagettl: 15000
|
||||||
|
|
||||||
showplayerfacesonmap: true
|
showplayerfacesonmap: true
|
||||||
|
@ -144,13 +144,16 @@ public class DynmapPlugin extends JavaPlugin {
|
|||||||
webServer.handlers.put("/up/configuration", new ClientConfigurationHandler((Map<?, ?>) configuration.getProperty("web")));
|
webServer.handlers.put("/up/configuration", new ClientConfigurationHandler((Map<?, ?>) configuration.getProperty("web")));
|
||||||
|
|
||||||
if (configuration.getNode("web").getBoolean("allowwebchat", false)) {
|
if (configuration.getNode("web").getBoolean("allowwebchat", false)) {
|
||||||
SendMessageHandler messageHandler = new SendMessageHandler();
|
SendMessageHandler messageHandler = new SendMessageHandler() {{
|
||||||
messageHandler.onMessageReceived.addListener(new Listener<SendMessageHandler.Message>() {
|
maximumMessageInterval = configuration.getNode("web").getInt("webchat-interval", 1000);
|
||||||
|
onMessageReceived.addListener(new Listener<SendMessageHandler.Message>() {
|
||||||
@Override
|
@Override
|
||||||
public void triggered(Message t) {
|
public void triggered(Message t) {
|
||||||
webChat(t.name, t.message);
|
webChat(t.name, t.message);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}};
|
||||||
|
|
||||||
webServer.handlers.put("/up/sendmessage", messageHandler);
|
webServer.handlers.put("/up/sendmessage", messageHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package org.dynmap.web.handlers;
|
package org.dynmap.web.handlers;
|
||||||
|
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.dynmap.Event;
|
import org.dynmap.Event;
|
||||||
@ -18,6 +21,12 @@ public class SendMessageHandler implements HttpHandler {
|
|||||||
|
|
||||||
private static final JSONParser parser = new JSONParser();
|
private static final JSONParser parser = new JSONParser();
|
||||||
public Event<Message> onMessageReceived = new Event<SendMessageHandler.Message>();
|
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
|
@Override
|
||||||
public void handle(String path, HttpRequest request, HttpResponse response) throws Exception {
|
public void handle(String path, HttpRequest request, HttpResponse response) throws Exception {
|
||||||
if (!request.method.equals(HttpMethod.Post)) {
|
if (!request.method.equals(HttpMethod.Post)) {
|
||||||
@ -29,18 +38,53 @@ public class SendMessageHandler implements HttpHandler {
|
|||||||
InputStreamReader reader = new InputStreamReader(request.body);
|
InputStreamReader reader = new InputStreamReader(request.body);
|
||||||
|
|
||||||
JSONObject o = (JSONObject)parser.parse(reader);
|
JSONObject o = (JSONObject)parser.parse(reader);
|
||||||
Message message = new Message();
|
final Message message = new Message();
|
||||||
message.name = String.valueOf(o.get("name"));
|
message.name = String.valueOf(o.get("name"));
|
||||||
message.message = String.valueOf(o.get("message"));
|
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);
|
onMessageReceived.trigger(message);
|
||||||
|
|
||||||
response.fields.put(HttpField.ContentLength, "0");
|
response.fields.put(HttpField.ContentLength, "0");
|
||||||
response.status = HttpStatus.OK;
|
response.status = HttpStatus.OK;
|
||||||
response.getBody();
|
response.getBody();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Message {
|
public static class Message {
|
||||||
public String name;
|
public String name;
|
||||||
public String message;
|
public String message;
|
||||||
}
|
}
|
||||||
|
public static class WebUser {
|
||||||
|
public long nextMessageTime;
|
||||||
|
public String name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user