📄 msession.java
字号:
package net.jumperz.app.MGuardian;
import net.jumperz.util.*;
import net.jumperz.net.*;
import net.jumperz.net.exception.*;
import java.io.*;
import java.net.*;
import java.util.*;
import net.jumperz.app.MGuardian.rule.*;
import net.jumperz.app.MGuardian.plugin.*;
public final class MSession
implements MCommand, MObserver1, MSubject2
{
public static final String SEC_LOG_PREFIX = "Alert:";
private static final int LOG_BUFSIZE = 300;
private static final int LOGFILENAME_BUFSIZE = 40;
private static final int ALERT_LOG_LINE_BUFSIZE = 200;
private static final String DECODE_CHARSET = "ISO-8859-1";
private static final String HTTP = "HTTP";
private static final String HTTPS = "HTTPS";
private static String targetHost;
private static int targetPort;
private static boolean replaceHostField;
private static boolean replaceLocationField;
private static MHttpFilter requestFilter;
private static MHttpFilter responseFilter;
private static String targetPeer;
private static Map additionalResponseHeaderMap;
private static Map additionalRequestHeaderMap;
private static List requestRuleList;
private static List responseRuleList;
private static MTimer timer;
private static int requestHeaderTimeOut;
private static int requestBodyTimeOut;
private static int connectTimeOut;
private static List listenerList = new ArrayList();
private Socket clientSideSocket;
private Socket serverSideSocket;
private BufferedInputStream clientInputStream;
private OutputStream clientOutputStream;
private BufferedInputStream serverInputStream;
private OutputStream serverOutputStream;
private boolean keepAlive = true;
private String clientRemoteAddr;
private int clientRemotePort;
private String clientLocalAddr;
private int clientLocalPort;
private boolean timedOut = false;
private boolean isHttpsFlag = false;
private String protocol;
private MSubject2 subject2 = new MSubject2Impl();
//loop
private boolean pass = false;
private int time;
private MResultCache resultCache;
private MHttpRequest request;
private MHttpResponse response;
private String sessionResult;
private Set passRuleSet;
private Exception exception;
private MAbstractRule triggeredRule;
private String logId;
private String pluginClassName;
private String pluginMessage;
//-----------------------------------------------------------------------------------
public MSession( Socket in_socket )
throws Exception
{
clientSideSocket = in_socket;
clientRemoteAddr = clientSideSocket.getInetAddress().getHostAddress();
clientRemotePort = clientSideSocket.getPort();
clientLocalAddr = clientSideSocket.getLocalAddress().getHostAddress();
clientLocalPort = clientSideSocket.getLocalPort();
getClientSideStreams();
isHttpsFlag = ( clientSideSocket instanceof javax.net.ssl.SSLSocket );
if( isHttpsFlag == true )
{
protocol = HTTPS;
}
else
{
protocol = HTTP;
}
}
//-----------------------------------------------------------------------------------
private void setup()
{
timer.register1( this );
MAddressManager.getInstance().add( clientRemoteAddr );
}
//-----------------------------------------------------------------------------------
private void cleanup()
{
timer.removeObserver1( this );
MAddressManager.getInstance().remove( clientRemoteAddr );
}
//--------------------------------------------------------------------------------
public void execute()
{
try
{
setup();
while( keepAlive )
{
resetLoopParameters();
recvRequest();
if( checkRequest() )
{
if( response == null )
{
filterRequest();
modifyRequest();
connect();
sendRequest();
recvResponse();
}
else
{
if( !response.isKeepAliveResponse() )
{
keepAlive = false;
}
}
if( checkResponse() )
{
filterResponse();
modifyResponse();
sendResponse();
}
else
{
keepAlive = false;
}
}
else
{
keepAlive = false;
}
getAccessLog();
clearHttpData();
}
}
catch( SocketException e )
{
String message = e.getMessage();
if( false
|| message.indexOf( "onnection reset" ) > -1
|| message.indexOf( "ocket closed" ) > -1
|| message.indexOf( "Broken pipe" ) > -1
|| message.indexOf( "Connection timed out" ) > -1
|| message.indexOf( "socket write error" ) > -1
|| message.indexOf( "onnection refused" ) > -1
|| message.indexOf( " recv failed" ) > -1
)
{
//ignore :)
}
else
{
e.printStackTrace();
if( timedOut == false )
{
getExceptionLog( e );
}
}
}
catch( MHttpException e ) {} //ignore :)
catch( MHttpIOException e ) {} //ignroe :)
catch( Exception e )
{
e.printStackTrace();
if( timedOut == false )
{
getExceptionLog( e );
}
}
finally
{
clearHttpData();
cleanup();
closeConnection();
}
}
//--------------------------------------------------------------------------------
private void getExceptionLog( Exception e )
{
exception = e;
notify2( "getExceptionLog", this );
}
//-----------------------------------------------------------------------------------
private void clearHttpData()
{
if( request != null )
{
request.deleteTmpFile();
}
if( response != null )
{
response.deleteTmpFile();
}
request = null;
response = null;
}
//-----------------------------------------------------------------------------------
private void resetLoopParameters()
{
time = 0;
pass = false;
resultCache = new MResultCache();
sessionResult = null;
request = null;
response = null;
passRuleSet = new HashSet();
exception = null;
logId = null;
triggeredRule = null;
pluginClassName = null;
pluginMessage = null;
}
//-----------------------------------------------------------------------------------
private void getAccessLog()
{
notify2( "getAccessLog", this );
}
//-----------------------------------------------------------------------------------
private void getAlertLog( MAbstractRule rule )
{
triggeredRule = rule;
notify2( "getAlertLog", this );
}
//--------------------------------------------------------------------------------
private void getPluginAlertLog( MGuardianPlugin plugin, Map pluginResult )
{
pluginClassName = plugin.getClass().getName();
pluginMessage = "";
if( pluginResult.containsKey( "message" ) )
{
pluginMessage = ( String )pluginResult.get( "message" );
}
notify2( "getPluginAlertLog", this );
}
//--------------------------------------------------------------------------------
public String getPluginMessage()
{
return pluginMessage;
}
//--------------------------------------------------------------------------------
public String getPluginClassName()
{
return pluginClassName;
}
//--------------------------------------------------------------------------------
private boolean checkResponse()
throws IOException
{
if( responseRuleList == null
|| pass == true
)
{
return true;
}
logId = null;
boolean logged = false;
int count = responseRuleList.size();
for( int i = 0; i < count; ++i )
{
MAbstractResponseRule rule = ( MAbstractResponseRule )responseRuleList.get( i );
if( passRuleSet.contains( rule.getId() ) )
{
resultCache.cacheResult( rule, false );
continue;
}
if( rule.matches( response, resultCache ) )
{
if( logId == null )
{
logId = generateLogId();
}
String command = rule.getCommand();
String action = rule.getAction();
// log
if( rule.getLogFlag() )
{
if( logged == false )
{
saveRequest( logId );
saveResponse( logId );
logged = true;
}
getAlertLog( rule );
}
// command
if( rule.hasCommand() )
{
execCommand( command, rule, logId );
}
// plugin
List pluginList = rule.getPluginList();
List pluginArgList = rule.getPluginArgList();
if( pluginList != null )
{
int pluginCount = pluginList.size();
for( int j = 0; j < pluginCount; ++j )
{
MGuardianPlugin plugin = ( MGuardianPlugin )pluginList.get( j );
String pluginArg = ( String )pluginArgList.get( j );
Map sessionInfo = new HashMap();
sessionInfo.put( "request" , request );
sessionInfo.put( "response" , response );
sessionInfo.put( "rule" , rule );
sessionInfo.put( "logId" , logId );
sessionInfo.put( "clientSideSocket" , clientSideSocket );
sessionInfo.put( "serverSideSocket" , serverSideSocket );
sessionInfo.put( "protocol" , protocol );
sessionInfo.put( "arg" , pluginArg );
// execute plugin!
Map pluginResult = plugin.execute( sessionInfo );
if( pluginResult != null )
{
if( pluginResult.containsKey( "log" ) )
{
Boolean getLog = ( Boolean )pluginResult.get( "log" );
if( getLog.booleanValue() == true )
{
if( logged == false )
{
saveRequest( logId );
saveResponse( logId );
logged = true;
}
getPluginAlertLog( plugin, pluginResult );
}
}
if( pluginResult.containsKey( "command" ) )
{
String pluginCommand = ( String )pluginResult.get( "command" );
execCommand( pluginCommand, rule, logId );
}
if( pluginResult.containsKey( "block" ) )
{
Boolean doBlock = ( Boolean )pluginResult.get( "block" );
if( doBlock.booleanValue() == true )
{
String pluginMessage = "";
if( pluginResult.containsKey( "message" ) )
{
pluginMessage = ( String )pluginResult.get( "message" );
}
sessionResult = "blocked_by_plugin/" + plugin.getClass().getName() + "/" + pluginMessage;
return false;
}
}
if( pluginResult.containsKey( "pass" ) )
{
Boolean doBlock = ( Boolean )pluginResult.get( "pass" );
if( doBlock.booleanValue() == true )
{
pass = true;
}
}
if( pluginResult.containsKey( "request" ) )
{
request = ( MHttpRequest )pluginResult.get( "request" );
}
if( pluginResult.containsKey( "response" ) )
{
response = ( MHttpResponse )pluginResult.get( "response" );
}
}
}
}
// action
if( action.equalsIgnoreCase( MAbstractRule.ACTION_BLOCK ) )
{
sessionResult = "blocked_by_rule/" + rule.toString();
return false;
}
else if( action.equalsIgnoreCase( MAbstractRule.ACTION_PASS_ALL ) )
{
pass = true;
}
else if( action.equalsIgnoreCase( MAbstractRule.ACTION_PASS_RULE ) )
{
passRuleSet.addAll( rule.getPassRuleList() );
}
//pass?
if( pass == true )
{
return true;
}
}
}
return true;
}
//-----------------------------------------------------------------------------------
private boolean checkRequest()
throws IOException
{
if( requestRuleList == null )
{
return true;
}
logId = null;
boolean logged = false;
int count = requestRuleList.size();
for( int i = 0; i < count; ++i )
{
MAbstractRequestRule rule = ( MAbstractRequestRule )requestRuleList.get( i );
if( passRuleSet.contains( rule.getId() ) )
{
resultCache.cacheResult( rule, false );
continue;
}
if( rule.matches( request , resultCache ) )
{
if( logId == null )
{
logId = generateLogId();
}
String command = rule.getCommand();
String action = rule.getAction();
// log
if( rule.getLogFlag() )
{
if( logged == false )
{
saveRequest( logId );
logged = true;
}
getAlertLog( rule );
}
// command
if( rule.hasCommand() )
{
execCommand( command, rule, logId );
}
// plugin
List pluginList = rule.getPluginList();
List pluginArgList = rule.getPluginArgList();
if( pluginList != null )
{
int pluginCount = pluginList.size();
for( int j = 0; j < pluginCount; ++j )
{
MGuardianPlugin plugin = ( MGuardianPlugin )pluginList.get( j );
String pluginArg = ( String )pluginArgList.get( j );
Map sessionInfo = new HashMap();
sessionInfo.put( "request" , request );
sessionInfo.put( "response" , response );
sessionInfo.put( "rule" , rule );
sessionInfo.put( "logId" , logId );
sessionInfo.put( "clientSideSocket" , clientSideSocket );
sessionInfo.put( "serverSideSocket" , serverSideSocket );
sessionInfo.put( "protocol" , protocol );
sessionInfo.put( "arg" , pluginArg );
// execute plugin!
Map pluginResult = plugin.execute( sessionInfo );
if( pluginResult != null )
{
if( pluginResult.containsKey( "log" ) )
{
Boolean getLog = ( Boolean )pluginResult.get( "log" );
if( getLog.booleanValue() == true )
{
if( logged == false )
{
saveRequest( logId );
logged = true;
}
getPluginAlertLog( plugin, pluginResult );
}
}
if( pluginResult.containsKey( "command" ) )
{
String pluginCommand = ( String )pluginResult.get( "command" );
execCommand( pluginCommand, rule, logId );
}
if( pluginResult.containsKey( "block" ) )
{
Boolean doBlock = ( Boolean )pluginResult.get( "block" );
if( doBlock.booleanValue() == true )
{
String pluginMessage = "";
if( pluginResult.containsKey( "message" ) )
{
pluginMessage = ( String )pluginResult.get( "message" );
}
sessionResult = "blocked_by_plugin/" + plugin.getClass().getName() + "/" + pluginMessage;
return false;
}
}
if( pluginResult.containsKey( "pass" ) )
{
Boolean doBlock = ( Boolean )pluginResult.get( "pass" );
if( doBlock.booleanValue() == true )
{
pass = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -