Ejemplo n.º 1
0
        /// <summary>
        /// Unloads a module with the specified id.
        /// </summary>
        /// <param name='id'>
        /// The identifier of the module to unload.
        /// </param>
        public virtual void Unload(Guid id)
        {
            lock (ScannerList)
            {
                try
                {
                    Debugger.Status("Attempt to remove module: " + id.ToString());

                    //Remove the scanner items and invoke garbage collection.
                    if (ScannerList.ContainsKey(id))
                    {
                        ScannerList.Remove(id);
                        ScheduledList.Remove(id);
                    }
                    else if (HandlerList.ContainsKey(id))
                    {
                        HandlerList.Remove(id);
                    }
                    else
                    {
                        throw new Exception("Module not found.");
                    }

                    GC.Collect();
                    Debugger.Success();
                }
                catch (Exception e)
                {
                    Debugger.Failure(e);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads the scanner modules stored in a specified path.
        /// </summary>
        /// <param name='path'>
        /// The path that should be searched for scanners.
        /// </param>
        public virtual void Load(string path)
        {
            var handlersToLoad = new List <Type>();
            var scannersToLoad = new List <Type>();

            try
            {
                Debugger.Status("Searching for loadable modules...");

                // Determine if the directory actually exists before attempting to load the directory.
                if (!Directory.Exists(path))
                {
                    throw new DirectoryNotFoundException();
                }

                //Loop through all of the assembly files in the directory.
                foreach (var file in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(
                             f => f.EndsWith(".exe", StringComparison.CurrentCultureIgnoreCase) ||
                             f.EndsWith(".dll", StringComparison.CurrentCultureIgnoreCase)))
                {
                    try
                    {
                        /*
                         * Load each assembly into memory and attempt to search it for any `Scanner` types.
                         */
                        var assembly = Assembly.LoadFile(file);
                        foreach (var type in assembly.GetTypes())
                        {
                            if (type.IsSubclassOf(typeof(Scanner)))
                            {
                                //Add this module to the list of scanners to load.
                                scannersToLoad.Add(type);
                            }

                            if (type.IsSubclassOf(typeof(Handler)))
                            {
                                //Add this module to the list of handlers to load.
                                handlersToLoad.Add(type);
                            }
                        }
                    }
                    catch { continue; }
                }

                //Notify user that searching was successful, as well as the number of modules found.
                Debugger.Success();
                Debugger.Put(2, "\tFound {0} loadable modules", Console.ForegroundColor, scannersToLoad.Count);
                Debugger.Put(1, "Loading modules into memory...");

                lock (ScannerList)
                {
                    foreach (var type in scannersToLoad)
                    {
                        /*
                         * LOAD SCANNERS
                         */
                        try
                        {
                            Debugger.Status("\tLoading scanner '" + type.Name + "'");

                            /*
                             * Create a new instance of the class and determine if the class has
                             * been previously loaded (we ignore duplicates--first come, first server).
                             */
                            var module = (Scanner)Activator.CreateInstance(type, new[] { Debugger });
                            if (ScannerList.ContainsKey(module.Id))
                            {
                                throw new Exception("Scanner has already been added!");
                            }

                            /*
                             * Create entries for the module in the scheduler list as well as the
                             * module list.  The next run date is computed based on the module frequency.
                             */
                            ScannerList.Add(module.Id, module);
                            ScheduledList.Add(module.Id, DateTime.Now.AddSeconds(module.Frequency));

                            Debugger.Success();
                            Debugger.Put(3, "\t\tId: {0}\n\t\tVersion: {1}",
                                         ConsoleColor.DarkCyan,
                                         module.Id,
                                         module.Version);
                        }
                        catch (Exception e)
                        {
                            Debugger.Failure(e.InnerException ?? e);
                        }
                    }
                }

                lock (HandlerList)
                {
                    /*
                     * LOAD HANDLERS
                     */
                    try
                    {
                        foreach (var type in handlersToLoad)
                        {
                            Debugger.Status("\tLoading handler '" + type.Name + "'");

                            /*
                             * Create a new instance of the class and determine if the class has
                             * been previously loaded (we ignore duplicates--first come, first server).
                             */
                            var module = (Handler)Activator.CreateInstance(type, new[] { Debugger });
                            if (HandlerList.ContainsKey(module.Id))
                            {
                                throw new Exception("Handler has already been added!");
                            }

                            //Add this handler to the list of handlers.
                            HandlerList.Add(module.Id, module);

                            Debugger.Success();
                            Debugger.Put(3, "\t\tId: {0}\n\t\tVersion: {1}",
                                         ConsoleColor.DarkCyan,
                                         module.Id,
                                         module.Version);
                        }
                    }
                    catch (Exception e)
                    {
                        Debugger.Failure(e.InnerException ?? e);
                    }
                }
            }
            catch (Exception e)
            {
                Debugger.Failure(e);

                //If the module path specified didn't exist we should exit after notifying the user!
                Environment.Exit(-200);
            }
        }