Exemple #1
0
        /// <summary>
        /// Starts DNS filtering
        /// </summary>
        /// <param name="dnsApiConfiguration">Dns proxy configuration
        /// (<seealso cref="DnsApiConfiguration"/>)</param>
        /// <exception cref="NotSupportedException">Thrown
        /// if current API version is not supported</exception>
        /// <exception cref="ArgumentNullException">Thrown,
        /// if <see cref="dnsApiConfiguration"/> is not specified</exception>
        /// <exception cref="InvalidOperationException">Thrown, if cannot starting the proxy server
        /// for any reason</exception>
        public void StartDnsFiltering(DnsApiConfiguration dnsApiConfiguration)
        {
            lock (SYNC_ROOT)
            {
                try
                {
                    if (dnsApiConfiguration == null)
                    {
                        throw new ArgumentNullException(
                                  "dnsApiConfiguration",
                                  "dnsApiConfiguration is not specified");
                    }

                    if (!dnsApiConfiguration.IsEnabled)
                    {
                        LOG.InfoFormat("DNS filtering is disabled, doing nothing");
                        return;
                    }

                    LOG.InfoFormat("Starting the DNS filtering");
                    m_DnsProxyServer = new Dns.DnsProxyServer.DnsProxyServer(
                        dnsApiConfiguration.DnsProxySettings,
                        dnsApiConfiguration.DnsProxyServerCallbackConfiguration);
                    m_CurrentDnsProxySettings = dnsApiConfiguration.DnsProxySettings;
                    m_DnsProxyServer.Start();
                    LOG.InfoFormat("Starting the DNS filtering has been successfully completed");
                }
                catch (Exception ex)
                {
                    LOG.ErrorFormat("Starting the DNS filtering failed with an error", ex);
                    throw;
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Starts DNS filtering
        /// </summary>
        /// <param name="dnsApiConfiguration">Dns proxy configuration
        /// (<seealso cref="DnsApiConfiguration"/>)</param>
        /// <exception cref="NotSupportedException">Thrown
        /// if current API version is not supported</exception>
        /// <exception cref="ArgumentNullException">Thrown,
        /// if <see cref="dnsApiConfiguration"/> is not specified</exception>
        /// <exception cref="InvalidOperationException">Thrown, if cannot starting the proxy server
        /// for any reason</exception>
        public static void StartDnsFiltering(DnsApiConfiguration dnsApiConfiguration)
        {
            lock (SyncRoot)
            {
                try
                {
                    if (dnsApiConfiguration == null)
                    {
                        throw new ArgumentNullException(
                                  nameof(dnsApiConfiguration),
                                  "dnsApiConfiguration is not specified");
                    }

                    if (!dnsApiConfiguration.IsEnabled)
                    {
                        Logger.Info("DNS filtering is disabled, doing nothing");
                        return;
                    }

                    Logger.Info("Starting the DNS filtering");
                    AddDnsSuffixesAndDefaultFallbacks(dnsApiConfiguration.DnsProxySettings);
                    m_DnsProxyServer = new DnsProxyServer.DnsProxyServer(
                        dnsApiConfiguration.DnsProxySettings,
                        dnsApiConfiguration.DnsProxyServerCallbackConfiguration);
                    m_DnsProxyServer.Start();
                    Logger.Info("Starting the DNS filtering has been successfully completed");
                }
                catch (Exception ex)
                {
                    Logger.Error("Starting the DNS filtering failed with an error: {0}", ex);
                    throw;
                }
            }
        }
Exemple #3
0
        internal static AGDnsApi.AGDnsProxyServerCallbacks ToNativeObject(
            IDnsProxyServerCallbackConfiguration dnsProxyServerCallbackConfiguration,
            IDnsProxyServer proxyServer)
        {
            CertificateVerificationCallback certificateVerificationCallback = new CertificateVerificationCallback();
            ProxyServerCallbacksAdapter     proxyServerCallbacksAdapter     =
                new ProxyServerCallbacksAdapter(
                    dnsProxyServerCallbackConfiguration,
                    certificateVerificationCallback,
                    proxyServer);

            return(proxyServerCallbacksAdapter.DnsProxyServerCallbacks);
        }
        /// <summary>
        /// Creates an instance of the adapter
        /// </summary>
        /// <param name="dnsServerCallbackConfiguration">An object implementing the callbacks interface
        /// (<seealso cref="IDnsProxyServerCallbackConfiguration"/>)</param>
        /// <param name="certificateVerificationCallback">An object implementing certificate verification interface
        /// (<seealso cref="ICertificateVerificationCallback"/>)</param>
        /// <param name="proxyServer">An instance of <see cref="IDnsProxyServer"/></param>
        internal ProxyServerCallbacksAdapter(
            IDnsProxyServerCallbackConfiguration dnsServerCallbackConfiguration,
            ICertificateVerificationCallback certificateVerificationCallback,
            IDnsProxyServer proxyServer)
        {
            m_DnsServerCallbackConfiguration  = dnsServerCallbackConfiguration;
            m_CertificateVerificationCallback = certificateVerificationCallback;
            m_ProxyServer = proxyServer;

            // Initialize a native callbacks object
            DnsProxyServerCallbacks =
                new AGDnsApi.AGDnsProxyServerCallbacks
            {
                ag_dns_request_processed_cb    = AGCOnDnsRequestProcessed,
                ag_certificate_verification_cb = AGCOnCertificationVerificationProcessed
            };
        }
Exemple #5
0
        /// <summary>
        /// Stops the dns filtering
        /// If it is not started yet, does nothing.
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown, if cannot closing the proxy server
        /// for any reason</exception>
        public void StopDnsFiltering()
        {
            lock (SYNC_ROOT)
            {
                try
                {
                    LOG.InfoFormat("Stopping the DNS filtering");
                    if (m_DnsProxyServer == null)
                    {
                        return;
                    }

                    m_DnsProxyServer.Stop();
                    m_DnsProxyServer = null;
                    LOG.InfoFormat("Stopping the DNS filtering has been successfully completed");
                }
                catch (Exception ex)
                {
                    LOG.ErrorFormat("Stopping the DNS filtering failed with an error", ex);
                    throw;
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Stops the dns filtering
        /// If it is not started yet, does nothing.
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown, if cannot closing the proxy server
        /// for any reason</exception>
        public static void StopDnsFiltering()
        {
            lock (SyncRoot)
            {
                try
                {
                    Logger.Info("Stopping the DNS filtering");
                    if (m_DnsProxyServer == null)
                    {
                        return;
                    }

                    m_DnsProxyServer.Stop();
                    m_DnsProxyServer = null;
                    Logger.Info("Stopping the DNS filtering has been successfully completed");
                }
                catch (Exception ex)
                {
                    Logger.Error("Stopping the DNS filtering failed with an error: {0}", ex);
                    throw;
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// Reloads DNS filtering
        /// <param name="newDnsApiConfiguration">Dns proxy configuration
        /// (<seealso cref="DnsApiConfiguration"/>)</param>
        /// <param name="force">Determines, whether the DNS filtering must be reloaded,
        /// independently of whether configuration changed or not</param>
        /// <exception cref="ArgumentNullException">Thrown, if <see cref="newDnsApiConfiguration"/>
        /// is not specified</exception>
        /// <exception cref="ArgumentException">Thrown, if <see cref="DnsProxySettings"/>
        /// is not specified within the <see cref="newDnsApiConfiguration"/></exception>
        /// <exception cref="NotSupportedException">Thrown
        /// if current API version is not supported</exception>
        /// <exception cref="InvalidOperationException">Thrown, if cannot starting the proxy server
        /// for any reason</exception>
        /// <exception cref="InvalidOperationException">Thrown, if cannot closing the proxy server
        /// for any reason</exception>
        /// </summary>
        public void ReloadDnsFiltering(DnsApiConfiguration newDnsApiConfiguration, bool force)
        {
            lock (SYNC_ROOT)
            {
                IDnsProxyServer newDnsProxyServer = null;
                try
                {
                    LOG.InfoFormat("Reloading the DNS filtering");
                    if (m_DnsProxyServer == null ||
                        m_CurrentDnsProxySettings == null)
                    {
                        LOG.InfoFormat(
                            "Start DNS filtering, because the DNS server is not started and/or configurations are not set");

                        StartDnsFiltering(newDnsApiConfiguration);
                        return;
                    }

                    if (newDnsApiConfiguration == null)
                    {
                        throw new ArgumentNullException(
                                  "newDnsApiConfiguration",
                                  "newDnsApiConfiguration is not specified");
                    }

                    if (newDnsApiConfiguration.DnsProxySettings == null)
                    {
                        throw new ArgumentException(
                                  "DnsProxySettings is not initialized",
                                  "newDnsApiConfiguration");
                    }

                    bool isConfigurationChanged = !m_CurrentDnsProxySettings.Equals(newDnsApiConfiguration.DnsProxySettings);
                    if (!force &&
                        !isConfigurationChanged)
                    {
                        LOG.InfoFormat("The DNS server configuration hasn't been changed, no need to reload");
                        return;
                    }

                    newDnsProxyServer = new Dns.DnsProxyServer.DnsProxyServer(
                        newDnsApiConfiguration.DnsProxySettings,
                        newDnsApiConfiguration.DnsProxyServerCallbackConfiguration);

                    m_DnsProxyServer.Stop();
                    m_DnsProxyServer = newDnsProxyServer;
                    if (newDnsApiConfiguration.IsEnabled)
                    {
                        LOG.InfoFormat("DNS filtering is enabled, starting DNS proxy server");
                        m_DnsProxyServer.Start();
                    }

                    m_CurrentDnsProxySettings = newDnsApiConfiguration.DnsProxySettings;
                    LOG.InfoFormat("Reloading the DNS filtering has been successfully completed");
                }
                catch (Exception ex)
                {
                    LOG.ErrorFormat("Reloading the DNS filtering failed with an error", ex);
                    if (newDnsProxyServer != null &&
                        newDnsProxyServer.IsStarted)
                    {
                        // if the new DNS proxy server has been already started we should stop it,
                        // otherwise - let the existed proxy server works
                        StopDnsFiltering();
                    }

                    throw;
                }
            }
        }