session/ip info
This commit is contained in:
parent
8206a1ae84
commit
31e761e731
2 changed files with 26 additions and 9 deletions
|
@ -8,6 +8,8 @@ public class IpAccessMonitor
|
||||||
private static Object sync = new Object();
|
private static Object sync = new Object();
|
||||||
private static HashMap<String,Long> ipAccess = new HashMap<String,Long>();
|
private static HashMap<String,Long> ipAccess = new HashMap<String,Long>();
|
||||||
private static long MAX_IDLE = 900000; // 15 minutes
|
private static long MAX_IDLE = 900000; // 15 minutes
|
||||||
|
private static long CLEANUP_INTERVAL = 10000; // 10 seconds
|
||||||
|
private static long lastCleanup;
|
||||||
|
|
||||||
public static boolean touchIpAccess( String ip )
|
public static boolean touchIpAccess( String ip )
|
||||||
{
|
{
|
||||||
|
@ -15,14 +17,22 @@ public class IpAccessMonitor
|
||||||
synchronized( sync )
|
synchronized( sync )
|
||||||
{
|
{
|
||||||
Long lastTime = ipAccess.get( ip );
|
Long lastTime = ipAccess.get( ip );
|
||||||
if ( lastTime == null || t - lastTime.longValue() > MAX_IDLE )
|
|
||||||
{
|
|
||||||
ipAccess.put( ip, Long.valueOf( t ) );
|
ipAccess.put( ip, Long.valueOf( t ) );
|
||||||
cleanup(t);
|
return lastTime == null || t - lastTime.longValue() > MAX_IDLE;
|
||||||
return true; // new session detected
|
|
||||||
}
|
}
|
||||||
ipAccess.put( ip, Long.valueOf( t ) ); // touch existing session
|
}
|
||||||
return false;
|
|
||||||
|
public static int getSessionCount()
|
||||||
|
{
|
||||||
|
long t = System.currentTimeMillis();
|
||||||
|
synchronized( sync )
|
||||||
|
{
|
||||||
|
if ( t - lastCleanup > CLEANUP_INTERVAL )
|
||||||
|
{
|
||||||
|
cleanup( t );
|
||||||
|
lastCleanup = t;
|
||||||
|
}
|
||||||
|
return ipAccess.size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -152,8 +152,15 @@ public class RouteServer extends Thread implements Comparable<RouteServer>
|
||||||
|
|
||||||
InetAddress ip = clientSocket.getInetAddress();
|
InetAddress ip = clientSocket.getInetAddress();
|
||||||
String sIp = xff == null ? (ip==null ? "null" : ip.toString() ) : xff;
|
String sIp = xff == null ? (ip==null ? "null" : ip.toString() ) : xff;
|
||||||
String sessionMode = IpAccessMonitor.touchIpAccess( sIp ) ? " new " : " ";
|
boolean newSession = IpAccessMonitor.touchIpAccess( sIp );
|
||||||
System.out.println( formattedTimestamp() + sessionMode + " ip=" + sIp + " -> " + getline );
|
String sessionInfo = " new";
|
||||||
|
if ( !newSession )
|
||||||
|
{
|
||||||
|
int sessionCount = IpAccessMonitor.getSessionCount();
|
||||||
|
sessionInfo = " " + Math.min( sessionCount, 999 );
|
||||||
|
sessionInfo = sessionInfo.substring( sessionInfo.length() - 4 );
|
||||||
|
}
|
||||||
|
System.out.println( formattedTimestamp() + sessionInfo + " ip=" + sIp + " -> " + getline );
|
||||||
|
|
||||||
String url = getline.split(" ")[1];
|
String url = getline.split(" ")[1];
|
||||||
HashMap<String,String> params = getUrlParams(url);
|
HashMap<String,String> params = getUrlParams(url);
|
||||||
|
|
Loading…
Reference in a new issue