Add support for hidewebchatip option, to replace webchat IP with alias

This commit is contained in:
Mike Primm 2011-06-12 19:35:53 -07:00
parent 40ee132277
commit e8a6bf46cf
4 changed files with 35 additions and 1 deletions

View File

@ -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

View File

@ -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<SendMessageHandler.Message>() {
@Override
public void triggered(Message t) {

View File

@ -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<String,String> useralias = new HashMap<String,String>();
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);
}

View File

@ -24,10 +24,13 @@ public class SendMessageHandler implements HttpHandler {
public Event<Message> onMessageReceived = new Event<SendMessageHandler.Message>();
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<String, WebUser> disallowedUsers = new HashMap<String, WebUser>();
private LinkedList<WebUser> disallowedUserQueue = new LinkedList<WebUser>();
private Object disallowedUsersLock = new Object();
private HashMap<String,String> useralias = new HashMap<String,String>();
private int aliasindex = 1;
@Override
public void handle(String path, HttpRequest request, HttpResponse response) throws Exception {
@ -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();