From e8a6bf46cf552e2301dcf90dd870ca624bc45dda Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Sun, 12 Jun 2011 19:35:53 -0700 Subject: [PATCH] Add support for hidewebchatip option, to replace webchat IP with alias --- configuration.txt | 2 ++ .../dynmap/InternalClientUpdateComponent.java | 2 ++ .../dynmap/JsonFileClientUpdateComponent.java | 16 ++++++++++++++++ .../dynmap/web/handlers/SendMessageHandler.java | 16 +++++++++++++++- 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/configuration.txt b/configuration.txt index 44c82bf8..3159aef8 100644 --- a/configuration.txt +++ b/configuration.txt @@ -8,11 +8,13 @@ components: sendposition: true allowwebchat: true webchat-interval: 5 + hidewebchatip: false #- class: org.dynmap.JsonFileClientUpdateComponent # writeinterval: 1 # sendhealth: true # sendposition: true # allowwebchat: false + # hidewebchatip: false - class: org.dynmap.SimpleWebChatComponent allowchat: true diff --git a/src/main/java/org/dynmap/InternalClientUpdateComponent.java b/src/main/java/org/dynmap/InternalClientUpdateComponent.java index 4afc855c..6d1adecb 100644 --- a/src/main/java/org/dynmap/InternalClientUpdateComponent.java +++ b/src/main/java/org/dynmap/InternalClientUpdateComponent.java @@ -12,6 +12,7 @@ public class InternalClientUpdateComponent extends ClientUpdateComponent { public InternalClientUpdateComponent(DynmapPlugin plugin, final ConfigurationNode configuration) { super(plugin, configuration); final Boolean allowwebchat = configuration.getBoolean("allowwebchat", false); + final Boolean hidewebchatip = configuration.getBoolean("hidewebchatip", false); final float webchatInterval = configuration.getFloat("webchat-interval", 1); final String spammessage = plugin.configuration.getString("spammessage", "You may only chat once every %interval% seconds."); @@ -29,6 +30,7 @@ public class InternalClientUpdateComponent extends ClientUpdateComponent { SendMessageHandler messageHandler = new SendMessageHandler() {{ maximumMessageInterval = (int)(webchatInterval * 1000); spamMessage = "\""+spammessage+"\""; + hideip = hidewebchatip; onMessageReceived.addListener(new Listener() { @Override public void triggered(Message t) { diff --git a/src/main/java/org/dynmap/JsonFileClientUpdateComponent.java b/src/main/java/org/dynmap/JsonFileClientUpdateComponent.java index 6a9bb24b..0ac2af25 100644 --- a/src/main/java/org/dynmap/JsonFileClientUpdateComponent.java +++ b/src/main/java/org/dynmap/JsonFileClientUpdateComponent.java @@ -8,6 +8,7 @@ import java.io.InputStreamReader; import java.io.FileInputStream; import java.io.Reader; import java.io.IOException; +import java.util.HashMap; import java.util.Iterator; import java.util.Timer; import java.util.TimerTask; @@ -30,11 +31,17 @@ public class JsonFileClientUpdateComponent extends ClientUpdateComponent { protected long currentTimestamp = 0; protected long lastTimestamp = 0; protected JSONParser parser = new JSONParser(); + private Boolean hidewebchatip; + + private HashMap useralias = new HashMap(); + private int aliasindex = 1; + private Charset cs_utf8 = Charset.forName("UTF-8"); public JsonFileClientUpdateComponent(final DynmapPlugin plugin, final ConfigurationNode configuration) { super(plugin, configuration); final boolean allowwebchat = configuration.getBoolean("allowwebchat", false); jsonInterval = (long)(configuration.getFloat("writeinterval", 1) * 1000); + hidewebchatip = configuration.getBoolean("hidewebchatip", false); task = new TimerTask() { @Override public void run() { @@ -151,6 +158,15 @@ public class JsonFileClientUpdateComponent extends ClientUpdateComponent { if(ts.equals("null")) ts = "0"; if (Long.parseLong(ts) >= (lastTimestamp)) { String name = String.valueOf(o.get("name")); + if(hidewebchatip) { + String n = useralias.get(name); + if(n == null) { /* Make ID */ + n = String.format("web-%03d", aliasindex); + aliasindex++; + useralias.put(name, n); + } + name = n; + } String message = String.valueOf(o.get("message")); webChat(name, message); } diff --git a/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java b/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java index e2945df2..bfb2ca4a 100644 --- a/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java +++ b/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java @@ -24,11 +24,14 @@ public class SendMessageHandler implements HttpHandler { public Event onMessageReceived = new Event(); private Charset cs_utf8 = Charset.forName("UTF-8"); public int maximumMessageInterval = 1000; + public boolean hideip = false; public String spamMessage = "\"You may only chat once every %interval% seconds.\""; private HashMap disallowedUsers = new HashMap(); private LinkedList disallowedUserQueue = new LinkedList(); private Object disallowedUsersLock = new Object(); - + private HashMap useralias = new HashMap(); + private int aliasindex = 1; + @Override public void handle(String path, HttpRequest request, HttpResponse response) throws Exception { if (!request.method.equals(HttpMethod.Post)) { @@ -49,6 +52,17 @@ public class SendMessageHandler implements HttpHandler { message.name = String.valueOf(o.get("name")); else message.name = request.rmtaddr.getAddress().getHostAddress(); + if(hideip) { /* If hiding IP, find or assign alias */ + synchronized(disallowedUsersLock) { + String n = useralias.get(message.name); + if(n == null) { /* Make ID */ + n = String.format("web-%03d", aliasindex); + aliasindex++; + useralias.put(message.name, n); + } + message.name = n; + } + } message.message = String.valueOf(o.get("message")); final long now = System.currentTimeMillis();