📄 jasen.java
字号:
/*
* @(#)Jasen.java 30/10/2004
*
* Copyright (c) 2004, 2005 jASEN.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
*
* 3. The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* 4. Any modification or additions to the software must be contributed back
* to the project.
*
* 5. Any investigation or reverse engineering of source code or binary to
* enable emails to bypass the filters, and hence inflict spam and or viruses
* onto users who use or do not use jASEN could subject the perpetrator to
* criminal and or civil liability.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JASEN.ORG,
* OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package org.jasen.core.engine;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
import org.jasen.config.DefaultJasenConfigurationLoader;
import org.jasen.config.JasenConfiguration;
import org.jasen.config.JasenDigester;
import org.jasen.config.JasenPluginConfiguration;
import org.jasen.core.JasenMessageWrapper;
import org.jasen.core.PluginContainer;
import org.jasen.core.StandardScanResult;
import org.jasen.core.parsers.SpamHTMLParser;
import org.jasen.error.ErrorHandlerBroker;
import org.jasen.error.JasenException;
import org.jasen.error.JasenParseException;
import org.jasen.error.ParseErrorType;
import org.jasen.error.SystemErrorHandler;
import org.jasen.interfaces.DNSResolver;
import org.jasen.interfaces.InetAddressResolver;
import org.jasen.interfaces.JasenConfigurationLoader;
import org.jasen.interfaces.JasenErrorHandler;
import org.jasen.interfaces.JasenMessage;
import org.jasen.interfaces.JasenPlugin;
import org.jasen.interfaces.JasenScanResult;
import org.jasen.interfaces.JasenTestResult;
import org.jasen.interfaces.MimeMessageParser;
import org.jasen.interfaces.MimeMessageTokenizer;
import org.jasen.interfaces.ParserData;
import org.jasen.interfaces.ProbabilityCalculator;
import org.jasen.interfaces.ReceivedHeaderParser;
import org.jasen.update.JasenAutoUpdateConfiguration;
import org.jasen.update.JasenAutoUpdateManager;
import org.jasen.update.JasenAutoUpdater;
import org.jasen.util.FileUtils;
import org.xml.sax.SAXException;
/**
* <P>
* The core scanning class of the jASEN framework.
* </P>
* <P>
* Jasen scans a MimeMessage using the internal logic and any registered plugins and returns a scan result object indicating the results of the scan
* </P>
* <P>
* NOTE: You should not call this class directly. Use the JasenScanner singleton as it provides thread safety during updates
* </P>
* @see org.jasen.JasenScanner
* @author Jason Polites
*/
public class Jasen
{
static final Logger logger = Logger.getLogger(Jasen.class);
public static final float NO_THRESHOLD = -1.0f;
private Vector plugins;
private ProbabilityCalculator calculator;
private MimeMessageParser mimeParser;
private Class headerParserClass;
private MimeMessageTokenizer tokenizer;
private JasenErrorHandler errorHandler;
private JasenConfiguration config;
private JasenConfigurationLoader configurationLoader;
private InetAddressResolver inetAddressResolver;
private DNSResolver dnsResolver;
private Class scanResultClass;
private float boundary = 0.0f;
private ClassLoader contextClassLoader;
/**
* The index into the test results array for the probability
*/
public static final int RESULT_INDEX_PROBABILITY = 0;
/**
* The index into the test results array for the plugin execution time
*/
public static final int RESULT_INDEX_TIME = 1;
/**
* The index into the test results array for the plugin name
*/
public static final int RESULT_INDEX_NAME = 2;
/**
* The index into the test results array for the plugin display name
*/
public static final int RESULT_INDEX_DISPLAY = 3;
/**
*
*/
public Jasen() {
super ();
}
/**
* Initialises the engine with the default configuration
* @throws JasenException
*
*/
public void init() throws JasenException {
if(configurationLoader == null) {
configurationLoader = new DefaultJasenConfigurationLoader();
}
init(configurationLoader);
}
/**
* Initialises the engine with the configuration file specified
* @param config The absolute path to the configuration file
* @throws JasenException
* @deprecated Use init(JasenConfigurationLoader loader)
*/
public void init(String config) throws JasenException {
InputStream in = null;
try
{
in = new FileInputStream(config);
init(in);
}
catch (FileNotFoundException e)
{
throw new JasenException(e);
}
finally
{
if(in != null) {
try
{
in.close();
}
catch (IOException ignore)
{
// Ignore this error, just print for debugging
ErrorHandlerBroker.getInstance().getErrorHandler().handleException(ignore);
}
}
}
}
/**
* Initialises the engine with the configuration document passed as a stream
* @param in The input stream from which the configuration may be read
* @throws JasenException
* @deprecated Use init(JasenConfigurationLoader loader)
*/
public void init(InputStream in) throws JasenException {
initInternal(in);
}
/**
* Initialises the engine with the configuration loader provided
* @param loader
* @throws JasenException
*/
public void init(JasenConfigurationLoader loader) throws JasenException {
try {
configurationLoader = loader;
initInternal(configurationLoader.openConfigurationStream());
}
finally {
loader.closeConfigurationStream();
}
}
/**
* Initialises the engine with the configuration document passed as a stream
* @param in The input stream from which the configuration may be read
* @throws JasenException
*/
private synchronized void initInternal(InputStream in) throws JasenException {
logger.debug("Initialising jASEN engine");
try
{
// Create a digester
JasenDigester digester = new JasenDigester();
config = (JasenConfiguration)digester.parse(in);
// Install the engine config
JasenEngineConfiguration.getInstance().setConfidence(Float.parseFloat(config.getConfidence()));
JasenEngineConfiguration.getInstance().setGuess(Float.parseFloat(config.getGuess()));
JasenEngineConfiguration.getInstance().setEsf(Float.parseFloat(config.getEsf()));
JasenEngineConfiguration.getInstance().setFtt(Integer.parseInt(config.getFtt()));
// Set the default html parser config
if(config.getParserConfiguration() != null) {
JasenEngineConfiguration.getInstance().setParserContrastThreshold(Float.parseFloat(config.getParserConfiguration().getContrastThreshold()));
JasenEngineConfiguration.getInstance().setParserMicroElementSize(Integer.parseInt(config.getParserConfiguration().getMicroElementSize()));
JasenEngineConfiguration.getInstance().setParserMicroFontSize(Integer.parseInt(config.getParserConfiguration().getMicroFontSize()));
}
// Look for an existing classloader
ClassLoader loader = getContextClassLoader();
if(loader == null) {
// Look for updates already in the update classpath
String path = JasenAutoUpdater.UPDATE_LIB_PATH;
File libPath = new File(path);
if(libPath.exists() && libPath.isDirectory()) {
// Get any jars
URL[] jars = FileUtils.listJars(new File(path));
if(jars != null && jars.length > 0) {
loader = new URLClassLoader(jars, getClass().getClassLoader());
}
else
{
loader = getClass().getClassLoader();
}
}
else
{
loader = getClass().getClassLoader();
}
}
// Create the mimeParser
if(config.getMimeParser() != null)
mimeParser = (MimeMessageParser)Class.forName(config.getMimeParser(), true, loader).newInstance();
// Create the header mimeParser
if(config.getHeaderParser() != null)
headerParserClass = Class.forName(config.getHeaderParser(), true, loader);
// Create the tokenizer
if(config.getTokenizer() != null) {
tokenizer = (MimeMessageTokenizer)Class.forName(config.getTokenizer(), true, loader).newInstance();
if(config.getTokenLimit() != null) {
tokenizer.setTokenLimit(Integer.parseInt(config.getTokenLimit()));
}
}
// Create the calculator
if(config.getCalculator() != null)
calculator = (ProbabilityCalculator)Class.forName(config.getCalculator(), true, loader).newInstance();
if(config.getResult() != null)
scanResultClass = Class.forName(config.getResult(), true, loader);
// Probability boundary
if(config.getBoundary() != null)
boundary = Float.parseFloat(config.getBoundary());
// Resolvers
if(config.getDnsResolver() != null)
dnsResolver = (DNSResolver)Class.forName(config.getDnsResolver(), true, loader).newInstance();
if(config.getInetResolver() != null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -