From: Marcus <the...@us...> - 2004-03-27 20:25:30
|
Update of /cvsroot/junk/junk/WEB-INF/classes/junk/plugin/scanner In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2846 Modified Files: ScannerPlugin.java Log Message: moved config class inside this class; minor improvements Index: ScannerPlugin.java =================================================================== RCS file: /cvsroot/junk/junk/WEB-INF/classes/junk/plugin/scanner/ScannerPlugin.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** ScannerPlugin.java 25 Mar 2004 15:25:34 -0000 1.9 --- ScannerPlugin.java 27 Mar 2004 20:14:18 -0000 1.10 *************** *** 43,46 **** --- 43,48 ---- import java.net.UnknownHostException; + import java.util.ArrayList; + import java.util.HashMap; import java.util.Iterator; *************** *** 75,79 **** public static final String KEY_SCANNER_CONFIG = "scanner-config"; ! /** the key under which the scanning status is saved in the servlet context */ public static final String KEY_SCANNER_RUNNING = "scanner-running"; --- 77,83 ---- public static final String KEY_SCANNER_CONFIG = "scanner-config"; ! /** the key under which the scanning status ! * is saved in the servlet context ! */ public static final String KEY_SCANNER_RUNNING = "scanner-running"; *************** *** 104,114 **** private boolean isRunning = false; ! /** semaphore */ private boolean isScanning = false; - /** empty standard constructor */ - public ScannerPlugin() { - } - /** destroys the plugin. */ public void destroy() { --- 108,116 ---- private boolean isRunning = false; ! /** semaphore ! * also used to tell the spider whether the scanner scans right now ! */ private boolean isScanning = false; /** destroys the plugin. */ public void destroy() { *************** *** 127,132 **** this.saveMap(this.hosts); ! log.debug("juNK scanner plugin (" + ScannerPlugin.VERSION ! + ") thread terminated"); } --- 129,133 ---- this.saveMap(this.hosts); ! log.info("juNK scanner plugin (" + VERSION + ") thread terminated"); } *************** *** 137,141 **** */ public void init(ActionServlet a, ModuleConfig m) ! throws ServletException { this.actionServlet = a; --- 138,143 ---- */ public void init(ActionServlet a, ModuleConfig m) ! throws ServletException ! { this.actionServlet = a; *************** *** 143,156 **** this.conf = new ScannerConfig(a, m); } catch (IllegalArgumentException e) { ! log.fatal("Loading config FAILED. This is FATAL.", e); return; // if some argument was illegal we are out of here } ! this.actionServlet.getServletContext().setAttribute(ScannerPlugin.KEY_SCANNER_CONFIG, ! this.conf); ! //load the persistent map and save it in the context ! this.actionServlet.getServletContext().setAttribute(ScannerPlugin.KEY_HOSTS_MAP, this.loadMap()); --- 145,158 ---- this.conf = new ScannerConfig(a, m); } catch (IllegalArgumentException e) { ! log.fatal("Loading config FAILED. This is FATAL. " ! + "The scanner will exit NOW.", e); return; // if some argument was illegal we are out of here } ! // this.actionServlet.getServletContext().setAttribute(ScannerPlugin.KEY_SCANNER_CONFIG, ! // this.conf); //load the persistent map and save it in the context ! this.actionServlet.getServletContext().setAttribute(KEY_HOSTS_MAP, this.loadMap()); *************** *** 159,166 **** this.scanThread = new Thread(this); this.scanThread.start(); } ! log.info("juNK scanner plugin (" + ScannerPlugin.VERSION ! + ") thread started"); } --- 161,172 ---- this.scanThread = new Thread(this); this.scanThread.start(); + } else { + log.fatal( + "Scanner thread could not be started. It seems it is already running."); + + return; } ! log.info("juNK scanner plugin (" + VERSION + ") thread started"); } *************** *** 232,236 **** try { //try to connect to the smb port ! sock.connect(new InetSocketAddress(ip, ScannerPlugin.SMB_PORT), this.conf.getTcpTimeout()); --- 238,242 ---- try { //try to connect to the smb port ! sock.connect(new InetSocketAddress(ip, SMB_PORT), this.conf.getTcpTimeout()); *************** *** 335,344 **** this.isScanning = true; //set the semaphore ! this.actionServlet.getServletContext().setAttribute(ScannerPlugin.KEY_SCANNER_RUNNING, new Boolean(this.isScanning)); //get the old hostmap this.hosts = (HostMap) this.actionServlet.getServletContext() ! .getAttribute(ScannerPlugin.KEY_HOSTS_MAP); //get subnet iterator --- 341,350 ---- this.isScanning = true; //set the semaphore ! this.actionServlet.getServletContext().setAttribute(KEY_SCANNER_RUNNING, new Boolean(this.isScanning)); //get the old hostmap this.hosts = (HostMap) this.actionServlet.getServletContext() ! .getAttribute(KEY_HOSTS_MAP); //get subnet iterator *************** *** 364,368 **** no.setOnline(Boolean.FALSE); //set it offline ! this.hosts.put(ip, no); //save it --- 370,374 ---- no.setOnline(Boolean.FALSE); //set it offline ! log.debug(no.toString()); this.hosts.put(ip, no); //save it *************** *** 376,391 **** //calculate scanning time long time = System.currentTimeMillis() - start; ! log.info(i.toString() + " scan completed in " + time + " ms"); } //store the results in the context ! this.actionServlet.getServletContext().setAttribute(ScannerPlugin.KEY_HOSTS_MAP, ! hosts); this.isScanning = false; ! this.actionServlet.getServletContext().setAttribute(ScannerPlugin.KEY_SCANNER_RUNNING, new Boolean(this.isScanning)); return true; } } --- 382,563 ---- //calculate scanning time long time = System.currentTimeMillis() - start; ! log.debug(i.toString() + " scan completed in " + time + " ms"); } //store the results in the context ! this.actionServlet.getServletContext().setAttribute(KEY_HOSTS_MAP, hosts); this.isScanning = false; ! this.actionServlet.getServletContext().setAttribute(KEY_SCANNER_RUNNING, new Boolean(this.isScanning)); return true; } + + /** handles the config of the scanner plugin. + * @author Marcus Proest (theevilflow at users dot sf dot net) + */ + class ScannerConfig { + /** plugin configuration key defining the sleep time of the scanner thread */ + protected static final String KEY_THREAD_SLEEP = "thread-sleep"; + + /** plugin configuration key defining the scanning interval */ + protected static final String KEY_SCAN_INTERVAL = "scan-interval"; + + /** plugin configuration key defining the ip range */ + protected static final String KEY_IP_RANGE = "ip-range"; + + /** plugin configuration key defining the tcp timeout */ + protected static final String KEY_TCP_TO = "tcp-timeout"; + + /** plugin configuration key defining the save map path */ + protected static final String KEY_SAVE_MAP = "map-save-file"; + + /** the ip subnets to scan over */ + private ArrayList ipSubnets = null; + + /** log writer */ + private Log log = LogFactory.getLog(ScannerPlugin.class); + + /** initial ipsubnetsString */ + private String ipSubnetsString = null; + + /** the path to load and save the hostmap to */ + private String savemappath = "data/hostmap"; + + /** the tcp timeout */ + private int tcpTimeout = 100; + + /** the scanning interval */ + private long interval = 300000; + + /** the time the scannerthread sleeps */ + private long sleep = 10000; + + /** Creates a new instance of ScannerConfig. + * protected standard constructor to prevent instantiation outside + * the package. + */ + protected ScannerConfig() { + } + + /** Creates a new instance of ScannerConfig. + * @param m ModuleConfig + * @param a ActionServlet + * @throws IllegalArgumentException + * thrown if reading the configuration failed + */ + protected ScannerConfig(ActionServlet a, ModuleConfig m) + throws IllegalArgumentException + { + HashMap conf = junk.plugin.PluginTools.getPluginConfig(ScannerPlugin.class, + m); + + String path = a.getServletContext().getRealPath("/WEB-INF"); + + this.applyConfig(conf, path); + } + + /** Returns the interval value. + * @return returns the interval value + */ + public long getInterval() { + return this.interval; + } + + /** Returns the path to the saved hostmap. + * @return the path to the saved hostmap + */ + public String getSaveMapPath() { + return this.savemappath; + } + + /** Returns the sleep value. + * @return returns the sleep value + */ + public long getSleep() { + return this.sleep; + } + + /** return the initial ipSubnets String + * @return initial ipsubnets String + */ + public String getSubnets() { + return this.ipSubnetsString; + } + + /** Returns the tcp timeout value. + * @return tcp timeout + */ + public int getTcpTimeout() { + return this.tcpTimeout; + } + + /** Returns an iterator over the configured ip subnets. + * @return an iterator over the configured ip subnets + */ + protected Iterator getIpSubnetIterator() { + return this.ipSubnets.iterator(); + } + + /** Apply the config values. + * @param conf configuration values + * @throws IllegalArgumentException + * thrown if reading the configuration failed + * @param basepath global path to the web-inf directory + */ + private void applyConfig(HashMap conf, String basepath) + throws IllegalArgumentException + { + try { + this.sleep = Long.parseLong((String) conf.get( + ScannerConfig.KEY_THREAD_SLEEP)); + } catch (Exception e) { + log.warn("Loading parameter " + ScannerConfig.KEY_THREAD_SLEEP + + " FAILED, defaulting to " + this.sleep, e); + } + + try { + this.interval = Long.parseLong((String) conf.get( + ScannerConfig.KEY_SCAN_INTERVAL)); + } catch (Exception e) { + log.warn("Loading parameter " + ScannerConfig.KEY_SCAN_INTERVAL + + "FAILED, defaulting to " + this.interval, e); + } + + try { + String subnetConfigs = (String) conf.get(ScannerConfig.KEY_IP_RANGE); + this.ipSubnetsString = subnetConfigs; + + String[] scArray = subnetConfigs.split(","); + this.ipSubnets = new ArrayList(scArray.length); + + for (int x = 0; x < scArray.length; x++) { + this.ipSubnets.add(x, new IpSubnet(scArray[x])); + } + } catch (Exception e) { + //this value cannot be defaulted, so failing ist fatal. + throw new IllegalArgumentException("Loading parameter " + + ScannerConfig.KEY_IP_RANGE + " FAILED. (" + + e.getMessage() + ")"); + } + + try { + this.tcpTimeout = Integer.parseInt((String) conf.get( + ScannerConfig.KEY_TCP_TO)); + } catch (Exception e) { + log.warn("Loading parameter " + ScannerConfig.KEY_TCP_TO + + " FAILED, defaulting to " + this.tcpTimeout, e); + } + + try { + String filename = (String) conf.get(ScannerConfig.KEY_SAVE_MAP); + this.savemappath = basepath + File.separatorChar + ".." + + File.separatorChar + filename; + } catch (Exception e) { + log.warn("Loading parameter " + ScannerConfig.KEY_SAVE_MAP + + " FAILED, defaulting to " + this.savemappath, e); + } + } + } } |