diff --git a/brouter-server/src/main/java/btools/server/IpAccessMonitor.java b/brouter-server/src/main/java/btools/server/IpAccessMonitor.java new file mode 100644 index 0000000..2aa1c8e --- /dev/null +++ b/brouter-server/src/main/java/btools/server/IpAccessMonitor.java @@ -0,0 +1,42 @@ +package btools.server; + +import java.util.HashMap; +import java.util.Map; + +public class IpAccessMonitor +{ + private static Object sync = new Object(); + private static HashMap ipAccess = new HashMap(); + private static long MAX_IDLE = 900000; // 15 minutes + + public static boolean touchIpAccess( String ip ) + { + long t = System.currentTimeMillis(); + synchronized( sync ) + { + Long lastTime = ipAccess.get( ip ); + if ( lastTime == null || t - lastTime.longValue() > MAX_IDLE ) + { + ipAccess.put( ip, Long.valueOf( t ) ); + cleanup(t); + return true; // new session detected + } + ipAccess.put( ip, Long.valueOf( t ) ); // touch existing session + return false; + } + } + + private static void cleanup( long t ) + { + HashMap newMap = new HashMap(ipAccess.size()); + for( Map.Entry e : ipAccess.entrySet() ) + { + if ( t - e.getValue().longValue() <= MAX_IDLE ) + { + newMap.put( e.getKey(), e.getValue() ); + } + } + ipAccess = newMap; + } + +} diff --git a/brouter-server/src/main/java/btools/server/RouteServer.java b/brouter-server/src/main/java/btools/server/RouteServer.java index dbb3624..ce79ec2 100644 --- a/brouter-server/src/main/java/btools/server/RouteServer.java +++ b/brouter-server/src/main/java/btools/server/RouteServer.java @@ -82,6 +82,7 @@ public class RouteServer extends Thread implements Comparable String getline = null; String agent = null; String encodings = null; + String xff = null; // X-Forwarded-For // more headers until first empty line for(;;) @@ -102,13 +103,18 @@ public class RouteServer extends Thread implements Comparable { getline = line; } - if ( line.startsWith( "User-Agent: " ) ) + line = line.toLowerCase(); + if ( line.startsWith( "user-agent: " ) ) { - agent = line.substring( "User-Agent: ".length() ); + agent = line.substring( "user-agent: ".length() ); } - if ( line.startsWith( "Accept-Encoding: " ) ) + if ( line.startsWith( "accept-encoding: " ) ) { - encodings = line.substring( "Accept-Encoding: ".length() ); + encodings = line.substring( "accept-encoding: ".length() ); + } + if ( line.startsWith( "x-forwarded-for: " ) ) + { + xff = line.substring( "x-forwarded-for: ".length() ); } } @@ -143,8 +149,11 @@ public class RouteServer extends Thread implements Comparable return; } + InetAddress ip = clientSocket.getInetAddress(); - System.out.println( formattedTimestamp() + " ip=" + (ip==null ? "null" : ip.toString() ) + " -> " + getline ); + String sIp = xff == null ? (ip==null ? "null" : ip.toString() ) : xff; + String sessionMode = IpAccessMonitor.touchIpAccess( sIp ) ? " new " : " "; + System.out.println( formattedTimestamp() + sessionMode + " ip=" + sIp + " -> " + getline ); String url = getline.split(" ")[1]; HashMap params = getUrlParams(url);