Merge pull request #208 from mikeprimm/master

Add better HTTP proxy support for webchat, add hidewebchatip option to alias web chat IP addresses
This commit is contained in:
mikeprimm 2011-06-12 19:40:54 -07:00
commit d1f94fbf8e
4 changed files with 43 additions and 3 deletions

View File

@ -8,11 +8,13 @@ components:
sendposition: true sendposition: true
allowwebchat: true allowwebchat: true
webchat-interval: 5 webchat-interval: 5
hidewebchatip: false
#- class: org.dynmap.JsonFileClientUpdateComponent #- class: org.dynmap.JsonFileClientUpdateComponent
# writeinterval: 1 # writeinterval: 1
# sendhealth: true # sendhealth: true
# sendposition: true # sendposition: true
# allowwebchat: false # allowwebchat: false
# hidewebchatip: false
- class: org.dynmap.SimpleWebChatComponent - class: org.dynmap.SimpleWebChatComponent
allowchat: true allowchat: true

View File

@ -12,6 +12,7 @@ public class InternalClientUpdateComponent extends ClientUpdateComponent {
public InternalClientUpdateComponent(DynmapPlugin plugin, final ConfigurationNode configuration) { public InternalClientUpdateComponent(DynmapPlugin plugin, final ConfigurationNode configuration) {
super(plugin, configuration); super(plugin, configuration);
final Boolean allowwebchat = configuration.getBoolean("allowwebchat", false); final Boolean allowwebchat = configuration.getBoolean("allowwebchat", false);
final Boolean hidewebchatip = configuration.getBoolean("hidewebchatip", false);
final float webchatInterval = configuration.getFloat("webchat-interval", 1); final float webchatInterval = configuration.getFloat("webchat-interval", 1);
final String spammessage = plugin.configuration.getString("spammessage", "You may only chat once every %interval% seconds."); 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() {{ SendMessageHandler messageHandler = new SendMessageHandler() {{
maximumMessageInterval = (int)(webchatInterval * 1000); maximumMessageInterval = (int)(webchatInterval * 1000);
spamMessage = "\""+spammessage+"\""; spamMessage = "\""+spammessage+"\"";
hideip = hidewebchatip;
onMessageReceived.addListener(new Listener<SendMessageHandler.Message>() { onMessageReceived.addListener(new Listener<SendMessageHandler.Message>() {
@Override @Override
public void triggered(Message t) { public void triggered(Message t) {

View File

@ -8,6 +8,7 @@ import java.io.InputStreamReader;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.Reader; import java.io.Reader;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
@ -30,11 +31,17 @@ public class JsonFileClientUpdateComponent extends ClientUpdateComponent {
protected long currentTimestamp = 0; protected long currentTimestamp = 0;
protected long lastTimestamp = 0; protected long lastTimestamp = 0;
protected JSONParser parser = new JSONParser(); 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"); private Charset cs_utf8 = Charset.forName("UTF-8");
public JsonFileClientUpdateComponent(final DynmapPlugin plugin, final ConfigurationNode configuration) { public JsonFileClientUpdateComponent(final DynmapPlugin plugin, final ConfigurationNode configuration) {
super(plugin, configuration); super(plugin, configuration);
final boolean allowwebchat = configuration.getBoolean("allowwebchat", false); final boolean allowwebchat = configuration.getBoolean("allowwebchat", false);
jsonInterval = (long)(configuration.getFloat("writeinterval", 1) * 1000); jsonInterval = (long)(configuration.getFloat("writeinterval", 1) * 1000);
hidewebchatip = configuration.getBoolean("hidewebchatip", false);
task = new TimerTask() { task = new TimerTask() {
@Override @Override
public void run() { public void run() {
@ -151,6 +158,15 @@ public class JsonFileClientUpdateComponent extends ClientUpdateComponent {
if(ts.equals("null")) ts = "0"; if(ts.equals("null")) ts = "0";
if (Long.parseLong(ts) >= (lastTimestamp)) { if (Long.parseLong(ts) >= (lastTimestamp)) {
String name = String.valueOf(o.get("name")); 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")); String message = String.valueOf(o.get("message"));
webChat(name, message); webChat(name, message);
} }

View File

@ -24,10 +24,13 @@ public class SendMessageHandler implements HttpHandler {
public Event<Message> onMessageReceived = new Event<SendMessageHandler.Message>(); public Event<Message> onMessageReceived = new Event<SendMessageHandler.Message>();
private Charset cs_utf8 = Charset.forName("UTF-8"); private Charset cs_utf8 = Charset.forName("UTF-8");
public int maximumMessageInterval = 1000; public int maximumMessageInterval = 1000;
public boolean hideip = false;
public String spamMessage = "\"You may only chat once every %interval% seconds.\""; public String spamMessage = "\"You may only chat once every %interval% seconds.\"";
private HashMap<String, WebUser> disallowedUsers = new HashMap<String, WebUser>(); private HashMap<String, WebUser> disallowedUsers = new HashMap<String, WebUser>();
private LinkedList<WebUser> disallowedUserQueue = new LinkedList<WebUser>(); private LinkedList<WebUser> disallowedUserQueue = new LinkedList<WebUser>();
private Object disallowedUsersLock = new Object(); private Object disallowedUsersLock = new Object();
private HashMap<String,String> useralias = new HashMap<String,String>();
private int aliasindex = 1;
@Override @Override
public void handle(String path, HttpRequest request, HttpResponse response) throws Exception { public void handle(String path, HttpRequest request, HttpResponse response) throws Exception {
@ -41,8 +44,25 @@ public class SendMessageHandler implements HttpHandler {
JSONObject o = (JSONObject)parser.parse(reader); JSONObject o = (JSONObject)parser.parse(reader);
final Message message = new Message(); final Message message = new Message();
//message.name = String.valueOf(o.get("name")); //Can't trust client....we don't need to on internal web server /* If proxied client address, get original */
if(request.fields.containsKey("X-Forwarded-For"))
message.name = request.fields.get("X-Forwarded-For");
/* If from loopback, we're probably getting from proxy - need to trust client */
else if(request.rmtaddr.getAddress().isLoopbackAddress())
message.name = String.valueOf(o.get("name"));
else
message.name = request.rmtaddr.getAddress().getHostAddress(); 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")); message.message = String.valueOf(o.get("message"));
final long now = System.currentTimeMillis(); final long now = System.currentTimeMillis();