📄 msession.java
字号:
}
}
if( pluginResult.containsKey( "request" ) )
{
request = ( MHttpRequest )pluginResult.get( "request" );
}
if( pluginResult.containsKey( "response" ) )
{
response = ( MHttpResponse )pluginResult.get( "response" );
}
if( pluginResult.containsKey( "clientSideSocket" ) )
{
clientSideSocket = ( Socket )pluginResult.get( "clientSideSocket" );
getClientSideStreams();
}
if( pluginResult.containsKey( "serverSideSocket" ) )
{
closeServerConnection();
serverSideSocket = ( Socket )pluginResult.get( "serverSideSocket" );
getServerSideStreams();
}
}
}
}
// 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() );
}
if( pass == true )
{
return true;
}
}
}
return true;
}
//--------------------------------------------------------------------------------
private void getServerSideStreams()
throws IOException
{
serverInputStream = new BufferedInputStream( serverSideSocket.getInputStream() );
serverOutputStream = serverSideSocket.getOutputStream();
}
//--------------------------------------------------------------------------------
private void getClientSideStreams()
throws IOException
{
clientInputStream = new BufferedInputStream( clientSideSocket.getInputStream() );
clientOutputStream = clientSideSocket.getOutputStream();
}
//-----------------------------------------------------------------------------------
private void saveResponse( String logId )
throws IOException
{
MHttpDataLogger.log( response, clientRemoteAddr, logId + "_" + MHttpDataLogger.RES_LOG_SUFFIX );
}
//-----------------------------------------------------------------------------------
private void saveRequest( String logId )
throws IOException
{
MHttpDataLogger.log( request, clientRemoteAddr, logId + "_" + MHttpDataLogger.REQ_LOG_SUFFIX );
}
//-----------------------------------------------------------------------------------
private String generateLogId()
{
StringBuffer strBuf = new StringBuffer( LOGFILENAME_BUFSIZE );
strBuf.append( System.currentTimeMillis() );
strBuf.append( "_" );
strBuf.append( clientRemotePort );
return strBuf.toString();
}
//--------------------------------------------------------------------------------
public String getLogId()
{
return logId;
}
//-----------------------------------------------------------------------------------
private void execCommand( String command, MAbstractRule rule, String logId )
//throws IOException
{
command = command.replaceAll( "%id", rule.getId() );
command = command.replaceAll( "%revision", Integer.toString( rule.getRevision() ) );
command = command.replaceAll( "%name", rule.getName() );
command = command.replaceAll( "%addr", clientRemoteAddr );
command = command.replaceAll( "%port", String.valueOf( clientRemotePort ) );
command = command.replaceAll( "%logId", logId );
command = command.replaceAll( "%logDir", MHttpDataLogger.getSessionLogDirName() );
command = command.replaceAll( "%logDirName", MHttpDataLogger.getSessionLogDirName() );
// %protocol
if( isHttps() )
{
command = command.replaceAll( "%protocol", HTTPS );
}
else
{
command = command.replaceAll( "%protocol", HTTP );
}
//Runtime.getRuntime().exec( command );
MCommandExecuter.getInstance().addCommand( command );
}
//-----------------------------------------------------------------------------------
public static void setAdditionalResponseHeaderMap( Map in_additionalReponseHeaderMap )
{
additionalResponseHeaderMap = in_additionalReponseHeaderMap;
}
//-----------------------------------------------------------------------------------
public static void setAdditionalRequestHeaderMap( Map in_additionalRequestHeaderMap )
{
additionalRequestHeaderMap = in_additionalRequestHeaderMap;
}
//-----------------------------------------------------------------------------------
public static void setRequestFilter( MHttpFilter in_requestFilter )
{
requestFilter = in_requestFilter;
}
//-----------------------------------------------------------------------------------
public static void setResponseFilter( MHttpFilter in_responseFilter )
{
responseFilter = in_responseFilter;
}
//-----------------------------------------------------------------------------------
private void filterRequest()
{
if( requestFilter != null )
{
requestFilter.doFilter( request );
}
}
//-----------------------------------------------------------------------------------
private void filterResponse()
{
if( responseFilter != null )
{
responseFilter.doFilter( response );
}
}
//-----------------------------------------------------------------------------------
private void connect()
throws IOException
{
if( serverSideSocket == null )
{
doConnect();
getServerSideStreams();
}
}
//-----------------------------------------------------------------------------------
private void doConnect()
throws IOException
{
SocketAddress sockAddr = new InetSocketAddress( targetHost, targetPort );
serverSideSocket = new Socket();
serverSideSocket.connect( sockAddr, connectTimeOut );
}
//-----------------------------------------------------------------------------------
private void recvRequest()
throws Exception
{
request = new MHttpRequest();
request.init( clientInputStream );
request.setAddr( clientRemoteAddr );
request.setPort( clientRemotePort );
keepAlive = request.isKeepAliveRequest();
}
//-----------------------------------------------------------------------------------
private void modifyRequest()
{
if( replaceHostField == true )
{
request.setHeaderValue( "Host", MSession.targetPeer );
}
// add additionalHeader
if( additionalRequestHeaderMap != null )
{
Iterator i = additionalRequestHeaderMap.keySet().iterator();
while( i.hasNext() )
{
String key = ( String )i.next();
String value = ( String )additionalRequestHeaderMap.get( key );
value = replaceSpecialString( value );
request.setHeaderValue( key, value );
}
}
}
//-----------------------------------------------------------------------------------
private String replaceSpecialString( String in )
{
String tmpStr = in;
// %addr
tmpStr = tmpStr.replaceAll( "%addr", clientRemoteAddr );
// %protocol
if( isHttps() )
{
tmpStr = tmpStr.replaceAll( "%protocol", HTTPS );
}
else
{
tmpStr = tmpStr.replaceAll( "%protocol", HTTP );
}
return tmpStr;
}
//-----------------------------------------------------------------------------------
private boolean isHttps()
{
return isHttpsFlag;
}
//-----------------------------------------------------------------------------------
private void sendRequest()
throws IOException
{
serverOutputStream.write( request.getHeader() );
if( request.hasBody() )
{
MStreamUtil.connectStream( request.getBodyInputStream(), serverOutputStream );
}
}
//-----------------------------------------------------------------------------------
private void recvResponse()
throws Exception
{
if( request.getMethodType() == MHttpRequest.HEAD )
{
response = new MHttpResponse( serverInputStream, true );
}
else
{
response = new MHttpResponse( serverInputStream );
}
// 100 continue
if( response.getStatusCode() == 100 )
{
response = new MHttpResponse( serverInputStream );
}
if( response.headerExists( "Connection" )
&& response.getHeaderValue( "Connection" ).equalsIgnoreCase( "close" )
)
{
keepAlive = false;
}
}
//-----------------------------------------------------------------------------------
private void sendResponse()
throws IOException
{
clientOutputStream.write( response.getHeader() );
if( response.hasBody() )
{
MStreamUtil.connectStream( response.getBodyInputStream(), clientOutputStream );
}
}
//-----------------------------------------------------------------------------------
private void modifyResponse()
{
/*
// check Location field
if( replaceLocationField )
{
if( response.headerExists( "Location" ) )
{
String location = response.getHeaderValue( "Location" );
if( location.indexOf( targetPeer ) == 7 )
{
if( isHttps() )
{
location = location.replaceFirst( "^http", "https" );
response.setHeaderValue( "Location", location );
}
}
}
}
*/
// add additionalHeader
if( additionalResponseHeaderMap != null )
{
Iterator i = additionalResponseHeaderMap.keySet().iterator();
while( i.hasNext() )
{
String key = ( String )i.next();
String value = ( String )additionalResponseHeaderMap.get( key );
response.setHeaderValue( key, value );
}
}
}
//-----------------------------------------------------------------------------------
public final void breakCommand()
{
closeConnection();
}
//-----------------------------------------------------------------------------------
public final void closeConnection()
{
closeClientConnection();
closeServerConnection();
}
//-----------------------------------------------------------------------------------
public final void closeClientConnection()
{
try
{
if( !clientSideSocket.isClosed() )
{
clientSideSocket.close();
}
}
catch( IOException e )
{
e.printStackTrace();
}
}
//-----------------------------------------------------------------------------------
public final void closeServerConnection()
{
if( serverSideSocket != null )
{
MSocketCloser.getInstance().addSocket( serverSideSocket );
}
}
//-----------------------------------------------------------------------------------
public static void setResponseRuleList( List in_responseRuleList )
{
MSession.responseRuleList = in_responseRuleList;
}
//-----------------------------------------------------------------------------------
public static void setRequestRuleList( List in_requestRuleList )
{
MSession.requestRuleList = in_requestRuleList;
}
//-----------------------------------------------------------------------------------
public void update()
{
time++;
if( request != null )
{
int state = request.getState();
if( state == MHttpRequest.RECV_REQUEST_HEADER )
{
if( time > MSession.requestHeaderTimeOut )
{
doTimeOut();
}
}
else if( state == MHttpRequest.RECV_REQUEST_BODY )
{
if( time > MSession.requestBodyTimeOut )
{
doTimeOut();
}
}
}
}
//-----------------------------------------------------------------------------------
private void doTimeOut()
{
if( timedOut == true )
{
return;
}
else
{
timedOut = true;
getTimeoutLog();
closeConnection();
}
}
//-----------------------------------------------------------------------------------
private void getTimeoutLog()
{
notify2( "getTimeoutLog", this );
}
//--------------------------------------------------------------------------------
public String getHostField()
{
String hostField = "-";
if( request != null )
{
if( request.headerExists( "Host" ) )
{
return request.getHeaderValue( "Host" );
}
}
return hostField;
}
//-----------------------------------------------------------------------------------
public static void setTimer( MTimer in_timer )
{
timer = in_timer;
}
//--------------------------------------------------------------------------------
public static void setConnectTimeOut( int i )
{
connectTimeOut = i;
}
//-----------------------------------------------------------------------------------
public static void setRequestBodyTimeOut( int in_requestBodyTimeOut )
{
requestBodyTimeOut = in_requestBodyTimeOut;
}
//-----------------------------------------------------------------------------------
public static void setRequestHeaderTimeOut( int in_requestHeaderTimeOut )
{
requestHeaderTimeOut = in_requestHeaderTimeOut;
}
//-----------------------------------------------------------------------------------
public static void setTargetPeer( String in_targetHost, int in_targetPort )
{
targetHost = in_targetHost;
targetPort = in_targetPort;
if( targetPort == 80 )
{
targetPeer = targetHost;
}
else
{
targetPeer = targetHost + ":" + String.valueOf( targetPort );
}
}
//-----------------------------------------------------------------------------------
public static void setReplaceHostField(boolean b)
{
replaceHostField = b;
}
//-----------------------------------------------------------------------------------
public static void setReplaceLocationField(boolean b)
{
replaceLocationField = b;
}
//-----------------------------------------------------------------------------------
public String getClientLocalAddr()
{
return clientLocalAddr;
}
//-----------------------------------------------------------------------------------
public int getClientLocalPort()
{
return clientLocalPort;
}
//-----------------------------------------------------------------------------------
public String getClientRemoteAddr()
{
return clientRemoteAddr;
}
//-----------------------------------------------------------------------------------
public int getClientRemotePort()
{
return clientRemotePort;
}
//-----------------------------------------------------------------------------------
public String getProtocol()
{
return protocol;
}
//-----------------------------------------------------------------------------------
public MHttpRequest getRequest()
{
return request;
}
//-----------------------------------------------------------------------------------
public MHttpResponse getResponse()
{
return response;
}
//-----------------------------------------------------------------------------------
public String getSessionResult()
{
return sessionResult;
}
//-----------------------------------------------------------------------------------
public void notify2( Object event, Object source )
{
subject2.notify2( event, source );
}
//----------------------------------------------------------------
public void register2( MObserver2 observer )
{
subject2.register2( observer );
}
//----------------------------------------------------------------
public void removeObservers2()
{
subject2.removeObservers2();
}
//----------------------------------------------------------------
public void removeObserver2( MObserver2 observer )
{
subject2.removeObserver2( observer );
}
//-----------------------------------------------------------------------------------
public Exception getException()
{
return exception;
}
//-----------------------------------------------------------------------------------
public Socket getClientSideSocket()
{
return clientSideSocket;
}
//-----------------------------------------------------------------------------------
public MAbstractRule getTriggeredRule()
{
return triggeredRule;
}
//-----------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -