Ejemplo n.º 1
0
        public DnsApplication(IDnsServer dnsServer, string name)
        {
            _dnsServer = dnsServer;
            _name      = name;

            _appContext = new DnsApplicationAssemblyLoadContext(_dnsServer.ApplicationFolder);

            //load app assemblies
            Assembly[]      loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            List <Assembly> appAssemblies    = new List <Assembly>();

            foreach (string dllFile in Directory.GetFiles(_dnsServer.ApplicationFolder, "*.dll", SearchOption.TopDirectoryOnly))
            {
                string dllFileName = Path.GetFileNameWithoutExtension(dllFile);

                bool isLoaded = false;

                foreach (Assembly loadedAssembly in loadedAssemblies)
                {
                    AssemblyName assemblyName = loadedAssembly.GetName();

                    if (assemblyName.CodeBase != null)
                    {
                        if (Path.GetFileNameWithoutExtension(assemblyName.CodeBase).Equals(dllFileName, StringComparison.OrdinalIgnoreCase))
                        {
                            isLoaded = true;
                            break;
                        }
                    }
                    else if ((assemblyName.Name != null) && assemblyName.Name.Equals(dllFileName, StringComparison.OrdinalIgnoreCase))
                    {
                        isLoaded = true;
                        break;
                    }
                }

                if (isLoaded)
                {
                    continue;
                }

                try
                {
                    string pdbFile = Path.Combine(_dnsServer.ApplicationFolder, Path.GetFileNameWithoutExtension(dllFile) + ".pdb");

                    if (File.Exists(pdbFile))
                    {
                        using (FileStream dllStream = new FileStream(dllFile, FileMode.Open, FileAccess.Read))
                        {
                            using (FileStream pdbStream = new FileStream(pdbFile, FileMode.Open, FileAccess.Read))
                            {
                                appAssemblies.Add(_appContext.LoadFromStream(dllStream, pdbStream));
                            }
                        }
                    }
                    else
                    {
                        using (FileStream dllStream = new FileStream(dllFile, FileMode.Open, FileAccess.Read))
                        {
                            appAssemblies.Add(_appContext.LoadFromStream(dllStream));
                        }
                    }
                }
                catch (Exception ex)
                {
                    _dnsServer.WriteLog(ex);
                }
            }

            //load apps
            Dictionary <string, IDnsApplication>                 dnsApplications                 = new Dictionary <string, IDnsApplication>();
            Dictionary <string, IDnsAppRecordRequestHandler>     dnsAppRecordRequestHandlers     = new Dictionary <string, IDnsAppRecordRequestHandler>(2);
            Dictionary <string, IDnsRequestController>           dnsRequestControllers           = new Dictionary <string, IDnsRequestController>(1);
            Dictionary <string, IDnsAuthoritativeRequestHandler> dnsAuthoritativeRequestHandlers = new Dictionary <string, IDnsAuthoritativeRequestHandler>(1);
            Dictionary <string, IDnsQueryLogger>                 dnsQueryLoggers                 = new Dictionary <string, IDnsQueryLogger>(1);

            Type dnsApplicationInterface = typeof(IDnsApplication);

            foreach (Assembly appAssembly in appAssemblies)
            {
                try
                {
                    foreach (Type classType in appAssembly.ExportedTypes)
                    {
                        bool isDnsApp = false;

                        foreach (Type interfaceType in classType.GetInterfaces())
                        {
                            if (interfaceType == dnsApplicationInterface)
                            {
                                isDnsApp = true;
                                break;
                            }
                        }

                        if (isDnsApp)
                        {
                            try
                            {
                                IDnsApplication app = Activator.CreateInstance(classType) as IDnsApplication;

                                dnsApplications.Add(classType.FullName, app);

                                if (app is IDnsAppRecordRequestHandler appRecordHandler)
                                {
                                    dnsAppRecordRequestHandlers.Add(classType.FullName, appRecordHandler);
                                }

                                if (app is IDnsRequestController requestController)
                                {
                                    dnsRequestControllers.Add(classType.FullName, requestController);
                                }

                                if (app is IDnsAuthoritativeRequestHandler requestHandler)
                                {
                                    dnsAuthoritativeRequestHandlers.Add(classType.FullName, requestHandler);
                                }

                                if (app is IDnsQueryLogger logger)
                                {
                                    dnsQueryLoggers.Add(classType.FullName, logger);
                                }

                                if (_version is null)
                                {
                                    _version = appAssembly.GetName().Version;
                                }
                            }
                            catch (Exception ex)
                            {
                                _dnsServer.WriteLog(ex);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    _dnsServer.WriteLog(ex);
                }
            }

            if (_version is null)
            {
                if (dnsApplications.Count > 0)
                {
                    _version = new Version(1, 0);
                }
                else
                {
                    _version = new Version(0, 0);
                }
            }

            _dnsApplications                 = dnsApplications;
            _dnsAppRecordRequestHandlers     = dnsAppRecordRequestHandlers;
            _dnsRequestControllers           = dnsRequestControllers;
            _dnsAuthoritativeRequestHandlers = dnsAuthoritativeRequestHandlers;
            _dnsQueryLoggers                 = dnsQueryLoggers;
        }