📄 mconfig.java
字号:
package net.jumperz.app.MGuardian;
import java.io.*;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.spec.InvalidKeySpecException;
import java.util.*;
import java.net.*;
import javax.net.ServerSocketFactory;
import net.jumperz.security.MSecurityUtil;
import net.jumperz.util.*;
import net.jumperz.util.shutdown.*;
import net.jumperz.net.*;
import net.jumperz.app.MGuardian.rule.*;
public final class MConfig
{
private static MConfig instance = new MConfig();
private File configDir;
private Properties control;
private Properties shutdownProperties;
private int threadCount;
private String logFileName;
private int targetPort;
private String targetHost;
private boolean replaceHostField;
//private boolean replaceLocationField;
private String sessionLogDirName;
private int maxConnectionCount;
private int connectTimeOut;
private int requestHeaderTimeOut;
private int requestBodyTimeOut;
private boolean limitRequestHeader;
private boolean limitResponseHeader;
private Map additionalRequestHeaderMap;
private Map additionalResponseHeaderMap;
private Set allowedAddressSet;
private MHttpFilter requestFilter;
private MHttpFilter responseFilter;
private List acceptorList;
private List requestRuleList;
private List responseRuleList;
private String urlDecodingCharset;
private long commandInterval;
private int maxQueueCount;
private boolean enableConsole;
private static final String DEFAULT_THREAD_COUNT = "20";
private static final String DEFAULT_MAX_CONNECTION_COUNT = "35";
private static final String DEFAULT_CONNECT_TIME_OUT = "5";
private static final String DEFAULT_REQUEST_LINE_TIME_OUT = "15";
private static final String DEFAULT_REQUEST_HEADER_TIME_OUT = "25";
private static final String DEFAULT_REQUEST_BODY_TIME_OUT = "3600"; // 1Hour
private static final String DEFAULT_REPLACE_HOST_FIELD = "false";
private static final String DEFAULT_REPLACE_LOCATION_FIELD = "false";
private static final String DEFAULT_ADD_CLIENT_ADDR_FIELD = "true";
private static final String DEFAULT_LIMIT_RESPONSE_HEADER = "false";
private static final String DEFAULT_LIMIT_REQUEST_HEADER = "false";
private static final String DEFAULT_URL_DECODING_CHARSET = "US-ASCII";
private static final String DEFAULT_COMMAND_INTERVAL = "1"; // 1 second
private static final String DEFAULT_MAX_QUEUE_COUNT = "1000";
private static final String DEFAULT_ENABLE_CONSOLE = "false";
private static final String CONTROL = "control";
private static final String ADDITIONAL_REQUEST_HEADER = "additionalRequestHeader";
private static final String ADDITIONAL_RESPONSE_HEADER = "additionalResponseHeader";
private static final String LISTENING_SOCKET = "listeningSocket";
private static final String ALLOWED_ADDRESS = "allowedAddress";
private static final String ALLOWED_REQUEST_HEADER = "allowedRequestHeader";
private static final String ALLOWED_RESPONSE_HEADER = "allowedResponseHeader";
private static final String RULE = "rule";
private static final String SHUTDOWN = "shutdown";
/*
+--guardian/
+-----control
+-----additionalRequestHeader
+-----additionalResponseHeader
+-----allowedAddress
+-----allowedRequestHeader
+-----allowedResponseHeader
+-----listeningSocket
+-----rule
+-----shutdown
*/
//-----------------------------------------------------------------------------------
private MConfig()
{
acceptorList = new ArrayList();
}
//-----------------------------------------------------------------------------------
public static MConfig getInstance()
{
return instance;
}
//-----------------------------------------------------------------------------------
public void load( String configDirName )
throws KeyManagementException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException, InvalidKeySpecException, CertificateException, IOException
{
checkConfigDir( configDirName );
loadControl();
loadAdditionalRequestHeader();
loadAdditionalResponseHeader();
loadAllowedAddress();
loadAllowedRequestHeader();
loadAllowedResponseHeader();
loadListeningSocket();
loadRule();
loadShutdown();
}
//--------------------------------------------------------------------------------
private void loadShutdown()
throws IOException
{
MAbstractShutdown.load( configDir.getCanonicalPath() + "/" + SHUTDOWN );
}
//-----------------------------------------------------------------------------------
private void loadRule()
throws IOException
{
MRuleFactory ruleFactory = new MRuleFactory();
ruleFactory.loadFromFile( configDir.getCanonicalPath() + "/" + RULE );
requestRuleList = ruleFactory.getRequestRuleList();
responseRuleList = ruleFactory.getResponseRuleList();
}
//-----------------------------------------------------------------------------------
public List getAcceptorList()
{
return acceptorList;
}
//-----------------------------------------------------------------------------------
private void checkConfigDir( String configDirName )
throws IOException
{
configDir = new File( configDirName );
if( !configDir.exists() )
{
throw new IOException( configDirName + " not found." );
}
if( !configDir.isDirectory() )
{
throw new IOException( configDirName + " is not a directory." );
}
}
//-----------------------------------------------------------------------------------
private void loadControl()
throws IOException
{
control = new Properties();
MSystemUtil.loadProperties( control, new FileInputStream( configDir.getCanonicalPath() + "/" + CONTROL ) );
targetPort = Integer.parseInt( control.getProperty( "targetPort" ) );
targetHost = control.getProperty( "targetHost" );
if( targetHost == null )
{
throw new IOException( "'targetHost' must be set." );
}
replaceHostField = Boolean.valueOf( control.getProperty( "replaceHostField", DEFAULT_REPLACE_HOST_FIELD ) ).booleanValue();
//replaceLocationField = Boolean.valueOf( controlProperties.getProperty( "replaceLocationField", DEFAULT_REPLACE_LOCATION_FIELD ) ).booleanValue();
logFileName = control.getProperty( "logFileName" );
if( !control.containsKey( "sessionLogDirName" )
&& control.containsKey( "alertLogDirName" )
)
{
// for old version
sessionLogDirName = control.getProperty( "alertLogDirName" );
}
else
{
sessionLogDirName = control.getProperty( "sessionLogDirName" );
}
if( sessionLogDirName == null )
{
throw new IOException( "'sessionLogDirName' must be set." );
}
control.setProperty( "sessionLogDirName", sessionLogDirName );
limitRequestHeader = Boolean.valueOf( control.getProperty( "limitRequestHeader", DEFAULT_LIMIT_REQUEST_HEADER ) ).booleanValue();
limitResponseHeader = Boolean.valueOf( control.getProperty( "limitResponseHeader", DEFAULT_LIMIT_RESPONSE_HEADER ) ).booleanValue();
threadCount = Integer.parseInt( control.getProperty( "threadCount", DEFAULT_THREAD_COUNT ) );
connectTimeOut = Integer.parseInt( control.getProperty( "connectTimeOut", DEFAULT_CONNECT_TIME_OUT ) ) * 1000;
requestHeaderTimeOut = Integer.parseInt( control.getProperty( "requestHeaderTimeOut", DEFAULT_REQUEST_HEADER_TIME_OUT ) );
requestBodyTimeOut = Integer.parseInt( control.getProperty( "requestBodyTimeOut", DEFAULT_REQUEST_BODY_TIME_OUT ) );
maxConnectionCount = Integer.parseInt( control.getProperty( "maxConnectionCount", DEFAULT_MAX_CONNECTION_COUNT ) );
urlDecodingCharset = control.getProperty( "urlDecodingCharset", DEFAULT_URL_DECODING_CHARSET );
commandInterval = Long.parseLong( control.getProperty( "commandInterval", DEFAULT_COMMAND_INTERVAL ) );
maxQueueCount = Integer.parseInt( control.getProperty( "maxQueueCount", DEFAULT_MAX_QUEUE_COUNT ) );
enableConsole = Boolean.valueOf( control.getProperty( "enableConsole", DEFAULT_ENABLE_CONSOLE ) ).booleanValue();
}
//-----------------------------------------------------------------------------------
public String toString()
{
StringBuffer strBuf = new StringBuffer();
strBuf.append( "targetPort=" );strBuf.append( targetPort );strBuf.append( "\n" );
strBuf.append( "targetHost=" );strBuf.append( targetHost );strBuf.append( "\n" );
strBuf.append( "replaceHostField=" );strBuf.append( replaceHostField );strBuf.append( "\n" );
//strBuf.append( "replaceLocationField=" );strBuf.append( replaceLocationField );strBuf.append( "\n" );
strBuf.append( "logFileName=" );strBuf.append( logFileName );strBuf.append( "\n" );
strBuf.append( "sessionLogDirName=" );strBuf.append( sessionLogDirName );strBuf.append( "\n" );
strBuf.append( "limitRequestHeader=" );strBuf.append( limitRequestHeader );strBuf.append( "\n" );
strBuf.append( "limitResponseHeader=" );strBuf.append( limitResponseHeader );strBuf.append( "\n" );
strBuf.append( "threadCount=" );strBuf.append( threadCount );strBuf.append( "\n" );
strBuf.append( "requestHeaderTimeOut=" );strBuf.append( requestHeaderTimeOut );strBuf.append( "\n" );
strBuf.append( "requestBodyTimeOut=" );strBuf.append( requestBodyTimeOut );strBuf.append( "\n" );
strBuf.append( "maxConnectionCount=" );strBuf.append( maxConnectionCount );strBuf.append( "\n" );
strBuf.append( "additionalResponseHeaderMap=" );strBuf.append( additionalResponseHeaderMap );strBuf.append( "\n" );
strBuf.append( "additionalRequestHeaderMap=" );strBuf.append( additionalRequestHeaderMap );strBuf.append( "\n" );
strBuf.append( "requestFilter=" );strBuf.append( requestFilter );strBuf.append( "\n" );
strBuf.append( "responseFilter=" );strBuf.append( responseFilter );strBuf.append( "\n" );
strBuf.append( "allowedAddressList=" );strBuf.append( allowedAddressSet );strBuf.append( "\n" );
strBuf.append( "acceptorList=" );strBuf.append( acceptorList );strBuf.append( "\n" );
strBuf.append( "urlDecodingCharset=" );strBuf.append( urlDecodingCharset );strBuf.append( "\n" );
strBuf.append( "commandInterval=" );strBuf.append( commandInterval );strBuf.append( "\n" );
strBuf.append( "maxQueueCount=" );strBuf.append( maxQueueCount );strBuf.append( "\n" );
return strBuf.toString();
}
//-----------------------------------------------------------------------------------
private void loadListeningSocket()
throws KeyManagementException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException, InvalidKeySpecException, CertificateException, IOException
{
String line = null;
String protocol = null;
String port = null;
String host = null;
String privateKeyFileName = null;
String certificateFileName = null;
String algorithm = null;
BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream( configDir.getCanonicalPath() + "/" + LISTENING_SOCKET ), MCharset.CS_ISO_8859_1 ) );
try
{
for( int lineNumber = 1; ; ++lineNumber )
{
line = reader.readLine();
if( line == null )
{
break;
}
if( !MStringUtil.isComment( line ) )
{
if( line.indexOf( "<listen>" ) == 0 )
{
if( protocol != null
|| port != null
|| host != null
|| privateKeyFileName != null
|| certificateFileName != null
|| algorithm != null
)
{
throwParseError( configDir.getCanonicalPath() + "/" + LISTENING_SOCKET, lineNumber );
}
}
else if( line.indexOf( "</listen>" ) == 0 )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -